[hg] main-silver: #231646 - Show current index of match in searc...

  • From: Milutin Kristofic < >
  • To:
  • Subject: [hg] main-silver: #231646 - Show current index of match in searc...
  • Date: Thu, 05 Sep 2013 12:33:57 -0700

changeset 3fd4c5fa0492 in main-silver ((none))
details: http://hg.netbeans.org/main-silver/rev/3fd4c5fa0492
description:
        #231646 - Show current index of match in searchbar

diffstat:

 editor.search/src/org/netbeans/modules/editor/search/Bundle.properties       
        |   2 +
 editor.search/src/org/netbeans/modules/editor/search/SearchBar.java          
        |  69 +++++++--
 
editor.search/src/org/netbeans/modules/editor/search/actions/FindNextAction.java
     |   5 +
 
editor.search/src/org/netbeans/modules/editor/search/actions/FindPreviousAction.java
 |   5 +
 4 files changed, 64 insertions(+), 17 deletions(-)

diffs (222 lines):

diff --git 
a/editor.search/src/org/netbeans/modules/editor/search/Bundle.properties 
b/editor.search/src/org/netbeans/modules/editor/search/Bundle.properties
--- a/editor.search/src/org/netbeans/modules/editor/search/Bundle.properties
+++ b/editor.search/src/org/netbeans/modules/editor/search/Bundle.properties
@@ -18,6 +18,8 @@
 1_matches=1 match
 #{0} - number of matches
 n_matches={0} matches
+#{1} - number of matches, {0} - is n-th matched item 
+i_n_matches={0} of {1} matches
 
 CTL_ExpandButton_Mnemonic=x
 TOOLTIP_ExpandButton=Search pop-up menu (Alt + X)
diff --git 
a/editor.search/src/org/netbeans/modules/editor/search/SearchBar.java 
b/editor.search/src/org/netbeans/modules/editor/search/SearchBar.java
--- a/editor.search/src/org/netbeans/modules/editor/search/SearchBar.java
+++ b/editor.search/src/org/netbeans/modules/editor/search/SearchBar.java
@@ -56,6 +56,8 @@
 import java.util.regex.PatternSyntaxException;
 import javax.swing.*;
 import javax.swing.border.Border;
+import javax.swing.event.CaretEvent;
+import javax.swing.event.CaretListener;
 import javax.swing.event.DocumentEvent;
 import javax.swing.event.DocumentListener;
 import javax.swing.event.PopupMenuEvent;
@@ -100,6 +102,7 @@
     private final List<PropertyChangeListener> actualComponentListeners = 
new LinkedList<>();
     private FocusAdapter focusAdapterForComponent;
     private KeyListener keyListenerForComponent;
+    private CaretListener caretListenerForComponent;
     private PropertyChangeListener propertyChangeListenerForComponent;
     private final JLabel findLabel;
     private final JComboBox<String> incSearchComboBox;
@@ -446,6 +449,17 @@
         };
     }
 
+    private CaretListener createCaretListenerForComponent() {
+        return new CaretListener() {
+
+            @Override
+            public void caretUpdate(CaretEvent e) {
+                int num = SearchBar.getInstance().showNumberOfMatches(null, 
-1);
+                SearchBar.getInstance().showNumberOfMatches(null, num);
+            }
+        };
+    }
+
     private FocusAdapter createFocusAdapterForComponent() {
         return new FocusAdapter() {
 
@@ -857,20 +871,20 @@
                 // valid regexp
                 incSearchTextField.setForeground(DEFAULT_FG_COLOR); //NOI18N
                 
org.netbeans.editor.Utilities.setStatusText(getActualTextComponent(), "", 
StatusDisplayer.IMPORTANCE_INCREMENTAL_FIND);
-                
changeHighlightCheckboxName(getCountFindMatches(findSupport));
+                showNumberOfMatches(findSupport, -1);
             } else {
                 // invalid regexp
                 incSearchTextField.setForeground(INVALID_REGEXP);
                 
org.netbeans.editor.Utilities.setStatusBoldText(getActualTextComponent(), 
NbBundle.getMessage(
                         SearchBar.class, 
"incremental-search-invalid-regexp", patternErrorMsg)); //NOI18N
-                changeHighlightCheckboxName(0);
+                showNumberOfMatches(findSupport, 0);
             }
         } else {
             if (findSupport.incSearch(searchProps.getProperties(), 
caretPosition) || empty) {
                 // text found - reset incremental search text field's 
foreground
                 incSearchTextField.setForeground(DEFAULT_FG_COLOR); //NOI18N
                 
org.netbeans.editor.Utilities.setStatusText(getActualTextComponent(), "", 
StatusDisplayer.IMPORTANCE_INCREMENTAL_FIND);
-                
changeHighlightCheckboxName(getCountFindMatches(findSupport));
+                showNumberOfMatches(findSupport, -1);
                 lastIncrementalSearchWasSuccessful = true;
             } else {
                 // text not found - indicate error in incremental search
@@ -883,7 +897,7 @@
                     Toolkit.getDefaultToolkit().beep();
                     lastIncrementalSearchWasSuccessful = false;
                 }
-                changeHighlightCheckboxName(0);
+                showNumberOfMatches(findSupport, 0);
             }
         }
     }
