Skip to content
This repository
Browse code

Session saving.

  • Loading branch information...
commit 2d3a39629db5c4e1aedf0ebd78afbd5ff95b18c2 1 parent 331e62b
Caleb Eggensperger authored
3  .gitignore
@@ -19,4 +19,5 @@ ctrl-c/ctrlc-windows/Debug/*
19 19 ctrl-c/ctrlc-windows/Release/*
20 20 ctrl-c/ctrlc-windows/*.sdf
21 21 ctrl-c/ctrlc-windows/*.opensdf
22   -ctrl-c/ctrlc-windows/*.user
  22 +ctrl-c/ctrlc-windows/*.user
  23 +saved_session.dat
4 .settings/org.eclipse.jdt.core.prefs
@@ -18,7 +18,7 @@ org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation=16
18 18 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_qualified_allocation_expression=16
19 19 org.eclipse.jdt.core.formatter.alignment_for_assignment=0
20 20 org.eclipse.jdt.core.formatter.alignment_for_binary_expression=16
21   -org.eclipse.jdt.core.formatter.alignment_for_compact_if=16
  21 +org.eclipse.jdt.core.formatter.alignment_for_compact_if=0
22 22 org.eclipse.jdt.core.formatter.alignment_for_conditional_expression=80
23 23 org.eclipse.jdt.core.formatter.alignment_for_enum_constants=0
24 24 org.eclipse.jdt.core.formatter.alignment_for_expressions_in_array_initializer=16
@@ -286,6 +286,6 @@ org.eclipse.jdt.core.formatter.tabulation.char=tab
286 286 org.eclipse.jdt.core.formatter.tabulation.size=4
287 287 org.eclipse.jdt.core.formatter.use_on_off_tags=false
288 288 org.eclipse.jdt.core.formatter.use_tabs_only_for_leading_indentations=false
289   -org.eclipse.jdt.core.formatter.wrap_before_binary_operator=true
  289 +org.eclipse.jdt.core.formatter.wrap_before_binary_operator=false
290 290 org.eclipse.jdt.core.formatter.wrap_before_or_operator_multicatch=true
291 291 org.eclipse.jdt.core.formatter.wrap_outer_expressions_when_nested=true
2  src/org/proofpad/ErrorWindow.java
@@ -80,7 +80,7 @@ public ErrorWindow(Throwable e) {
80 80 opts,
81 81 opts[1]);
82 82 if (choice == JOptionPane.OK_OPTION) {
83   - Main.quit();
  83 + Main.saveUserDataAndExit();
84 84 }
85 85
86 86 }
63 src/org/proofpad/Main.java
@@ -54,14 +54,16 @@
54 54 System.getProperty("os.name").toLowerCase().indexOf("windows") != -1;
55 55 public static final boolean JAVA_7 = System.getProperty("java.version").startsWith("1.7");
56 56
57   - private static String userDataPath = new File(getJarPath()).getParent() +
  57 + static final String SESSION_PATH = new File(getJarPath()).getParent() +
  58 + System.getProperty("file.separator") + "saved_session.dat";
  59 + static final String USER_DATA_PATH = new File(getJarPath()).getParent() +
58 60 System.getProperty("file.separator") +
59 61 "user_data.dat";
60 62 public static final String UPLOAD_URL = "http://www.calebegg.com/ppuserdata";
61 63 public static UserData userData = null;
62 64 static {
63 65 try {
64   - ObjectInputStream ois = new ObjectInputStream(new FileInputStream(userDataPath));
  66 + ObjectInputStream ois = new ObjectInputStream(new FileInputStream(USER_DATA_PATH));
65 67 userData = (UserData) ois.readObject();
66 68 ois.close();
67 69 } catch (Exception e) {
@@ -70,7 +72,6 @@
70 72 if (userData == null) {
71 73 userData = new UserData();
72 74 }
73   - System.out.println(userData);
74 75 }
75 76
76 77 public static boolean startingUp = true;
@@ -125,15 +126,32 @@ public static void main(String[] args) throws FileNotFoundException, IOException
125 126 app.setQuitHandler(new QuitHandler() {
126 127 @Override public void handleQuitRequestWith(QuitEvent qe,
127 128 QuitResponse qr) {
128   - for (Iterator<PPWindow> ii = PPWindow.windows.iterator(); ii.hasNext();) {
129   - PPWindow win = ii.next();
130   - if (!win.promptIfUnsavedAndQuit(ii)) {
131   - break;
  129 + boolean needToCloseWindows = true;
  130 + if (Prefs.saveSession.get()) {
  131 + needToCloseWindows = false;
  132 + Session session = new Session(PPWindow.windows);
  133 + ObjectOutputStream oos;
  134 + try {
  135 + oos = new ObjectOutputStream(new FileOutputStream(
  136 + SESSION_PATH));
  137 + oos.writeObject(session);
  138 + oos.close();
  139 + } catch (Exception e) {
  140 + e.printStackTrace();
  141 + needToCloseWindows = true;
  142 + }
  143 + }
  144 + if (needToCloseWindows) {
  145 + for (Iterator<PPWindow> ii = PPWindow.windows.iterator(); ii.hasNext();) {
  146 + PPWindow win = ii.next();
  147 + if (!win.promptIfUnsavedAndQuit(ii)) {
  148 + break;
  149 + }
132 150 }
133 151 }
134 152 PPWindow.updateWindowMenu();
135   - if (PPWindow.windows.size() <= 0) {
136   - quit();
  153 + if (!needToCloseWindows || PPWindow.windows.size() <= 0) {
  154 + saveUserDataAndExit();
137 155 } else {
138 156 qr.cancelQuit();
139 157 }
@@ -147,7 +165,7 @@ public static void main(String[] args) throws FileNotFoundException, IOException
147 165 app.addAppEventListener(new AppForegroundListener() {
148 166 @Override public void appMovedToBackground(AppForegroundEvent arg0) {
149 167 if (PPWindow.windows.size() == 0 && !startingUp) {
150   - Main.quit();
  168 + Main.saveUserDataAndExit();
151 169 }
152 170 }
153 171 @Override public void appRaisedToForeground(AppForegroundEvent arg0) {}
@@ -182,7 +200,24 @@ public static void main(String[] args) throws FileNotFoundException, IOException
182 200 }
183 201 SwingUtilities.invokeLater(new Runnable() {
184 202 @Override public void run() {
185   - logtime("Start creating main window");
  203 + Prefs.saveSession.set(true);
  204 + logtime("Start restoring session");
  205 + if (Prefs.saveSession.get()) {
  206 + try {
  207 + ObjectInputStream sessionRestoreStream = new ObjectInputStream(
  208 + new FileInputStream(SESSION_PATH));
  209 + Session session = (Session) sessionRestoreStream.readObject();
  210 + sessionRestoreStream.close();
  211 + session.restore();
  212 + } catch (Exception e) {
  213 + e.printStackTrace();
  214 + Prefs.saveSession.set(false);
  215 + JOptionPane.showMessageDialog(null, "Failed to restore from saved session. " +
  216 + "Session saving has been turned off to prevent any future data loss.",
  217 + "Session restore failed", JOptionPane.ERROR_MESSAGE);
  218 + }
  219 + }
  220 + logtime("Session restore complete.");
186 221 if (PPWindow.windows.isEmpty()) {
187 222 PPWindow win = new PPWindow();
188 223 startingUp = false;
@@ -226,11 +261,11 @@ public static void main(String[] args) throws FileNotFoundException, IOException
226 261 });
227 262 }
228 263
229   - protected static void quit() {
  264 + protected static void saveUserDataAndExit() {
230 265 ObjectOutputStream oos;
231 266 // System.out.println("Quitting");
232 267 try {
233   - oos = new ObjectOutputStream(new FileOutputStream(userDataPath));
  268 + oos = new ObjectOutputStream(new FileOutputStream(USER_DATA_PATH));
234 269 oos.writeObject(userData);
235 270 oos.close();
236 271 } catch (Exception e) {
@@ -241,8 +276,6 @@ protected static void quit() {
241 276
242 277 static String getJarPath() {
243 278 try {
244   - System.out.println(URLDecoder.decode(Main.class.getProtectionDomain().getCodeSource().getLocation()
245   - .getPath(), "UTF-8"));
246 279 return URLDecoder.decode(Main.class.getProtectionDomain().getCodeSource().getLocation()
247 280 .getPath(), "UTF-8");
248 281 } catch (UnsupportedEncodingException e) { }
1  src/org/proofpad/OutputWindow.java
@@ -88,7 +88,6 @@ public void showWithText(String output, Runnable after) {
88 88 .getLayoutComponent(BorderLayout.PAGE_START);
89 89 if (oldPgStartComp != null) getRootPane().remove(oldPgStartComp);
90 90 getRootPane().add(summaries, BorderLayout.PAGE_START);
91   -
92 91 if (afterPreview != null) afterPreview.run();
93 92 afterPreview = after;
94 93 Component oldCenterComp = ((BorderLayout) getRootPane().getLayout())
6 src/org/proofpad/PPWindow.java
@@ -650,7 +650,7 @@ boolean promptIfUnsavedAndQuit(Iterator<PPWindow> ii) {
650 650 windows.remove(PPWindow.this);
651 651 }
652 652 if (windows.size() == 0 && !Main.OSX) {
653   - Main.quit();
  653 + Main.saveUserDataAndExit();
654 654 }
655 655 return true;
656 656 }
@@ -728,6 +728,10 @@ boolean saveFile() {
728 728 setTitle(openFile.getName() + (!Main.OSX ? " - Proof Pad" : ""));
729 729 } catch (IOException e) {
730 730 e.printStackTrace();
  731 + JOptionPane.showMessageDialog(this, "Saving to " + openFile + " failed. Make sure you" +
  732 + " have permission to write to this location, or try a different location." +
  733 + e.getMessage(), "Error saving", JOptionPane.ERROR_MESSAGE);
  734 + setSaved(false);
731 735 }
732 736 return true;
733 737 }
1  src/org/proofpad/Prefs.java
@@ -12,6 +12,7 @@
12 12 public static final BooleanPref customAcl2 = new BooleanPref("customacl2", false);
13 13 public static final BooleanPref firstRun = new BooleanPref("firstRun", true);
14 14 public static final BooleanPref showLineNumbers = new BooleanPref("linenums", false);
  15 + public static final BooleanPref saveSession = new BooleanPref("savesession", true);
15 16 public static final IntPref alwaysSend = new IntPref("alwaysSend", Codes.ASK_EVERY_TIME);
16 17 public static final IntPref fontSize = new IntPref("fontsize", 12);
17 18 public static final IntPref widthGuide = new IntPref("widthguide", 60);
53 src/org/proofpad/Session.java
... ... @@ -0,0 +1,53 @@
  1 +package org.proofpad;
  2 +
  3 +import java.awt.Rectangle;
  4 +import java.io.File;
  5 +import java.io.Serializable;
  6 +import java.util.List;
  7 +
  8 +public class Session implements Serializable {
  9 + private static final long serialVersionUID = 4806457808825043549L;
  10 +
  11 + private static class SavedWindow implements Serializable {
  12 + public SavedWindow() { }
  13 + private static final long serialVersionUID = -3654242987588009951L;
  14 + File file;
  15 + Rectangle location;
  16 + int admittedLoc;
  17 + String contents;
  18 + }
  19 +
  20 + private final SavedWindow[] savedWindows;
  21 +
  22 + public Session(List<PPWindow> windows) {
  23 + savedWindows = new SavedWindow[windows.size()];
  24 + int i = 0;
  25 + for (PPWindow win : windows) {
  26 + SavedWindow sw = new SavedWindow();
  27 + sw.file = win.openFile;
  28 + sw.location = win.getBounds();
  29 + sw.admittedLoc = win.proofBar.getReadOnlyIndex();
  30 + if (!win.isSaved) {
  31 + sw.contents = win.editor.getText();
  32 + } else {
  33 + sw.contents = null;
  34 + }
  35 + savedWindows[i] = sw;
  36 + i++;
  37 + }
  38 + }
  39 +
  40 + public void restore() {
  41 + for (SavedWindow sw : savedWindows) {
  42 + if (sw == null) continue;
  43 + PPWindow win = new PPWindow(sw.file);
  44 + win.setBounds(sw.location);
  45 + if (sw.contents != null) {
  46 + win.editor.setText(sw.contents);
  47 + }
  48 + win.setVisible(true);
  49 + }
  50 + }
  51 +
  52 +
  53 +}

0 comments on commit 2d3a396

Please sign in to comment.
Something went wrong with that request. Please try again.