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 / RecordJobsTableModel.java
1 package com.nexuiz.demorecorder.ui.swinggui.tablemodels;\r
2 \r
3 import java.io.File;\r
4 import java.util.List;\r
5 \r
6 import javax.swing.JOptionPane;\r
7 import javax.swing.table.AbstractTableModel;\r
8 \r
9 import org.jdesktop.swingx.JXTable;\r
10 \r
11 import com.nexuiz.demorecorder.application.DemoRecorderApplication;\r
12 import com.nexuiz.demorecorder.application.DemoRecorderException;\r
13 import com.nexuiz.demorecorder.application.DemoRecorderUtils;\r
14 import com.nexuiz.demorecorder.application.jobs.RecordJob;\r
15 import com.nexuiz.demorecorder.ui.swinggui.SwingGUI;\r
16 \r
17 /**\r
18  * Columns:\r
19  * - Job Name\r
20  * - Engine path\r
21  * - Engine parameters\r
22  * - Demo file\r
23  * - Relative demo path\r
24  * - dpvideo path\r
25  * - video destination\r
26  * - execute before cap\r
27  * - execute after cap\r
28  * - start second\r
29  * - end second\r
30  * - status\r
31  * @author Marius\r
32  *\r
33  */\r
34 public class RecordJobsTableModel extends AbstractTableModel {\r
35         \r
36         private static final long serialVersionUID = 5024144640874313910L;\r
37         \r
38         public static final int JOB_NAME = 0;\r
39         public static final int ENGINE_PATH = 1;\r
40         public static final int ENGINE_PARAMETERS = 2;\r
41         public static final int DEMO_FILE_PATH = 3;\r
42         public static final int RELATIVE_DEMO_PATH = 4;\r
43         public static final int DPVIDEO_PATH = 5;\r
44         public static final int VIDEO_DESTINATION_PATH = 6;\r
45         public static final int EXECUTE_BEFORE_CAP = 7;\r
46         public static final int EXECUTE_AFTER_CAP = 8;\r
47         public static final int START_SECOND = 9;\r
48         public static final int END_SECOND = 10;\r
49         public static final int STATUS = 11;\r
50         \r
51         private static final int columns[] = {\r
52                 JOB_NAME,\r
53                 ENGINE_PATH,\r
54                 ENGINE_PARAMETERS,\r
55                 DEMO_FILE_PATH,\r
56                 RELATIVE_DEMO_PATH,\r
57                 DPVIDEO_PATH,\r
58                 VIDEO_DESTINATION_PATH,\r
59                 EXECUTE_BEFORE_CAP,\r
60                 EXECUTE_AFTER_CAP,\r
61                 START_SECOND,\r
62                 END_SECOND,\r
63                 STATUS\r
64         };\r
65         \r
66         private DemoRecorderApplication appLayer;\r
67         private List<RecordJob> jobList = null;\r
68         \r
69         public RecordJobsTableModel(DemoRecorderApplication appLayer) {\r
70                 this.appLayer = appLayer;\r
71                 this.jobList = this.appLayer.getRecordJobs();\r
72         }\r
73         \r
74         public void deleteRecordJob(int modelRowIndex, int viewRowIndex) {\r
75                 try {\r
76                         RecordJob job = this.jobList.get(modelRowIndex);\r
77                         if (this.appLayer.deleteRecordJob(job)) {\r
78                                 this.jobList.remove(job);\r
79                                 fireTableRowsDeleted(viewRowIndex, viewRowIndex);\r
80                         }\r
81                 } catch (IndexOutOfBoundsException e) {\r
82                         throw new DemoRecorderException("Couldn't find correspondig job for modelRowIndex " + modelRowIndex\r
83                                         + " and viewRowIndex " + viewRowIndex, e);\r
84                 }\r
85         }\r
86         \r
87         public void loadNewJobQueue(SwingGUI gui, File path, JXTable jobsTable) {\r
88                 int result = JOptionPane.showConfirmDialog(gui, "Do you want to overwrite the current job queue? When pressing 'no' the loaded jobs will be added to the current queue!", "Confirm overwrite", JOptionPane.YES_NO_OPTION);\r
89                 boolean overwrite = false;\r
90                 if (result == JOptionPane.YES_OPTION) {\r
91                         overwrite = true;\r
92                 }\r
93                 int count = this.appLayer.loadJobQueue(path, overwrite);\r
94                 this.jobList = this.appLayer.getRecordJobs();\r
95                 fireTableDataChanged();\r
96                 if (count > 0) {\r
97                         jobsTable.setRowSelectionInterval(jobsTable.getRowCount() - count, jobsTable.getRowCount() - 1);\r
98                 }\r
99         }\r
100         \r
101         public RecordJob getRecordJob(int modelRowIndex) {\r
102                 return this.jobList.get(modelRowIndex);\r
103         }\r
104 \r
105         public int getColumnCount() {\r
106                 return columns.length;\r
107         }\r
108 \r
109         public int getRowCount() {\r
110                 return this.jobList.size();\r
111         }\r
112 \r
113         public Object getValueAt(int rowIndex, int columnIndex) {\r
114                 RecordJob job = this.jobList.get(rowIndex);\r
115                 if (job == null) {\r
116                         return null;\r
117                 }\r
118                 \r
119                 if (columnIndex < 0 || columnIndex >= columns.length) {\r
120                         return null;\r
121                 }\r
122                 \r
123                 String cellData = "UNDEF";\r
124                 switch (columnIndex) {\r
125                 case JOB_NAME:\r
126                         cellData = job.getJobName(); break;\r
127                 case ENGINE_PATH:\r
128                         cellData = job.getEnginePath().getAbsolutePath(); break;\r
129                 case ENGINE_PARAMETERS:\r
130                         cellData = job.getEngineParameters(); break;\r
131                 case DEMO_FILE_PATH:\r
132                         cellData = DemoRecorderUtils.getJustFileNameOfPath(job.getDemoFile()); break;\r
133                 case RELATIVE_DEMO_PATH:\r
134                         cellData = job.getRelativeDemoPath(); break;\r
135                 case DPVIDEO_PATH:\r
136                         cellData = job.getDpVideoPath().getAbsolutePath(); break;\r
137                 case VIDEO_DESTINATION_PATH:\r
138                         cellData = job.getVideoDestination().getAbsolutePath(); break;\r
139                 case EXECUTE_BEFORE_CAP:\r
140                         cellData = job.getExecuteBeforeCap(); break;\r
141                 case EXECUTE_AFTER_CAP:\r
142                         cellData = job.getExecuteAfterCap(); break;\r
143                 case START_SECOND:\r
144                         cellData = String.valueOf(job.getStartSecond()); break;\r
145                 case END_SECOND:\r
146                         cellData = String.valueOf(job.getEndSecond()); break;\r
147                 case STATUS:\r
148                         if (job.getState() == RecordJob.State.DONE) {\r
149                                 cellData = "done";\r
150                         } else if (job.getState() == RecordJob.State.ERROR) {\r
151                                 cellData = "error";\r
152                         } else if (job.getState() == RecordJob.State.ERROR_PLUGIN) {\r
153                                 cellData = "plug-in error";\r
154                         } else if (job.getState() == RecordJob.State.PROCESSING) {\r
155                                 cellData = "processing";\r
156                         } else if (job.getState() == RecordJob.State.WAITING) {\r
157                                 cellData = "waiting";\r
158                         }\r
159                 }\r
160                 \r
161                 return cellData;\r
162         }\r
163 \r
164         @Override\r
165         public String getColumnName(int column) {\r
166                 if (column < 0 || column >= columns.length) {\r
167                         return "";\r
168                 }\r
169                 \r
170                 String columnName = "UNDEFINED";\r
171                 switch (column) {\r
172                 case JOB_NAME:\r
173                         columnName = "Name"; break;\r
174                 case ENGINE_PATH:\r
175                         columnName = "Engine path"; break;\r
176                 case ENGINE_PARAMETERS:\r
177                         columnName = "Engine parameters"; break;\r
178                 case DEMO_FILE_PATH:\r
179                         columnName = "Demo name"; break;\r
180                 case RELATIVE_DEMO_PATH:\r
181                         columnName = "Relative demo path"; break;\r
182                 case DPVIDEO_PATH:\r
183                         columnName = "DPVideo path"; break;\r
184                 case VIDEO_DESTINATION_PATH:\r
185                         columnName = "Video destination"; break;\r
186                 case EXECUTE_BEFORE_CAP:\r
187                         columnName = "Exec before"; break;\r
188                 case EXECUTE_AFTER_CAP:\r
189                         columnName = "Exec after"; break;\r
190                 case START_SECOND:\r
191                         columnName = "Start"; break;\r
192                 case END_SECOND:\r
193                         columnName = "End"; break;\r
194                 case STATUS:\r
195                         columnName = "Status"; break;\r
196                 }\r
197                 \r
198                 return columnName;\r
199         }\r
200         \r
201         public List<RecordJob> getRecordJobs() {\r
202                 return this.jobList;\r
203         }\r
204 }\r