NexuizDemoRecorder: added ability to save and load templates (might make sense for...
[divverent/nexuiz.git] / misc / tools / NexuizDemoRecorder / main / src / main / java / com / nexuiz / demorecorder / ui / swinggui / tablemodels / RecordJobTemplatesTableModel.java
1 package com.nexuiz.demorecorder.ui.swinggui.tablemodels;\r
2 \r
3 import java.io.File;\r
4 import java.io.FileInputStream;\r
5 import java.io.FileOutputStream;\r
6 import java.io.ObjectInputStream;\r
7 import java.io.ObjectOutputStream;\r
8 import java.util.ArrayList;\r
9 import java.util.List;\r
10 \r
11 import javax.swing.JOptionPane;\r
12 import javax.swing.table.AbstractTableModel;\r
13 \r
14 import org.jdesktop.swingx.JXTable;\r
15 \r
16 import com.nexuiz.demorecorder.application.DemoRecorderApplication;\r
17 import com.nexuiz.demorecorder.application.DemoRecorderException;\r
18 import com.nexuiz.demorecorder.application.DemoRecorderUtils;\r
19 import com.nexuiz.demorecorder.ui.swinggui.RecordJobTemplate;\r
20 import com.nexuiz.demorecorder.ui.swinggui.SwingGUI;\r
21 \r
22 /**\r
23  * Columns:\r
24  * - Job Name\r
25  * - Engine path\r
26  * - Engine parameters\r
27  * - Demo file\r
28  * - Relative demo path\r
29  * - dpvideo path\r
30  * - video destination\r
31  * - execute before cap\r
32  * - execute after cap\r
33  * - start second\r
34  * - end second\r
35  * - status\r
36  * @author Marius\r
37  *\r
38  */\r
39 public class RecordJobTemplatesTableModel extends AbstractTableModel {\r
40         \r
41         private static final long serialVersionUID = 6541517890817708306L;\r
42         \r
43         public static final int TEMPLATE_NAME = 0;\r
44         public static final int TEMPLATE_SUMMARY = 1;\r
45         public static final int JOB_NAME = 2;\r
46         public static final int ENGINE_PATH = 3;\r
47         public static final int ENGINE_PARAMETERS = 4;\r
48         public static final int DEMO_FILE_PATH = 5;\r
49         public static final int RELATIVE_DEMO_PATH = 6;\r
50         public static final int DPVIDEO_PATH = 7;\r
51         public static final int VIDEO_DESTINATION_PATH = 8;\r
52         public static final int EXECUTE_BEFORE_CAP = 9;\r
53         public static final int EXECUTE_AFTER_CAP = 10;\r
54         \r
55         private static final int columns[] = {\r
56                 TEMPLATE_NAME,\r
57                 TEMPLATE_SUMMARY,\r
58                 JOB_NAME,\r
59                 ENGINE_PATH,\r
60                 ENGINE_PARAMETERS,\r
61                 DEMO_FILE_PATH,\r
62                 RELATIVE_DEMO_PATH,\r
63                 DPVIDEO_PATH,\r
64                 VIDEO_DESTINATION_PATH,\r
65                 EXECUTE_BEFORE_CAP,\r
66                 EXECUTE_AFTER_CAP\r
67         };\r
68         \r
69         private List<RecordJobTemplate> templates;\r
70         \r
71         public RecordJobTemplatesTableModel() {\r
72                 templates = new ArrayList<RecordJobTemplate>();\r
73                 \r
74                 //load table content\r
75                 File path = DemoRecorderUtils.computeLocalFile(DemoRecorderApplication.PREFERENCES_DIRNAME, SwingGUI.TEMPLATE_TABLE_CONTENT_FILENAME);\r
76                 this.loadTemplateListFromFile(path, true);\r
77         }\r
78         \r
79         public void deleteRecordJobTemplate(int modelRowIndex, int viewRowIndex) {\r
80                 try {\r
81                         this.templates.remove(modelRowIndex);\r
82                         fireTableRowsDeleted(viewRowIndex, viewRowIndex);\r
83                 } catch (IndexOutOfBoundsException e) {\r
84                         throw new DemoRecorderException("Couldn't find correspondig template for modelRowIndex " + modelRowIndex\r
85                                         + " and viewRowIndex " + viewRowIndex, e);\r
86                 }\r
87         }\r
88         \r
89         public void addRecordJobTemplate(RecordJobTemplate template) {\r
90                 this.templates.add(template);\r
91                 int position = this.templates.size() - 1;\r
92                 fireTableRowsInserted(position, position);\r
93         }\r
94         \r
95         public RecordJobTemplate getRecordJobTemplate(int modelRowIndex) {\r
96                 return this.templates.get(modelRowIndex);\r
97         }\r
98 \r
99         public int getColumnCount() {\r
100                 return columns.length;\r
101         }\r
102 \r
103         public int getRowCount() {\r
104                 return this.templates.size();\r
105         }\r
106         \r
107         public void saveTemplateListToFile(File path) {\r
108                 DemoRecorderUtils.attemptFileCreation(path);\r
109                 \r
110                 String exceptionMessage = "Could not save the templates to file " + path.getAbsolutePath();\r
111                 \r
112                 if (!path.exists()) {\r
113                         DemoRecorderException ex = new DemoRecorderException(exceptionMessage);\r
114                         DemoRecorderUtils.showNonCriticalErrorDialog(ex);\r
115                         return;\r
116                 }\r
117                 \r
118                 try {\r
119                         FileOutputStream fout = new FileOutputStream(path);\r
120                         ObjectOutputStream oos = new ObjectOutputStream(fout);\r
121                         oos.writeObject(this.templates);\r
122                         oos.close();\r
123                 } catch (Exception e) {\r
124                         DemoRecorderUtils.showNonCriticalErrorDialog(exceptionMessage, e, true);\r
125                 }\r
126         }\r
127         \r
128         @SuppressWarnings("unchecked")\r
129         private int loadTemplateListFromFile(File path, boolean overwrite) {\r
130                 if (!path.exists()) {\r
131                         return 0;\r
132                 }\r
133                 \r
134                 List<RecordJobTemplate> newTemplateList;\r
135                 try {\r
136                         FileInputStream fin = new FileInputStream(path);\r
137                         ObjectInputStream ois = new ObjectInputStream(fin);\r
138                         newTemplateList = (List<RecordJobTemplate>) ois.readObject();\r
139                         if (overwrite) {\r
140                                 this.templates = newTemplateList;\r
141                         } else {\r
142                                 this.templates.addAll(newTemplateList);\r
143                         }\r
144                         return newTemplateList.size();\r
145                 } catch (Exception e) {\r
146                         DemoRecorderUtils.showNonCriticalErrorDialog("Could not load the templates from file " + path.getAbsolutePath(), e, true);\r
147                         return 0;\r
148                 }\r
149                 \r
150         }\r
151         \r
152         public void loadNewTemplateList(SwingGUI gui, File path, JXTable templatesTable) {\r
153                 int result = JOptionPane.showConfirmDialog(gui, "Do you want to overwrite the current template list? When pressing 'no' the loaded templates will be added to the current list!", "Confirm overwrite", JOptionPane.YES_NO_OPTION);\r
154                 boolean overwrite = false;\r
155                 if (result == JOptionPane.YES_OPTION) {\r
156                         overwrite = true;\r
157                 }\r
158                 int count = loadTemplateListFromFile(path, overwrite);\r
159                 fireTableDataChanged();\r
160                 if (count > 0) {\r
161                         templatesTable.setRowSelectionInterval(templatesTable.getRowCount() - count, templatesTable.getRowCount() - 1);\r
162                 }\r
163         }\r
164 \r
165         public Object getValueAt(int rowIndex, int columnIndex) {\r
166                 RecordJobTemplate template = this.templates.get(rowIndex);\r
167                 if (template == null) {\r
168                         return null;\r
169                 }\r
170                 \r
171                 if (columnIndex < 0 || columnIndex >= columns.length) {\r
172                         return null;\r
173                 }\r
174                 \r
175                 String cellData = "UNDEF";\r
176                 switch (columnIndex) {\r
177                 case TEMPLATE_NAME:\r
178                         cellData = template.getName(); break;\r
179                 case TEMPLATE_SUMMARY:\r
180                         cellData = template.getSummary(); break;\r
181                 case JOB_NAME:\r
182                         cellData = template.getJobName(); break;\r
183                 case ENGINE_PATH:\r
184                         cellData = template.getEnginePath().getAbsolutePath(); break;\r
185                 case ENGINE_PARAMETERS:\r
186                         cellData = template.getEngineParameters(); break;\r
187                 case DEMO_FILE_PATH:\r
188                         cellData = DemoRecorderUtils.getJustFileNameOfPath(template.getDemoFile()); break;\r
189                 case RELATIVE_DEMO_PATH:\r
190                         cellData = template.getRelativeDemoPath(); break;\r
191                 case DPVIDEO_PATH:\r
192                         cellData = template.getDpVideoPath().getAbsolutePath(); break;\r
193                 case VIDEO_DESTINATION_PATH:\r
194                         cellData = template.getVideoDestination().getAbsolutePath(); break;\r
195                 case EXECUTE_BEFORE_CAP:\r
196                         cellData = template.getExecuteBeforeCap(); break;\r
197                 case EXECUTE_AFTER_CAP:\r
198                         cellData = template.getExecuteAfterCap(); break;\r
199                 }\r
200                 \r
201                 return cellData;\r
202         }\r
203 \r
204         @Override\r
205         public String getColumnName(int column) {\r
206                 if (column < 0 || column >= columns.length) {\r
207                         return "";\r
208                 }\r
209                 \r
210                 String columnName = "UNDEFINED";\r
211                 switch (column) {\r
212                 case TEMPLATE_NAME:\r
213                         columnName = "Name"; break;\r
214                 case TEMPLATE_SUMMARY:\r
215                         columnName = "Summary"; break;\r
216                 case JOB_NAME:\r
217                         columnName = "Job name"; break;\r
218                 case ENGINE_PATH:\r
219                         columnName = "Engine path"; break;\r
220                 case ENGINE_PARAMETERS:\r
221                         columnName = "Engine parameters"; break;\r
222                 case DEMO_FILE_PATH:\r
223                         columnName = "Demo directory"; break;\r
224                 case RELATIVE_DEMO_PATH:\r
225                         columnName = "Relative demo path"; break;\r
226                 case DPVIDEO_PATH:\r
227                         columnName = "DPVideo path"; break;\r
228                 case VIDEO_DESTINATION_PATH:\r
229                         columnName = "Video destination"; break;\r
230                 case EXECUTE_BEFORE_CAP:\r
231                         columnName = "Exec before"; break;\r
232                 case EXECUTE_AFTER_CAP:\r
233                         columnName = "Exec after"; break;\r
234                 }\r
235                 \r
236                 return columnName;\r
237         }\r
238         \r
239         public List<RecordJobTemplate> getRecordJobTemplates() {\r
240                 return this.templates;\r
241         }\r
242 }\r