This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 216833
Collapse All | Expand All

(-)jumpto/src/org/netbeans/modules/jumpto/type/GoToPanel.java (-2 / +5 lines)
Lines 30-35 Link Here
30
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
30
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
31
 * Microsystems, Inc. All Rights Reserved.
31
 * Microsystems, Inc. All Rights Reserved.
32
 *
32
 *
33
 * markiewb@netbeans.org
34
 *
33
 * If you wish your version of this file to be governed by only the CDDL
35
 * If you wish your version of this file to be governed by only the CDDL
34
 * or only the GPL Version 2, indicate your decision by adding
36
 * or only the GPL Version 2, indicate your decision by adding
35
 * "[Contributor] elects to include this software in this distribution
37
 * "[Contributor] elects to include this software in this distribution
Lines 71-76 Link Here
71
import javax.swing.event.ListSelectionEvent;
73
import javax.swing.event.ListSelectionEvent;
72
import javax.swing.event.ListSelectionListener;
74
import javax.swing.event.ListSelectionListener;
73
import javax.swing.text.BadLocationException;
75
import javax.swing.text.BadLocationException;
76
import javax.swing.text.Document;
74
import org.netbeans.modules.jumpto.SearchHistory;
77
import org.netbeans.modules.jumpto.SearchHistory;
75
import org.netbeans.spi.jumpto.type.TypeDescriptor;
78
import org.netbeans.spi.jumpto.type.TypeDescriptor;
76
import org.openide.filesystems.FileObject;
79
import org.openide.filesystems.FileObject;
Lines 131-137 Link Here
131
        
134
        
132
        // matchesList.setBackground( bgColorBrighter );
135
        // matchesList.setBackground( bgColorBrighter );
133
        // matchesScrollPane1.setBackground( bgColorBrighter );
136
        // matchesScrollPane1.setBackground( bgColorBrighter );
134
        matchesList.setCellRenderer( contentProvider.getListCellRenderer( matchesList ) );
137
        matchesList.setCellRenderer( contentProvider.getListCellRenderer( matchesList, nameField.getDocument()));
135
        contentProvider.setListModel( this, null );
138
        contentProvider.setListModel( this, null );
136
        
139
        
137
        PatternListener pl = new PatternListener( this );
140
        PatternListener pl = new PatternListener( this );
Lines 570-576 Link Here
570
        
573
        
571
    public static interface ContentProvider {
574
    public static interface ContentProvider {
572
        
575
        
573
        public ListCellRenderer getListCellRenderer( JList list );
576
        public ListCellRenderer getListCellRenderer( JList list, Document document);
574
        
577
        
575
        public void setListModel( GoToPanel panel, String text );
578
        public void setListModel( GoToPanel panel, String text );
576
        
579
        
(-)jumpto/src/org/netbeans/modules/jumpto/type/GoToTypeAction.java (-4 / +44 lines)
Lines 30-35 Link Here
30
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2011 Sun
30
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2011 Sun
31
 * Microsystems, Inc. All Rights Reserved.
31
 * Microsystems, Inc. All Rights Reserved.
32
 *
32
 *
33
 * markiewb@netbeans.org
34
 *
33
 * If you wish your version of this file to be governed by only the CDDL
35
 * If you wish your version of this file to be governed by only the CDDL
34
 * or only the GPL Version 2, indicate your decision by adding
36
 * or only the GPL Version 2, indicate your decision by adding
35
 * "[Contributor] elects to include this software in this distribution
37
 * "[Contributor] elects to include this software in this distribution
Lines 85-90 Link Here
85
import javax.swing.ListModel;
87
import javax.swing.ListModel;
86
import javax.swing.SwingUtilities;
88
import javax.swing.SwingUtilities;
87
import javax.swing.event.ChangeEvent;
89
import javax.swing.event.ChangeEvent;
90
import javax.swing.event.DocumentEvent;
91
import javax.swing.event.DocumentListener;
92
import javax.swing.text.BadLocationException;
93
import javax.swing.text.Document;
88
import org.netbeans.api.jumpto.type.TypeBrowser;
94
import org.netbeans.api.jumpto.type.TypeBrowser;
89
import org.netbeans.api.project.ui.OpenProjects;
95
import org.netbeans.api.project.ui.OpenProjects;
90
import org.netbeans.modules.jumpto.EntitiesListCellRenderer;
96
import org.netbeans.modules.jumpto.EntitiesListCellRenderer;
Lines 219-226 Link Here
219
    
225
    
220
    
226
    
221
    @Override
227
    @Override
222
    public ListCellRenderer getListCellRenderer( JList list ) {
228
    public ListCellRenderer getListCellRenderer(JList list, Document nameFieldDocument) {
223
        return new Renderer( list );        
229
230
        Renderer renderer = new Renderer(list);
231
        //notify the render of the changed search text
232
        nameFieldDocument.addDocumentListener(renderer);
233
234
        return renderer;
224
    }
235
    }
225
    
236
    
226
    
237
    
Lines 622-628 Link Here
622
        task.waitFinished();
633
        task.waitFinished();
623
    }
634
    }
