SciNotes: fix bug with autosaving 27/8627/1
Calixte DENIZET [Wed, 8 Aug 2012 22:33:23 +0000 (00:33 +0200)]
Change-Id: I2aedb0d311da415dd5f0f2f913dd145c724c8942

scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/SciNotes.java
scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/SciNotesAutosave.java

index c53e43f..26f2f99 100644 (file)
@@ -167,16 +167,15 @@ public class SciNotes extends SwingScilabTab {
         ConfigSciNotesManager.createUserCopy();
         ScilabTabFactory.getInstance().addTabFactory(SciNotesTabFactory.getInstance());
         Scilab.registerInitialHook(new Runnable() {
-            @Override
-            public void run() {
-                updateSciNotes();
-            }
-        });
+                @Override
+                public void run() {
+                    updateSciNotes();
+                }
+            });
 
         Document doc = ScilabXMLUtilities.readDocument(System.getenv("SCI") + "/modules/console/etc/Actions-Configuration.xml");
         actionToName = XConfiguration.get(doc, "name", String.class, "action", String.class, XPATH_SCINOTES_ACTION);
         XConfiguration.addXConfigurationListener(new SciNotesConfiguration());
-        SciNotesAutosave.autosave();
     }
 
     /**
@@ -186,6 +185,7 @@ public class SciNotes extends SwingScilabTab {
         super(SCINOTES, uuid);
         setAssociatedXMLIDForHelp("scinotes");
         this.uuid = UUID.fromString(uuid);
+        SciNotesAutosave.autosave();
         editor = this;
         scinotesList.add(this);
         numberOfUntitled = 0;
@@ -195,23 +195,23 @@ public class SciNotes extends SwingScilabTab {
         contentPane = new SciNotesContents(this);
         tabPane = contentPane.getScilabTabbedPane();
         tabPane.addChangeListener(new ChangeListener() {
-            @Override
-            public void stateChanged(ChangeEvent e) {
-                if (getTextPane() != null) {
-                    // updateUI();
-                    getTextPane().updateInfosWhenFocused();
-                    getTextPane().requestFocus();
-                    getTextPane().highlightWords(IncrementalSearchAction.getWord(SciNotes.this), IncrementalSearchAction.getExact(SciNotes.this));
-
-                    // Update encoding menu
-                    EncodingAction.updateEncodingMenu((ScilabDocument) getTextPane().getDocument());
-
-                    // Update End Of Line menu
-                    EndOfLineAction.updateEolMenu((ScilabDocument) getTextPane().getDocument());
-                    setTitle(getTextPane().getTitle());
+                @Override
+                public void stateChanged(ChangeEvent e) {
+                    if (getTextPane() != null) {
+                        // updateUI();
+                        getTextPane().updateInfosWhenFocused();
+                        getTextPane().requestFocus();
+                        getTextPane().highlightWords(IncrementalSearchAction.getWord(SciNotes.this), IncrementalSearchAction.getExact(SciNotes.this));
+
+                        // Update encoding menu
+                        EncodingAction.updateEncodingMenu((ScilabDocument) getTextPane().getDocument());
+
+                        // Update End Of Line menu
+                        EndOfLineAction.updateEolMenu((ScilabDocument) getTextPane().getDocument());
+                        setTitle(getTextPane().getTitle());
+                    }
                 }
-            }
-        });
+            });
         this.setContentPane(contentPane);
     }
 
@@ -233,6 +233,7 @@ public class SciNotes extends SwingScilabTab {
             setAllMenus();
         }
         if (conf.autosave) {
+            SciNotesAutosave.stopAutosave();
             SciNotesAutosave.autosave();
         }
         updatePanes(conf);
@@ -424,15 +425,15 @@ public class SciNotes extends SwingScilabTab {
         try {
             SwingUtilities.invokeAndWait(new Runnable() {
 
-                @Override
-                public void run() {
-                    launchSciNotes();
-                    // Open an empty file if no tabs were opened at launch.
-                    if (editor.getTabPane().getTabCount() == 0) {
-                        editor.openFile(null, 0, null);
+                    @Override
+                    public void run() {
+                        launchSciNotes();
+                        // Open an empty file if no tabs were opened at launch.
+                        if (editor.getTabPane().getTabCount() == 0) {
+                            editor.openFile(null, 0, null);
+                        }
                     }
-                }
-            });
+                });
         } catch (InterruptedException e) {
             Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
             throw new RuntimeException(e);
@@ -456,12 +457,12 @@ public class SciNotes extends SwingScilabTab {
         try {
             SwingUtilities.invokeAndWait(new Runnable() {
 
-                @Override
-                public void run() {
-                    launchSciNotes();
-                    editor.openFile(filePath, 0, null);
-                }
-            });
+                    @Override
+                    public void run() {
+                        launchSciNotes();
+                        editor.openFile(filePath, 0, null);
+                    }
+                });
         } catch (InterruptedException e) {
             Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
             throw new RuntimeException(e);
@@ -487,12 +488,12 @@ public class SciNotes extends SwingScilabTab {
         try {
             SwingUtilities.invokeAndWait(new Runnable() {
 
-                @Override
-                public void run() {
-                    launchSciNotes();
-                    editor.openFile(filePath, lineNumber, functionName);
-                }
-            });
+                    @Override
+                    public void run() {
+                        launchSciNotes();
+                        editor.openFile(filePath, lineNumber, functionName);
+                    }
+                });
         } catch (InterruptedException e) {
             Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
             throw new RuntimeException(e);
@@ -518,12 +519,12 @@ public class SciNotes extends SwingScilabTab {
         try {
             SwingUtilities.invokeAndWait(new Runnable() {
 
-                @Override
-                public void run() {
-                    launchSciNotes();
-                    editor.openFile(filePath, 0, option);
-                }
-            });
+                    @Override
+                    public void run() {
+                        launchSciNotes();
+                        editor.openFile(filePath, 0, option);
+                    }
+                });
         } catch (InterruptedException e) {
             Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
             throw new RuntimeException(e);
@@ -629,11 +630,11 @@ public class SciNotes extends SwingScilabTab {
             try {
                 SwingUtilities.invokeAndWait(new Runnable() {
 
-                    @Override
-                    public void run() {
-                        restorePreviousSession();
-                    }
-                });
+                        @Override
+                        public void run() {
+                            restorePreviousSession();
+                        }
+                    });
             } catch (InterruptedException e) {
                 Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
                 throw new RuntimeException(e);
@@ -662,30 +663,30 @@ public class SciNotes extends SwingScilabTab {
         }
 
         SwingUtilities.invokeLater(new Runnable() {
-            @Override
-            public void run() {
-                RestoreOpenedFilesAction.displayDialog((JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, SciNotes.this), getUUID().toString());
-                List<File> list = RestoreOpenedFilesAction.getSelectedFiles();
-
-                if (list != null && list.size() != 0) {
-                    for (File f : list) {
-                        openFile(f.getPath(), 0, null);
-                    }
-                } else {
-                    if (getTabPane().getTabCount() == 0 || getTextPane(0).getName() == null) {
-                        openFile(null, 0, null);
+                @Override
+                public void run() {
+                    RestoreOpenedFilesAction.displayDialog((JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, SciNotes.this), getUUID().toString());
+                    List<File> list = RestoreOpenedFilesAction.getSelectedFiles();
+
+                    if (list != null && list.size() != 0) {
+                        for (File f : list) {
+                            openFile(f.getPath(), 0, null);
+                        }
+                    } else {
+                        if (getTabPane().getTabCount() == 0 || getTextPane(0).getName() == null) {
+                            openFile(null, 0, null);
+                        }
                     }
-                }
 
-                setWindowIcon("accessories-text-editor");
+                    setWindowIcon("accessories-text-editor");
 
-                if (navigator != null) {
-                    navigator.updateTree();
-                }
+                    if (navigator != null) {
+                        navigator.updateTree();
+                    }
 
-                WindowsConfigurationManager.restorationFinished(SciNotes.this);
-            }
-        });
+                    WindowsConfigurationManager.restorationFinished(SciNotes.this);
+                }
+            });
     }
 
     /**
@@ -1038,14 +1039,14 @@ public class SciNotes extends SwingScilabTab {
             }
 
             switch (answer) {
-                case YES_OPTION: // Yes, continue
-                    break;
-                case NO_OPTION:// No, exit and returns true
-                    return true;
-                case CANCEL_OPTION: // Cancel, exit and return false
-                    return false;
-                default:
-                    break;
+            case YES_OPTION: // Yes, continue
+                break;
+            case NO_OPTION:// No, exit and returns true
+                return true;
+            case CANCEL_OPTION: // Cancel, exit and return false
+                return false;
+            default:
+                break;
             }
         }
 
@@ -1246,7 +1247,7 @@ public class SciNotes extends SwingScilabTab {
             String fileName = f.getName();
             if (fileName.lastIndexOf(DOT) != -1) {
                 if (fileName.substring(fileName.lastIndexOf(DOT), fileName.length()).length() >= 2
-                        && fileName.substring(fileName.lastIndexOf(DOT), fileName.length()).length() <= 4) {
+                    && fileName.substring(fileName.lastIndexOf(DOT), fileName.length()).length() <= 4) {
                     hasNoExtension = false;
                 }
             }
@@ -1738,22 +1739,22 @@ public class SciNotes extends SwingScilabTab {
                     messageBox.displayAndWait(); // Waits for a user action
 
                     switch (messageBox.getSelectedButton()) {
-                        case 1: // Reload
-                            if ((i == 0) && (getTabPane().getTabCount() == 1)) {
-                                for (int j = 0; j < tabPane.getChangeListeners().length; j++) {
-                                    tabPane.removeChangeListener(tabPane.getChangeListeners()[j]);
-                                }
+                    case 1: // Reload
+                        if ((i == 0) && (getTabPane().getTabCount() == 1)) {
+                            for (int j = 0; j < tabPane.getChangeListeners().length; j++) {
+                                tabPane.removeChangeListener(tabPane.getChangeListeners()[j]);
                             }
-                            tabPane.remove(i);
-                            f = new File(textPaneAt.getName());
-                            index = i;
-                            break;
-                        case 2: // Overwrite 2
-                            overwrite(i);
-                            alreadyOpened = true;
-                            break;
-                        default: // Ignore
-                            alreadyOpened = true;
+                        }
+                        tabPane.remove(i);
+                        f = new File(textPaneAt.getName());
+                        index = i;
+                        break;
+                    case 2: // Overwrite 2
+                        overwrite(i);
+                        alreadyOpened = true;
+                        break;
+                    default: // Ignore
+                        alreadyOpened = true;
                     }
                 } else {
                     alreadyOpened = true;
@@ -2285,7 +2286,7 @@ public class SciNotes extends SwingScilabTab {
         ScilabEditorPane theTextPane = addEmptyTab();
         ScilabDocument styleDocument = null;
         int choice = JOptionPane.showConfirmDialog(SciNotes.this, String.format(SciNotesMessages.FILE_DOESNT_EXIST, f.getName()), SCINOTES,
-                     JOptionPane.YES_NO_OPTION);
+                                                   JOptionPane.YES_NO_OPTION);
         if (choice == 0) { // OK
             styleDocument = (ScilabDocument) theTextPane.getDocument();
             styleDocument.disableUndoManager();
@@ -2380,16 +2381,16 @@ public class SciNotes extends SwingScilabTab {
         try {
             SwingUtilities.invokeAndWait(new Runnable() {
 
-                @Override
-                public void run() {
-                    SciNotes[] arr = scinotesList.toArray(new SciNotes[0]);
-                    for (int i = 0; i < arr.length; i++) {
-                        arr[i].setProtectOpenFileList(true);
-                        ExitAction.doExit(arr[i]);
+                    @Override
+                    public void run() {
+                        SciNotes[] arr = scinotesList.toArray(new SciNotes[0]);
+                        for (int i = 0; i < arr.length; i++) {
+                            arr[i].setProtectOpenFileList(true);
+                            ExitAction.doExit(arr[i]);
+                        }
+                        scinotesList.clear();
                     }
-                    scinotesList.clear();
-                }
-            });
+                });
         } catch (InterruptedException e) {
             Logger.getLogger(SciNotes.class.getName()).severe(e.toString());
         } catch (InvocationTargetException e) {
index 95c5fed..0354ad8 100644 (file)
@@ -31,16 +31,16 @@ public class SciNotesAutosave implements ActionListener {
 
     static {
         Scilab.registerFinalHook(new Runnable() {
-            public void run() {
-                for (File f : toRemove) {
-                    if (f.exists()) {
-                        try {
-                            f.delete();
-                        } catch (Exception e) { }
+                public void run() {
+                    for (File f : toRemove) {
+                        if (f.exists()) {
+                            try {
+                                f.delete();
+                            } catch (Exception e) { }
+                        }
                     }
                 }
-            }
-        });
+            });
     }
 
     private Timer timer;
@@ -94,18 +94,18 @@ public class SciNotesAutosave implements ActionListener {
                         if (!as.sourceFlag) {
                             file = new File(as.singleDirectory, filename);
                         }
-                        boolean different = new File(sep.getName()).equals(file);
+                        boolean identic = new File(sep.getName()).equals(file);
 
                         boolean success = SaveFile.doSave(sep, i, file, ed.getEditorKit(), false, true);
                         if (!success) {
                             ed.getInfoBar().setText(String.format(SciNotesMessages.AUTOSAVE_ERROR, filename));
                             return;
                         } else {
-                            if (!different) {
+                            ((ScilabDocument) sep.getDocument()).setContentModifiedSinceBackup(false);
+                            if (identic) {
                                 ((ScilabDocument) sep.getDocument()).setContentModified(false);
                                 sep.setLastModified(file.lastModified());
                             } else {
-                                ((ScilabDocument) sep.getDocument()).setContentModifiedSinceBackup(false);
                                 if (as.automaticDelete) {
                                     toRemove.add(file);
                                 }