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;
@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
// 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;