SciNotes: Wait a little time before to highlight something 44/11444/2
Calixte DENIZET [Fri, 3 May 2013 13:12:42 +0000 (15:12 +0200)]
Change-Id: I8353635a4a802386f13896b71cf6a7539ca14cba

scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/MatchingBlockManager.java
scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/ScilabEditorPane.java

index 787937a..2276518 100644 (file)
@@ -211,7 +211,7 @@ public class MatchingBlockManager {
     /**
      * Remove the highlights if they exist.
      */
-    private synchronized void update() {
+    public synchronized void update() {
         if (first != null) {
             highlighter.removeHighlight(first);
             first = null;
@@ -241,10 +241,12 @@ public class MatchingBlockManager {
                         highlighter.removeHighlight(second);
                     }
                 }
-                if (mpos != null && ScilabLexerConstants.isOpenClose(tok) && ocPainter != null && (!isMouse || isOCMouseover)) {
-                    createHighlights(mpos, insideOc, ocIncluded, ocPainter);
-                } else if (mpos != null && kwPainter != null && (!isMouse || isKWMouseover)) {
-                    createHighlights(mpos, insideKw, kwIncluded, kwPainter);
+                if (mpos != null) {
+                    if (ScilabLexerConstants.isOpenClose(tok) && ocPainter != null && (!isMouse || isOCMouseover)) {
+                        createHighlights(mpos, insideOc, ocIncluded, ocPainter);
+                    } else if (kwPainter != null && (!isMouse || isKWMouseover)) {
+                        createHighlights(mpos, insideKw, kwIncluded, kwPainter);
+                    }
                 }
             } catch (NullPointerException e) { }
         }
@@ -382,20 +384,20 @@ public class MatchingBlockManager {
                                 Shape bounds, JTextComponent c, View view) {
             try {
                 Rectangle r = (Rectangle) view.modelToView(offs0, Position.Bias.Forward,
-                              offs1, Position.Bias.Backward, bounds);
+                                                           offs1, Position.Bias.Backward, bounds);
                 g.setColor(color);
 
                 switch (type) {
-                    case UNDERLINED :
-                        g.drawLine(r.x, r.y + r.height - 1, r.x + r.width - 1, r.y + r.height - 1);
-                        return r;
-                    case FRAMED :
-                        g.drawRect(r.x, r.y, r.width - 1, r.height - 1);
-                        return r;
-                    case FILLED :
-                    default :
-                        g.fillRect(r.x, r.y, r.width, r.height);
-                        return r;
+                case UNDERLINED :
+                    g.drawLine(r.x, r.y + r.height - 1, r.x + r.width - 1, r.y + r.height - 1);
+                    return r;
+                case FRAMED :
+                    g.drawRect(r.x, r.y, r.width - 1, r.height - 1);
+                    return r;
+                case FILLED :
+                default :
+                    g.fillRect(r.x, r.y, r.width, r.height);
+                    return r;
                 }
             } catch (BadLocationException e) {
                 return null;
index 8481adb..6f24916 100644 (file)
@@ -19,6 +19,8 @@ import java.awt.Graphics;
 import java.awt.Point;
 import java.awt.Rectangle;
 import java.awt.Shape;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import java.awt.event.FocusEvent;
 import java.awt.event.FocusListener;
 import java.awt.event.KeyEvent;
@@ -44,6 +46,7 @@ import javax.swing.JScrollBar;
 import javax.swing.JScrollPane;
 import javax.swing.JSplitPane;
 import javax.swing.SwingUtilities;
+import javax.swing.Timer;
 import javax.swing.UIManager;
 import javax.swing.event.CaretEvent;
 import javax.swing.event.CaretListener;
@@ -79,9 +82,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;
 
@@ -111,6 +114,8 @@ public class ScilabEditorPane extends JEditorPane implements Highlighter.Highlig
     private String infoBar = "";
     private String shortName = "";
     private String title = "";
+    private Timer selectionTimer;
+    private Timer matcherTimer;
 
     private Point mousePoint;
 
@@ -171,32 +176,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;
@@ -211,77 +216,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;
                             }
-                        } catch (BadLocationException ex) { }
-                    } else if (ScilabEditorPane.this.editor != null) {
-                        ScilabEditorPane.this.editor.getInfoBar().setText(SciNotesMessages.CLICKABLE_URL);
-                        infoBarChanged = true;
+                        }
+                    } else {
                         if (hand) {
                             setCursor(TEXTCURSOR);
                             hand = false;
                         }
-                    }
-                } 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 {
-                            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);
+                        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())) {
-                    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) { }
+                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());
@@ -419,8 +424,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) {
@@ -525,11 +530,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);
+                }
+            });
     }
 
     /**
@@ -795,28 +800,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);
-                    }
-                    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));
+                public void run() {
+                    try {
+                        if (setCaret) {
+                            setCaretPosition(pos);
                         }
-                    } else {
-                        if (rect.y > scrollbar.getMaximum()) {
-                            scrollbar.setMaximum(rect.y);
+                        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);
                         }
-                        scrollbar.setValue(rect.y);
-                    }
-                } catch (BadLocationException e) { }
-            }
-        });
+                    } catch (BadLocationException e) { }
+                }
+            });
     }
 
     /**
@@ -840,20 +845,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);
+                        }
                     }
-                }
-            });
+                });
         }
     }
 
@@ -1121,29 +1126,39 @@ public class ScilabEditorPane extends JEditorPane implements Highlighter.Highlig
      * @param e event
      */
     public void caretUpdate(CaretEvent e) {
-        boolean search = false;
         if (hasBeenSaved) {
             removeHighlightForLine();
         }
 
         if (hasFocus()) {
-            String str = getSelectedText();
+            final String str = getSelectedText();
             if (str != null && str.length() != 0) {
-                int tok = lexer.getKeyword(getSelectionStart(), false);
-                int s = lexer.start + lexer.yychar();
-                if (ScilabLexerConstants.isSearchable(tok) && getSelectionStart() == s && getSelectionEnd() == s + lexer.yylength()) {
-                    highlightWords(tok, SearchManager.generatePattern(str, false, true, false), false);
+                if (selectionTimer == null) {
+                    selectionTimer = new Timer(1000, new ActionListener() {
+                            public void actionPerformed(ActionEvent e) {
+                                final String str = getSelectedText();
+                                if (str != null && str.length() != 0) {
+                                    int tok = lexer.getKeyword(getSelectionStart(), false);
+                                    int s = lexer.start + lexer.yychar();
+                                    if (ScilabLexerConstants.isSearchable(tok) && getSelectionStart() == s && getSelectionEnd() == s + lexer.yylength()) {
+                                        highlightWords(tok, SearchManager.generatePattern(str, false, true, false), false);
+                                    } else {
+                                        highlightWords(str, false);
+                                    }
+
+                                    if (highlightedWords.size() > 1 && editor != null && editor.getInfoBar() != null) {
+                                        editor.getInfoBar().setText(String.format(SciNotesMessages.OCCURENCES_FOUND, Integer.toString(highlightedWords.size())));
+                                    }
+                                    removeHighlightOnPosition(getSelectionStart());
+                                }
+                                selectionTimer = null;
+                            }
+                        });
+                    selectionTimer.setRepeats(false);
+                    selectionTimer.start();
                 } else {
-                    highlightWords(str, false);
+                    selectionTimer.restart();
                 }
-
-                if (highlightedWords.size() > 1) {
-                    if (editor != null) {
-                        editor.getInfoBar().setText(String.format(SciNotesMessages.OCCURENCES_FOUND, Integer.toString(highlightedWords.size())));
-                    }
-                    search = true;
-                }
-                removeHighlightOnPosition(getSelectionStart());
             } else {
                 removeHighlightedWords();
             }
@@ -1156,13 +1171,27 @@ public class ScilabEditorPane extends JEditorPane implements Highlighter.Highlig
         int pos = getCaretPosition();
 
         if (matchingKeywordsEnable || matchingOpenersEnable) {
-            int tok = lexer.getKeyword(pos, false);
-            matchLR.searchMatchingBlock(false, tok, lexer.start + lexer.yychar());
-            tok = lexer.getKeyword(pos, true);
-            matchRL.searchMatchingBlock(false, tok, lexer.start + lexer.yychar() + lexer.yylength());
+            if (matcherTimer == null) {
+                matcherTimer = new Timer(100, new ActionListener() {
+                        public void actionPerformed(ActionEvent e) {
+                            int pos = getCaretPosition();
+                            int tok = lexer.getKeyword(pos, false);
+                            matchLR.searchMatchingBlock(false, tok, lexer.start + lexer.yychar());
+                            tok = lexer.getKeyword(pos, true);
+                            matchRL.searchMatchingBlock(false, tok, lexer.start + lexer.yychar() + lexer.yylength());
+                            matcherTimer = null;
+                        }
+                    });
+                matcherTimer.setRepeats(false);
+                matcherTimer.start();
+            } else {
+                matcherTimer.restart();
+                matchLR.update();
+                matchRL.update();
+            }
         }
 
-        if (!search && !readonly && !binary && editor != null) {
+        if (!readonly && !binary && editor != null) {
             editor.getInfoBar().setText(((ScilabDocument) getDocument()).getCurrentFunction(pos));
         }
     }
@@ -1477,17 +1506,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();
+                    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);
                     }
-                    caret.setBlinkRate(blinkRate);
-                    caret.setVisible(true);
-                }
-            });
+                });
             super.setCaret(caret);
         } else {
             super.setCaret(c);