1 package com.nexuiz.demorecorder.ui.swinggui.tablemodels;
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;
11 import javax.swing.JOptionPane;
12 import javax.swing.table.AbstractTableModel;
14 import org.jdesktop.swingx.JXTable;
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;
28 * - Relative demo path
31 * - execute before cap
39 public class RecordJobTemplatesTableModel extends AbstractTableModel {
41 private static final long serialVersionUID = 6541517890817708306L;
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;
55 private static final int columns[] = {
64 VIDEO_DESTINATION_PATH,
69 private List<RecordJobTemplate> templates;
71 public RecordJobTemplatesTableModel() {
72 templates = new ArrayList<RecordJobTemplate>();
75 File path = DemoRecorderUtils.computeLocalFile(DemoRecorderApplication.PREFERENCES_DIRNAME, SwingGUI.TEMPLATE_TABLE_CONTENT_FILENAME);
76 this.loadTemplateListFromFile(path, true);
79 public void deleteRecordJobTemplate(int modelRowIndex, int viewRowIndex) {
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);
89 public void addRecordJobTemplate(RecordJobTemplate template) {
90 this.templates.add(template);
91 int position = this.templates.size() - 1;
92 fireTableRowsInserted(position, position);
95 public RecordJobTemplate getRecordJobTemplate(int modelRowIndex) {
96 return this.templates.get(modelRowIndex);
99 public int getColumnCount() {
100 return columns.length;
103 public int getRowCount() {
104 return this.templates.size();
107 public void saveTemplateListToFile(File path) {
108 DemoRecorderUtils.attemptFileCreation(path);
110 String exceptionMessage = "Could not save the templates to file " + path.getAbsolutePath();
112 if (!path.exists()) {
113 DemoRecorderException ex = new DemoRecorderException(exceptionMessage);
114 DemoRecorderUtils.showNonCriticalErrorDialog(ex);
119 FileOutputStream fout = new FileOutputStream(path);
120 ObjectOutputStream oos = new ObjectOutputStream(fout);
121 oos.writeObject(this.templates);
123 } catch (Exception e) {
124 DemoRecorderUtils.showNonCriticalErrorDialog(exceptionMessage, e, true);
128 @SuppressWarnings("unchecked")
129 private int loadTemplateListFromFile(File path, boolean overwrite) {
130 if (!path.exists()) {
134 List<RecordJobTemplate> newTemplateList;
136 FileInputStream fin = new FileInputStream(path);
137 ObjectInputStream ois = new ObjectInputStream(fin);
138 newTemplateList = (List<RecordJobTemplate>) ois.readObject();
140 this.templates = newTemplateList;
142 this.templates.addAll(newTemplateList);
144 return newTemplateList.size();
145 } catch (Exception e) {
146 DemoRecorderUtils.showNonCriticalErrorDialog("Could not load the templates from file " + path.getAbsolutePath(), e, true);
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) {
158 int count = loadTemplateListFromFile(path, overwrite);
159 fireTableDataChanged();
161 templatesTable.setRowSelectionInterval(templatesTable.getRowCount() - count, templatesTable.getRowCount() - 1);
165 public Object getValueAt(int rowIndex, int columnIndex) {
166 RecordJobTemplate template = this.templates.get(rowIndex);
167 if (template == null) {
171 if (columnIndex < 0 || columnIndex >= columns.length) {
175 String cellData = "UNDEF";
176 switch (columnIndex) {
178 cellData = template.getName(); break;
179 case TEMPLATE_SUMMARY:
180 cellData = template.getSummary(); break;
182 cellData = template.getJobName(); break;
184 cellData = template.getEnginePath().getAbsolutePath(); break;
185 case ENGINE_PARAMETERS:
186 cellData = template.getEngineParameters(); break;
188 cellData = DemoRecorderUtils.getJustFileNameOfPath(template.getDemoFile()); break;
189 case RELATIVE_DEMO_PATH:
190 cellData = template.getRelativeDemoPath(); break;
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;
205 public String getColumnName(int column) {
206 if (column < 0 || column >= columns.length) {
210 String columnName = "UNDEFINED";
213 columnName = "Name"; break;
214 case TEMPLATE_SUMMARY:
215 columnName = "Summary"; break;
217 columnName = "Job name"; break;
219 columnName = "Engine path"; break;
220 case ENGINE_PARAMETERS:
221 columnName = "Engine parameters"; break;
223 columnName = "Demo directory"; break;
224 case RELATIVE_DEMO_PATH:
225 columnName = "Relative demo path"; break;
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;
239 public List<RecordJobTemplate> getRecordJobTemplates() {
240 return this.templates;