@@ -896,33 +910,50 @@
         find(false);
     }
 
-
-    private int getCountFindMatches(EditorFindSupport findSupport) {
-            int num = 0;
+    /**
+     * @param findSupport if null EditorFindSupport.getInstance() is used
+     * @param numOfMatches if numOfMatches < 0, calculate numOfMatches and 
position, else show numOfMatches.
+     * @return 
+     */
+    public int showNumberOfMatches(EditorFindSupport findSupport, int 
numOfMatches) {
+        if (findSupport == null) {
+            findSupport = EditorFindSupport.getInstance();
+        }
+        int pos = 0;
+        if (numOfMatches < 0) {
+            int currentpos = getActualTextComponent().getSelectionStart();
             try {
                 int[] blocks = findSupport.getBlocks(new int [] {-1, -1}, 
getActualTextComponent().getDocument(), 0, 
getActualTextComponent().getDocument().getLength());
                 for (int i : blocks) {
                     if (i > 0) {
-                        num++;
+                        numOfMatches++;
+                        if (i < currentpos) {
+                            pos++;
+                        }
                     }
                 }
             } catch (BadLocationException ex) {
                 Exceptions.printStackTrace(ex);
             }
-            return num == 0 ? 0 : (num + 1) / 2;
+            numOfMatches = numOfMatches == 0 ? 0 : (numOfMatches + 1) / 2;
+            pos = (pos == 0 ? 0 : (pos + 1) / 2) + 1;
+            
     }
-
-    private void changeHighlightCheckboxName(int num) {
         if (incSearchTextField.getText().isEmpty()) {
             Mnemonics.setLocalizedText(matches, ""); //NOI18N
-        } else if (num == 0) {
+        } else if (numOfMatches == 0) {
             Mnemonics.setLocalizedText(matches, 
NbBundle.getMessage(SearchBar.class, "0_matches")); //NOI18N
-        } else if (num == 1) {
+        } else if (numOfMatches == 1) {
             Mnemonics.setLocalizedText(matches, 
NbBundle.getMessage(SearchBar.class, "1_matches")); //NOI18N
         } else {
-            Mnemonics.setLocalizedText(matches, 
NbBundle.getMessage(SearchBar.class, "n_matches", num)); //NOI18N
+            if (pos == 0) {
+                Mnemonics.setLocalizedText(matches, 
NbBundle.getMessage(SearchBar.class, "n_matches", numOfMatches)); //NOI18N
+            } else {
+                Mnemonics.setLocalizedText(matches, 
NbBundle.getMessage(SearchBar.class, "i_n_matches", pos, numOfMatches)); 
//NOI18N
         }
     }
+        return numOfMatches;
+    }
 
     private void find(boolean next) {
         String incrementalSearchText = incSearchTextField.getText();
@@ -937,11 +968,11 @@
         if (findSupport.find(actualfindProps, !next) || empty) {
             // text found - reset incremental search text field's foreground
             incSearchTextField.setForeground(DEFAULT_FG_COLOR); //NOI18N
-            changeHighlightCheckboxName(getCountFindMatches(findSupport));
+            showNumberOfMatches(findSupport, -1);
         } else {
             // text not found - indicate error in incremental search text 
field with red foreground
             incSearchTextField.setForeground(NOT_FOUND);
-            changeHighlightCheckboxName(0);
+            showNumberOfMatches(findSupport, 0);
             Toolkit.getDefaultToolkit().beep();
         }
     }
@@ -1008,7 +1039,7 @@
             }
 
             
EditorFindSupport.getInstance().putFindProperties(searchProps.getProperties());
-            
changeHighlightCheckboxName(getCountFindMatches(EditorFindSupport.getInstance()));
+            showNumberOfMatches(null, -1);
         }
     }
 
