# HG changeset patch # User benno.markiewicz@googlemail.com # Date 1344899857 -7200 # Node ID e48a3e04cab93da29e1aa31e5b4803e26f306c88 # Parent 2c9f8cf4b256ebe531b35bc207ddf522e265905b [feature] highlights matching search pattern in the "go to type" dialog + test diff --git a/jumpto/nbproject/project.properties b/jumpto/nbproject/project.properties --- a/jumpto/nbproject/project.properties +++ b/jumpto/nbproject/project.properties @@ -1,54 +1,54 @@ -# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. -# -# Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. -# -# Oracle and Java are registered trademarks of Oracle and/or its affiliates. -# Other names may be trademarks of their respective owners. -# -# The contents of this file are subject to the terms of either the GNU -# General Public License Version 2 only ("GPL") or the Common -# Development and Distribution License("CDDL") (collectively, the -# "License"). You may not use this file except in compliance with the -# License. You can obtain a copy of the License at -# http://www.netbeans.org/cddl-gplv2.html -# or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the -# specific language governing permissions and limitations under the -# License. When distributing the software, include this License Header -# Notice in each file and include the License file at -# nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this -# particular file as subject to the "Classpath" exception as provided -# by Oracle in the GPL Version 2 section of the License file that -# accompanied this code. If applicable, add the following below the -# License Header, with the fields enclosed by brackets [] replaced by -# your own identifying information: -# "Portions Copyrighted [year] [name of copyright owner]" -# -# Contributor(s): -# -# The Original Software is the Accelerators module. -# The Initial Developer of the Original Software is Andrei Badea. -# Portions Copyright 2005-2006 Andrei Badea. -# All Rights Reserved. -# -# If you wish your version of this file to be governed by only the CDDL -# or only the GPL Version 2, indicate your decision by adding -# "[Contributor] elects to include this software in this distribution -# under the [CDDL or GPL Version 2] license." If you do not indicate a -# single choice of license, a recipient has the option to distribute -# your version of this file under either the CDDL, the GPL Version 2 or -# to extend the choice of license to its licensees as provided above. -# However, if you add GPL Version 2 code and therefore, elected the GPL -# Version 2 license, then the option applies only if the new code is -# made subject to such option by the copyright holder. -# -# Contributor(s): Andrei Badea - -javac.compilerargs=-Xlint:unchecked -javac.source=1.6 -makenbm.override.license=LICENSE -nbm.module.author=Andrei Badea, Petr Hrebejk -spec.version.base=1.29.0 - -test.config.stableBTD.includes=**/*Test.class -test.config.stableBTD.excludes=\ - **/GoToTypeActionTest.class +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. +# +# Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved. +# +# Oracle and Java are registered trademarks of Oracle and/or its affiliates. +# Other names may be trademarks of their respective owners. +# +# The contents of this file are subject to the terms of either the GNU +# General Public License Version 2 only ("GPL") or the Common +# Development and Distribution License("CDDL") (collectively, the +# "License"). You may not use this file except in compliance with the +# License. You can obtain a copy of the License at +# http://www.netbeans.org/cddl-gplv2.html +# or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the +# specific language governing permissions and limitations under the +# License. When distributing the software, include this License Header +# Notice in each file and include the License file at +# nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this +# particular file as subject to the "Classpath" exception as provided +# by Oracle in the GPL Version 2 section of the License file that +# accompanied this code. If applicable, add the following below the +# License Header, with the fields enclosed by brackets [] replaced by +# your own identifying information: +# "Portions Copyrighted [year] [name of copyright owner]" +# +# Contributor(s): +# +# The Original Software is the Accelerators module. +# The Initial Developer of the Original Software is Andrei Badea. +# Portions Copyright 2005-2006 Andrei Badea. +# All Rights Reserved. +# +# If you wish your version of this file to be governed by only the CDDL +# or only the GPL Version 2, indicate your decision by adding +# "[Contributor] elects to include this software in this distribution +# under the [CDDL or GPL Version 2] license." If you do not indicate a +# single choice of license, a recipient has the option to distribute +# your version of this file under either the CDDL, the GPL Version 2 or +# to extend the choice of license to its licensees as provided above. +# However, if you add GPL Version 2 code and therefore, elected the GPL +# Version 2 license, then the option applies only if the new code is +# made subject to such option by the copyright holder. +# +# Contributor(s): Andrei Badea + +javac.compilerargs=-Xlint:unchecked +javac.source=1.6 +makenbm.override.license=LICENSE +nbm.module.author=Andrei Badea, Petr Hrebejk +spec.version.base=1.29.0 + +test.config.stableBTD.includes=**/*Test.class +test.config.stableBTD.excludes=\ + **/GoToTypeActionTest.class diff --git a/jumpto/src/org/netbeans/modules/jumpto/type/GoToPanel.form b/jumpto/src/org/netbeans/modules/jumpto/type/GoToPanel.form --- a/jumpto/src/org/netbeans/modules/jumpto/type/GoToPanel.form +++ b/jumpto/src/org/netbeans/modules/jumpto/type/GoToPanel.form @@ -1,4 +1,4 @@ - +
diff --git a/jumpto/src/org/netbeans/modules/jumpto/type/GoToPanel.java b/jumpto/src/org/netbeans/modules/jumpto/type/GoToPanel.java --- a/jumpto/src/org/netbeans/modules/jumpto/type/GoToPanel.java +++ b/jumpto/src/org/netbeans/modules/jumpto/type/GoToPanel.java @@ -563,6 +563,9 @@ } dialog.oldText = text; dialog.contentProvider.setListModel(dialog,text); + + //recreate the renderer because the renderer needs the current text for highlighting + dialog.matchesList.setCellRenderer( dialog.contentProvider.getListCellRenderer( dialog.matchesList ) ); } } @@ -579,5 +582,5 @@ public boolean hasValidContent (); } - + } diff --git a/jumpto/src/org/netbeans/modules/jumpto/type/GoToTypeAction.java b/jumpto/src/org/netbeans/modules/jumpto/type/GoToTypeAction.java --- a/jumpto/src/org/netbeans/modules/jumpto/type/GoToTypeAction.java +++ b/jumpto/src/org/netbeans/modules/jumpto/type/GoToTypeAction.java @@ -11,7 +11,7 @@ * Development and Distribution License("CDDL") (collectively, the * "License"). You may not use this file except in compliance with the * License. You can obtain a copy of the License at - * http://www.netbeans.org/cddl-gplv2.html + * http://www.netbeans.org/cddl-gplv2. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the * specific language governing permissions and limitations under the * License. When distributing the software, include this License Header @@ -47,7 +47,6 @@ import org.netbeans.spi.jumpto.type.SearchType; import org.netbeans.spi.jumpto.type.TypeProvider; import org.netbeans.spi.jumpto.type.TypeDescriptor; -import java.awt.BorderLayout; import java.awt.Component; import java.awt.Color; import java.awt.Container; @@ -69,6 +68,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.EnumSet; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -220,7 +220,13 @@ @Override public ListCellRenderer getListCellRenderer( JList list ) { - return new Renderer( list ); + + String searchText=""; + if (null!=panel && null!=panel.nameField){ + searchText=panel.nameField.getText(); + } + + return new Renderer( list , searchText, nameKind); } @@ -638,10 +644,12 @@ private Color fgSelectionColor; private JList jList; + private final String searchText; + private final ITypeNameFormatter typeNameFormatter; @SuppressWarnings("LeakingThisInConstructor") - public Renderer( JList list ) { - + public Renderer( JList list, String searchText , SearchType nameKind) { + this.searchText=searchText; jList = list; Container container = list.getParent(); @@ -713,7 +721,15 @@ ); bgSelectionColor = list.getSelectionBackground(); fgSelectionColor = list.getSelectionForeground(); + + EnumSet caseInsensitivTypes = EnumSet.of(SearchType.CASE_INSENSITIVE_EXACT_NAME, SearchType.CASE_INSENSITIVE_PREFIX, SearchType.CASE_INSENSITIVE_REGEXP); + boolean caseSensitive=!caseInsensitivTypes.contains(nameKind); + + + this.typeNameFormatter = new HighlightingTypeNameFormatter(fgColor, bgColor,caseSensitive); } + + public @Override Component getListCellRendererComponent( JList list, Object value, @@ -749,9 +765,15 @@ if ( value instanceof TypeDescriptor ) { long time = System.currentTimeMillis(); - TypeDescriptor td = (TypeDescriptor)value; + TypeDescriptor td = (TypeDescriptor)value; + jlName.setIcon(td.getIcon()); - jlName.setText(td.getTypeName()); + + String typeName = td.getTypeName(); + + String formattedTypeName = typeNameFormatter.formatTypeName(typeName, searchText); + + jlName.setText(String.format("%s", formattedTypeName)); jlPkg.setText(td.getContextName()); setProjectName(jlPrj, td.getProjectName()); jlPrj.setIcon(td.getProjectIcon()); diff --git a/jumpto/src/org/netbeans/modules/jumpto/type/HighlightingTypeNameFormatter.java b/jumpto/src/org/netbeans/modules/jumpto/type/HighlightingTypeNameFormatter.java new file mode 100644 --- /dev/null +++ b/jumpto/src/org/netbeans/modules/jumpto/type/HighlightingTypeNameFormatter.java @@ -0,0 +1,137 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2012 Sun Microsystems, Inc. + */ +package org.netbeans.modules.jumpto.type; + +import java.awt.Color; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.BitSet; +import java.util.List; + +/** + * + * @author Bender + */ +class HighlightingTypeNameFormatter implements ITypeNameFormatter { + + private static final String FORMATPATTERN = "%s"; + private final boolean caseSensitive; + private String formatPattern; + + HighlightingTypeNameFormatter(Color bgColor, Color fgColor, boolean caseSensitive) { + String bgColorHighlight = Integer.toHexString(bgColor.getRGB()).substring(2); + String fgColorHighlight = Integer.toHexString(fgColor.getRGB()).substring(2); + formatPattern = String.format(FORMATPATTERN, bgColorHighlight, fgColorHighlight, "%s"); + this.caseSensitive = caseSensitive; + } + + List splitByCamelCaseAndWildcards(String searchText) { + //AbcDeFGhiJo -> [Abc, De, F, Ghi, Jo] + StringBuilder sb = new StringBuilder(searchText.length()); + for (char c : searchText.toCharArray()) { + if (Character.isUpperCase(c)) { + //add magic split marker into text before the uppercase char + //example: AbcDeFGhiJo -> &Abc&De&F&Ghi&Jo + sb.append("&"); + sb.append(c); + } else { + sb.append(c); + } + } + //split by camelcase (using the split marker) or the wildcards *,? + String[] split = sb.toString().split("[&|\\*|\\?]"); + return Arrays.asList(split); + } + + @Override + public String formatTypeName(String typeName, String textToFind) { + + if (null == textToFind || "".equals(textToFind)) { + return typeName; + } + BitSet bitSet = new BitSet(typeName.length()); + List parts = splitByCamelCaseAndWildcards(textToFind); + + String convertedTypeName = caseSensitive ? typeName : typeName.toLowerCase(); + //mark the chars to be highlighted + int startIndex = 0; + for (String camelCasePart : parts) { + + int indexOf = convertedTypeName.indexOf(caseSensitive ? camelCasePart : camelCasePart.toLowerCase(), startIndex); + if (indexOf != -1) { + + //mark the chars + bitSet.set(indexOf, indexOf + camelCasePart.length(), true); + } else { + break; + } + startIndex = indexOf + camelCasePart.length(); + } + + //highlight the marked chars via tags + StringBuilder formattedTypeName = new StringBuilder(); + int i = 0; + while (i < typeName.toCharArray().length) { + + boolean isMarked = bitSet.get(i); + + if (isMarked) { + int numberOfContinuousHighlights = bitSet.nextClearBit(i) - i; + String part = typeName.substring(i, i + numberOfContinuousHighlights); + formattedTypeName.append(String.format(formatPattern, part)); + i += numberOfContinuousHighlights; + } else { + formattedTypeName.append(typeName.charAt(i)); + i++; + } + } + return formattedTypeName.toString(); + } + + /** + * Allow to inject a pattern, so it can be tested easier. + * @param pattern + */ + void setFormatPattern(String pattern) { + this.formatPattern = pattern; + } +} diff --git a/jumpto/src/org/netbeans/modules/jumpto/type/ITypeNameFormatter.java b/jumpto/src/org/netbeans/modules/jumpto/type/ITypeNameFormatter.java new file mode 100644 --- /dev/null +++ b/jumpto/src/org/netbeans/modules/jumpto/type/ITypeNameFormatter.java @@ -0,0 +1,53 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2012 Sun Microsystems, Inc. + */ +package org.netbeans.modules.jumpto.type; + +import java.awt.Color; + +/** + * + * @author Bender + */ +interface ITypeNameFormatter { + + String formatTypeName(String typeName, String textToFind); +} diff --git a/jumpto/test/unit/src/org/netbeans/modules/jumpto/type/HighlightingTypeNameFormatterTest.java b/jumpto/test/unit/src/org/netbeans/modules/jumpto/type/HighlightingTypeNameFormatterTest.java new file mode 100644 --- /dev/null +++ b/jumpto/test/unit/src/org/netbeans/modules/jumpto/type/HighlightingTypeNameFormatterTest.java @@ -0,0 +1,117 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2012 Sun Microsystems, Inc. + */ +package org.netbeans.modules.jumpto.type; + +import java.awt.Color; +import java.util.List; +import org.junit.Test; +import static org.junit.Assert.*; +import org.junit.Before; + +/** + * + * @author markiewb + */ +public class HighlightingTypeNameFormatterTest { + + private Color fg = Color.WHITE; + private Color bg = Color.BLACK; + private HighlightingTypeNameFormatter cut; + + @Before + public void before() { + cut = new HighlightingTypeNameFormatter(fg, bg, true); + cut.setFormatPattern("[%s]"); + } + + /** + * Test of formatTypeName method, of class HighlightingTypeNameFormatter. + */ + @Test + public void testFormatTypeName_CamelCase() { + String typeName = "AbstractDummyBarTest"; + + assertEquals("[AbstractDummyBarTest]", cut.formatTypeName(typeName, "AbstractDummyBarTest")); + assertEquals("[Abstra]ct[D]ummy[B]arTest", cut.formatTypeName(typeName, "AbstraDB")); + assertEquals("[A]bstract[Dum]my[B]arTest", cut.formatTypeName(typeName, "ADumB")); + assertEquals("[A]bstract[D]ummy[Ba]rTest", cut.formatTypeName(typeName, "ADBa")); + assertEquals("[A]bstract[D]ummy[B]ar[Test]", cut.formatTypeName(typeName, "ADBTest")); + assertEquals("[Ab]stract[Du]mmy[B]ar[Test]", cut.formatTypeName(typeName, "AbDuBTest")); + } + + @Test + public void testFormatTypeName_NullOrEmpty() { + String typeName = "AbstractDummyBarTest"; + assertEquals("AbstractDummyBarTest", cut.formatTypeName(typeName, null)); + assertEquals("AbstractDummyBarTest", cut.formatTypeName(typeName, "")); + } + + @Test + public void testFormatTypeName_Wildcard_CaseSensitive() { + cut = new HighlightingTypeNameFormatter(fg, bg, true); + cut.setFormatPattern("[%s]"); + String typeName = "AbstractDummyBarTest"; + + assertEquals("[A]bstractDummyBar[Test]", cut.formatTypeName(typeName, "A*Test")); + assertEquals("[A]bstractDummy[B]ar[Test]", cut.formatTypeName(typeName, "A*B*Test")); + assertEquals("[A]bstractDummy[BarTest]", cut.formatTypeName(typeName, "A*Bar*Test")); + } + + @Test + public void testFormatTypeName_Wildcard_CaseInSensitive() { + cut = new HighlightingTypeNameFormatter(fg, bg, false); + cut.setFormatPattern("[%s]"); + String typeName = "AbstractDummyBarTest"; + + assertEquals("[A]bstractDummyBar[Test]", cut.formatTypeName(typeName, "A*Test")); + assertEquals("[Ab]stractDummyBar[Test]", cut.formatTypeName(typeName, "A*B*Test")); + assertEquals("[A]bstractDummy[BarTest]", cut.formatTypeName(typeName, "A*Bar*Test")); + } + + @Test + public void testFormatTypeName_FullFormat() { + cut = new HighlightingTypeNameFormatter(fg, bg, false); + String typeName = "AbstractDummyBarTest"; + + assertEquals("AbstractDummyBarTest", cut.formatTypeName(typeName, "ADBTest")); + } +}