auto-save added

This commit is contained in:
bancika
2011-02-26 11:26:49 +00:00
parent ab5c9ad143
commit a0b7cdc30e
8 changed files with 128 additions and 30 deletions
@@ -1,8 +1,4 @@
<map> <map>
<entry>
<string>autoEdit</string>
<boolean>true</boolean>
</entry>
<entry> <entry>
<string>default.org.diylc.components.passive.TrimmerPotentiometer:Type</string> <string>default.org.diylc.components.passive.TrimmerPotentiometer:Type</string>
<org.diylc.components.passive.TrimmerPotentiometer_-TrimmerType>FLAT_LARGE</org.diylc.components.passive.TrimmerPotentiometer_-TrimmerType> <org.diylc.components.passive.TrimmerPotentiometer_-TrimmerType>FLAT_LARGE</org.diylc.components.passive.TrimmerPotentiometer_-TrimmerType>
@@ -12,9 +8,13 @@
<string>C:\Documents and Settings\Branislav Stojkovic\Desktop</string> <string>C:\Documents and Settings\Branislav Stojkovic\Desktop</string>
</entry> </entry>
<entry> <entry>
<string>autoCreatePads</string> <string>abnormalExit</string>
<boolean>false</boolean> <boolean>false</boolean>
</entry> </entry>
<entry>
<string>stickyPoints</string>
<boolean>true</boolean>
</entry>
<entry> <entry>
<string>theme</string> <string>theme</string>
<org.diylc.core.Theme> <org.diylc.core.Theme>
@@ -33,16 +33,12 @@
</gridColor> </gridColor>
</org.diylc.core.Theme> </org.diylc.core.Theme>
</entry> </entry>
<entry>
<string>stickyPoints</string>
<boolean>true</boolean>
</entry>
<entry> <entry>
<string>antiAliasing</string> <string>antiAliasing</string>
<boolean>true</boolean> <boolean>true</boolean>
</entry> </entry>
<entry> <entry>
<string>continuousCreation</string> <string>snapToGrid</string>
<boolean>true</boolean> <boolean>true</boolean>
</entry> </entry>
<entry> <entry>
@@ -50,15 +46,23 @@
<boolean>false</boolean> <boolean>false</boolean>
</entry> </entry>
<entry> <entry>
<string>snapToGrid</string> <string>exportGrid</string>
<boolean>true</boolean> <boolean>true</boolean>
</entry> </entry>
<entry>
<string>autoEdit</string>
<boolean>false</boolean>
</entry>
<entry>
<string>autoCreatePads</string>
<boolean>false</boolean>
</entry>
<entry>
<string>continuousCreation</string>
<boolean>false</boolean>
</entry>
<entry> <entry>
<string>showTemplatesAtStartup</string> <string>showTemplatesAtStartup</string>
<boolean>false</boolean> <boolean>false</boolean>
</entry> </entry>
<entry>
<string>exportGrid</string>
<boolean>true</boolean>
</entry>
</map> </map>
@@ -41,6 +41,7 @@ public interface IPlugInPort {
public static final String AUTO_PADS_KEY = "autoCreatePads"; public static final String AUTO_PADS_KEY = "autoCreatePads";
public static final String CONTINUOUS_CREATION_KEY = "continuousCreation"; public static final String CONTINUOUS_CREATION_KEY = "continuousCreation";
public static final String AUTO_EDIT_KEY = "autoEdit"; public static final String AUTO_EDIT_KEY = "autoEdit";
public static final String ABNORMAL_EXIT_KEY = "abnormalExit";
/** /**
* Returns size of the canvas that takes project dimensions into account as * Returns size of the canvas that takes project dimensions into account as
@@ -96,8 +97,9 @@ public interface IPlugInPort {
* Saves the current project into the specified file. * Saves the current project into the specified file.
* *
* @param fileName * @param fileName
* @param isBackup
*/ */
void saveProjectToFile(String fileName); void saveProjectToFile(String fileName, boolean isBackup);
/** /**
* @return the current file name. * @return the current file name.
@@ -242,10 +242,10 @@ public class Presenter implements IPlugInPort {
} }
@Override @Override
public void saveProjectToFile(String fileName) { public void saveProjectToFile(String fileName, boolean isBackup) {
LOG.info(String.format("saveProjectToFile(%s)", fileName)); LOG.info(String.format("saveProjectToFile(%s)", fileName));
try { try {
projectFileManager.serializeProjectToFile(currentProject, fileName); projectFileManager.serializeProjectToFile(currentProject, fileName, isBackup);
} catch (Exception ex) { } catch (Exception ex) {
LOG.error("Could not save file", ex); LOG.error("Could not save file", ex);
view.showMessage("Could not save file " + fileName + ". Check the log for details.", view.showMessage("Could not save file " + fileName + ". Check the log for details.",
@@ -430,7 +430,8 @@ public class Presenter implements IPlugInPort {
updateSelection(newSelection); updateSelection(newSelection);
messageDispatcher.dispatchMessage(EventType.REPAINT); messageDispatcher.dispatchMessage(EventType.REPAINT);
if (ConfigurationManager.getInstance().readBoolean(IPlugInPort.AUTO_EDIT_KEY, false)) { if (ConfigurationManager.getInstance().readBoolean(
IPlugInPort.AUTO_EDIT_KEY, false)) {
editSelection(); editSelection();
} }
if (ConfigurationManager.getInstance().readBoolean( if (ConfigurationManager.getInstance().readBoolean(
@@ -87,16 +87,21 @@ public class ProjectFileManager {
fireFileStatusChanged(); fireFileStatusChanged();
} }
public void serializeProjectToFile(Project project, String fileName) throws IOException { public void serializeProjectToFile(Project project, String fileName, boolean isBackup)
throws IOException {
if (!isBackup) {
LOG.info(String.format("saveProjectToFile(%s)", fileName)); LOG.info(String.format("saveProjectToFile(%s)", fileName));
}
FileOutputStream fos; FileOutputStream fos;
fos = new FileOutputStream(fileName); fos = new FileOutputStream(fileName);
xStream.toXML(project, fos); xStream.toXML(project, fos);
fos.close(); fos.close();
if (!isBackup) {
this.currentFileName = fileName; this.currentFileName = fileName;
this.modified = false; this.modified = false;
fireFileStatusChanged(); fireFileStatusChanged();
} }
}
public Project deserializeProjectFromFile(String fileName, List<String> warnings) public Project deserializeProjectFromFile(String fileName, List<String> warnings)
throws SAXException, IOException, ParserConfigurationException { throws SAXException, IOException, ParserConfigurationException {
@@ -268,7 +268,7 @@ public class ActionFactory {
@Override @Override
public Void doInBackground() throws Exception { public Void doInBackground() throws Exception {
LOG.debug("Saving to " + file.getAbsolutePath()); LOG.debug("Saving to " + file.getAbsolutePath());
plugInPort.saveProjectToFile(file.getAbsolutePath()); plugInPort.saveProjectToFile(file.getAbsolutePath(), false);
return null; return null;
} }
@@ -289,7 +289,7 @@ public class ActionFactory {
@Override @Override
public Void doInBackground() throws Exception { public Void doInBackground() throws Exception {
LOG.debug("Saving to " + plugInPort.getCurrentFileName()); LOG.debug("Saving to " + plugInPort.getCurrentFileName());
plugInPort.saveProjectToFile(plugInPort.getCurrentFileName()); plugInPort.saveProjectToFile(plugInPort.getCurrentFileName(), false);
return null; return null;
} }
@@ -336,7 +336,7 @@ public class ActionFactory {
@Override @Override
public Void doInBackground() throws Exception { public Void doInBackground() throws Exception {
LOG.debug("Saving to " + file.getAbsolutePath()); LOG.debug("Saving to " + file.getAbsolutePath());
plugInPort.saveProjectToFile(file.getAbsolutePath()); plugInPort.saveProjectToFile(file.getAbsolutePath(), false);
return null; return null;
} }
@@ -43,6 +43,7 @@ import org.diylc.images.IconLoader;
import org.diylc.presenter.Presenter; import org.diylc.presenter.Presenter;
import org.diylc.swing.ISwingUI; import org.diylc.swing.ISwingUI;
import org.diylc.swing.gui.editor.PropertyEditorDialog; import org.diylc.swing.gui.editor.PropertyEditorDialog;
import org.diylc.swing.plugins.autosave.AutoSavePlugin;
import org.diylc.swing.plugins.canvas.CanvasPlugin; import org.diylc.swing.plugins.canvas.CanvasPlugin;
import org.diylc.swing.plugins.config.ConfigPlugin; import org.diylc.swing.plugins.config.ConfigPlugin;
import org.diylc.swing.plugins.edit.EditMenuPlugin; import org.diylc.swing.plugins.edit.EditMenuPlugin;
@@ -53,6 +54,7 @@ import org.diylc.swing.plugins.statusbar.StatusBar;
import org.diylc.swing.plugins.toolbox.ToolBox; import org.diylc.swing.plugins.toolbox.ToolBox;
import com.diyfever.gui.ButtonDialog; import com.diyfever.gui.ButtonDialog;
import com.diyfever.gui.miscutils.ConfigurationManager;
public class MainFrame extends JFrame implements ISwingUI { public class MainFrame extends JFrame implements ISwingUI {
@@ -99,6 +101,7 @@ public class MainFrame extends JFrame implements ISwingUI {
canvasPlugin = new CanvasPlugin(this); canvasPlugin = new CanvasPlugin(this);
presenter.installPlugin(canvasPlugin); presenter.installPlugin(canvasPlugin);
presenter.installPlugin(new FramePlugin()); presenter.installPlugin(new FramePlugin());
presenter.installPlugin(new AutoSavePlugin(this));
presenter.createNewProject(); presenter.createNewProject();
@@ -107,6 +110,8 @@ public class MainFrame extends JFrame implements ISwingUI {
@Override @Override
public void windowClosed(WindowEvent e) { public void windowClosed(WindowEvent e) {
if (presenter.allowFileAction()) { if (presenter.allowFileAction()) {
ConfigurationManager.getInstance().writeValue(IPlugInPort.ABNORMAL_EXIT_KEY,
false);
dispose(); dispose();
presenter.dispose(); presenter.dispose();
System.exit(0); System.exit(0);
@@ -116,6 +121,8 @@ public class MainFrame extends JFrame implements ISwingUI {
@Override @Override
public void windowClosing(WindowEvent e) { public void windowClosing(WindowEvent e) {
if (presenter.allowFileAction()) { if (presenter.allowFileAction()) {
ConfigurationManager.getInstance().writeValue(IPlugInPort.ABNORMAL_EXIT_KEY,
false);
dispose(); dispose();
presenter.dispose(); presenter.dispose();
System.exit(0); System.exit(0);
@@ -203,7 +210,8 @@ public class MainFrame extends JFrame implements ISwingUI {
} }
@Override @Override
public boolean editProperties(List<PropertyWrapper> properties, Set<PropertyWrapper> defaultedProperties) { public boolean editProperties(List<PropertyWrapper> properties,
Set<PropertyWrapper> defaultedProperties) {
PropertyEditorDialog editor = DialogFactory.getInstance().createPropertyEditorDialog( PropertyEditorDialog editor = DialogFactory.getInstance().createPropertyEditorDialog(
properties, "Edit Selection"); properties, "Edit Selection");
editor.setVisible(true); editor.setVisible(true);
@@ -0,0 +1,74 @@
package org.diylc.swing.plugins.autosave;
import java.io.File;
import java.util.EnumSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.SwingUtilities;
import org.diylc.common.EventType;
import org.diylc.common.IPlugIn;
import org.diylc.common.IPlugInPort;
import org.diylc.core.IView;
import com.diyfever.gui.miscutils.ConfigurationManager;
public class AutoSavePlugin implements IPlugIn {
private static final String AUTO_SAVE_FILE_NAME = "autoSave.diy";
private ExecutorService executor;
private IPlugInPort plugInPort;
private IView view;
public AutoSavePlugin(IView view) {
this.view = view;
executor = Executors.newSingleThreadExecutor();
}
@Override
public void connect(IPlugInPort plugInPort) {
this.plugInPort = plugInPort;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
boolean wasAbnormal = ConfigurationManager.getInstance().readBoolean(
IPlugInPort.ABNORMAL_EXIT_KEY, false);
if (wasAbnormal && new File(AUTO_SAVE_FILE_NAME).exists()) {
int decision = view
.showConfirmDialog(
"It appears that aplication was not closed normally in the previous session. Do you want to open the last auto-saved file?",
"Auto-Save", IView.YES_NO_OPTION, IView.QUESTION_MESSAGE);
if (decision == IView.YES_OPTION) {
AutoSavePlugin.this.plugInPort.loadProjectFromFile(AUTO_SAVE_FILE_NAME);
}
}
// Set abnormal flag to true, GUI side of the app must flip to
// false
// when app closes regularly.
ConfigurationManager.getInstance().writeValue(IPlugInPort.ABNORMAL_EXIT_KEY, true);
}
});
}
@Override
public EnumSet<EventType> getSubscribedEventTypes() {
return EnumSet.of(EventType.PROJECT_MODIFIED);
}
@Override
public void processMessage(EventType eventType, Object... params) {
if (eventType == EventType.PROJECT_MODIFIED) {
executor.execute(new Runnable() {
@Override
public void run() {
plugInPort.saveProjectToFile(AUTO_SAVE_FILE_NAME, true);
}
});
}
}
}
@@ -236,6 +236,10 @@
<changeType>IMPROVEMENT</changeType> <changeType>IMPROVEMENT</changeType>
<description>Send to Back/Bring to Front are more flexible</description> <description>Send to Back/Bring to Front are more flexible</description>
</com.diyfever.gui.update.Change> </com.diyfever.gui.update.Change>
<com.diyfever.gui.update.Change>
<changeType>NEW_FEATURE</changeType>
<description>Auto-save project and restore if app crashes</description>
</com.diyfever.gui.update.Change>
<com.diyfever.gui.update.Change> <com.diyfever.gui.update.Change>
<changeType>NEW_FEATURE</changeType> <changeType>NEW_FEATURE</changeType>
<description>Added support for themes</description> <description>Added support for themes</description>