mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-10 17:22:09 +10:00
auto-save added
This commit is contained in:
+3
-1
@@ -41,6 +41,7 @@ public interface IPlugInPort {
|
||||
public static final String AUTO_PADS_KEY = "autoCreatePads";
|
||||
public static final String CONTINUOUS_CREATION_KEY = "continuousCreation";
|
||||
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
|
||||
@@ -96,8 +97,9 @@ public interface IPlugInPort {
|
||||
* Saves the current project into the specified file.
|
||||
*
|
||||
* @param fileName
|
||||
* @param isBackup
|
||||
*/
|
||||
void saveProjectToFile(String fileName);
|
||||
void saveProjectToFile(String fileName, boolean isBackup);
|
||||
|
||||
/**
|
||||
* @return the current file name.
|
||||
|
||||
+6
-5
@@ -242,10 +242,10 @@ public class Presenter implements IPlugInPort {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveProjectToFile(String fileName) {
|
||||
public void saveProjectToFile(String fileName, boolean isBackup) {
|
||||
LOG.info(String.format("saveProjectToFile(%s)", fileName));
|
||||
try {
|
||||
projectFileManager.serializeProjectToFile(currentProject, fileName);
|
||||
projectFileManager.serializeProjectToFile(currentProject, fileName, isBackup);
|
||||
} catch (Exception ex) {
|
||||
LOG.error("Could not save file", ex);
|
||||
view.showMessage("Could not save file " + fileName + ". Check the log for details.",
|
||||
@@ -385,7 +385,7 @@ public class Presenter implements IPlugInPort {
|
||||
LOG.error("Error instatiating component of type: "
|
||||
+ componentTypeSlot.getInstanceClass().getName(), e);
|
||||
}
|
||||
|
||||
|
||||
if (ConfigurationManager.getInstance().readBoolean(IPlugInPort.AUTO_EDIT_KEY,
|
||||
false)) {
|
||||
editSelection();
|
||||
@@ -429,8 +429,9 @@ public class Presenter implements IPlugInPort {
|
||||
}
|
||||
updateSelection(newSelection);
|
||||
messageDispatcher.dispatchMessage(EventType.REPAINT);
|
||||
|
||||
if (ConfigurationManager.getInstance().readBoolean(IPlugInPort.AUTO_EDIT_KEY, false)) {
|
||||
|
||||
if (ConfigurationManager.getInstance().readBoolean(
|
||||
IPlugInPort.AUTO_EDIT_KEY, false)) {
|
||||
editSelection();
|
||||
}
|
||||
if (ConfigurationManager.getInstance().readBoolean(
|
||||
|
||||
+10
-5
@@ -87,15 +87,20 @@ public class ProjectFileManager {
|
||||
fireFileStatusChanged();
|
||||
}
|
||||
|
||||
public void serializeProjectToFile(Project project, String fileName) throws IOException {
|
||||
LOG.info(String.format("saveProjectToFile(%s)", fileName));
|
||||
public void serializeProjectToFile(Project project, String fileName, boolean isBackup)
|
||||
throws IOException {
|
||||
if (!isBackup) {
|
||||
LOG.info(String.format("saveProjectToFile(%s)", fileName));
|
||||
}
|
||||
FileOutputStream fos;
|
||||
fos = new FileOutputStream(fileName);
|
||||
xStream.toXML(project, fos);
|
||||
fos.close();
|
||||
this.currentFileName = fileName;
|
||||
this.modified = false;
|
||||
fireFileStatusChanged();
|
||||
if (!isBackup) {
|
||||
this.currentFileName = fileName;
|
||||
this.modified = false;
|
||||
fireFileStatusChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public Project deserializeProjectFromFile(String fileName, List<String> warnings)
|
||||
|
||||
+3
-3
@@ -268,7 +268,7 @@ public class ActionFactory {
|
||||
@Override
|
||||
public Void doInBackground() throws Exception {
|
||||
LOG.debug("Saving to " + file.getAbsolutePath());
|
||||
plugInPort.saveProjectToFile(file.getAbsolutePath());
|
||||
plugInPort.saveProjectToFile(file.getAbsolutePath(), false);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -289,7 +289,7 @@ public class ActionFactory {
|
||||
@Override
|
||||
public Void doInBackground() throws Exception {
|
||||
LOG.debug("Saving to " + plugInPort.getCurrentFileName());
|
||||
plugInPort.saveProjectToFile(plugInPort.getCurrentFileName());
|
||||
plugInPort.saveProjectToFile(plugInPort.getCurrentFileName(), false);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -336,7 +336,7 @@ public class ActionFactory {
|
||||
@Override
|
||||
public Void doInBackground() throws Exception {
|
||||
LOG.debug("Saving to " + file.getAbsolutePath());
|
||||
plugInPort.saveProjectToFile(file.getAbsolutePath());
|
||||
plugInPort.saveProjectToFile(file.getAbsolutePath(), false);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
+9
-1
@@ -43,6 +43,7 @@ import org.diylc.images.IconLoader;
|
||||
import org.diylc.presenter.Presenter;
|
||||
import org.diylc.swing.ISwingUI;
|
||||
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.config.ConfigPlugin;
|
||||
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 com.diyfever.gui.ButtonDialog;
|
||||
import com.diyfever.gui.miscutils.ConfigurationManager;
|
||||
|
||||
public class MainFrame extends JFrame implements ISwingUI {
|
||||
|
||||
@@ -99,6 +101,7 @@ public class MainFrame extends JFrame implements ISwingUI {
|
||||
canvasPlugin = new CanvasPlugin(this);
|
||||
presenter.installPlugin(canvasPlugin);
|
||||
presenter.installPlugin(new FramePlugin());
|
||||
presenter.installPlugin(new AutoSavePlugin(this));
|
||||
|
||||
presenter.createNewProject();
|
||||
|
||||
@@ -107,6 +110,8 @@ public class MainFrame extends JFrame implements ISwingUI {
|
||||
@Override
|
||||
public void windowClosed(WindowEvent e) {
|
||||
if (presenter.allowFileAction()) {
|
||||
ConfigurationManager.getInstance().writeValue(IPlugInPort.ABNORMAL_EXIT_KEY,
|
||||
false);
|
||||
dispose();
|
||||
presenter.dispose();
|
||||
System.exit(0);
|
||||
@@ -116,6 +121,8 @@ public class MainFrame extends JFrame implements ISwingUI {
|
||||
@Override
|
||||
public void windowClosing(WindowEvent e) {
|
||||
if (presenter.allowFileAction()) {
|
||||
ConfigurationManager.getInstance().writeValue(IPlugInPort.ABNORMAL_EXIT_KEY,
|
||||
false);
|
||||
dispose();
|
||||
presenter.dispose();
|
||||
System.exit(0);
|
||||
@@ -203,7 +210,8 @@ public class MainFrame extends JFrame implements ISwingUI {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean editProperties(List<PropertyWrapper> properties, Set<PropertyWrapper> defaultedProperties) {
|
||||
public boolean editProperties(List<PropertyWrapper> properties,
|
||||
Set<PropertyWrapper> defaultedProperties) {
|
||||
PropertyEditorDialog editor = DialogFactory.getInstance().createPropertyEditorDialog(
|
||||
properties, "Edit Selection");
|
||||
editor.setVisible(true);
|
||||
|
||||
+74
@@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user