Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Session saving.

  • Loading branch information...
commit 2d3a39629db5c4e1aedf0ebd78afbd5ff95b18c2 1 parent 331e62b
@calebegg authored
View
3  .gitignore
@@ -19,4 +19,5 @@ ctrl-c/ctrlc-windows/Debug/*
ctrl-c/ctrlc-windows/Release/*
ctrl-c/ctrlc-windows/*.sdf
ctrl-c/ctrlc-windows/*.opensdf
-ctrl-c/ctrlc-windows/*.user
+ctrl-c/ctrlc-windows/*.user
+saved_session.dat
View
4 .settings/org.eclipse.jdt.core.prefs
@@ -18,7 +18,7 @@ org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation=16
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_qualified_allocation_expression=16
org.eclipse.jdt.core.formatter.alignment_for_assignment=0
org.eclipse.jdt.core.formatter.alignment_for_binary_expression=16
-org.eclipse.jdt.core.formatter.alignment_for_compact_if=16
+org.eclipse.jdt.core.formatter.alignment_for_compact_if=0
org.eclipse.jdt.core.formatter.alignment_for_conditional_expression=80
org.eclipse.jdt.core.formatter.alignment_for_enum_constants=0
org.eclipse.jdt.core.formatter.alignment_for_expressions_in_array_initializer=16
@@ -286,6 +286,6 @@ org.eclipse.jdt.core.formatter.tabulation.char=tab
org.eclipse.jdt.core.formatter.tabulation.size=4
org.eclipse.jdt.core.formatter.use_on_off_tags=false
org.eclipse.jdt.core.formatter.use_tabs_only_for_leading_indentations=false
-org.eclipse.jdt.core.formatter.wrap_before_binary_operator=true
+org.eclipse.jdt.core.formatter.wrap_before_binary_operator=false
org.eclipse.jdt.core.formatter.wrap_before_or_operator_multicatch=true
org.eclipse.jdt.core.formatter.wrap_outer_expressions_when_nested=true
View
2  src/org/proofpad/ErrorWindow.java
@@ -80,7 +80,7 @@ public ErrorWindow(Throwable e) {
opts,
opts[1]);
if (choice == JOptionPane.OK_OPTION) {
- Main.quit();
+ Main.saveUserDataAndExit();
}
}
View
63 src/org/proofpad/Main.java
@@ -54,14 +54,16 @@
System.getProperty("os.name").toLowerCase().indexOf("windows") != -1;
public static final boolean JAVA_7 = System.getProperty("java.version").startsWith("1.7");
- private static String userDataPath = new File(getJarPath()).getParent() +
+ static final String SESSION_PATH = new File(getJarPath()).getParent() +
+ System.getProperty("file.separator") + "saved_session.dat";
+ static final String USER_DATA_PATH = new File(getJarPath()).getParent() +
System.getProperty("file.separator") +
"user_data.dat";
public static final String UPLOAD_URL = "http://www.calebegg.com/ppuserdata";
public static UserData userData = null;
static {
try {
- ObjectInputStream ois = new ObjectInputStream(new FileInputStream(userDataPath));
+ ObjectInputStream ois = new ObjectInputStream(new FileInputStream(USER_DATA_PATH));
userData = (UserData) ois.readObject();
ois.close();
} catch (Exception e) {
@@ -70,7 +72,6 @@
if (userData == null) {
userData = new UserData();
}
- System.out.println(userData);
}
public static boolean startingUp = true;
@@ -125,15 +126,32 @@ public static void main(String[] args) throws FileNotFoundException, IOException
app.setQuitHandler(new QuitHandler() {
@Override public void handleQuitRequestWith(QuitEvent qe,
QuitResponse qr) {
- for (Iterator<PPWindow> ii = PPWindow.windows.iterator(); ii.hasNext();) {
- PPWindow win = ii.next();
- if (!win.promptIfUnsavedAndQuit(ii)) {
- break;
+ boolean needToCloseWindows = true;
+ if (Prefs.saveSession.get()) {
+ needToCloseWindows = false;
+ Session session = new Session(PPWindow.windows);
+ ObjectOutputStream oos;
+ try {
+ oos = new ObjectOutputStream(new FileOutputStream(
+ SESSION_PATH));
+ oos.writeObject(session);
+ oos.close();
+ } catch (Exception e) {
+ e.printStackTrace();
+ needToCloseWindows = true;
+ }
+ }
+ if (needToCloseWindows) {
+ for (Iterator<PPWindow> ii = PPWindow.windows.iterator(); ii.hasNext();) {
+ PPWindow win = ii.next();
+ if (!win.promptIfUnsavedAndQuit(ii)) {
+ break;
+ }
}
}
PPWindow.updateWindowMenu();
- if (PPWindow.windows.size() <= 0) {
- quit();
+ if (!needToCloseWindows || PPWindow.windows.size() <= 0) {
+ saveUserDataAndExit();
} else {
qr.cancelQuit();
}
@@ -147,7 +165,7 @@ public static void main(String[] args) throws FileNotFoundException, IOException
app.addAppEventListener(new AppForegroundListener() {
@Override public void appMovedToBackground(AppForegroundEvent arg0) {
if (PPWindow.windows.size() == 0 && !startingUp) {
- Main.quit();
+ Main.saveUserDataAndExit();
}
}
@Override public void appRaisedToForeground(AppForegroundEvent arg0) {}
@@ -182,7 +200,24 @@ public static void main(String[] args) throws FileNotFoundException, IOException
}
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() {
- logtime("Start creating main window");
+ Prefs.saveSession.set(true);
+ logtime("Start restoring session");
+ if (Prefs.saveSession.get()) {
+ try {
+ ObjectInputStream sessionRestoreStream = new ObjectInputStream(
+ new FileInputStream(SESSION_PATH));
+ Session session = (Session) sessionRestoreStream.readObject();
+ sessionRestoreStream.close();
+ session.restore();
+ } catch (Exception e) {
+ e.printStackTrace();
+ Prefs.saveSession.set(false);
+ JOptionPane.showMessageDialog(null, "Failed to restore from saved session. " +
+ "Session saving has been turned off to prevent any future data loss.",
+ "Session restore failed", JOptionPane.ERROR_MESSAGE);
+ }
+ }
+ logtime("Session restore complete.");
if (PPWindow.windows.isEmpty()) {
PPWindow win = new PPWindow();
startingUp = false;
@@ -226,11 +261,11 @@ public static void main(String[] args) throws FileNotFoundException, IOException
});
}
- protected static void quit() {
+ protected static void saveUserDataAndExit() {
ObjectOutputStream oos;
// System.out.println("Quitting");
try {
- oos = new ObjectOutputStream(new FileOutputStream(userDataPath));
+ oos = new ObjectOutputStream(new FileOutputStream(USER_DATA_PATH));
oos.writeObject(userData);
oos.close();
} catch (Exception e) {
@@ -241,8 +276,6 @@ protected static void quit() {
static String getJarPath() {
try {
- System.out.println(URLDecoder.decode(Main.class.getProtectionDomain().getCodeSource().getLocation()
- .getPath(), "UTF-8"));
return URLDecoder.decode(Main.class.getProtectionDomain().getCodeSource().getLocation()
.getPath(), "UTF-8");
} catch (UnsupportedEncodingException e) { }
View
1  src/org/proofpad/OutputWindow.java
@@ -88,7 +88,6 @@ public void showWithText(String output, Runnable after) {
.getLayoutComponent(BorderLayout.PAGE_START);
if (oldPgStartComp != null) getRootPane().remove(oldPgStartComp);
getRootPane().add(summaries, BorderLayout.PAGE_START);
-
if (afterPreview != null) afterPreview.run();
afterPreview = after;
Component oldCenterComp = ((BorderLayout) getRootPane().getLayout())
View
6 src/org/proofpad/PPWindow.java
@@ -650,7 +650,7 @@ boolean promptIfUnsavedAndQuit(Iterator<PPWindow> ii) {
windows.remove(PPWindow.this);
}
if (windows.size() == 0 && !Main.OSX) {
- Main.quit();
+ Main.saveUserDataAndExit();
}
return true;
}
@@ -728,6 +728,10 @@ boolean saveFile() {
setTitle(openFile.getName() + (!Main.OSX ? " - Proof Pad" : ""));
} catch (IOException e) {
e.printStackTrace();
+ JOptionPane.showMessageDialog(this, "Saving to " + openFile + " failed. Make sure you" +
+ " have permission to write to this location, or try a different location." +
+ e.getMessage(), "Error saving", JOptionPane.ERROR_MESSAGE);
+ setSaved(false);
}
return true;
}
View
1  src/org/proofpad/Prefs.java
@@ -12,6 +12,7 @@
public static final BooleanPref customAcl2 = new BooleanPref("customacl2", false);
public static final BooleanPref firstRun = new BooleanPref("firstRun", true);
public static final BooleanPref showLineNumbers = new BooleanPref("linenums", false);
+ public static final BooleanPref saveSession = new BooleanPref("savesession", true);
public static final IntPref alwaysSend = new IntPref("alwaysSend", Codes.ASK_EVERY_TIME);
public static final IntPref fontSize = new IntPref("fontsize", 12);
public static final IntPref widthGuide = new IntPref("widthguide", 60);
View
53 src/org/proofpad/Session.java
@@ -0,0 +1,53 @@
+package org.proofpad;
+
+import java.awt.Rectangle;
+import java.io.File;
+import java.io.Serializable;
+import java.util.List;
+
+public class Session implements Serializable {
+ private static final long serialVersionUID = 4806457808825043549L;
+
+ private static class SavedWindow implements Serializable {
+ public SavedWindow() { }
+ private static final long serialVersionUID = -3654242987588009951L;
+ File file;
+ Rectangle location;
+ int admittedLoc;
+ String contents;
+ }
+
+ private final SavedWindow[] savedWindows;
+
+ public Session(List<PPWindow> windows) {
+ savedWindows = new SavedWindow[windows.size()];
+ int i = 0;
+ for (PPWindow win : windows) {
+ SavedWindow sw = new SavedWindow();
+ sw.file = win.openFile;
+ sw.location = win.getBounds();
+ sw.admittedLoc = win.proofBar.getReadOnlyIndex();
+ if (!win.isSaved) {
+ sw.contents = win.editor.getText();
+ } else {
+ sw.contents = null;
+ }
+ savedWindows[i] = sw;
+ i++;
+ }
+ }
+
+ public void restore() {
+ for (SavedWindow sw : savedWindows) {
+ if (sw == null) continue;
+ PPWindow win = new PPWindow(sw.file);
+ win.setBounds(sw.location);
+ if (sw.contents != null) {
+ win.editor.setText(sw.contents);
+ }
+ win.setVisible(true);
+ }
+ }
+
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.