624
635
625
    private static final class Renderer extends EntitiesListCellRenderer {
636
    private static final class Renderer extends EntitiesListCellRenderer implements DocumentListener {
626
         
637
         
627
        private MyPanel rendererComponent;
638
        private MyPanel rendererComponent;
628
        private JLabel jlName = new JLabel();
639
        private JLabel jlName = new JLabel();
Lines 638-643 Link Here
638
        private Color fgSelectionColor;
649
        private Color fgSelectionColor;
639
        
650
        
640
        private JList jList;
651
        private JList jList;
652
        private String searchText = "";
653
        private final HighlightingTypeNameFormatter typeNameFormatter;
641
654
642
        @SuppressWarnings("LeakingThisInConstructor")
655
        @SuppressWarnings("LeakingThisInConstructor")
643
        public Renderer( JList list ) {
656
        public Renderer( JList list ) {
Lines 713-718 Link Here
713
                            );
726
                            );
714
            bgSelectionColor = list.getSelectionBackground();
727
            bgSelectionColor = list.getSelectionBackground();
715
            fgSelectionColor = list.getSelectionForeground();        
728
            fgSelectionColor = list.getSelectionForeground();        
729
            //TODO: get caseSensitive mode and init the highlighter
730
            boolean caseSensitive=false;
731
            this.typeNameFormatter = new HighlightingTypeNameFormatter(fgColor, bgColor, caseSensitive);
716
        }
732
        }
717
        
733
        
718
        public @Override Component getListCellRendererComponent( JList list,
734
        public @Override Component getListCellRendererComponent( JList list,
Lines 751-757 Link Here
751
                long time = System.currentTimeMillis();
767
                long time = System.currentTimeMillis();
752
                TypeDescriptor td = (TypeDescriptor)value;                
768
                TypeDescriptor td = (TypeDescriptor)value;                
753
                jlName.setIcon(td.getIcon());
769
                jlName.setIcon(td.getIcon());
754
                jlName.setText(td.getTypeName());
770
771
                //highlight matching search text patterns in type
772
                String typeName = td.getTypeName();
773
                String formattedTypeName = typeNameFormatter.formatTypeName(typeName, searchText);
774
                jlName.setText(String.format("<html>%s</html>", formattedTypeName));
755
                jlPkg.setText(td.getContextName());
775
                jlPkg.setText(td.getContextName());
756
                setProjectName(jlPrj, td.getProjectName());
776
                setProjectName(jlPrj, td.getProjectName());
757
                jlPrj.setIcon(td.getProjectIcon());
777
                jlPrj.setIcon(td.getProjectIcon());
Lines 778-783 Link Here
778
            jList.setFixedCellWidth(jv.getExtentSize().width);
798
            jList.setFixedCellWidth(jv.getExtentSize().width);
779
        }
799
        }
780
800
801
        @Override
802
        public void insertUpdate(DocumentEvent e) {
803
            changedUpdate(e);
804
        }
805
806
        @Override
807
        public void removeUpdate(DocumentEvent e) {
808
            changedUpdate(e);
809
        }
810
811
        @Override
812
        public void changedUpdate(DocumentEvent e) {
813
814
            try {
815
                String text = e.getDocument().getText(0, e.getDocument().getLength());
816
                searchText = text;
817
            } catch (BadLocationException ex) {
818
                searchText = "";
819
            }
820
        }
781
     } // Renderer
821
     } // Renderer
