1 package com.nexuiz.demorecorder.ui.swinggui.tablemodels;
\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
11 import javax.swing.JOptionPane;
\r
12 import javax.swing.table.AbstractTableModel;
\r
14 import org.jdesktop.swingx.JXTable;
\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
26 * - Engine parameters
\r
28 * - Relative demo path
\r
30 * - video destination
\r
31 * - execute before cap
\r
32 * - execute after cap
\r
39 public class RecordJobTemplatesTableModel extends AbstractTableModel {
\r
41 private static final long serialVersionUID = 6541517890817708306L;
\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
55 private static final int columns[] = {
\r
64 VIDEO_DESTINATION_PATH,
\r
69 private List<RecordJobTemplate> templates;
\r
71 public RecordJobTemplatesTableModel() {
\r
72 templates = new ArrayList<RecordJobTemplate>();
\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
79 public void deleteRecordJobTemplate(int modelRowIndex, int viewRowIndex) {
\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
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
95 public RecordJobTemplate getRecordJobTemplate(int modelRowIndex) {
\r
96 return this.templates.get(modelRowIndex);
\r
99 public int getColumnCount() {
\r
100 return columns.length;
\r
103 public int getRowCount() {
\r
104 return this.templates.size();
\r
107 public void saveTemplateListToFile(File path) {
\r
108 DemoRecorderUtils.attemptFileCreation(path);
\r
110 String exceptionMessage = "Could not save the templates to file " + path.getAbsolutePath();
\r
112 if (!path.exists()) {
\r
113 DemoRecorderException ex = new DemoRecorderException(exceptionMessage);
\r
114 DemoRecorderUtils.showNonCriticalErrorDialog(ex);
\r
119 FileOutputStream fout = new FileOutputStream(path);
\r
120 ObjectOutputStream oos = new ObjectOutputStream(fout);
\r
121 oos.writeObject(this.templates);
\r
123 } catch (Exception e) {
\r
124 DemoRecorderUtils.showNonCriticalErrorDialog(exceptionMessage, e, true);
\r
128 @SuppressWarnings("unchecked")
\r
129 private int loadTemplateListFromFile(File path, boolean overwrite) {
\r
130 if (!path.exists()) {
\r
134 List<RecordJobTemplate> newTemplateList;
\r
136 FileInputStream fin = new FileInputStream(path);
\r
137 ObjectInputStream ois = new ObjectInputStream(fin);
\r
138 newTemplateList = (List<RecordJobTemplate>) ois.readObject();
\r
140 this.templates = newTemplateList;
\r
142 this.templates.addAll(newTemplateList);
\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
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
158 int count = loadTemplateListFromFile(path, overwrite);
\r
159 fireTableDataChanged();
\r
161 templatesTable.setRowSelectionInterval(templatesTable.getRowCount() - count, templatesTable.getRowCount() - 1);
\r
165 public Object getValueAt(int rowIndex, int columnIndex) {
\r
166 RecordJobTemplate template = this.templates.get(rowIndex);
\r
167 if (template == null) {
\r
171 if (columnIndex < 0 || columnIndex >= columns.length) {
\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
182 cellData = template.getJobName(); break;
\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
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
205 public String getColumnName(int column) {
\r
206 if (column < 0 || column >= columns.length) {
\r
210 String columnName = "UNDEFINED";
\r
212 case TEMPLATE_NAME:
\r
213 columnName = "Name"; break;
\r
214 case TEMPLATE_SUMMARY:
\r
215 columnName = "Summary"; break;
\r
217 columnName = "Job name"; break;
\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
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
239 public List<RecordJobTemplate> getRecordJobTemplates() {
\r
240 return this.templates;
\r