Scinotes: show a messagebox when an external modif has been made on the current file
[scilab.git] / scilab / modules / scinotes / src / java / org / scilab / modules / scinotes / ScilabEditorPane.java
index 2cda36e..367a85e 100644 (file)
@@ -60,6 +60,12 @@ import javax.swing.text.View;
 import org.scilab.modules.commons.OS;
 import org.scilab.modules.commons.gui.ScilabCaret;
 import org.scilab.modules.console.utils.ScilabLaTeXViewer;
+import org.scilab.modules.gui.messagebox.MessageBox;
+import org.scilab.modules.gui.messagebox.ScilabMessageBox;
+import org.scilab.modules.gui.messagebox.ScilabModalDialog;
+import org.scilab.modules.gui.messagebox.ScilabModalDialog.AnswerOption;
+import org.scilab.modules.gui.messagebox.ScilabModalDialog.ButtonType;
+import org.scilab.modules.gui.messagebox.ScilabModalDialog.IconType;
 import org.scilab.modules.gui.utils.WebBrowser;
 import org.scilab.modules.scinotes.actions.CopyAsHTMLAction;
 import org.scilab.modules.scinotes.actions.OpenSourceFileOnKeywordAction;
@@ -73,9 +79,9 @@ import org.scilab.modules.scinotes.utils.SciNotesMessages;
  *
  */
 public class ScilabEditorPane extends JEditorPane implements Highlighter.HighlightPainter,
