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