JAL-3141 Added a 'Save Project as...' File menu item, always asks for filename. Alter...
authorBen Soares <bsoares@dundee.ac.uk>
Mon, 14 Jan 2019 23:47:38 +0000 (23:47 +0000)
committerBen Soares <bsoares@dundee.ac.uk>
Mon, 14 Jan 2019 23:47:38 +0000 (23:47 +0000)
resources/lang/Messages.properties
resources/lang/Messages_es.properties
src/jalview/gui/Desktop.java
src/jalview/gui/Jalview2XML.java
src/jalview/jbgui/GDesktop.java

index 0dec27d..0747100 100644 (file)
@@ -30,6 +30,7 @@ action.minimize_associated_windows = Minimize Associated Windows
 action.close_all = Close all
 action.load_project = Load Project
 action.save_project = Save Project
+action.save_project_as = Save Project as...
 action.quit = Quit
 action.expand_views = Expand Views
 action.gather_views = Gather Views
index e703bd2..087105f 100644 (file)
@@ -30,6 +30,7 @@ action.minimize_associated_windows = Minimizar ventanas asociadas
 action.close_all = Cerrar todo
 action.load_project = Cargar proyecto
 action.save_project = Guardar proyecto
+action.save_project_as = Guardar proyecto como...
 action.quit = Salir
 action.expand_views = Expandir vistas
 action.gather_views = Capturar vistas
index 8d9e366..6e3c11b 100644 (file)
@@ -26,6 +26,7 @@ import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
 import jalview.bin.Cache;
 import jalview.bin.Jalview;
+import jalview.io.BackupFiles;
 import jalview.io.DataSourceType;
 import jalview.io.FileFormat;
 import jalview.io.FileFormatException;
@@ -1607,20 +1608,39 @@ public class Desktop extends jalview.jbgui.GDesktop
   @Override
   public void saveState_actionPerformed(ActionEvent e)
   {
-    JalviewFileChooser chooser = new JalviewFileChooser("jvp",
-            "Jalview Project");
+    saveState_actionPerformed(e, false);
+  }
 
-    chooser.setFileView(new JalviewFileView());
-    chooser.setDialogTitle(MessageManager.getString("label.save_state"));
+  public void saveState_actionPerformed(ActionEvent e, boolean saveAs)
+  {
+    java.io.File projectFile = getProjectFile();
+    // autoSave indicates we already have a file and don't need to ask
+    boolean autoSave = projectFile != null && !saveAs && BackupFiles.getEnabled();
 
-    int value = chooser.showSaveDialog(this);
+    System.out.println("autoSave="+autoSave+", projectFile='"+projectFile+"', saveAs="+saveAs+", Backups "+(BackupFiles.getEnabled()?"enabled":"disabled"));
 
-    if (value == JalviewFileChooser.APPROVE_OPTION)
+    boolean approveSave = false;
+    if (!autoSave)
     {
-      final Desktop me = this;
-      final java.io.File choice = chooser.getSelectedFile();
-      setProjectFile(choice);
+      JalviewFileChooser chooser = new JalviewFileChooser("jvp",
+              "Jalview Project");
 
+      chooser.setFileView(new JalviewFileView());
+      chooser.setDialogTitle(MessageManager.getString("label.save_state"));
+
+      int value = chooser.showSaveDialog(this);
+
+      if (value == JalviewFileChooser.APPROVE_OPTION)
+      {
+        projectFile = chooser.getSelectedFile();
+        setProjectFile(projectFile);
+        approveSave = true;
+      }
+    }
+
+    if (approveSave || autoSave) {
+      final Desktop me = this;
+      final java.io.File chosenFile = projectFile;
       new Thread(new Runnable()
       {
         @Override
@@ -1629,38 +1649,49 @@ public class Desktop extends jalview.jbgui.GDesktop
           // TODO: refactor to Jalview desktop session controller action.
           setProgressBar(MessageManager.formatMessage(
                   "label.saving_jalview_project", new Object[]
-                  { choice.getName() }), choice.hashCode());
+                  { chosenFile.getName() }), chosenFile.hashCode());
           jalview.bin.Cache.setProperty("LAST_DIRECTORY",
-                  choice.getParent());
+                  chosenFile.getParent());
           // TODO catch and handle errors for savestate
           // TODO prevent user from messing with the Desktop whilst we're saving
           try
           {
-            new Jalview2XML().saveState(choice);
+            BackupFiles backupfiles = new BackupFiles(chosenFile);
+
+            new Jalview2XML().saveState(backupfiles.getTempFile());
+
+            backupfiles.setWriteSuccess(true);
+            backupfiles.rollBackupsAndRenameTempFile();
           } catch (OutOfMemoryError oom)
           {
             new OOMWarning(
-                    "Whilst saving current state to " + choice.getName(),
+                    "Whilst saving current state to " + chosenFile.getName(),
                     oom);
           } catch (Exception ex)
           {
             Cache.log.error(
-                    "Problems whilst trying to save to " + choice.getName(),
+                    "Problems whilst trying to save to " + chosenFile.getName(),
                     ex);
             JvOptionPane.showMessageDialog(me,
                     MessageManager.formatMessage(
                             "label.error_whilst_saving_current_state_to",
                             new Object[]
-                            { choice.getName() }),
+                            { chosenFile.getName() }),
                     MessageManager.getString("label.couldnt_save_project"),
                     JvOptionPane.WARNING_MESSAGE);
           }
