PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/jumpto/src/org/netbeans/modules/jumpto/common/HighlightingNameFormatter.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 211 lines | 99 code | 18 blank | 94 comment | 13 complexity | b98c88a9b541d5d1354223df4a23ea64 MD5 | raw file
  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. *
  40. * Portions Copyrighted 2012 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.jumpto.common;
  43. /*
  44. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  45. *
  46. * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
  47. *
  48. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  49. * Other names may be trademarks of their respective owners.
  50. *
  51. * The contents of this file are subject to the terms of either the GNU
  52. * General Public License Version 2 only ("GPL") or the Common
  53. * Development and Distribution License("CDDL") (collectively, the
  54. * "License"). You may not use this file except in compliance with the
  55. * License. You can obtain a copy of the License at
  56. * http://www.netbeans.org/cddl-gplv2.html
  57. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  58. * specific language governing permissions and limitations under the
  59. * License. When distributing the software, include this License Header
  60. * Notice in each file and include the License file at
  61. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  62. * particular file as subject to the "Classpath" exception as provided
  63. * by Oracle in the GPL Version 2 section of the License file that
  64. * accompanied this code. If applicable, add the following below the
  65. * License Header, with the fields enclosed by brackets [] replaced by
  66. * your own identifying information:
  67. * "Portions Copyrighted [year] [name of copyright owner]"
  68. *
  69. * If you wish your version of this file to be governed by only the CDDL
  70. * or only the GPL Version 2, indicate your decision by adding
  71. * "[Contributor] elects to include this software in this distribution
  72. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  73. * single choice of license, a recipient has the option to distribute
  74. * your version of this file under either the CDDL, the GPL Version 2 or
  75. * to extend the choice of license to its licensees as provided above.
  76. * However, if you add GPL Version 2 code and therefore, elected the GPL
  77. * Version 2 license, then the option applies only if the new code is
  78. * made subject to such option by the copyright holder.
  79. *
  80. * Contributor(s): markiewb@netbeans.org
  81. *
  82. * Portions Copyrighted 2012 Sun Microsystems, Inc.
  83. */
  84. import java.awt.Color;
  85. import java.util.Arrays;
  86. import java.util.BitSet;
  87. import java.util.List;
  88. import org.netbeans.api.annotations.common.NonNull;
  89. import org.openide.util.Parameters;
  90. /**
  91. * Highlight the match of text patterns. The matching parts will be placed
  92. * within HTML-tags, so it can be used easily within the Swing UI.
  93. * @author markiewb
  94. */
  95. public class HighlightingNameFormatter {
  96. private static final String COLOR_FORMAT_PATTERN = "<font style=\"background-color:%s; font-weight:bold; color:%s; white-space:nowrap\">%s</font>"; //NOI18N
  97. private static final String BOLD_FORMAT_PATTERN = "<b>%s</b>"; //NOI18N
  98. private static final String BASE_COLOR_FORMAT_PATTERN = "<font color=\"#%s\">%s</font>"; //NOI18N
  99. private final String formatPattern;
  100. private HighlightingNameFormatter(
  101. @NonNull final String pattern) {
  102. Parameters.notNull("pattern", pattern); //NOI18N
  103. this.formatPattern = pattern;
  104. }
  105. @NonNull
  106. public String formatName(
  107. @NonNull final String name,
  108. @NonNull final String textToFind,
  109. final boolean caseSensitive) {
  110. if (null == textToFind || "".equals(textToFind)) {
  111. return name;
  112. }
  113. BitSet bitSet = new BitSet(name.length());
  114. List<String> parts = splitByCamelCaseAndWildcards(textToFind);
  115. String convertedTypeName = caseSensitive ? name : name.toLowerCase();
  116. //mark the chars to be highlighted
  117. int startIndex = 0;
  118. for (String camelCasePart : parts) {
  119. int indexOf = convertedTypeName.indexOf(caseSensitive ? camelCasePart : camelCasePart.toLowerCase(), startIndex);
  120. if (indexOf != -1) {
  121. //mark the chars
  122. bitSet.set(indexOf, indexOf + camelCasePart.length(), true);
  123. } else {
  124. break;
  125. }
  126. startIndex = indexOf + camelCasePart.length();
  127. }
  128. //highlight the marked chars via tags
  129. StringBuilder formattedTypeName = new StringBuilder();
  130. int i = 0;
  131. while (i < name.toCharArray().length) {
  132. boolean isMarked = bitSet.get(i);
  133. if (isMarked) {
  134. int numberOfContinuousHighlights = bitSet.nextClearBit(i) - i;
  135. String part = name.substring(i, i + numberOfContinuousHighlights);
  136. formattedTypeName.append(String.format(formatPattern, part));
  137. i += numberOfContinuousHighlights;
  138. } else {
  139. formattedTypeName.append(name.charAt(i));
  140. i++;
  141. }
  142. }
  143. return formattedTypeName.toString();
  144. }
  145. @NonNull
  146. public String formatName(
  147. @NonNull final String name,
  148. @NonNull final String textToFind,
  149. final boolean caseSensitive,
  150. @NonNull final Color baseColor) {
  151. final String res = formatName(name, textToFind, caseSensitive);
  152. return String.format(
  153. BASE_COLOR_FORMAT_PATTERN,
  154. Integer.toHexString(baseColor.getRGB()).substring(2),
  155. res);
  156. }
  157. private List<String> splitByCamelCaseAndWildcards(String searchText) {
  158. //AbcDeFGhiJo -> [Abc, De, F, Ghi, Jo]
  159. StringBuilder sb = new StringBuilder(searchText.length());
  160. for (char c : searchText.toCharArray()) {
  161. if (Character.isUpperCase(c)) {
  162. //add magic split marker into text before the uppercase char
  163. //example: AbcDeFGhiJo -> &Abc&De&F&Ghi&Jo
  164. sb.append("&");
  165. sb.append(c);
  166. } else {
  167. sb.append(c);
  168. }
  169. }
  170. //split by camelcase (using the split marker) or the wildcards *,?
  171. String[] split = sb.toString().split("[&|\\*|\\?]");
  172. return Arrays.asList(split);
  173. }
  174. @NonNull
  175. public static HighlightingNameFormatter createColorFormatter(
  176. @NonNull final Color bgColor,
  177. @NonNull final Color fgColor) {
  178. final String bgColorHighlight = Integer.toHexString(bgColor.getRGB()).substring(2);
  179. final String fgColorHighlight = Integer.toHexString(fgColor.getRGB()).substring(2);
  180. return new HighlightingNameFormatter(
  181. String.format(COLOR_FORMAT_PATTERN, bgColorHighlight, fgColorHighlight, "%s")); //NOI18N
  182. }
  183. @NonNull
  184. public static HighlightingNameFormatter createBoldFormatter() {
  185. return new HighlightingNameFormatter(
  186. String.format(BOLD_FORMAT_PATTERN, "%s")); //NOI18N
  187. }
  188. @NonNull
  189. /*test*/ static HighlightingNameFormatter createCustomFormatter(
  190. @NonNull final String format) {
  191. return new HighlightingNameFormatter(format);
  192. }
  193. }