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