-          setProgressBar(null, choice.hashCode());
+          setProgressBar(null, chosenFile.hashCode());
         }
       }).start();
     }
   }
 
+  @Override
+  public void saveAsState_actionPerformed(ActionEvent e)
+  {
+    saveState_actionPerformed(e, true);
+  }
+
   private void setProjectFile(File choice)
   {
     this.projectFile = choice;
index 2c3d2d2..a02bdfc 100644 (file)
@@ -464,14 +464,15 @@ public class Jalview2XML
     try
     {
 
-      BackupFiles backupfiles = new BackupFiles(statefile);
-      fos = new FileOutputStream(backupfiles.getTempFile());
+      //BackupFiles backupfiles = new BackupFiles(statefile);
+      //fos = new FileOutputStream(backupfiles.getTempFile());
+      fos = new FileOutputStream(statefile);
 
       JarOutputStream jout = new JarOutputStream(fos);
       saveState(jout);
 
-      backupfiles.setWriteSuccess(true);
-      backupfiles.rollBackupsAndRenameTempFile();
+      //backupfiles.setWriteSuccess(true);
+      //backupfiles.rollBackupsAndRenameTempFile();
 
     } catch (Exception e)
     {
index a4afb74..e610d9b 100755 (executable)
@@ -76,6 +76,8 @@ public class GDesktop extends JFrame
 
   JMenuItem saveState = new JMenuItem();
 
+  JMenuItem saveAsState = new JMenuItem();
+
   JMenuItem loadState = new JMenuItem();
 
   JMenu inputMenu = new JMenu();
@@ -248,6 +250,15 @@ public class GDesktop extends JFrame
         saveState_actionPerformed(e);
       }
     });
+    saveAsState.setText(MessageManager.getString("action.save_project_as"));
+    saveAsState.addActionListener(new ActionListener()
+    {
+      @Override
+      public void actionPerformed(ActionEvent e)
+      {
+        saveAsState_actionPerformed(e);
+      }
+    });
     loadState.setText(MessageManager.getString("action.load_project"));
     loadState.addActionListener(new ActionListener()
     {
@@ -424,6 +435,7 @@ public class GDesktop extends JFrame
     FileMenu.add(inputSequence);
     FileMenu.addSeparator();
     FileMenu.add(saveState);
+    FileMenu.add(saveAsState);
     FileMenu.add(loadState);
     FileMenu.addSeparator();
     FileMenu.add(quit);
@@ -590,6 +602,10 @@ public class GDesktop extends JFrame
   {
   }
 
+  public void saveAsState_actionPerformed(ActionEvent e)
+  {
+  }
+
   /**
    * DOCUMENT ME!
    *