NexuizDemoRecorder: added ability to save and load templates (might make sense for...
authorgreenmarine <greenmarine@f962a42d-fe04-0410-a3ab-8c8b0445ebaa>
Thu, 18 Feb 2010 21:51:58 +0000 (21:51 +0000)
committergreenmarine <greenmarine@f962a42d-fe04-0410-a3ab-8c8b0445ebaa>
Thu, 18 Feb 2010 21:51:58 +0000 (21:51 +0000)
Load dialog for templates or jobs allows the user to select whether to overwrite the current list or whether to append the loaded items to the current list

git-svn-id: svn://svn.icculus.org/nexuiz/trunk@8653 f962a42d-fe04-0410-a3ab-8c8b0445ebaa

misc/tools/NexuizDemoRecorder/main/pom.xml
misc/tools/NexuizDemoRecorder/main/src/main/java/com/nexuiz/demorecorder/application/DemoRecorderApplication.java
misc/tools/NexuizDemoRecorder/main/src/main/java/com/nexuiz/demorecorder/ui/swinggui/SwingGUI.java
misc/tools/NexuizDemoRecorder/main/src/main/java/com/nexuiz/demorecorder/ui/swinggui/tablemodels/RecordJobTemplatesTableModel.java
misc/tools/NexuizDemoRecorder/main/src/main/java/com/nexuiz/demorecorder/ui/swinggui/tablemodels/RecordJobsTableModel.java

index 712d291..e5a6988 100644 (file)
@@ -4,7 +4,7 @@
        <groupId>NexuizDemoRecorder</groupId>
        <artifactId>NexuizDemoRecorder</artifactId>
        <packaging>jar</packaging>
-       <version>0.2</version>
+       <version>0.3-SNAPSHOT</version>
        <name>NexuizDemoRecorder</name>
        <url>http://maven.apache.org</url>
        <dependencies>
index 2ca7b3d..d6717c6 100644 (file)
@@ -311,26 +311,40 @@ public class DemoRecorderApplication {
        \r
        private void loadJobQueue() {\r
                File defaultFile = DemoRecorderUtils.computeLocalFile(PREFERENCES_DIRNAME, JOBQUEUE_FILENAME);\r
-               this.loadJobQueue(defaultFile);\r
+               this.loadJobQueue(defaultFile, true);\r
        }\r
        \r
+       /**\r
+        * Loads the jobs from the given file path. If override is enabled, the previous\r
+        * job list will be overwritten with the newly loaded list. Otherwise the loaded jobs\r
+        * are added to the already existing list.\r
+        * @param path\r
+        * @param override\r
+        * @return the number of jobs loaded from the file\r
+        */\r
        @SuppressWarnings("unchecked")\r
-       public void loadJobQueue(File path) {\r
+       public int loadJobQueue(File path, boolean override) {\r
                if (!path.exists()) {\r
-                       return;\r
+                       return 0;\r
                }\r
                \r
                try {\r
                        FileInputStream fin = new FileInputStream(path);\r
                        ObjectInputStream ois = new ObjectInputStream(fin);\r
-                       this.jobs = (List<RecordJob>) ois.readObject();\r
-                       for (RecordJob currentJob : this.jobs) {\r
+                       List<RecordJob> newList = (List<RecordJob>) ois.readObject();\r
+                       for (RecordJob currentJob : newList) {\r
                                currentJob.setAppLayer(this);\r
                        }\r
+                       if (override) {\r
+                               this.jobs = newList;\r
+                       } else {\r
+                               this.jobs.addAll(newList);\r
+                       }\r
+                       return newList.size();\r
                } catch (Exception e) {\r
                        DemoRecorderUtils.showNonCriticalErrorDialog("Could not load the job queue file " + path.getAbsolutePath(), e, true);\r
+                       return 0;\r
                }\r
-               \r
        }\r
        \r
        public void saveJobQueue() {\r
index 3b5f5de..6778794 100644 (file)
@@ -100,11 +100,14 @@ public class SwingGUI extends JFrame implements WindowListener, DemoRecorderUI {
        private ActionListener menuButtonActionListener = new MenuButtonActionListener();\r
        private JMenuItem fileLoadQueue = new JMenuItem("Load job queue", getIcon("fileopen.png"));\r
        private JMenuItem fileSaveQueue = new JMenuItem("Save job queue", getIcon("filesave.png"));\r
+       private JMenuItem fileLoadTemplates = new JMenuItem("Load templates", getIcon("fileopen.png"));\r
+       private JMenuItem fileSaveTemplates = new JMenuItem("Save templates", getIcon("filesave.png"));\r
        private JMenuItem filePreferences = new JMenuItem("Preferences", getIcon("advanced.png"));\r
        private JMenuItem fileExit = new JMenuItem("Exit", getIcon("exit.png"));\r
        private JMenuItem helpHelp = new JMenuItem("Show help", getIcon("help.png"));\r
        private JMenuItem helpAbout = new JMenuItem("About", getIcon("info.png"));\r
        private JFileChooser jobQueueSaveAsFC = new JFileChooser();\r
+       private JFileChooser templatesSaveAsFC = new JFileChooser();\r
        \r
        private JButton jobs_create = new JButton(LABEL_JOB_CREATE, getIcon("edit_add.png"));\r
        private JButton jobs_createFromTempl = new JButton(LABEL_JOB_CREATE_FROM_TEMPL, getIcon("view_right_p.png"));\r
@@ -316,12 +319,16 @@ public class SwingGUI extends JFrame implements WindowListener, DemoRecorderUI {
                JMenu fileMenu = new JMenu("File");\r
                fileMenu.add(fileLoadQueue);\r
                fileMenu.add(fileSaveQueue);\r
+               fileMenu.add(fileLoadTemplates);\r
+               fileMenu.add(fileSaveTemplates);\r
                fileMenu.add(filePreferences);\r
                fileMenu.add(fileExit);\r
                menuBar.add(fileMenu);\r
                \r
                fileLoadQueue.addActionListener(menuButtonActionListener);\r
                fileSaveQueue.addActionListener(menuButtonActionListener);\r
+               fileLoadTemplates.addActionListener(menuButtonActionListener);\r
+               fileSaveTemplates.addActionListener(menuButtonActionListener);\r
                filePreferences.addActionListener(menuButtonActionListener);\r
                fileExit.addActionListener(menuButtonActionListener);\r
 \r
@@ -456,7 +463,7 @@ public class SwingGUI extends JFrame implements WindowListener, DemoRecorderUI {
                                        File selectedFile = jobQueueSaveAsFC.getSelectedFile();\r
                                        if (selectedFile.isFile()) {\r
                                                RecordJobsTableModel tableModel = (RecordJobsTableModel) jobsTable.getModel();\r
-                                               tableModel.loadNewJobQueue(selectedFile);\r
+                                               tableModel.loadNewJobQueue(SwingGUI.this, selectedFile, jobsTable);\r
                                                configureTableButtons();\r
                                        }\r
                                }\r
@@ -477,6 +484,32 @@ public class SwingGUI extends JFrame implements WindowListener, DemoRecorderUI {
                                        }\r
                                        appLayer.saveJobQueue(selectedFile);\r
                                }\r
+                       } else if (e.getSource() == fileLoadTemplates) {\r
+                               int result = templatesSaveAsFC.showOpenDialog(SwingGUI.this);\r
+                               if (result == JFileChooser.APPROVE_OPTION) {\r
+                                       File selectedFile = templatesSaveAsFC.getSelectedFile();\r
+                                       if (selectedFile.isFile()) {\r
+                                               RecordJobTemplatesTableModel tableModel = (RecordJobTemplatesTableModel) templatesTable.getModel();\r
+                                               tableModel.loadNewTemplateList(SwingGUI.this, selectedFile, templatesTable);\r
+                                               configureTableButtons();\r
+                                       }\r
+                               }\r
+                       } else if (e.getSource() == fileSaveTemplates) {\r
+                               int result = templatesSaveAsFC.showSaveDialog(SwingGUI.this);\r
+                               if (result == JFileChooser.APPROVE_OPTION) {\r
+                                       File selectedFile = templatesSaveAsFC.getSelectedFile();\r
+                                       if (!DemoRecorderUtils.getFileExtension(selectedFile).equals("templ")) {\r
+                                               selectedFile = new File(selectedFile.getAbsoluteFile() + ".templ");\r
+                                       }\r
+                                       if (selectedFile.exists()) {\r
+                                               int confirm = JOptionPane.showConfirmDialog(SwingGUI.this, "File already exists. Are you sure you want to overwrite it?", "Confirm overwrite", JOptionPane.YES_NO_OPTION);\r
+                                               if (confirm == JOptionPane.NO_OPTION) {\r
+                                                       return;\r
+                                               }\r
+                                       }\r
+                                       RecordJobTemplatesTableModel model = (RecordJobTemplatesTableModel) templatesTable.getModel();\r
+                                       model.saveTemplateListToFile(selectedFile);\r
+                               }\r
                        } else if (e.getSource() == filePreferences) {\r
                                preferencesDialog.showDialog();\r
                        } else if (e.getSource() == fileExit) {\r
index ddb52f5..f5aa069 100644 (file)
@@ -8,8 +8,11 @@ import java.io.ObjectOutputStream;
 import java.util.ArrayList;\r
 import java.util.List;\r
 \r
+import javax.swing.JOptionPane;\r
 import javax.swing.table.AbstractTableModel;\r
 \r
+import org.jdesktop.swingx.JXTable;\r
+\r
 import com.nexuiz.demorecorder.application.DemoRecorderApplication;\r
 import com.nexuiz.demorecorder.application.DemoRecorderException;\r
 import com.nexuiz.demorecorder.application.DemoRecorderUtils;\r
@@ -70,7 +73,7 @@ public class RecordJobTemplatesTableModel extends AbstractTableModel {
                \r
                //load table content\r
                File path = DemoRecorderUtils.computeLocalFile(DemoRecorderApplication.PREFERENCES_DIRNAME, SwingGUI.TEMPLATE_TABLE_CONTENT_FILENAME);\r
-               this.loadTemplateListFromFile(path);\r
+               this.loadTemplateListFromFile(path, true);\r
        }\r
        \r
        public void deleteRecordJobTemplate(int modelRowIndex, int viewRowIndex) {\r
@@ -123,9 +126,9 @@ public class RecordJobTemplatesTableModel extends AbstractTableModel {
        }\r
        \r
        @SuppressWarnings("unchecked")\r
-       public void loadTemplateListFromFile(File path) {\r
+       private int loadTemplateListFromFile(File path, boolean overwrite) {\r
                if (!path.exists()) {\r
-                       return;\r
+                       return 0;\r
                }\r
                \r
                List<RecordJobTemplate> newTemplateList;\r
@@ -133,12 +136,30 @@ public class RecordJobTemplatesTableModel extends AbstractTableModel {
                        FileInputStream fin = new FileInputStream(path);\r
                        ObjectInputStream ois = new ObjectInputStream(fin);\r
                        newTemplateList = (List<RecordJobTemplate>) ois.readObject();\r
+                       if (overwrite) {\r
+                               this.templates = newTemplateList;\r
+                       } else {\r
+                               this.templates.addAll(newTemplateList);\r
+                       }\r
+                       return newTemplateList.size();\r
                } catch (Exception e) {\r
                        DemoRecorderUtils.showNonCriticalErrorDialog("Could not load the templates from file " + path.getAbsolutePath(), e, true);\r
-                       return;\r
+                       return 0;\r
+               }\r
+               \r
+       }\r
+       \r
+       public void loadNewTemplateList(SwingGUI gui, File path, JXTable templatesTable) {\r
+               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
+               boolean overwrite = false;\r
+               if (result == JOptionPane.YES_OPTION) {\r
+                       overwrite = true;\r
+               }\r
+               int count = loadTemplateListFromFile(path, overwrite);\r
+               fireTableDataChanged();\r
+               if (count > 0) {\r
+                       templatesTable.setRowSelectionInterval(templatesTable.getRowCount() - count, templatesTable.getRowCount() - 1);\r
                }\r
-               this.templates = newTemplateList;\r
-//             fireTableRowsInserted(0, this.templates.size());\r
        }\r
 \r
        public Object getValueAt(int rowIndex, int columnIndex) {\r
index 2d2f5fc..d789696 100644 (file)
@@ -3,12 +3,16 @@ package com.nexuiz.demorecorder.ui.swinggui.tablemodels;
 import java.io.File;\r
 import java.util.List;\r
 \r
+import javax.swing.JOptionPane;\r
 import javax.swing.table.AbstractTableModel;\r
 \r
+import org.jdesktop.swingx.JXTable;\r
+\r
 import com.nexuiz.demorecorder.application.DemoRecorderApplication;\r
 import com.nexuiz.demorecorder.application.DemoRecorderException;\r
 import com.nexuiz.demorecorder.application.DemoRecorderUtils;\r
 import com.nexuiz.demorecorder.application.jobs.RecordJob;\r
+import com.nexuiz.demorecorder.ui.swinggui.SwingGUI;\r
 \r
 /**\r
  * Columns:\r
@@ -80,10 +84,18 @@ public class RecordJobsTableModel extends AbstractTableModel {
                }\r
        }\r
        \r
-       public void loadNewJobQueue(File path) {\r
-               this.appLayer.loadJobQueue(path);\r
+       public void loadNewJobQueue(SwingGUI gui, File path, JXTable jobsTable) {\r
+               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
+               boolean overwrite = false;\r
+               if (result == JOptionPane.YES_OPTION) {\r
+                       overwrite = true;\r
+               }\r
+               int count = this.appLayer.loadJobQueue(path, overwrite);\r
                this.jobList = this.appLayer.getRecordJobs();\r
                fireTableDataChanged();\r
+               if (count > 0) {\r
+                       jobsTable.setRowSelectionInterval(jobsTable.getRowCount() - count, jobsTable.getRowCount() - 1);\r
+               }\r
        }\r
        \r
        public RecordJob getRecordJob(int modelRowIndex) {\r