-                                                  CaretListener, MouseListener,
-                                                  MouseMotionListener, Cloneable,
-                                                  KeyListener {
+    CaretListener, MouseListener,
+    MouseMotionListener, Cloneable,
+    KeyListener {
 
     private static final long serialVersionUID = 4322071415211939097L;
 
@@ -109,6 +115,7 @@ public class ScilabEditorPane extends JEditorPane implements Highlighter.Highlig
     private Point mousePoint;
 
     private long lastModified;
+    private long lastExternalModification;
 
     /* matchLR matches Left to Right ... */
     private MatchingBlockManager matchLR;
@@ -164,32 +171,32 @@ public class ScilabEditorPane extends JEditorPane implements Highlighter.Highlig
         enableMatchingOpeners(SciNotesOptions.getSciNotesDisplay().highlightBrackets);
         setFocusable(true);
         addFocusListener(new FocusListener() {
-                public void focusGained(FocusEvent e) {
-                    updateInfosWhenFocused();
-                    if (ScilabEditorPane.this.editor != null) {
-                        NavigatorWindow nav = ScilabEditorPane.this.editor.getNavigator();
-                        if (nav != null) {
-                            nav.update((ScilabDocument) getDocument());
-                        }
+            public void focusGained(FocusEvent e) {
+                updateInfosWhenFocused();
+                if (ScilabEditorPane.this.editor != null) {
+                    NavigatorWindow nav = ScilabEditorPane.this.editor.getNavigator();
+                    if (nav != null) {
+                        nav.update((ScilabDocument) getDocument());
                     }
                 }
+            }
 
-                public void focusLost(FocusEvent e) {
-                    ((ScilabDocument) getDocument()).setFocused(false);
-                }
-            });
+            public void focusLost(FocusEvent e) {
+                ((ScilabDocument) getDocument()).setFocused(false);
+            }
+        });
 
         addKeywordListener(new KeywordAdapter.MouseOverAdapter() {
-                public void caughtKeyword(KeywordEvent e) {
-                    if (ScilabLexerConstants.isClickable(e.getType())) {
-                        if (ctrlHit) {
-                            setCursor(HANDCURSOR);
-                            hand = true;
-                            try {
-                                String url = ((ScilabDocument) getDocument()).getText(e.getStart(), e.getLength());
-                                if (ScilabLexerConstants.isClickable(e.getType())) {
-                                    String text;
-                                    switch (e.getType()) {
+            public void caughtKeyword(KeywordEvent e) {
+                if (ScilabLexerConstants.isClickable(e.getType())) {
+                    if (ctrlHit) {
+                        setCursor(HANDCURSOR);
+                        hand = true;
+                        try {
+                            String url = ((ScilabDocument) getDocument()).getText(e.getStart(), e.getLength());
+                            if (ScilabLexerConstants.isClickable(e.getType())) {
+                                String text;
+                                switch (e.getType()) {
                                     case ScilabLexerConstants.URL :
                                         text = SciNotesMessages.OPENURL;
                                         break;
@@ -204,77 +211,77 @@ public class ScilabEditorPane extends JEditorPane implements Highlighter.Highlig
                                         break;
                                     default :
                                         text = null;
-                                    }
+                                }
 
-                                    if (text != null && ScilabEditorPane.this.editor != null) {
-                                        ScilabEditorPane.this.editor.getInfoBar().setText(text + url);
-                                        infoBarChanged = true;
-                                    }
+                                if (text != null && ScilabEditorPane.this.editor != null) {
+                                    ScilabEditorPane.this.editor.getInfoBar().setText(text + url);
+                                    infoBarChanged = true;
                                 }
-                            } catch (BadLocationException ex) { }
-                        } else if (ScilabEditorPane.this.editor != null) {
-                            ScilabEditorPane.this.editor.getInfoBar().setText(SciNotesMessages.CLICKABLE_URL);
-                            infoBarChanged = true;
-                            if (hand) {
-                                setCursor(TEXTCURSOR);
-                                hand = false;
                             }
-                        }
-                    } else {
+                        } catch (BadLocationException ex) { }
+                    } else if (ScilabEditorPane.this.editor != null) {
+                        ScilabEditorPane.this.editor.getInfoBar().setText(SciNotesMessages.CLICKABLE_URL);
+                        infoBarChanged = true;
                         if (hand) {
                             setCursor(TEXTCURSOR);
                             hand = false;
                         }
-                        if (infoBarChanged && ScilabEditorPane.this.editor != null) {
-                            ScilabEditorPane.this.editor.getInfoBar().setText(infoBar);
-                            infoBarChanged = false;
-                        }
-                        if (ScilabLexerConstants.isLaTeX(e.getType())) {
-                            try {
-                                int start = e.getStart();
-                                int end = start + e.getLength();
-                                String exp = ((ScilabDocument) getDocument()).getText(start, e.getLength());
-                                int height = edComponent.getScrollPane().getHeight() + edComponent.getScrollPane().getVerticalScrollBar().getValue();
-                                ScilabLaTeXViewer.displayExpression(ScilabEditorPane.this, height, exp, start, end);
-                            } catch (BadLocationException ex) { }
-                        } else {
-                            ScilabLaTeXViewer.removeLaTeXViewer(ScilabEditorPane.this);
-                        }
                     }
-                }
-            });
-
-        addKeywordListener(new KeywordAdapter.MouseClickedAdapter() {
-                public void caughtKeyword(KeywordEvent e) {
-                    if (ctrlHit && ScilabLexerConstants.isClickable(e.getType())) {
+                } else {
+                    if (hand) {
+                        setCursor(TEXTCURSOR);
+                        hand = false;
+                    }
+                    if (infoBarChanged && ScilabEditorPane.this.editor != null) {
+                        ScilabEditorPane.this.editor.getInfoBar().setText(infoBar);
+                        infoBarChanged = false;
+                    }
+                    if (ScilabLexerConstants.isLaTeX(e.getType())) {
                         try {
-                            hand = false;
-                            ctrlHit = false;
-                            infoBarChanged = false;
-                            setCursor(TEXTCURSOR);
-                            if (ScilabEditorPane.this.editor != null) {
-                                ScilabEditorPane.this.editor.getInfoBar().setText(infoBar);
-                            }
-                            String url = ((ScilabDocument) getDocument()).getText(e.getStart(), e.getLength());
-                            if (ScilabLexerConstants.URL == e.getType() || ScilabLexerConstants.MAIL == e.getType()) {
-                                WebBrowser.openUrl(url);
-                            } else if (ScilabLexerConstants.isOpenable(e.getType())) {
-                                OpenSourceFileOnKeywordAction.openSource(ScilabEditorPane.this, url);
-                            }
+                            int start = e.getStart();
+                            int end = start + e.getLength();
+                            String exp = ((ScilabDocument) getDocument()).getText(start, e.getLength());
+                            int height = edComponent.getScrollPane().getHeight() + edComponent.getScrollPane().getVerticalScrollBar().getValue();
+                            ScilabLaTeXViewer.displayExpression(ScilabEditorPane.this, height, exp, start, end);
                         } catch (BadLocationException ex) { }
+                    } else {
+                        ScilabLaTeXViewer.removeLaTeXViewer(ScilabEditorPane.this);
                     }
                 }
-            });
+            }
+        });
+
+        addKeywordListener(new KeywordAdapter.MouseClickedAdapter() {
+            public void caughtKeyword(KeywordEvent e) {
+                if (ctrlHit && ScilabLexerConstants.isClickable(e.getType())) {
+                    try {
+                        hand = false;
+                        ctrlHit = false;
+                        infoBarChanged = false;
+                        setCursor(TEXTCURSOR);
+                        if (ScilabEditorPane.this.editor != null) {
+                            ScilabEditorPane.this.editor.getInfoBar().setText(infoBar);
+                        }
+                        String url = ((ScilabDocument) getDocument()).getText(e.getStart(), e.getLength());
+                        if (ScilabLexerConstants.URL == e.getType() || ScilabLexerConstants.MAIL == e.getType()) {
+                            WebBrowser.openUrl(url);
+                        } else if (ScilabLexerConstants.isOpenable(e.getType())) {
+                            OpenSourceFileOnKeywordAction.openSource(ScilabEditorPane.this, url);
+                        }
+                    } catch (BadLocationException ex) { }
+                }
+            }
+        });
 
         getScrollPane().addMouseWheelListener(new MouseWheelListener() {
-                public void mouseWheelMoved(MouseWheelEvent e) {
-                    if ((OS.get() == OS.MAC && e.isMetaDown()) || e.isControlDown()) {
-                        int n = e.getWheelRotation();
-                        SciNotes.updateFontSize(n);
-                        e.consume();
-                    }
+            public void mouseWheelMoved(MouseWheelEvent e) {
+                if ((OS.get() == OS.MAC && e.isMetaDown()) || e.isControlDown()) {
+                    int n = e.getWheelRotation();
+                    SciNotes.updateFontSize(n);
+                    e.consume();
                 }
-            });
+            }
+        });
 
         addKeyListener(this);
         setTransferHandler(new CopyAsHTMLAction.HTMLTransferHandler());
@@ -410,8 +417,8 @@ public class ScilabEditorPane extends JEditorPane implements Highlighter.Highlig
     public void keyPressed(KeyEvent e) {
         // Workaround for bug 7238
         if (e.getKeyLocation() == KeyEvent.KEY_LOCATION_NUMPAD
-            && e.getKeyCode() == KeyEvent.VK_DELETE
-            && e.getKeyChar() != KeyEvent.VK_DELETE) {
+                && e.getKeyCode() == KeyEvent.VK_DELETE
+                && e.getKeyChar() != KeyEvent.VK_DELETE) {
             e.setKeyCode(KeyEvent.VK_DECIMAL);
             ctrlHit = false;
         } else if (mousePoint != null && e.getKeyCode() == KeyEvent.VK_CONTROL) {
@@ -516,11 +523,11 @@ public class ScilabEditorPane extends JEditorPane implements Highlighter.Highlig
      */
     public void init(final int pos) {
         SwingUtilities.invokeLater(new Runnable() {
-                public void run() {
-                    requestFocus();
-                    setCaretPosition(pos);
-                }
-            });
+            public void run() {
+                requestFocus();
+                setCaretPosition(pos);
+            }
+        });
     }
 
     /**
@@ -544,8 +551,13 @@ public class ScilabEditorPane extends JEditorPane implements Highlighter.Highlig
         doc.getUndoManager().enableUndoRedoButtons();
 
         if (editor != null) {
-            if (checkExternalModif()) {
+            if (checkExternalModif() && lastExternalModification < getLastModification()) {
                 editor.getInfoBar().setText(SciNotesMessages.EXTERNAL_MODIFICATION_INFO);
+                if (ScilabModalDialog.show(editor, String.format(SciNotesMessages.ASK_TO_RELOAD, getShortName()), SciNotesMessages.RELOAD, IconType.QUESTION_ICON, ButtonType.YES_NO) == AnswerOption.YES_OPTION) {
+                    editor.reload(getEditor().getTabPane().indexOfComponent(getEditorComponent()));
+                    editor.getTextPane().updateInfosWhenFocused();
+                }
+                lastExternalModification = getLastModification();
             } else {
                 editor.getInfoBar().setText(getInfoBarText());
             }
@@ -569,6 +581,22 @@ public class ScilabEditorPane extends JEditorPane implements Highlighter.Highlig
     }
 
     /**
+     * Get last modification on file
+     * @return the lastModified value
+     */
+    public long getLastModification() {
+        String path = getName();
+        if (path != null) {
+            File f = new File(path);
+            if (f != null && f.exists()) {
+                return f.lastModified();
+            }
+        }
+
+        return -1;
+    }
+
+    /**
      * @return the String which must be displayed in the infobar
      */
     public String getInfoBarText() {
@@ -765,28 +793,28 @@ public class ScilabEditorPane extends JEditorPane implements Highlighter.Highlig
      */
     public void scrollTextToPos(final int pos, final boolean setCaret, final boolean centered) {
         SwingUtilities.invokeLater(new Runnable() {
-                public void run() {
-                    try {
-                        if (setCaret) {
-                            setCaretPosition(pos);
+            public void run() {
+                try {
+                    if (setCaret) {
+                        setCaretPosition(pos);
+                    }
+                    JScrollBar scrollbar = edComponent.getScrollPane().getVerticalScrollBar();
+                    Rectangle rect = modelToView(pos);
+                    if (centered) {
+                        int value = scrollbar.getValue();
+                        int h = scrollbar.getHeight();
+                        if (rect.y < value || rect.y > value + h) {
+                            scrollbar.setValue(Math.max(0, rect.y - h / 2));
                         }
-                        JScrollBar scrollbar = edComponent.getScrollPane().getVerticalScrollBar();
-                        Rectangle rect = modelToView(pos);
-                        if (centered) {
-                            int value = scrollbar.getValue();
-                            int h = scrollbar.getHeight();
-                            if (rect.y < value || rect.y > value + h) {
-                                scrollbar.setValue(Math.max(0, rect.y - h / 2));
-                            }
-                        } else {
-                            if (rect.y > scrollbar.getMaximum()) {
-                                scrollbar.setMaximum(rect.y);
-                            }
-                            scrollbar.setValue(rect.y);
+                    } else {
+                        if (rect.y > scrollbar.getMaximum()) {
+                            scrollbar.setMaximum(rect.y);
                         }
-                    } catch (BadLocationException e) { }
-                }
-            });
+                        scrollbar.setValue(rect.y);
+                    }
+                } catch (BadLocationException e) { }
+            }
+        });
     }
 
     /**
@@ -810,20 +838,20 @@ public class ScilabEditorPane extends JEditorPane implements Highlighter.Highlig
         if (lineNumber >= 1 && lineNumber <= root.getElementCount()) {
             final int pos = root.getElement(lineNumber - 1).getStartOffset();
             SwingUtilities.invokeLater(new Runnable() {
-                    public void run() {
-                        scrollTextToPos(pos, setCaret, centered);
-                        if (highlight) {
-                            saveHighlightContourColor = highlightContourColor;
-                            highlightContourColor = null;
-                            saveHighlightColor = highlightColor;
-                            highlightColor = Color.YELLOW;
-                            saveHighlightEnable = highlightEnable;
-                            hasBeenSaved = true;
-                            enableHighlightedLine(false);
-                            enableHighlightedLine(true);
-                        }
+                public void run() {
+                    scrollTextToPos(pos, setCaret, centered);
+                    if (highlight) {
+                        saveHighlightContourColor = highlightContourColor;
+                        highlightContourColor = null;
+                        saveHighlightColor = highlightColor;
+                        highlightColor = Color.YELLOW;
+                        saveHighlightEnable = highlightEnable;
+                        hasBeenSaved = true;
+                        enableHighlightedLine(false);
+                        enableHighlightedLine(true);
                     }
-                });
+                }
+            });
         }
     }
 
@@ -1447,17 +1475,17 @@ public class ScilabEditorPane extends JEditorPane implements Highlighter.Highlig
             final Caret caret = new SciNotesCaret(this);
             setCaretColor(getCaretColor());
             SwingUtilities.invokeLater(new Runnable() {
-                    public void run() {
-                        int blinkRate = 500;
-                        Object o = UIManager.get("TextComponent.caretBlinkRate");
-                        if ((o != null) && (o instanceof Integer)) {
-                            Integer rate = (Integer) o;
-                            blinkRate = rate.intValue();
-                        }
-                        caret.setBlinkRate(blinkRate);
-                        caret.setVisible(true);
+                public void run() {
+                    int blinkRate = 500;
+                    Object o = UIManager.get("TextComponent.caretBlinkRate");
+                    if ((o != null) && (o instanceof Integer)) {
+                        Integer rate = (Integer) o;
+                        blinkRate = rate.intValue();
                     }
-                });
+                    caret.setBlinkRate(blinkRate);
+                    caret.setVisible(true);
+                }
+            });
             super.setCaret(caret);
         } else {
             super.setCaret(c);