@@ -1048,6 +1079,10 @@
         if (keyListenerForComponent == null) {
             keyListenerForComponent = createKeyListenerForComponent();
         }
+        if (caretListenerForComponent == null) {
+            caretListenerForComponent = createCaretListenerForComponent();
+        }
+        component.addCaretListener(caretListenerForComponent);
         component.addFocusListener(focusAdapterForComponent);
         
component.addPropertyChangeListener(propertyChangeListenerForComponent);
         component.addKeyListener(keyListenerForComponent);
diff --git 
a/editor.search/src/org/netbeans/modules/editor/search/actions/FindNextAction.java
 
b/editor.search/src/org/netbeans/modules/editor/search/actions/FindNextAction.java
--- 
a/editor.search/src/org/netbeans/modules/editor/search/actions/FindNextAction.java
+++ 
b/editor.search/src/org/netbeans/modules/editor/search/actions/FindNextAction.java
@@ -47,6 +47,7 @@
 import org.netbeans.editor.BaseKit;
 import org.netbeans.editor.EditorUI;
 import org.netbeans.modules.editor.search.EditorFindSupport;
+import org.netbeans.modules.editor.search.SearchBar;
 import org.netbeans.modules.editor.search.SearchNbEditorKit;
 import org.netbeans.spi.editor.AbstractEditorAction;
 
@@ -70,6 +71,10 @@
             }
             SearchNbEditorKit.openFindIfNecessary(eui, evt);
             EditorFindSupport.getInstance().find(null, false);
+            SearchBar searchBarInstance = SearchBar.getInstance();
+            if (searchBarInstance.isVisible()) {
+                searchBarInstance.showNumberOfMatches(null, -1);
+            }
         }
     }
 
diff --git 
a/editor.search/src/org/netbeans/modules/editor/search/actions/FindPreviousAction.java
 
b/editor.search/src/org/netbeans/modules/editor/search/actions/FindPreviousAction.java
--- 
a/editor.search/src/org/netbeans/modules/editor/search/actions/FindPreviousAction.java
+++ 
b/editor.search/src/org/netbeans/modules/editor/search/actions/FindPreviousAction.java
@@ -47,6 +47,7 @@
 import org.netbeans.editor.BaseKit;
 import org.netbeans.editor.EditorUI;
 import org.netbeans.modules.editor.search.EditorFindSupport;
+import org.netbeans.modules.editor.search.SearchBar;
 import org.netbeans.modules.editor.search.SearchNbEditorKit;
 import org.netbeans.spi.editor.AbstractEditorAction;
 
@@ -70,6 +71,10 @@
             }
             SearchNbEditorKit.openFindIfNecessary(eui, evt);
             EditorFindSupport.getInstance().find(null, true);
+            SearchBar searchBarInstance = SearchBar.getInstance();
+            if (searchBarInstance.isVisible()) {
+                searchBarInstance.showNumberOfMatches(null, -1);
+            }
         }
     }
 

[hg] main-silver: #231646 - Show current index of match in searc...

Milutin Kristofic 09/05/2013

<Possible follow-up(s)>

[hg] main-silver: #231646 - Show current index of match in searc...

Milutin Kristofic 09/05/2013

[hg] main-silver: #231646 - Show current index of match in searc...

Milutin Kristofic 09/05/2013

Project Features

About this Project

Editor was started in November 2009, is owned by Martin Ryzl, and has 147 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20131025.e7cbc9d). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close