782
    
822
    
783
    private class DialogButtonListener implements ActionListener {
823
    private class DialogButtonListener implements ActionListener {
(-)jumpto/src/org/netbeans/modules/jumpto/type/HighlightingTypeNameFormatter.java (+136 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s): markiewb@netbeans.org
39
 *
40
 * Portions Copyrighted 2012 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.jumpto.type;
43
44
import java.awt.Color;
45
import java.util.Arrays;
46
import java.util.BitSet;
47
import java.util.List;
48
49
/**
50
 * Highlight the match of text patterns. The matching parts will be placed 
51
 * within HTML-tags, so it can be used easily within the Swing UI.
52
 * @author markiewb
53
 */
54
class HighlightingTypeNameFormatter {
55
56
    private static final String FORMATPATTERN = "<span style=\"background-color:%s; font-weight:bold; color:%s\">%s</span>";
57
    private final boolean caseSensitive;
58
    private String formatPattern;
59
60
    HighlightingTypeNameFormatter(Color bgColor, Color fgColor, boolean caseSensitive) {
61
        String bgColorHighlight = Integer.toHexString(bgColor.getRGB()).substring(2);
62
        String fgColorHighlight = Integer.toHexString(fgColor.getRGB()).substring(2);
63
        formatPattern = String.format(FORMATPATTERN, bgColorHighlight, fgColorHighlight, "%s");
64
        this.caseSensitive = caseSensitive;
65
    }
66
67
    List<String> splitByCamelCaseAndWildcards(String searchText) {
68
        //AbcDeFGhiJo -> [Abc, De, F, Ghi, Jo]
69
        StringBuilder sb = new StringBuilder(searchText.length());
70
        for (char c : searchText.toCharArray()) {
71
            if (Character.isUpperCase(c)) {
72
                //add magic split marker into text before the uppercase char
73
                //example: AbcDeFGhiJo -> &Abc&De&F&Ghi&Jo
74
                sb.append("&");
75
                sb.append(c);
76
            } else {
77
                sb.append(c);
78
            }
79
        }
80
        //split by camelcase (using the split marker) or the wildcards *,?
81
        String[] split = sb.toString().split("[&|\\*|\\?]");
82
        return Arrays.asList(split);
83
    }
84
85
    public String formatTypeName(String typeName, String textToFind) {
86
87
        if (null == textToFind || "".equals(textToFind)) {
88
            return typeName;
89
        }
90
        BitSet bitSet = new BitSet(typeName.length());
91
        List<String> parts = splitByCamelCaseAndWildcards(textToFind);
92
93
        String convertedTypeName = caseSensitive ? typeName : typeName.toLowerCase();
94
        //mark the chars to be highlighted
95
        int startIndex = 0;
96
        for (String camelCasePart : parts) {
97
98
            int indexOf = convertedTypeName.indexOf(caseSensitive ? camelCasePart : camelCasePart.toLowerCase(), startIndex);
99
            if (indexOf != -1) {
100
101
                //mark the chars 
102
                bitSet.set(indexOf, indexOf + camelCasePart.length(), true);
103
            } else {
104
                break;
105
            }
106
            startIndex = indexOf + camelCasePart.length();
107
        }
108
109
        //highlight the marked chars via  tags
110
        StringBuilder formattedTypeName = new StringBuilder();
111
        int i = 0;
112
        while (i < typeName.toCharArray().length) {
113
114
            boolean isMarked = bitSet.get(i);
115
116
            if (isMarked) {
117
                int numberOfContinuousHighlights = bitSet.nextClearBit(i) - i;
118
                String part = typeName.substring(i, i + numberOfContinuousHighlights);
119
                formattedTypeName.append(String.format(formatPattern, part));
120
                i += numberOfContinuousHighlights;
121
            } else {
122
                formattedTypeName.append(typeName.charAt(i));
123
                i++;
124
            }
125
        }
126
        return formattedTypeName.toString();
127
    }
128
129
    /**
130
     * Allow to inject a pattern, so it can be tested easier.
131
     * @param pattern 
132
     */
133
    void setFormatPattern(String pattern) {
134
        this.formatPattern = pattern;
135
    }
136
}
(-)jumpto/test/unit/src/org/netbeans/modules/jumpto/type/HighlightingTypeNameFormatterTest.java (+115 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *   markiewb@netbeans.org
40
 *
41
 * Portions Copyrighted 2012 Sun Microsystems, Inc.
42
 */
43
package org.netbeans.modules.jumpto.type;
44
45
import java.awt.Color;
46
import java.util.List;
47
import org.junit.Test;
48
import static org.junit.Assert.*;
49
import org.junit.Before;
50
51
/**
52
 * Tests if the pattern will be highlighted the correct way.
53
 * @author markiewb
54
 */
55
public class HighlightingTypeNameFormatterTest {
56
57
    private Color fg = Color.WHITE;
58
    private Color bg = Color.BLACK;
59
    private HighlightingTypeNameFormatter cut;
60
61
    @Before
62
    public void before() {
63
        cut = new HighlightingTypeNameFormatter(fg, bg, true);
64
        cut.setFormatPattern("[%s]");
65
    }
66
67
    @Test
68
    public void testFormatTypeName_CamelCase() {
69
        String typeName = "AbstractDummyBarTest";
70
71
        assertEquals("[AbstractDummyBarTest]", cut.formatTypeName(typeName, "AbstractDummyBarTest"));
72
        assertEquals("[Abstra]ct[D]ummy[B]arTest", cut.formatTypeName(typeName, "AbstraDB"));
73
        assertEquals("[A]bstract[Dum]my[B]arTest", cut.formatTypeName(typeName, "ADumB"));
74
        assertEquals("[A]bstract[D]ummy[Ba]rTest", cut.formatTypeName(typeName, "ADBa"));
75
        assertEquals("[A]bstract[D]ummy[B]ar[Test]", cut.formatTypeName(typeName, "ADBTest"));
76
        assertEquals("[Ab]stract[Du]mmy[B]ar[Test]", cut.formatTypeName(typeName, "AbDuBTest"));
77
    }
78
79
    @Test
80
    public void testFormatTypeName_NullOrEmpty() {
81
        String typeName = "AbstractDummyBarTest";
82
        assertEquals("AbstractDummyBarTest", cut.formatTypeName(typeName, null));
83
        assertEquals("AbstractDummyBarTest", cut.formatTypeName(typeName, ""));
84
    }
85
86
    @Test
87
    public void testFormatTypeName_Wildcard_CaseSensitive() {
88
        cut = new HighlightingTypeNameFormatter(fg, bg, true);
89
        cut.setFormatPattern("[%s]");
90
        String typeName = "AbstractDummyBarTest";
91
92
        assertEquals("[A]bstractDummyBar[Test]", cut.formatTypeName(typeName, "A*Test"));
93
        assertEquals("[A]bstractDummy[B]ar[Test]", cut.formatTypeName(typeName, "A*B*Test"));
94
        assertEquals("[A]bstractDummy[BarTest]", cut.formatTypeName(typeName, "A*Bar*Test"));
95
    }
96
97
    @Test
98
    public void testFormatTypeName_Wildcard_CaseInSensitive() {
99
        cut = new HighlightingTypeNameFormatter(fg, bg, false);
100
        cut.setFormatPattern("[%s]");
101
        String typeName = "AbstractDummyBarTest";
102
103
        assertEquals("[A]bstractDummyBar[Test]", cut.formatTypeName(typeName, "A*Test"));
104
        assertEquals("[Ab]stractDummyBar[Test]", cut.formatTypeName(typeName, "A*B*Test"));
105
        assertEquals("[A]bstractDummy[BarTest]", cut.formatTypeName(typeName, "A*Bar*Test"));
106
    }
107
108
    @Test
109
    public void testFormatTypeName_FullFormat() {
110
        cut = new HighlightingTypeNameFormatter(fg, bg, false);
111
        String typeName = "AbstractDummyBarTest";
112
113
        assertEquals("<span style=\"background-color:ffffff; font-weight:bold; color:000000\">A</span>bstract<span style=\"background-color:ffffff; font-weight:bold; color:000000\">D</span>ummy<span style=\"background-color:ffffff; font-weight:bold; color:000000\">B</span>ar<span style=\"background-color:ffffff; font-weight:bold; color:000000\">Test</span>", cut.formatTypeName(typeName, "ADBTest"));
114
    }
115
}

Return to bug 216833