# HG changeset patch # Parent da94f9598740bf4f7f0db7da965431d413f7addc #224328: Find in Projects always using regular expressions diff --git a/api.search/apichanges.xml b/api.search/apichanges.xml --- a/api.search/apichanges.xml +++ b/api.search/apichanges.xml @@ -105,6 +105,57 @@ + + + Search pattern supports three match types (literal, basic wildcards, regexp) + + + + + +

+ Before, the search pattern distinguished two match types: + Regular Expression and Standard. The standard meant that the + text was searched literally in editor search, and that the + searched text could contain basic wildcards (* and ?) in + Find in Projects. +

+

+ Now, the Search pattern can specify exact match type: + Regular Expression, Basic Wildcards and Literal. Basic + wildcards are not supported by Editor search, but the + fallback to Literal Search is transparent for the editor. +

+

+ To support extra match types, a few parts have to be added + to the API: +

+
    +
  • Enumeration SearchPattern.MatchType which contains + all supported match types +
  • +
  • Method SearchPattern.create(String searchExpression, + boolean wholeWords, boolean matchCase, + MatchType matchType) +
  • +
  • Method SearchPattern.getMatchType() and + SearchPattern.changeMatchType(MatchType) +
  • +
  • Enumeration SearchPatternController.MultiOption for + listing SearchPattern properties that can be bound + to combo boxes. +
  • +
  • Method SearchPatternController.bind(MultiOption, + JComboBox) for binding SearchPattern properties to + combo boxes. +
  • +
+
+ + + +
+ Added method getIcon to class SearchScopeDefinition diff --git a/api.search/manifest.mf b/api.search/manifest.mf --- a/api.search/manifest.mf +++ b/api.search/manifest.mf @@ -1,5 +1,5 @@ Manifest-Version: 1.0 OpenIDE-Module: org.netbeans.api.search OpenIDE-Module-Localizing-Bundle: org/netbeans/api/search/Bundle.properties -OpenIDE-Module-Specification-Version: 1.10 +OpenIDE-Module-Specification-Version: 1.11 diff --git a/api.search/src/org/netbeans/api/search/SearchPattern.java b/api.search/src/org/netbeans/api/search/SearchPattern.java --- a/api.search/src/org/netbeans/api/search/SearchPattern.java +++ b/api.search/src/org/netbeans/api/search/SearchPattern.java @@ -43,6 +43,8 @@ */ package org.netbeans.api.search; +import org.openide.util.NbBundle; + /** * Pattern describes the search conditions * @@ -51,6 +53,74 @@ public final class SearchPattern { /** + * Specifies how the pattern is matched to the searched text. + * + * Please note that more items can be added to the enum in the future. + * + * @since api.search/1.11 + */ + @NbBundle.Messages({ + "LBL_MatchType_Literal=Literal", + "LBL_MatchType_Basic_Wildcards=Basic Wildcards", + "LBL_MatchType_Regular_Expression=Regular Expression" + }) + public static enum MatchType { + + /** + * Match the pattern literally. + */ + LITERAL(Bundle.LBL_MatchType_Literal(), 'L'), + /** + * The pattern can contain basic wildcards, star (*) for any string and + * questionaire (?) for any character. The escape character for these + * wildcards is backslash (\). + */ + BASIC(Bundle.LBL_MatchType_Basic_Wildcards(), 'r'), + /** + * The pattern follows java.util.regex.Pattern syntax. + */ + REGEXP(Bundle.LBL_MatchType_Regular_Expression(), 'R'); + private String displayName; + private char canonicalPatternFlag; + + private MatchType(String displayName, char canonicalPatternFlag) { + this.displayName = displayName; + this.canonicalPatternFlag = canonicalPatternFlag; + } + + @Override + public String toString() { + return displayName; + } + + private char getCanonicalPatternFlag() { + return canonicalPatternFlag; + } + + private static MatchType fromCanonicalPatternFlag(char ch) { + switch (ch) { + case 'R': + return REGEXP; + case 'r': + return BASIC; + case 'L': + return LITERAL; + default: + return BASIC; + } + } + + private static boolean isCanonicalPatternFlag(char ch) { + for (MatchType mt : MatchType.values()) { + if (mt.getCanonicalPatternFlag() == ch) { + return true; + } + } + return false; + } + } + + /** * SearchExpression - a text to search */ private String searchExpression; @@ -63,9 +133,9 @@ */ private boolean matchCase; /** - * if true, regular expression search was performed + * match type of this pattern */ - private boolean regExp; + private MatchType matchType; /** * Creates a new instance of SearchPattern @@ -76,11 +146,11 @@ * @param regExp if true, regular expression search was performed */ private SearchPattern(String searchExpression, boolean wholeWords, - boolean matchCase, boolean regExp) { + boolean matchCase, MatchType matchType) { this.searchExpression = searchExpression; this.wholeWords = wholeWords; this.matchCase = matchCase; - this.regExp = regExp; + this.matchType = matchType; } /** @@ -89,12 +159,31 @@ * @param searchExpression non-null String of a searched text * @param wholeWords if true, only whole words were searched * @param matchCase if true, case sensitive search was preformed - * @param regExp if true, regular expression search was performed + * @param regExp if true, regular expression search was performed; if false, + * search with basic wildcards was performed * @return a new SearchPattern in accordance with given parameters */ public static SearchPattern create(String searchExpression, boolean wholeWords, boolean matchCase, boolean regExp) { - return new SearchPattern(searchExpression, wholeWords, matchCase, regExp); + return new SearchPattern(searchExpression, wholeWords, matchCase, + regExp ? MatchType.REGEXP : MatchType.BASIC); + } + + /** + * Creates a new SearchPattern in accordance with given parameters + * + * @param searchExpression non-null String of a searched text + * @param wholeWords if true, only whole words were searched + * @param matchCase if true, case sensitive search was preformed + * @param matchType match type + * @return a new SearchPattern in accordance with given parameters + * + * @since api.search/1.11 + */ + public static SearchPattern create(String searchExpression, + boolean wholeWords, boolean matchCase, MatchType matchType) { + return new SearchPattern(searchExpression, wholeWords, matchCase, + matchType); } /** @@ -123,7 +212,16 @@ * @return true if the regExp parameter was used during search performing */ public boolean isRegExp() { - return regExp; + return matchType == MatchType.REGEXP; + } + + /** + * Get type of this pattern. + * + * @since api.search/1.11 + */ + public MatchType getMatchType() { + return matchType; } @Override @@ -135,7 +233,7 @@ return (this.searchExpression.equals(sp.getSearchExpression()) && this.wholeWords == sp.isWholeWords() && this.matchCase == sp.isMatchCase() - && this.regExp == sp.isRegExp()); + && this.matchType == sp.matchType); } @Override @@ -143,7 +241,7 @@ int result = 17; result = 37 * result + (this.wholeWords ? 1 : 0); result = 37 * result + (this.matchCase ? 1 : 0); - result = 37 * result + (this.regExp ? 1 : 0); + result = 37 * result + (this.matchType.hashCode()); result = 37 * result + this.searchExpression.hashCode(); return result; } @@ -160,7 +258,7 @@ return this; } else { return SearchPattern.create(expression, wholeWords, - matchCase, regExp); + matchCase, matchType); } } @@ -174,7 +272,7 @@ return this; } else { return SearchPattern.create(searchExpression, wholeWords, - matchCase, regExp); + matchCase, matchType); } } @@ -188,7 +286,7 @@ return this; } else { return SearchPattern.create(searchExpression, wholeWords, - matchCase, regExp); + matchCase, matchType); } } @@ -198,17 +296,32 @@ * */ public SearchPattern changeRegExp(boolean regExp) { - if (this.regExp == regExp) { + if (this.isRegExp() == regExp) { return this; } else { return SearchPattern.create(searchExpression, wholeWords, matchCase, regExp); } } - + + /** + * Create new instance with "match type" set to passed value, and other + * values copied from this instance. + * + * @since api.search/1.11 + */ + public SearchPattern changeMatchType(MatchType matchType) { + if (this.matchType == matchType) { + return this; + } else { + return SearchPattern.create(searchExpression, wholeWords, matchCase, + matchType); + } + } + String toCanonicalString() { char m = isMatchCase() ? 'M' : 'm'; - char r = isRegExp() ? 'R' : 'r'; + char r = matchType.getCanonicalPatternFlag(); char w = isWholeWords() ? 'W' : 'w'; return "" + m + r + w + "-" + getSearchExpression(); //NOI18N } @@ -217,16 +330,17 @@ //format mrw-findwhat if (canonicalString == null || Character.toUpperCase(canonicalString.charAt(0)) != 'M' - || Character.toUpperCase(canonicalString.charAt(1)) != 'R' + || !MatchType.isCanonicalPatternFlag(canonicalString.charAt(1)) || Character.toUpperCase(canonicalString.charAt(2)) != 'W' || canonicalString.charAt(3) != '-') { return null; } boolean matchCase = Character.isUpperCase(canonicalString.charAt(0)); - boolean regExp = Character.isUpperCase(canonicalString.charAt(1)); + MatchType matchType = MatchType.fromCanonicalPatternFlag( + canonicalString.charAt(1)); boolean wholeWords = Character.isUpperCase(canonicalString.charAt(2)); String findWhat = canonicalString.substring(4); - return new SearchPattern(findWhat, wholeWords, matchCase, regExp); + return new SearchPattern(findWhat, wholeWords, matchCase, matchType); } } diff --git a/api.search/src/org/netbeans/api/search/ui/ScopeOptionsController.java b/api.search/src/org/netbeans/api/search/ui/ScopeOptionsController.java --- a/api.search/src/org/netbeans/api/search/ui/ScopeOptionsController.java +++ b/api.search/src/org/netbeans/api/search/ui/ScopeOptionsController.java @@ -153,7 +153,8 @@ if (searchAndReplace) { jp.setLayout(new FlowLayout(FlowLayout.LEADING, 0, 0)); jp.add(ignoreListOptionPanel); - jp.add(chkFileNameRegex); + jp.add(new CheckBoxWithButtonPanel( + chkFileNameRegex, btnTestFileNamePattern)); jp.setMaximumSize(jp.getMinimumSize()); } else { FormLayoutHelper flh = new FormLayoutHelper(jp, diff --git a/api.search/src/org/netbeans/api/search/ui/SearchPatternController.java b/api.search/src/org/netbeans/api/search/ui/SearchPatternController.java --- a/api.search/src/org/netbeans/api/search/ui/SearchPatternController.java +++ b/api.search/src/org/netbeans/api/search/ui/SearchPatternController.java @@ -60,6 +60,7 @@ import org.netbeans.api.annotations.common.NullAllowed; import org.netbeans.api.search.SearchHistory; import org.netbeans.api.search.SearchPattern; +import org.netbeans.api.search.SearchPattern.MatchType; import org.netbeans.modules.search.FindDialogMemory; import org.netbeans.modules.search.ui.PatternChangeListener; import org.netbeans.modules.search.ui.ShorteningCellRenderer; @@ -84,10 +85,28 @@ public enum Option { MATCH_CASE, WHOLE_WORDS, REGULAR_EXPRESSION } + + /** + * Options of search patterns that are not boolean, but can have more than + * two values, e.g. {@link MatchType}. + * + * Please note that more items can be added to the enum in the future. + * + * @since api.search/1.11 + */ + public enum MultiOption { + + MATCH_TYPE + } + private final Map bindings = new EnumMap(Option.class); + private final Map comboBindings = + new EnumMap(MultiOption.class); private final Map options = new EnumMap(Option.class); + private final Map multiOptions = + new EnumMap(MultiOption.class); private final ItemListener listener; private boolean valid; private Color defaultTextColor = null; @@ -142,9 +161,18 @@ break; } } + for (Map.Entry be + : comboBindings.entrySet()) { + switch (be.getKey()) { + case MATCH_TYPE: + be.getValue().setSelectedItem(sp.getMatchType()); + break; + } + } options.put(Option.MATCH_CASE, sp.isMatchCase()); options.put(Option.WHOLE_WORDS, sp.isWholeWords()); options.put(Option.REGULAR_EXPRESSION, sp.isRegExp()); + multiOptions.put(MultiOption.MATCH_TYPE, sp.getMatchType()); } } }); @@ -225,6 +253,40 @@ button.setSelected(value); } if (option == Option.REGULAR_EXPRESSION) { + if ((getOption(MultiOption.MATCH_TYPE) == MatchType.REGEXP) + != value) { + setOption(MultiOption.MATCH_TYPE, + value ? MatchType.REGEXP : MatchType.LITERAL); + } + updateValidity(); + } + fireChange(); + } + + /** + * Get current value of an option of the search pattern. + */ + private Object getOption(MultiOption option) { + Parameters.notNull("option", option); //NOI18N + return multiOptions.get(option); + } + + /** + * Set value of a search pattern option. The correct item in corresponding + * combo box will be selected accordingly. + */ + private void setOption(MultiOption option, Object value) { + Parameters.notNull("option", option); //NOI18N + multiOptions.put(option, value); + JComboBox combo = comboBindings.get(option); + if (combo != null && combo.getSelectedItem() != value) { + combo.setSelectedItem(value); + } + if (option == MultiOption.MATCH_TYPE) { + if (getOption(Option.REGULAR_EXPRESSION) + != (MatchType.REGEXP == value)) { + setOption(Option.REGULAR_EXPRESSION, value == MatchType.REGEXP); + } updateValidity(); } fireChange(); @@ -237,7 +299,7 @@ return SearchPattern.create(getText(), getOption(Option.WHOLE_WORDS), getOption(Option.MATCH_CASE), - getOption(Option.REGULAR_EXPRESSION)); + (MatchType) getOption(MultiOption.MATCH_TYPE)); } /** @@ -248,7 +310,7 @@ setText(searchPattern.getSearchExpression()); setOption(Option.WHOLE_WORDS, searchPattern.isWholeWords()); setOption(Option.MATCH_CASE, searchPattern.isMatchCase()); - setOption(Option.REGULAR_EXPRESSION, searchPattern.isRegExp()); + setOption(MultiOption.MATCH_TYPE, searchPattern.getMatchType()); } /** @@ -278,6 +340,34 @@ } /** + * Bind a combo box to a SearchPattern option. + * + * @param option Option whose value the button should represent. + * @param comboBox Combo box to control and display the option. + * + * @since api.search/1.11 + */ + public void bind(@NonNull final MultiOption option, + @NonNull final JComboBox comboBox) { + Parameters.notNull("option", option); //NOI18N + Parameters.notNull("comboBox", comboBox); //NOI18N + + if (comboBindings.containsKey(option)) { + throw new IllegalStateException( + "Already bound with option " + option); // NOI18N + } + + comboBindings.put(option, comboBox); + comboBox.setSelectedItem(getOption(option)); + comboBox.addItemListener(new ItemListener() { + @Override + public void itemStateChanged(ItemEvent e) { + setOption(option, comboBox.getSelectedItem()); + } + }); + } + + /** * Unbind a button from a SearchPattern option. */ public void unbind(@NonNull Option option, @NonNull AbstractButton button) { diff --git a/api.search/src/org/netbeans/modules/search/BasicSearchCriteria.java b/api.search/src/org/netbeans/modules/search/BasicSearchCriteria.java --- a/api.search/src/org/netbeans/modules/search/BasicSearchCriteria.java +++ b/api.search/src/org/netbeans/modules/search/BasicSearchCriteria.java @@ -51,6 +51,7 @@ import javax.swing.event.ChangeListener; import org.netbeans.api.search.RegexpUtil; import org.netbeans.api.search.SearchPattern; +import org.netbeans.api.search.SearchPattern.MatchType; import org.netbeans.api.search.SearchScopeOptions; import org.openide.filesystems.FileObject; import org.openide.loaders.DataObject; @@ -113,7 +114,7 @@ */ setCaseSensitive(template.searchPattern.isMatchCase()); setWholeWords(template.searchPattern.isWholeWords()); - setRegexp(template.searchPattern.isRegExp()); + setMatchType(template.searchPattern.getMatchType()); setPreserveCase(template.preserveCase); setSearchInArchives(template.searcherOptions.isSearchInArchives()); setSearchInGenerated(template.searcherOptions.isSearchInGenerated()); @@ -243,8 +244,11 @@ return true; } - boolean isRegexp() { - return searchPattern.isRegExp(); + /** + * Get text pattern match type. + */ + MatchType getMatchType() { + return searchPattern.getMatchType(); } boolean isPreserveCase() { @@ -305,9 +309,9 @@ this.useIgnoreList = useIgnoreList; } - void setRegexp(boolean regexp) { + void setMatchType(MatchType matchType) { - searchPattern = searchPattern.changeRegExp(regexp); + searchPattern = searchPattern.changeMatchType(matchType); updateTextPattern(); replacePatternValid = validateReplacePattern(); updateUsability(true); diff --git a/api.search/src/org/netbeans/modules/search/BasicSearchForm.java b/api.search/src/org/netbeans/modules/search/BasicSearchForm.java --- a/api.search/src/org/netbeans/modules/search/BasicSearchForm.java +++ b/api.search/src/org/netbeans/modules/search/BasicSearchForm.java @@ -69,15 +69,17 @@ import org.netbeans.api.search.ReplacePattern; import org.netbeans.api.search.SearchHistory; import org.netbeans.api.search.SearchPattern; +import org.netbeans.api.search.SearchPattern.MatchType; import org.netbeans.api.search.provider.SearchInfo; import org.netbeans.api.search.ui.ComponentUtils; import org.netbeans.api.search.ui.FileNameController; import org.netbeans.api.search.ui.ScopeController; import org.netbeans.api.search.ui.ScopeOptionsController; import org.netbeans.api.search.ui.SearchPatternController; +import org.netbeans.api.search.ui.SearchPatternController.MultiOption; import org.netbeans.api.search.ui.SearchPatternController.Option; -import org.netbeans.modules.search.ui.CheckBoxWithButtonPanel; import org.netbeans.modules.search.ui.FormLayoutHelper; +import org.netbeans.modules.search.ui.LinkButtonPanel; import org.netbeans.modules.search.ui.PatternChangeListener; import org.netbeans.modules.search.ui.ShorteningCellRenderer; import org.netbeans.modules.search.ui.TextFieldFocusListener; @@ -256,14 +258,15 @@ chkWholeWords = new JCheckBox(); chkCaseSensitive = new JCheckBox(); - chkRegexp = new JCheckBox(); + textToFindType = new TextToFindTypeComboBox(); TextPatternCheckBoxGroup.bind( - chkCaseSensitive, chkWholeWords, chkRegexp, chkPreserveCase); + chkCaseSensitive, chkWholeWords, textToFindType, chkPreserveCase); setMnemonics(searchAndReplace); initFormPanel(searchAndReplace); + updateTextToFindInfo(); this.add(formPanel); /* find the editor components of combo-boxes: */ @@ -282,7 +285,8 @@ formPanel = new SearchFormPanel(); formPanel.addRow(lblTextToFind, cboxTextToFind.getComponent()); - formPanel.addRow(new JLabel(), lblTextToFindHint); + JPanel hintAndButtonPanel = initHintAndButtonPanel(); + formPanel.addRow(new JLabel(), hintAndButtonPanel); initContainingTextOptionsRow(searchAndReplace); if (searchAndReplace) { formPanel.addRow(lblReplacement, cboxReplacement); @@ -304,13 +308,21 @@ private void initContainingTextOptionsRow(boolean searchAndReplace) { JPanel jp = new JPanel(); + JPanel typeWithLabelPanel = new JPanel(); + typeWithLabelPanel.setLayout(new BoxLayout(typeWithLabelPanel, + BoxLayout.LINE_AXIS)); + JLabel typeLabel = new JLabel(); + typeLabel.setLabelFor(textToFindType); + lclz(typeLabel, "BasicSearchForm.textToFindType.label.text"); //NOI18N + typeLabel.setBorder(new EmptyBorder(0, 10, 0, 5)); + typeWithLabelPanel.add(typeLabel); + typeWithLabelPanel.add(textToFindType); if (searchAndReplace) { FormLayoutHelper flh = new FormLayoutHelper(jp, FormLayoutHelper.DEFAULT_COLUMN, FormLayoutHelper.DEFAULT_COLUMN); flh.addRow(chkCaseSensitive, chkPreserveCase); - flh.addRow(chkWholeWords, - new CheckBoxWithButtonPanel(chkRegexp, btnTestTextToFind)); + flh.addRow(chkWholeWords, typeWithLabelPanel); jp.setMaximumSize(jp.getMinimumSize()); formPanel.addRow(new JLabel(), jp); @@ -318,7 +330,7 @@ jp.setLayout(new BoxLayout(jp, BoxLayout.LINE_AXIS)); jp.add(chkCaseSensitive); jp.add(chkWholeWords); - jp.add(new CheckBoxWithButtonPanel(chkRegexp, btnTestTextToFind)); + jp.add(typeWithLabelPanel); formPanel.addRow(new JLabel(), jp); } } @@ -336,9 +348,9 @@ UiUtils.getText( "BasicSearchForm.chkCaseSensitive." //NOI18N + "AccessibleDescription")); //NOI18N - chkRegexp.getAccessibleContext().setAccessibleDescription( + textToFindType.getAccessibleContext().setAccessibleDescription( UiUtils.getText( - "BasicSearchForm.chkRegexp." //NOI18N + "BasicSearchForm.textToFindType." //NOI18N + "AccessibleDescription")); //NOI18N chkWholeWords.getAccessibleContext().setAccessibleDescription( UiUtils.getText( @@ -404,12 +416,18 @@ new ReplacementPatternListener()); } - chkRegexp.addItemListener(this); - cboxTextToFind.bind(Option.REGULAR_EXPRESSION, chkRegexp); + textToFindType.addItemListener(this); + cboxTextToFind.bind(MultiOption.MATCH_TYPE, textToFindType); cboxTextToFind.bind(Option.MATCH_CASE, chkCaseSensitive); cboxTextToFind.bind(Option.WHOLE_WORDS, chkWholeWords); + textToFindType.addActionListener(new ActionListener() { - boolean regexp = chkRegexp.isSelected(); + @Override + public void actionPerformed(ActionEvent e) { + } + }); + + boolean regexp = textToFindType.isRegexp(); boolean caseSensitive = chkCaseSensitive.isSelected(); chkWholeWords.setEnabled(!regexp); if (searchAndReplace) { @@ -448,7 +466,7 @@ public void stateChanged(ChangeEvent e) { SearchPattern sp = cboxTextToFind.getSearchPattern(); searchCriteria.setTextPattern(sp.getSearchExpression()); - searchCriteria.setRegexp(sp.isRegExp()); + searchCriteria.setMatchType(sp.getMatchType()); searchCriteria.setWholeWords(sp.isWholeWords()); searchCriteria.setCaseSensitive(sp.isMatchCase()); } @@ -519,7 +537,7 @@ chkWholeWords.setSelected(memory.isWholeWords()); chkCaseSensitive.setSelected(memory.isCaseSensitive()); - chkRegexp.setSelected(memory.isRegularExpression()); + textToFindType.setSelectedItem(memory.getMatchType()); scopeSettingsPanel.setFileNameRegexp(memory.isFilePathRegex()); scopeSettingsPanel.setUseIgnoreList(memory.IsUseIgnoreList()); @@ -535,7 +553,7 @@ private void setSearchCriteriaValues() { searchCriteria.setWholeWords(chkWholeWords.isSelected()); searchCriteria.setCaseSensitive(chkCaseSensitive.isSelected()); - searchCriteria.setRegexp(chkRegexp.isSelected()); + searchCriteria.setMatchType(textToFindType.getSelectedMatchType()); searchCriteria.setFileNameRegexp(scopeSettingsPanel.isFileNameRegExp()); searchCriteria.setUseIgnoreList(scopeSettingsPanel.isUseIgnoreList()); searchCriteria.setSearchInArchives( @@ -621,7 +639,7 @@ public void itemStateChanged(ItemEvent e) { final ItemSelectable toggle = e.getItemSelectable(); final boolean selected = (e.getStateChange() == ItemEvent.SELECTED); - if (toggle == chkRegexp) { + if (toggle == textToFindType) { if (cboxReplacement != null){ updateReplacePatternColor(); } @@ -634,12 +652,25 @@ } private void updateTextToFindInfo() { - String bungleKey = (searchCriteria.isRegexp()) - ? "BasicSearchForm.cboxTextToFind.info.re" //NOI18N - : "BasicSearchForm.cboxTextToFind.info"; //NOI18N - String text = UiUtils.getText(bungleKey); + String key; + switch (cboxTextToFind.getSearchPattern().getMatchType()) { + case LITERAL: + key = "BasicSearchForm.cboxTextToFind.info.literal"; //NOI18N + break; + case BASIC: + key = "BasicSearchForm.cboxTextToFind.info"; //NOI18N + break; + case REGEXP: + key = "BasicSearchForm.cboxTextToFind.info.re"; //NOI18N + break; + default: + key = "BasicSearchForm.cboxTextToFind.info"; //NOI18N + } + String text = UiUtils.getText(key); cboxTextToFind.getComponent().setToolTipText(text); lblTextToFindHint.setText(text); + btnTestTextToFindPanel.setButtonEnabled( + searchCriteria.getSearchPattern().isRegExp()); } private void updateFileNamePatternInfo() { @@ -677,7 +708,7 @@ } memory.setWholeWords(chkWholeWords.isSelected()); memory.setCaseSensitive(chkCaseSensitive.isSelected()); - memory.setRegularExpression(chkRegexp.isSelected()); + memory.setMatchType(textToFindType.getSelectedMatchType()); if (searchCriteria.isSearchAndReplace()) { memory.setPreserveCase(chkPreserveCase.isSelected()); } else { @@ -733,10 +764,11 @@ "BasicSearchForm.lblFileNamePattern.text"); //NOI18N lclz(chkWholeWords, "BasicSearchForm.chkWholeWords.text"); //NOI18N lclz(chkCaseSensitive, "BasicSearchForm.chkCaseSensitive.text");//NOI18N - lclz(chkRegexp, "BasicSearchForm.chkRegexp.text"); //NOI18N - + btnTestTextToFind.setText(UiUtils.getHtmlLink( "BasicSearchForm.btnTestTextToFind.text")); //NOI18N + btnTestTextToFind.setToolTipText(UiUtils.getText( + "BasicSearchForm.btnTestTextToFind.tooltip")); //NOI18N if (searchAndReplace) { @@ -746,7 +778,6 @@ } else { } - updateTextToFindInfo(); } private void lclz(AbstractButton ab, String msg) { @@ -765,11 +796,12 @@ private FileNameController cboxFileNamePattern; private JCheckBox chkWholeWords; private JCheckBox chkCaseSensitive; - private JCheckBox chkRegexp; + private TextToFindTypeComboBox textToFindType; private JCheckBox chkPreserveCase; private JTextComponent replacementPatternEditor; protected SearchFormPanel formPanel; private JButton btnTestTextToFind; + private LinkButtonPanel btnTestTextToFindPanel; private JLabel lblTextToFind; private JLabel lblTextToFindHint; private ScopeController cboxScope; @@ -782,6 +814,20 @@ private boolean invalidReplacePattern = false; private ScopeOptionsController scopeSettingsPanel; + private JPanel initHintAndButtonPanel() { + btnTestTextToFindPanel = new LinkButtonPanel(btnTestTextToFind); + btnTestTextToFindPanel.setButtonEnabled( + searchCriteria.getSearchPattern().isRegExp()); + lblTextToFindHint.setMaximumSize(new Dimension(Integer.MAX_VALUE, + (int) btnTestTextToFindPanel.getPreferredSize().getHeight())); + JPanel hintAndButtonPanel = new JPanel(); + hintAndButtonPanel.setLayout( + new BoxLayout(hintAndButtonPanel, BoxLayout.LINE_AXIS)); + hintAndButtonPanel.add(lblTextToFindHint); + hintAndButtonPanel.add(btnTestTextToFindPanel); + return hintAndButtonPanel; + } + /** * Form panel to which rows can be added. */ @@ -853,7 +899,7 @@ private JCheckBox matchCase; private JCheckBox wholeWords; - private JCheckBox regexp; + private TextToFindTypeComboBox textToFindType; private JCheckBox preserveCase; private boolean lastPreserveCaseValue; private boolean lastWholeWordsValue; @@ -861,19 +907,19 @@ private TextPatternCheckBoxGroup( JCheckBox matchCase, JCheckBox wholeWords, - JCheckBox regexp, + TextToFindTypeComboBox textToFindType, JCheckBox preserveCase) { this.matchCase = matchCase; this.wholeWords = wholeWords; - this.regexp = regexp; + this.textToFindType = textToFindType; this.preserveCase = preserveCase; } private void initListeners() { this.matchCase.addItemListener(this); this.wholeWords.addItemListener(this); - this.regexp.addItemListener(this); + this.textToFindType.addItemListener(this); if (this.preserveCase != null) { this.preserveCase.addItemListener(this); } @@ -889,8 +935,8 @@ } private void updateWholeWordsAllowed() { - if (regexp.isSelected() == wholeWords.isEnabled()) { - if (regexp.isSelected()) { + if (textToFindType.isRegexp() == wholeWords.isEnabled()) { + if (textToFindType.isRegexp()) { lastWholeWordsValue = wholeWords.isSelected(); wholeWords.setSelected(false); wholeWords.setEnabled(false); @@ -906,7 +952,7 @@ return; } if (preserveCase.isEnabled() - == (regexp.isSelected() || matchCase.isSelected())) { + == (textToFindType.isRegexp() || matchCase.isSelected())) { if (preserveCase.isEnabled()) { lastPreserveCaseValue = preserveCase.isSelected(); preserveCase.setSelected(false); @@ -923,14 +969,14 @@ ItemSelectable is = e.getItemSelectable(); if (is == matchCase) { matchCaseChanged(); - } else if (is == regexp) { + } else if (is == textToFindType) { regexpChanged(); } } static void bind(JCheckBox matchCase, JCheckBox wholeWords, - JCheckBox regexp, + TextToFindTypeComboBox regexp, JCheckBox preserveCase) { TextPatternCheckBoxGroup tpcbg = new TextPatternCheckBoxGroup( @@ -970,4 +1016,37 @@ return replacePattern.getReplaceExpression(); } } + + private static class TextToFindTypeComboBox extends JComboBox { + + public TextToFindTypeComboBox() { + super(new Object[]{MatchType.LITERAL, + MatchType.BASIC, MatchType.REGEXP}); + } + + public boolean isRegexp() { + return isSelected(MatchType.REGEXP); + } + + public boolean isBasic() { + return isSelected(MatchType.BASIC); + } + + public boolean isLiteral() { + return isSelected(MatchType.LITERAL); + } + + public boolean isSelected(MatchType type) { + return getSelectedItem() == type; + } + + private MatchType getSelectedMatchType() { + Object selected = getSelectedItem(); + if (selected instanceof MatchType) { + return (MatchType) selected; + } else { + throw new IllegalStateException("MatchType expected"); //NOI18N + } + } + } } diff --git a/api.search/src/org/netbeans/modules/search/BasicSearchProvider.java b/api.search/src/org/netbeans/modules/search/BasicSearchProvider.java --- a/api.search/src/org/netbeans/modules/search/BasicSearchProvider.java +++ b/api.search/src/org/netbeans/modules/search/BasicSearchProvider.java @@ -338,7 +338,7 @@ bsc.setTextPattern(searchPattern.getSearchExpression()); bsc.setCaseSensitive(searchPattern.isMatchCase()); bsc.setWholeWords(searchPattern.isWholeWords()); - bsc.setRegexp(searchPattern.isRegExp()); + bsc.setMatchType(searchPattern.getMatchType()); if (preserveCase != null) { bsc.setPreserveCase(preserveCase); } diff --git a/api.search/src/org/netbeans/modules/search/Bundle.properties b/api.search/src/org/netbeans/modules/search/Bundle.properties --- a/api.search/src/org/netbeans/modules/search/Bundle.properties +++ b/api.search/src/org/netbeans/modules/search/Bundle.properties @@ -211,7 +211,8 @@ BasicSearchForm.lblTextToFind.text=Containing &Text\: BasicSearchForm.cboxTextToFind.info=(* \= any string, ? \= any character, \\ \= escape for * ?) -BasicSearchForm.cboxTextToFind.info.re=(Use java.util.regex.Pattern regular expression syntax) +BasicSearchForm.cboxTextToFind.info.re=(Use java.util.regex.Pattern syntax) +BasicSearchForm.cboxTextToFind.info.literal=(The text will be used literally) BasicSearchForm.lblReplacement.text=&Replace With\: @@ -235,13 +236,13 @@ BasicSearchForm.chkCaseSensitive.AccessibleDescription=Whether the fulltext search should be case-sensitive. -BasicSearchForm.chkRegexp.text=Regular &Expression +BasicSearchForm.textToFindType.label.text=T&ype: BasicSearchForm.chkPreserveCase.text=Preser&ve Case when Replacing BasicSearchForm.chkPreserveCase.AccessibleDescription=Preserve case according to found instance of word. -BasicSearchForm.chkRegexp.AccessibleDescription=If checked, the search pattern will be used as a regular expression. +BasicSearchForm.textToFindType.AccessibleDescription=Choose whether you want to use the Text to Find value as literal text, text with basic wildcards, or regular expression. BasicSearchForm.chkArchives.text=Search in &Archives @@ -251,6 +252,7 @@ BasicSearchForm.chkFileNameRegex.tooltip=Use regular expression for the file path BasicSearchForm.btnTestTextToFind.text=test +BasicSearchForm.btnTestTextToFind.tooltip=Open window for testing of regular expressions BasicSearchForm.btnTestFileNamePattern.text=test diff --git a/api.search/src/org/netbeans/modules/search/FindDialogMemory.java b/api.search/src/org/netbeans/modules/search/FindDialogMemory.java --- a/api.search/src/org/netbeans/modules/search/FindDialogMemory.java +++ b/api.search/src/org/netbeans/modules/search/FindDialogMemory.java @@ -48,6 +48,7 @@ import java.util.Collections; import java.util.List; import java.util.prefs.Preferences; +import org.netbeans.api.search.SearchPattern.MatchType; import org.openide.util.NbBundle; import org.openide.util.NbPreferences; @@ -89,9 +90,9 @@ private boolean preserveCase; /** - * Storage of last used Regular Expression option. + * Storage of last used search pattern match type. */ - private boolean regularExpression; + private MatchType matchType; /** * whether a full text pattern was used last time @@ -164,7 +165,7 @@ private static final String PROP_WHOLE_WORDS = "whole_words"; //NOI18N private static final String PROP_CASE_SENSITIVE = "case_sensitive"; //NOI18N private static final String PROP_PRESERVE_CASE = "preserve_case"; //NOI18N - private static final String PROP_REGULAR_EXPRESSION = "regular_expression"; //NOI18N + private static final String PROP_MATCH_TYPE = "match_type"; //NOI18N private static final String PROP_SCOPE_TYPE_ID = "scope_type_id"; //NOI18N private static final String PROP_FILENAME_PATTERN_SPECIFIED = "filename_specified"; //NOI18N private static final String PROP_FILENAME_PATTERN_PREFIX = "filename_pattern_"; //NOI18N @@ -204,7 +205,12 @@ private void load () { wholeWords = prefs.getBoolean(PROP_WHOLE_WORDS, false); caseSensitive = prefs.getBoolean(PROP_CASE_SENSITIVE, false); - regularExpression = prefs.getBoolean(PROP_REGULAR_EXPRESSION, false); + try { + String name = prefs.get(PROP_MATCH_TYPE, MatchType.BASIC.name()); + matchType = MatchType.valueOf(name); + } catch (Exception e) { + matchType = MatchType.BASIC; + } preserveCase = prefs.getBoolean(PROP_PRESERVE_CASE, false); scopeTypeId = prefs.get(PROP_SCOPE_TYPE_ID, "open projects"); //NOI18N fileNamePatternSpecified = prefs.getBoolean(PROP_FILENAME_PATTERN_SPECIFIED, false); @@ -311,13 +317,13 @@ prefs.putBoolean(PROP_PRESERVE_CASE, preserveCase); } - public boolean isRegularExpression() { - return regularExpression; + public MatchType getMatchType() { + return matchType; } - public void setRegularExpression(boolean regularExpression) { - this.regularExpression = regularExpression; - prefs.putBoolean(PROP_REGULAR_EXPRESSION, regularExpression); + public void setMatchType(MatchType matchType) { + this.matchType = matchType; + prefs.put(PROP_MATCH_TYPE, matchType.name()); } public String getScopeTypeId() { diff --git a/api.search/src/org/netbeans/modules/search/MatchingObject.java b/api.search/src/org/netbeans/modules/search/MatchingObject.java --- a/api.search/src/org/netbeans/modules/search/MatchingObject.java +++ b/api.search/src/org/netbeans/modules/search/MatchingObject.java @@ -844,7 +844,7 @@ } String replacedString = resultModel.basicCriteria.getReplaceExpr(); - if (resultModel.basicCriteria.isRegexp()){ + if (resultModel.basicCriteria.getSearchPattern().isRegExp()){ Matcher m = resultModel.basicCriteria.getTextPattern().matcher(matchedSubstring); replacedString = m.replaceFirst(resultModel.basicCriteria.getReplaceString()); } else if (resultModel.basicCriteria.isPreserveCase()) { diff --git a/api.search/src/org/netbeans/modules/search/PatternSandbox.java b/api.search/src/org/netbeans/modules/search/PatternSandbox.java --- a/api.search/src/org/netbeans/modules/search/PatternSandbox.java +++ b/api.search/src/org/netbeans/modules/search/PatternSandbox.java @@ -477,7 +477,7 @@ this.regexp = regexp; this.matchCase = matchCase; initComponents(); - searchCriteria.setRegexp(true); + searchCriteria.setMatchType(SearchPattern.MatchType.REGEXP); } @Override diff --git a/api.search/src/org/netbeans/modules/search/RegexpMaker.java b/api.search/src/org/netbeans/modules/search/RegexpMaker.java deleted file mode 100644 --- a/api.search/src/org/netbeans/modules/search/RegexpMaker.java +++ /dev/null @@ -1,356 +0,0 @@ -/* - * 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): - * - * Portions Copyrighted 2007 Sun Microsystems, Inc. - */ - -package org.netbeans.modules.search; - -import java.util.regex.Pattern; - -/** - * Parser of simple regular expressions with only three supported special - * characters {@code '*'} (zero or more), {@code '?'} (zero or one) - * and {@code '\\'} (quotes the following character). - * - * @author Marian Petras - */ -final class RegexpMaker { - - /** regular expression representing a set of word characters */ - private static final String wordCharsExpr - = "[\\p{javaLetterOrDigit}_]"; //NOI18N - /** - * regular expression representing negative lookbehind - * for a {@linkplain #wordCharsExpr word character} - */ - private static final String checkNotAfterWordChar - = "(?Whole Words option is selected - * @return regular expression corresponding to the simple pattern - */ - static String makeRegexp(String simplePattern, boolean wholeWords) { - if (simplePattern.length() == 0) { //trivial case - return simplePattern; - } - - if (!wholeWords - && Pattern.matches("[a-zA-Z0-9 ]*", simplePattern)) { //NOI18N - return simplePattern; //trivial case - } - - StringBuilder buf = new StringBuilder(simplePattern.length() + 16); - boolean quoted = false; - boolean starPresent = false; - int minCount = 0; - - boolean bufIsEmpty = true; - char lastInputChar = '*'; //might be any other non-word character - for (char c : simplePattern.toCharArray()) { - if (quoted && (c == '?' || c == '*')) { - assert !starPresent && (minCount == 0); - if (wholeWords && bufIsEmpty) { - buf.append(checkNotAfterWordChar); - } - buf.append('\\'); - buf.append(c); - lastInputChar = c; - bufIsEmpty = false; - quoted = false; - } else if (c == '?') { - assert !quoted; - minCount++; - } else if (c == '*') { - assert !quoted; - starPresent = true; - } else { - if (starPresent || (minCount != 0)) { - if (wholeWords && bufIsEmpty && !starPresent) { - buf.append(checkNotAfterWordChar); - } - bufIsEmpty &= !addMetachars(buf, starPresent, minCount, wholeWords, !bufIsEmpty); - starPresent = false; - minCount = 0; - } - if(quoted) { // backslash was not used for escaping - buf.append("\\\\"); - quoted = false; - } - if (c == '\\') { - quoted = true; - } else { - if (wholeWords && bufIsEmpty && isWordChar(c)) { - buf.append(checkNotAfterWordChar); - } - if (isSpecialCharacter(c)) { - buf.append('\\'); - } - buf.append(c); - lastInputChar = c; - bufIsEmpty = false; - } - } - } - if (quoted) { - assert !starPresent && (minCount == 0); - buf.append('\\').append('\\'); - lastInputChar = '\\'; - bufIsEmpty = false; - quoted = false; - } else if (starPresent || (minCount != 0)) { - if (wholeWords && !starPresent && bufIsEmpty) { - buf.append(checkNotAfterWordChar); - } - bufIsEmpty &= !addMetachars(buf, starPresent, minCount, wholeWords, false); - if (wholeWords && !starPresent) { - buf.append(checkNotBeforeWordChar); - } - lastInputChar = '*'; //might be any other non-word character - starPresent = false; - minCount = 0; - } - if (wholeWords && isWordChar(lastInputChar)) { - buf.append(checkNotBeforeWordChar); - } - return buf.toString(); - } - - /** - * Checks whether the given character is a word character. - * @param c character to be checked - * @return {@code true} if the character is a word character, - * {@code false} otherwise - * @see #wordCharsExpr - */ - private static boolean isWordChar(char c) { - /* not necessary - just for performance */ - if ((c == '*') || (c == '\\')) { - return false; - } - - assert "[\\p{javaLetterOrDigit}_]".equals(wordCharsExpr) //NOI18N - : "update implementation of method isWordChar(char)"; //NOI18N - return (c == '_') || Character.isLetterOrDigit(c); - } - - /** - * Generates the part of a regular expression, that represents a sequence - * of simple expression's metacharacters {@code '*'} and {@code '?'}, - * and adds it to the given string buffer. - * - * @param buf string buffer to which the new part is to be added - * @param starPresent whether the sequence contained at least one - * {@code '*'} character - * @param minCount number of {@code '?'} characters in the sequence - * @param wholeWords whether the Whole Words option is selected - * @param middle whether the metachars are to be placed in the middle - * (i.e. not in the beginning or at the end) of the search - * expression - * @return {@code true} if something was added to the string buffer, - * {@code false} if the buffer was not modified - */ - private static boolean addMetachars(final StringBuilder buf, - boolean starPresent, - final int minCount, - final boolean wholeWords, - final boolean middle) { - assert starPresent || (minCount != 0); - - /* - * If 'Whole Words' is not activated, ignore stars in the beginning - * and at the end of the expression: - */ - if (starPresent && !wholeWords && !middle) { - starPresent = false; - } - - if ((minCount == 0) && !starPresent) { - return false; - } - - if (wholeWords) { - buf.append(wordCharsExpr); - } else { - buf.append('.'); - } - switch (minCount) { - case 0: - assert starPresent; - buf.append('*'); - break; - case 1: - if (starPresent) { - buf.append('+'); - } - break; - default: - if (wholeWords) { - buf.append('{').append(minCount); - if (starPresent) { - buf.append(','); - } - buf.append('}'); - } else { - for (int i = 1; i < minCount; i++) { - buf.append('.'); - } - if (starPresent) { - buf.append('+'); - } - } - } - if (starPresent && middle) { - buf.append('?'); //use reluctant variant of the quantifier - } - return true; - } - - /** - * Translates the given simple pattern (or several patterns) to a single - * regular expression. - * - * @param simplePatternList pattern list to be translated - * @return regular expression corresponding to the simple pattern - * (or to the list of simple patterns) - */ - static String makeMultiRegexp(String simplePatternList) { - if (simplePatternList.length() == 0) { //trivial case - return simplePatternList; - } - - if (Pattern.matches("[a-zA-Z0-9]*", simplePatternList)) { //NOI18N - return simplePatternList; //trivial case - } - - StringBuilder buf = new StringBuilder(simplePatternList.length() + 16); - boolean lastWasSeparator = false; - boolean quoted = false; - boolean starPresent = false; - for (char c : simplePatternList.toCharArray()) { - if (quoted) { - if ((c == 'n') || isSpecialCharacter(c)) { - buf.append('\\'); - } - buf.append(c); - quoted = false; - } else if ((c == ',') || (c == ' ')) { - if (starPresent) { - buf.append('.').append('*'); - starPresent = false; - } - lastWasSeparator = true; - } else { - if (lastWasSeparator && (buf.length() != 0)) { - buf.append('|'); - } - if (c == '?') { - buf.append('.'); - } else if (c == '*') { - starPresent = true; - } else { - if (starPresent) { - buf.append('.').append('*'); - starPresent = false; - } - if (c == '\\') { - quoted = true; - } else { - if (isSpecialCharacter(c)) { - buf.append('\\'); - } - buf.append(c); - } - } - lastWasSeparator = false; - } - } - if (quoted) { - buf.append('\\').append('\\'); - quoted = false; - } else if (starPresent) { - buf.append('.').append('*'); - starPresent = false; - } - return buf.toString(); - } - - private static boolean isSpecialCharacter(char c) { - return (c > 0x20) && (c < 0x80) && !isAlnum(c); - } - - private static boolean isAlnum(char c) { - return isAlpha(c) || isDigit(c); -} - - private static boolean isAlpha(char c) { - c |= 0x20; //to lower case - return (c >= 'a') && (c <= 'z'); - } - - private static boolean isDigit(char c) { - return (c >= '0') && (c <= '9'); - } - - /** - * Line-by-line is not sufficient and whole-file matching must be performed - * for a pattern. - */ - static boolean canBeMultilinePattern(String expr) { - return expr.matches(MULTILINE_REGEXP_PATTERN); - } -} diff --git a/api.search/src/org/netbeans/modules/search/TextRegexpUtil.java b/api.search/src/org/netbeans/modules/search/TextRegexpUtil.java --- a/api.search/src/org/netbeans/modules/search/TextRegexpUtil.java +++ b/api.search/src/org/netbeans/modules/search/TextRegexpUtil.java @@ -79,6 +79,20 @@ private TextRegexpUtil() { } + private static String makeLiteralRegexp(String literalPattern, + boolean wholeWords) { + + StringBuilder sb = new StringBuilder(); + if (wholeWords) { + sb.append(checkNotAfterWordChar); + } + sb.append(Pattern.quote(literalPattern)); + if (wholeWords) { + sb.append(checkNotBeforeWordChar); + } + return sb.toString(); + } + /** * Translates the given simple pattern to a regular expression. * @@ -194,8 +208,19 @@ LOG.log(Level.FINEST, " - textPatternExpr = \"{0}{1}", new Object[]{sp.getSearchExpression(), '"'}); //NOI18N } - String searchRegexp = makeRegexp(sp.getSearchExpression(), - sp.isWholeWords()); + String searchRegexp; + switch (sp.getMatchType()) { + case BASIC: + searchRegexp = makeRegexp( + sp.getSearchExpression(), sp.isWholeWords()); + break; + case LITERAL: + searchRegexp = makeLiteralRegexp( + sp.getSearchExpression(), sp.isWholeWords()); + break; + default: + throw new IllegalStateException(); + } if (LOG.isLoggable(Level.FINEST)) { LOG.log(Level.FINEST, " - regexp = \"{0}{1}", diff --git a/api.search/src/org/netbeans/modules/search/ui/CheckBoxWithButtonPanel.java b/api.search/src/org/netbeans/modules/search/ui/CheckBoxWithButtonPanel.java --- a/api.search/src/org/netbeans/modules/search/ui/CheckBoxWithButtonPanel.java +++ b/api.search/src/org/netbeans/modules/search/ui/CheckBoxWithButtonPanel.java @@ -41,19 +41,12 @@ */ package org.netbeans.modules.search.ui; -import java.awt.Cursor; import java.awt.FlowLayout; -import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; import javax.swing.JButton; import javax.swing.JCheckBox; -import javax.swing.JLabel; import javax.swing.JPanel; -import javax.swing.border.EmptyBorder; /** * Panel for a checkbox and a button that is enbled if and only if the checkbox @@ -63,11 +56,7 @@ implements ItemListener { private JCheckBox checkbox; - private JButton button; - private JLabel leftParenthesis; - private JLabel rightParenthesis; - private String enabledText; - private String disabledText; + private LinkButtonPanel buttonPanel; /** * Constructor. @@ -79,8 +68,7 @@ */ public CheckBoxWithButtonPanel(JCheckBox checkbox, JButton button) { this.checkbox = checkbox; - this.button = button; - initTexts(); + this.buttonPanel = new LinkButtonPanel(button); init(); } @@ -91,103 +79,25 @@ this.setLayout(new FlowLayout( FlowLayout.LEADING, 0, 0)); this.add(checkbox); - setLinkLikeButton(button); - leftParenthesis = new JLabel("("); //NOI18N - rightParenthesis = new JLabel(")"); //NOI18N - add(leftParenthesis); - add(button); - add(rightParenthesis); - MouseListener ml = createLabelMouseListener(); - leftParenthesis.addMouseListener(ml); - rightParenthesis.addMouseListener(ml); - button.setEnabled(false); + this.add(buttonPanel); this.setMaximumSize( this.getMinimumSize()); checkbox.addItemListener(this); if (checkbox.isSelected()) { - enableButton(); + buttonPanel.enableButton(); } else { - disableButton(); + buttonPanel.disableButton(); } } - /** - * Init values of enabled and disabled button texts. - */ - private void initTexts() { - enabledText = button.getText(); - if (enabledText.startsWith(UiUtils.HTML_LINK_PREFIX) - && enabledText.endsWith(UiUtils.HTML_LINK_SUFFIX)) { - disabledText = enabledText.substring( - UiUtils.HTML_LINK_PREFIX.length(), - enabledText.length() - UiUtils.HTML_LINK_SUFFIX.length()); - } else { - disabledText = enabledText; - } - } - - /** - * Create listener that delegates mouse clicks on parenthesis to the button. - */ - private MouseListener createLabelMouseListener() { - return new MouseAdapter() { - - @Override - public void mouseClicked(MouseEvent e) { - if (button.isEnabled()) { - for (ActionListener al : button.getActionListeners()) { - al.actionPerformed(null); - } - } - } - }; - } - - /** - * Set button border and background to look like a label with link. - */ - private void setLinkLikeButton(JButton button) { - button.setBorderPainted(false); - button.setContentAreaFilled(false); - button.setBorder(new EmptyBorder(0, 0, 0, 0)); - button.setCursor(Cursor.getPredefinedCursor( - Cursor.HAND_CURSOR)); - } - @Override public void itemStateChanged(ItemEvent e) { if (checkbox.isSelected()) { - enableButton(); + buttonPanel.enableButton(); } else { - disableButton(); + buttonPanel.disableButton(); } this.setMinimumSize(this.getPreferredSize()); // #214745 } - - /** - * Enable button and parentheses around it. - */ - private void enableButton() { - button.setText(enabledText); - button.setEnabled(true); - leftParenthesis.setCursor( - Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); - rightParenthesis.setCursor( - Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); - leftParenthesis.setEnabled(true); - rightParenthesis.setEnabled(true); - } - - /** - * Disable button and parentheses around it. - */ - private void disableButton() { - button.setText(disabledText); - button.setEnabled(false); - leftParenthesis.setCursor(Cursor.getDefaultCursor()); - rightParenthesis.setCursor(Cursor.getDefaultCursor()); - leftParenthesis.setEnabled(false); - rightParenthesis.setEnabled(false); - } } diff --git a/api.search/src/org/netbeans/modules/search/ui/LinkButtonPanel.java b/api.search/src/org/netbeans/modules/search/ui/LinkButtonPanel.java new file mode 100644 --- /dev/null +++ b/api.search/src/org/netbeans/modules/search/ui/LinkButtonPanel.java @@ -0,0 +1,167 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 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 2013 Sun Microsystems, Inc. + */ +package org.netbeans.modules.search.ui; + +import java.awt.Cursor; +import java.awt.FlowLayout; +import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; + +/** + * + * @author jhavlin + */ +public class LinkButtonPanel extends JPanel { + + private JButton button; + private JLabel leftParenthesis; + private JLabel rightParenthesis; + private String enabledText; + private String disabledText; + + public LinkButtonPanel(JButton button) { + this.button = button; + initTexts(); + init(); + } + + private void init() { + this.setLayout(new FlowLayout( + FlowLayout.LEADING, 0, 0)); + setLinkLikeButton(button); + leftParenthesis = new JLabel("("); //NOI18N + rightParenthesis = new JLabel(")"); //NOI18N + add(leftParenthesis); + add(button); + add(rightParenthesis); + MouseListener ml = createLabelMouseListener(); + leftParenthesis.addMouseListener(ml); + rightParenthesis.addMouseListener(ml); + button.setEnabled(false); + this.setMaximumSize( + this.getPreferredSize()); + } + + /** + * Init values of enabled and disabled button texts. + */ + private void initTexts() { + enabledText = button.getText(); + if (enabledText.startsWith(UiUtils.HTML_LINK_PREFIX) + && enabledText.endsWith(UiUtils.HTML_LINK_SUFFIX)) { + disabledText = enabledText.substring( + UiUtils.HTML_LINK_PREFIX.length(), + enabledText.length() - UiUtils.HTML_LINK_SUFFIX.length()); + } else { + disabledText = enabledText; + } + } + + /** + * Create listener that delegates mouse clicks on parenthesis to the button. + */ + private MouseListener createLabelMouseListener() { + return new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + if (button.isEnabled()) { + for (ActionListener al : button.getActionListeners()) { + al.actionPerformed(null); + } + } + } + }; + } + + /** + * Set button border and background to look like a label with link. + */ + private void setLinkLikeButton(JButton button) { + button.setBorderPainted(false); + button.setContentAreaFilled(false); + button.setBorder(new EmptyBorder(0, 0, 0, 0)); + button.setCursor(Cursor.getPredefinedCursor( + Cursor.HAND_CURSOR)); + } + + /** + * Enable button and parentheses around it. + */ + public void enableButton() { + button.setText(enabledText); + button.setEnabled(true); + leftParenthesis.setCursor( + Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); + rightParenthesis.setCursor( + Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); + leftParenthesis.setEnabled(true); + rightParenthesis.setEnabled(true); + this.setMinimumSize(this.getPreferredSize()); + } + + /** + * Disable button and parentheses around it. + */ + public void disableButton() { + button.setText(disabledText); + button.setEnabled(false); + leftParenthesis.setCursor(Cursor.getDefaultCursor()); + rightParenthesis.setCursor(Cursor.getDefaultCursor()); + leftParenthesis.setEnabled(false); + rightParenthesis.setEnabled(false); + this.setMinimumSize(this.getPreferredSize()); + } + + public void setButtonEnabled(boolean enabled) { + if (enabled) { + enableButton(); + } else { + disableButton(); + } + } +} diff --git a/api.search/test/unit/src/org/netbeans/api/search/RegexpUtilTest.java b/api.search/test/unit/src/org/netbeans/api/search/RegexpUtilTest.java new file mode 100644 --- /dev/null +++ b/api.search/test/unit/src/org/netbeans/api/search/RegexpUtilTest.java @@ -0,0 +1,191 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 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 2013 Sun Microsystems, Inc. + */ +package org.netbeans.api.search; + +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author jhavlin + */ +public class RegexpUtilTest { + + @Test + public void testMakeMultiRegexp() { + assertEquals("", makeMultiRegexp("")); + assertEquals("a", makeMultiRegexp("a")); + assertEquals("ab", makeMultiRegexp("ab")); + assertEquals("abc", makeMultiRegexp("abc")); + assertEquals("a.", makeMultiRegexp("a?")); + assertEquals("a.*", makeMultiRegexp("a*")); + assertEquals(".a", makeMultiRegexp("?a")); + assertEquals(".*a", makeMultiRegexp("*a")); + assertEquals("a.b", makeMultiRegexp("a?b")); + assertEquals(".a.", makeMultiRegexp("?a?")); + assertEquals("a.*b.c", makeMultiRegexp("a*b?c")); + assertEquals("a...*b", makeMultiRegexp("a?*?b")); + assertEquals("a..*b", makeMultiRegexp("a*?*b")); + + assertEquals("a|b", makeMultiRegexp("a b")); + assertEquals("a\\!b", makeMultiRegexp("a!b")); + assertEquals("a\\\"b", makeMultiRegexp("a\"b")); + assertEquals("a\\#b", makeMultiRegexp("a#b")); + assertEquals("a\\$b", makeMultiRegexp("a$b")); + assertEquals("a\\%b", makeMultiRegexp("a%b")); + assertEquals("a\\&b", makeMultiRegexp("a&b")); + assertEquals("a\\'b", makeMultiRegexp("a'b")); + assertEquals("a\\(b", makeMultiRegexp("a(b")); + assertEquals("a\\)b", makeMultiRegexp("a)b")); + assertEquals("a\\+b", makeMultiRegexp("a+b")); + assertEquals("a|b", makeMultiRegexp("a,b")); + assertEquals("a\\-b", makeMultiRegexp("a-b")); + assertEquals("a\\.b", makeMultiRegexp("a.b")); + assertEquals("a\\/b", makeMultiRegexp("a/b")); + + assertEquals("a0b", makeMultiRegexp("a0b")); + assertEquals("a1b", makeMultiRegexp("a1b")); + assertEquals("a2b", makeMultiRegexp("a2b")); + assertEquals("a3b", makeMultiRegexp("a3b")); + assertEquals("a4b", makeMultiRegexp("a4b")); + assertEquals("a5b", makeMultiRegexp("a5b")); + assertEquals("a6b", makeMultiRegexp("a6b")); + assertEquals("a7b", makeMultiRegexp("a7b")); + assertEquals("a8b", makeMultiRegexp("a8b")); + assertEquals("a9b", makeMultiRegexp("a9b")); + + assertEquals("a\\:b", makeMultiRegexp("a:b")); + assertEquals("a\\;b", makeMultiRegexp("a;b")); + assertEquals("a\\b", makeMultiRegexp("a>b")); + assertEquals("a\\@b", makeMultiRegexp("a@b")); + assertEquals("a\\[b", makeMultiRegexp("a[b")); + assertEquals("ab", makeMultiRegexp("a\\b")); + assertEquals("a\\]b", makeMultiRegexp("a]b")); + assertEquals("a\\^b", makeMultiRegexp("a^b")); + assertEquals("a\\_b", makeMultiRegexp("a_b")); + assertEquals("a\\`b", makeMultiRegexp("a`b")); + assertEquals("a\\{b", makeMultiRegexp("a{b")); + assertEquals("a\\|b", makeMultiRegexp("a|b")); + assertEquals("a\\}b", makeMultiRegexp("a}b")); + assertEquals("a\\~b", makeMultiRegexp("a~b")); + assertEquals("a\\\u007fb", makeMultiRegexp("a\u007fb")); + + assertEquals("a\u0080b", makeMultiRegexp("a\u0080b")); + assertEquals("a\u00c1b", makeMultiRegexp("a\u00c1b")); + + assertEquals("abc\\\\", makeMultiRegexp("abc\\")); + + assertEquals("", makeMultiRegexp("")); + assertEquals("", makeMultiRegexp(" ")); + assertEquals("", makeMultiRegexp(",")); + assertEquals("", makeMultiRegexp(", ")); + assertEquals("", makeMultiRegexp(" ,")); + assertEquals("a", makeMultiRegexp("a,")); + assertEquals("a", makeMultiRegexp("a ")); + assertEquals("a", makeMultiRegexp("a, ")); + assertEquals("a", makeMultiRegexp("a ,")); + assertEquals("a", makeMultiRegexp(",a")); + assertEquals("a", makeMultiRegexp(" a")); + assertEquals("a", makeMultiRegexp(", a")); + assertEquals("a", makeMultiRegexp(" ,a")); + assertEquals("a|b", makeMultiRegexp("a b")); + assertEquals("a|b", makeMultiRegexp("a,b")); + assertEquals("a|b", makeMultiRegexp("a, b")); + assertEquals("a|b", makeMultiRegexp("a ,b")); + assertEquals(" ", makeMultiRegexp("\\ ")); + assertEquals("\\,", makeMultiRegexp("\\,")); + assertEquals("\\,", makeMultiRegexp("\\, ")); + assertEquals(" ", makeMultiRegexp(",\\ ")); + assertEquals("\\, ", makeMultiRegexp("\\,\\ ")); + assertEquals(" ", makeMultiRegexp("\\ ,")); + assertEquals("\\,", makeMultiRegexp(" \\,")); + assertEquals(" \\,", makeMultiRegexp("\\ \\,")); + assertEquals("a", makeMultiRegexp("\\a,")); + assertEquals("a\\,", makeMultiRegexp("a\\,")); + assertEquals("a\\,", makeMultiRegexp("\\a\\,")); + assertEquals("a", makeMultiRegexp("\\a ")); + assertEquals("a ", makeMultiRegexp("a\\ ")); + assertEquals("a ", makeMultiRegexp("\\a\\ ")); + assertEquals("a|\\\\", makeMultiRegexp("a, \\")); + assertEquals("a| ", makeMultiRegexp("a,\\ ")); + assertEquals("a| \\\\", makeMultiRegexp("a,\\ \\")); + assertEquals("a\\,", makeMultiRegexp("a\\, ")); + assertEquals("a\\,|\\\\", makeMultiRegexp("a\\, \\")); + assertEquals("a\\, ", makeMultiRegexp("a\\,\\ ")); + assertEquals("a\\, \\\\", makeMultiRegexp("a\\,\\ \\")); + assertEquals("a", makeMultiRegexp("\\a, ")); + assertEquals("a|\\\\", makeMultiRegexp("\\a, \\")); + assertEquals("a| ", makeMultiRegexp("\\a,\\ ")); + assertEquals("a| \\\\", makeMultiRegexp("\\a,\\ \\")); + assertEquals("a\\,", makeMultiRegexp("\\a\\, ")); + assertEquals("a\\,|\\\\", makeMultiRegexp("\\a\\, \\")); + assertEquals("a\\, ", makeMultiRegexp("\\a\\,\\ ")); + assertEquals("a\\, \\\\", makeMultiRegexp("\\a\\,\\ \\")); + assertEquals("a|\\\\", makeMultiRegexp("a ,\\")); + assertEquals("a|\\,", makeMultiRegexp("a \\,")); + assertEquals("a|\\,\\\\", makeMultiRegexp("a \\,\\")); + assertEquals("a ", makeMultiRegexp("a\\ ,")); + assertEquals("a |\\\\", makeMultiRegexp("a\\ ,\\")); + assertEquals("a \\,", makeMultiRegexp("a\\ \\,")); + assertEquals("a \\,\\\\", makeMultiRegexp("a\\ \\,\\")); + assertEquals("a", makeMultiRegexp("\\a ,")); + assertEquals("a|\\\\", makeMultiRegexp("\\a ,\\")); + assertEquals("a|\\,", makeMultiRegexp("\\a \\,")); + assertEquals("a|\\,\\\\", makeMultiRegexp("\\a \\,\\")); + assertEquals("a ", makeMultiRegexp("\\a\\ ,")); + assertEquals("a |\\\\", makeMultiRegexp("\\a\\ ,\\")); + assertEquals("a \\,", makeMultiRegexp("\\a\\ \\,")); + assertEquals("a \\,\\\\", makeMultiRegexp("\\a\\ \\,\\")); + + assertEquals("a|b", makeMultiRegexp("a, b")); + assertEquals("a|.*b.", makeMultiRegexp("a,*b?")); + assertEquals("a|\\*b.", makeMultiRegexp("a,\\*b?")); + assertEquals("a|.*b\\?", makeMultiRegexp("a,*b\\?")); + } + + private String makeMultiRegexp(String string) { + return RegexpUtil.makeFileNamePattern( + SearchScopeOptions.create(string, false)).pattern(); + } +} \ No newline at end of file diff --git a/api.search/test/unit/src/org/netbeans/api/search/SearchHistoryTest.java b/api.search/test/unit/src/org/netbeans/api/search/SearchHistoryTest.java --- a/api.search/test/unit/src/org/netbeans/api/search/SearchHistoryTest.java +++ b/api.search/test/unit/src/org/netbeans/api/search/SearchHistoryTest.java @@ -76,7 +76,7 @@ /** */ public void testSearchPatternSize() throws Exception { - assertSize("SearchPattern size", 80, SearchPattern.create("searchText",true,true,false)); + assertSize("SearchPattern size", 256, SearchPattern.create("searchText",true,true,false)); } public void testSearchHistoryListSize() throws Exception{ diff --git a/api.search/test/unit/src/org/netbeans/api/search/SearchPatternTest.java b/api.search/test/unit/src/org/netbeans/api/search/SearchPatternTest.java --- a/api.search/test/unit/src/org/netbeans/api/search/SearchPatternTest.java +++ b/api.search/test/unit/src/org/netbeans/api/search/SearchPatternTest.java @@ -43,6 +43,7 @@ import org.junit.Test; import static org.junit.Assert.*; +import org.netbeans.api.search.SearchPattern.MatchType; public class SearchPatternTest { @@ -54,6 +55,21 @@ assertFalse(sp.isRegExp()); assertTrue(sp.isWholeWords()); assertEquals("Test", sp.getSearchExpression()); + assertEquals(MatchType.BASIC, sp.getMatchType()); + } + + @Test + public void testManuallyParseRegexp() throws Exception { + String canString = "MRW-Test"; + SearchPattern sp = SearchPattern.parsePattern(canString); + assertEquals(MatchType.REGEXP, sp.getMatchType()); + } + + @Test + public void testManuallyParseLiteral() throws Exception { + String canString = "MLW-Test"; + SearchPattern sp = SearchPattern.parsePattern(canString); + assertEquals(MatchType.LITERAL, sp.getMatchType()); } @Test @@ -62,5 +78,46 @@ String canString = sp.toCanonicalString(); assertEquals("mRW-ta", canString); + + sp = sp.changeMatchType(MatchType.BASIC); + assertEquals("mrW-ta", sp.toCanonicalString()); + + sp = sp.changeMatchType(MatchType.LITERAL); + assertEquals("mLW-ta", sp.toCanonicalString()); + } + + @Test + public void testMatchType() { + + assertFalse("Literal match type shoudn't be a regexp pattern", + SearchPattern.create("test", false, false, + MatchType.LITERAL).isRegExp()); + + assertFalse("Basic match type shoudn't be a regexp pattern", + SearchPattern.create("test", false, false, + MatchType.BASIC).isRegExp()); + + assertTrue("Regexp match type should be a regexp pattern", + SearchPattern.create("test", false, false, + MatchType.REGEXP).isRegExp()); + + assertTrue("Chaning match type to REGEXP should create regexp pattern", + SearchPattern.create("test", false, false, + MatchType.LITERAL).changeMatchType(MatchType.REGEXP) + .isRegExp()); + + assertEquals("Changing match type to LITERAL should work", + MatchType.LITERAL, SearchPattern.create("test", + false, false, true).changeMatchType(MatchType.LITERAL) + .getMatchType()); + + assertEquals("Chaning match type to BASIC should work", + MatchType.BASIC, SearchPattern.create("test", + false, false, true).changeMatchType(MatchType.BASIC) + .getMatchType()); + + assertEquals("If not specified exactly, match type should be BASIC", + MatchType.BASIC, SearchPattern.create("test", + false, false, false).getMatchType()); } } diff --git a/api.search/test/unit/src/org/netbeans/api/search/ui/SearchPatternControllerTest.java b/api.search/test/unit/src/org/netbeans/api/search/ui/SearchPatternControllerTest.java new file mode 100644 --- /dev/null +++ b/api.search/test/unit/src/org/netbeans/api/search/ui/SearchPatternControllerTest.java @@ -0,0 +1,102 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013 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 2013 Sun Microsystems, Inc. + */ +package org.netbeans.api.search.ui; + +import javax.swing.JComboBox; +import org.junit.Test; +import static org.junit.Assert.*; +import org.netbeans.api.search.SearchPattern; +import org.netbeans.api.search.SearchPattern.MatchType; +import org.netbeans.api.search.ui.SearchPatternController.MultiOption; + +/** + * + * @author jhavlin + */ +public class SearchPatternControllerTest { + + public SearchPatternControllerTest() { + } + + @Test + public void testBindComboBox() { + JComboBox cb = new JComboBox(); + SearchPatternController controller = + ComponentUtils.adjustComboForSearchPattern(cb); + JComboBox matchTypeCb = new JComboBox(new Object[]{ + MatchType.BASIC, MatchType.LITERAL, MatchType.REGEXP}); + assertEquals(MatchType.BASIC, matchTypeCb.getSelectedItem()); + controller.bind(MultiOption.MATCH_TYPE, matchTypeCb); + boolean thrown = false; + try { + controller.bind(MultiOption.MATCH_TYPE, matchTypeCb); + } catch (Exception e) { + thrown = true; + } + assertTrue("Exception should be thrown when trying to bind " + + "a property twice", thrown); + + controller.setSearchPattern( + SearchPattern.create("test", false, false, MatchType.LITERAL)); + assertEquals(MatchType.LITERAL, matchTypeCb.getSelectedItem()); + + controller.setSearchPattern( + SearchPattern.create("test", false, false, MatchType.REGEXP)); + assertEquals(MatchType.REGEXP, matchTypeCb.getSelectedItem()); + + controller.setSearchPattern( + SearchPattern.create("test", false, false, MatchType.BASIC)); + assertEquals(MatchType.BASIC, matchTypeCb.getSelectedItem()); + + matchTypeCb.setSelectedItem(MatchType.LITERAL); + assertEquals(MatchType.LITERAL, + controller.getSearchPattern().getMatchType()); + + matchTypeCb.setSelectedItem(MatchType.REGEXP); + assertEquals(MatchType.REGEXP, + controller.getSearchPattern().getMatchType()); + + matchTypeCb.setSelectedItem(MatchType.BASIC); + assertEquals(MatchType.BASIC, + controller.getSearchPattern().getMatchType()); + } +} diff --git a/api.search/test/unit/src/org/netbeans/modules/search/BasicSearchCriteriaTest.java b/api.search/test/unit/src/org/netbeans/modules/search/BasicSearchCriteriaTest.java --- a/api.search/test/unit/src/org/netbeans/modules/search/BasicSearchCriteriaTest.java +++ b/api.search/test/unit/src/org/netbeans/modules/search/BasicSearchCriteriaTest.java @@ -43,6 +43,7 @@ import org.junit.Test; import static org.junit.Assert.*; +import org.netbeans.api.search.SearchPattern; /** * @@ -56,7 +57,7 @@ @Test public void testDetectInvalidReplacePattern() { BasicSearchCriteria bsc = new BasicSearchCriteria(); - bsc.setRegexp(true); + bsc.setMatchType(SearchPattern.MatchType.REGEXP); bsc.setTextPattern("a"); bsc.setReplaceExpr("$1"); assertFalse(bsc.isUsable()); diff --git a/api.search/test/unit/src/org/netbeans/modules/search/MatchingObjectTest.java b/api.search/test/unit/src/org/netbeans/modules/search/MatchingObjectTest.java --- a/api.search/test/unit/src/org/netbeans/modules/search/MatchingObjectTest.java +++ b/api.search/test/unit/src/org/netbeans/modules/search/MatchingObjectTest.java @@ -209,7 +209,7 @@ BasicSearchCriteria bsc = new BasicSearchCriteria(); bsc.setTextPattern(find); - bsc.setRegexp(false); + bsc.setMatchType(SearchPattern.MatchType.BASIC); bsc.setReplaceExpr(replace); bsc.setPreserveCase(true); bsc.onOk(); diff --git a/api.search/test/unit/src/org/netbeans/modules/search/RegexpMakerTest.java b/api.search/test/unit/src/org/netbeans/modules/search/TextRegexpUtilTest.java rename from api.search/test/unit/src/org/netbeans/modules/search/RegexpMakerTest.java rename to api.search/test/unit/src/org/netbeans/modules/search/TextRegexpUtilTest.java --- a/api.search/test/unit/src/org/netbeans/modules/search/RegexpMakerTest.java +++ b/api.search/test/unit/src/org/netbeans/modules/search/TextRegexpUtilTest.java @@ -34,20 +34,22 @@ import java.lang.reflect.Field; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.netbeans.api.search.SearchPattern; +import org.netbeans.api.search.SearchPattern.MatchType; import org.netbeans.junit.NbTestCase; /** * * @author mp */ -public class RegexpMakerTest extends NbTestCase { +public class TextRegexpUtilTest extends NbTestCase { - public RegexpMakerTest() { + public TextRegexpUtilTest() { super("SimpleRegexpParserTest"); } private static String getClassField(String name) throws Exception { - Field field = RegexpMaker.class.getDeclaredField(name); + Field field = TextRegexpUtil.class.getDeclaredField(name); field.setAccessible(true); return (String) field.get(null); } @@ -55,45 +57,45 @@ public void testMakeRegexp() throws Exception { /* basics: */ - assertEquals("", RegexpMaker.makeRegexp("")); - assertEquals("a", RegexpMaker.makeRegexp("a")); - assertEquals("ab", RegexpMaker.makeRegexp("ab")); - assertEquals("abc", RegexpMaker.makeRegexp("abc")); + assertEquals("", makeRegexp("")); + assertEquals("a", makeRegexp("a")); + assertEquals("ab", makeRegexp("ab")); + assertEquals("abc", makeRegexp("abc")); /* special chars in the middle: */ - assertEquals("a.*?b.c", RegexpMaker.makeRegexp("a*b?c")); - assertEquals("a..+?b", RegexpMaker.makeRegexp("a?*?b")); - assertEquals("a.+?b", RegexpMaker.makeRegexp("a*?*b")); + assertEquals("a.*?b.c", makeRegexp("a*b?c")); + assertEquals("a..+?b", makeRegexp("a?*?b")); + assertEquals("a.+?b", makeRegexp("a*?*b")); /* ignore stars in the begining: */ - assertEquals("a", RegexpMaker.makeRegexp("*a")); - assertEquals(".a", RegexpMaker.makeRegexp("?a")); - assertEquals("a", RegexpMaker.makeRegexp("**a")); - assertEquals(".a", RegexpMaker.makeRegexp("*?a")); - assertEquals(".a", RegexpMaker.makeRegexp("?*a")); - assertEquals("..a", RegexpMaker.makeRegexp("??a")); + assertEquals("a", makeRegexp("*a")); + assertEquals(".a", makeRegexp("?a")); + assertEquals("a", makeRegexp("**a")); + assertEquals(".a", makeRegexp("*?a")); + assertEquals(".a", makeRegexp("?*a")); + assertEquals("..a", makeRegexp("??a")); /* ignore stars at the end: */ - assertEquals("a", RegexpMaker.makeRegexp("a*")); - assertEquals("a.", RegexpMaker.makeRegexp("a?")); - assertEquals("a", RegexpMaker.makeRegexp("a**")); - assertEquals("a.", RegexpMaker.makeRegexp("a*?")); - assertEquals("a.", RegexpMaker.makeRegexp("a?*")); - assertEquals("a..", RegexpMaker.makeRegexp("a??")); + assertEquals("a", makeRegexp("a*")); + assertEquals("a.", makeRegexp("a?")); + assertEquals("a", makeRegexp("a**")); + assertEquals("a.", makeRegexp("a*?")); + assertEquals("a.", makeRegexp("a?*")); + assertEquals("a..", makeRegexp("a??")); /* other usage of '*' and '?': */ - assertEquals(" .*?a", RegexpMaker.makeRegexp(" *a")); - assertEquals(" .a", RegexpMaker.makeRegexp(" ?a")); - assertEquals(" a", RegexpMaker.makeRegexp("* a")); - assertEquals(". a", RegexpMaker.makeRegexp("? a")); - assertEquals("\\,a", RegexpMaker.makeRegexp("*,a")); - assertEquals(".\\,a", RegexpMaker.makeRegexp("?,a")); - assertEquals("a.*? ", RegexpMaker.makeRegexp("a* ")); - assertEquals("a. ", RegexpMaker.makeRegexp("a? ")); - assertEquals("a ", RegexpMaker.makeRegexp("a *")); - assertEquals("a .", RegexpMaker.makeRegexp("a ?")); - assertEquals("a\\,", RegexpMaker.makeRegexp("a,*")); - assertEquals("a\\,.", RegexpMaker.makeRegexp("a,?")); + assertEquals(" .*?a", makeRegexp(" *a")); + assertEquals(" .a", makeRegexp(" ?a")); + assertEquals(" a", makeRegexp("* a")); + assertEquals(". a", makeRegexp("? a")); + assertEquals("\\,a", makeRegexp("*,a")); + assertEquals(".\\,a", makeRegexp("?,a")); + assertEquals("a.*? ", makeRegexp("a* ")); + assertEquals("a. ", makeRegexp("a? ")); + assertEquals("a ", makeRegexp("a *")); + assertEquals("a .", makeRegexp("a ?")); + assertEquals("a\\,", makeRegexp("a,*")); + assertEquals("a\\,.", makeRegexp("a,?")); /* whole words: */ @@ -101,151 +103,151 @@ final String checkNotAfterWordChar = getClassField("checkNotAfterWordChar"); final String checkNotBeforeWordChar = getClassField("checkNotBeforeWordChar"); - assertEquals("", RegexpMaker.makeRegexp("", true)); + assertEquals("", makeRegexp("", true)); assertEquals(checkNotAfterWordChar + "a" + checkNotBeforeWordChar, - RegexpMaker.makeRegexp("a", true)); + makeRegexp("a", true)); assertEquals(checkNotAfterWordChar + "a" + wordCharsExpr + "*?b" + wordCharsExpr + "c" + checkNotBeforeWordChar, - RegexpMaker.makeRegexp("a*b?c", true)); + makeRegexp("a*b?c", true)); assertEquals(checkNotAfterWordChar + "a" + wordCharsExpr + "{2,}?b" + checkNotBeforeWordChar, - RegexpMaker.makeRegexp("a?*?b", true)); + makeRegexp("a?*?b", true)); assertEquals(checkNotAfterWordChar + "a" + wordCharsExpr + "+?b" + checkNotBeforeWordChar, - RegexpMaker.makeRegexp("a*?*b", true)); + makeRegexp("a*?*b", true)); assertEquals(wordCharsExpr + "*a" + checkNotBeforeWordChar, - RegexpMaker.makeRegexp("*a", true)); + makeRegexp("*a", true)); assertEquals(checkNotAfterWordChar + wordCharsExpr + "a" + checkNotBeforeWordChar, - RegexpMaker.makeRegexp("?a", true)); + makeRegexp("?a", true)); assertEquals(wordCharsExpr + "*a" + checkNotBeforeWordChar, - RegexpMaker.makeRegexp("**a", true)); + makeRegexp("**a", true)); assertEquals(wordCharsExpr + "+a" + checkNotBeforeWordChar, - RegexpMaker.makeRegexp("*?a", true)); + makeRegexp("*?a", true)); assertEquals(wordCharsExpr + "+a" + checkNotBeforeWordChar, - RegexpMaker.makeRegexp("?*a", true)); + makeRegexp("?*a", true)); assertEquals(checkNotAfterWordChar + wordCharsExpr + "{2}a" + checkNotBeforeWordChar, - RegexpMaker.makeRegexp("??a", true)); + makeRegexp("??a", true)); assertEquals(checkNotAfterWordChar + "a" + wordCharsExpr + "*", - RegexpMaker.makeRegexp("a*", true)); + makeRegexp("a*", true)); assertEquals(checkNotAfterWordChar + "a" + wordCharsExpr + checkNotBeforeWordChar, - RegexpMaker.makeRegexp("a?", true)); + makeRegexp("a?", true)); assertEquals(checkNotAfterWordChar + "a" + wordCharsExpr + "*", - RegexpMaker.makeRegexp("a**", true)); + makeRegexp("a**", true)); assertEquals(checkNotAfterWordChar + "a" + wordCharsExpr + "+", - RegexpMaker.makeRegexp("a*?", true)); + makeRegexp("a*?", true)); assertEquals(checkNotAfterWordChar + "a" + wordCharsExpr + "+", - RegexpMaker.makeRegexp("a?*", true)); + makeRegexp("a?*", true)); assertEquals(checkNotAfterWordChar + "a" + wordCharsExpr + "{2}" + checkNotBeforeWordChar, - RegexpMaker.makeRegexp("a??", true)); + makeRegexp("a??", true)); assertEquals(" " + wordCharsExpr + "*?a" + checkNotBeforeWordChar, - RegexpMaker.makeRegexp(" *a", true)); + makeRegexp(" *a", true)); assertEquals(" " + wordCharsExpr + "a" + checkNotBeforeWordChar, - RegexpMaker.makeRegexp(" ?a", true)); + makeRegexp(" ?a", true)); assertEquals(wordCharsExpr + "* a" + checkNotBeforeWordChar, - RegexpMaker.makeRegexp("* a", true)); + makeRegexp("* a", true)); assertEquals(checkNotAfterWordChar + wordCharsExpr + " a" + checkNotBeforeWordChar, - RegexpMaker.makeRegexp("? a", true)); + makeRegexp("? a", true)); assertEquals(wordCharsExpr + "*\\,a" + checkNotBeforeWordChar, - RegexpMaker.makeRegexp("*,a", true)); + makeRegexp("*,a", true)); assertEquals(checkNotAfterWordChar + wordCharsExpr + "\\,a" + checkNotBeforeWordChar, - RegexpMaker.makeRegexp("?,a", true)); + makeRegexp("?,a", true)); assertEquals(checkNotAfterWordChar + "a" + wordCharsExpr + "*? ", - RegexpMaker.makeRegexp("a* ", true)); + makeRegexp("a* ", true)); assertEquals(checkNotAfterWordChar + "a" + wordCharsExpr + " ", - RegexpMaker.makeRegexp("a? ", true)); + makeRegexp("a? ", true)); assertEquals(checkNotAfterWordChar + "a " + wordCharsExpr + "*", - RegexpMaker.makeRegexp("a *", true)); + makeRegexp("a *", true)); assertEquals(checkNotAfterWordChar + "a " + wordCharsExpr + checkNotBeforeWordChar, - RegexpMaker.makeRegexp("a ?", true)); + makeRegexp("a ?", true)); assertEquals(checkNotAfterWordChar + "a\\," + wordCharsExpr + "*", - RegexpMaker.makeRegexp("a,*", true)); + makeRegexp("a,*", true)); assertEquals(checkNotAfterWordChar + "a\\," + wordCharsExpr + checkNotBeforeWordChar, - RegexpMaker.makeRegexp("a,?", true)); + makeRegexp("a,?", true)); - assertEquals("a b", RegexpMaker.makeRegexp("a b")); - assertEquals("a\\!b", RegexpMaker.makeRegexp("a!b")); - assertEquals("a\\\"b", RegexpMaker.makeRegexp("a\"b")); - assertEquals("a\\#b", RegexpMaker.makeRegexp("a#b")); - assertEquals("a\\$b", RegexpMaker.makeRegexp("a$b")); - assertEquals("a\\%b", RegexpMaker.makeRegexp("a%b")); - assertEquals("a\\&b", RegexpMaker.makeRegexp("a&b")); - assertEquals("a\\'b", RegexpMaker.makeRegexp("a'b")); - assertEquals("a\\(b", RegexpMaker.makeRegexp("a(b")); - assertEquals("a\\)b", RegexpMaker.makeRegexp("a)b")); - assertEquals("a\\+b", RegexpMaker.makeRegexp("a+b")); - assertEquals("a\\,b", RegexpMaker.makeRegexp("a,b")); - assertEquals("a\\-b", RegexpMaker.makeRegexp("a-b")); - assertEquals("a\\.b", RegexpMaker.makeRegexp("a.b")); - assertEquals("a\\/b", RegexpMaker.makeRegexp("a/b")); + assertEquals("a b", makeRegexp("a b")); + assertEquals("a\\!b", makeRegexp("a!b")); + assertEquals("a\\\"b", makeRegexp("a\"b")); + assertEquals("a\\#b", makeRegexp("a#b")); + assertEquals("a\\$b", makeRegexp("a$b")); + assertEquals("a\\%b", makeRegexp("a%b")); + assertEquals("a\\&b", makeRegexp("a&b")); + assertEquals("a\\'b", makeRegexp("a'b")); + assertEquals("a\\(b", makeRegexp("a(b")); + assertEquals("a\\)b", makeRegexp("a)b")); + assertEquals("a\\+b", makeRegexp("a+b")); + assertEquals("a\\,b", makeRegexp("a,b")); + assertEquals("a\\-b", makeRegexp("a-b")); + assertEquals("a\\.b", makeRegexp("a.b")); + assertEquals("a\\/b", makeRegexp("a/b")); - assertEquals("a0b", RegexpMaker.makeRegexp("a0b")); - assertEquals("a1b", RegexpMaker.makeRegexp("a1b")); - assertEquals("a2b", RegexpMaker.makeRegexp("a2b")); - assertEquals("a3b", RegexpMaker.makeRegexp("a3b")); - assertEquals("a4b", RegexpMaker.makeRegexp("a4b")); - assertEquals("a5b", RegexpMaker.makeRegexp("a5b")); - assertEquals("a6b", RegexpMaker.makeRegexp("a6b")); - assertEquals("a7b", RegexpMaker.makeRegexp("a7b")); - assertEquals("a8b", RegexpMaker.makeRegexp("a8b")); - assertEquals("a9b", RegexpMaker.makeRegexp("a9b")); + assertEquals("a0b", makeRegexp("a0b")); + assertEquals("a1b", makeRegexp("a1b")); + assertEquals("a2b", makeRegexp("a2b")); + assertEquals("a3b", makeRegexp("a3b")); + assertEquals("a4b", makeRegexp("a4b")); + assertEquals("a5b", makeRegexp("a5b")); + assertEquals("a6b", makeRegexp("a6b")); + assertEquals("a7b", makeRegexp("a7b")); + assertEquals("a8b", makeRegexp("a8b")); + assertEquals("a9b", makeRegexp("a9b")); - assertEquals("a\\:b", RegexpMaker.makeRegexp("a:b")); - assertEquals("a\\;b", RegexpMaker.makeRegexp("a;b")); - assertEquals("a\\b", RegexpMaker.makeRegexp("a>b")); - assertEquals("a\\@b", RegexpMaker.makeRegexp("a@b")); - assertEquals("a\\[b", RegexpMaker.makeRegexp("a[b")); - assertEquals("a\\\\a", RegexpMaker.makeRegexp("a\\a")); - assertEquals("a\\\\b", RegexpMaker.makeRegexp("a\\b")); - assertEquals("a\\\\c", RegexpMaker.makeRegexp("a\\c")); - assertEquals("a\\\\d", RegexpMaker.makeRegexp("a\\d")); - assertEquals("a\\\\e", RegexpMaker.makeRegexp("a\\e")); - assertEquals("a\\\\f", RegexpMaker.makeRegexp("a\\f")); - assertEquals("a\\\\g", RegexpMaker.makeRegexp("a\\g")); - assertEquals("a\\\\h", RegexpMaker.makeRegexp("a\\h")); - assertEquals("a\\\\i", RegexpMaker.makeRegexp("a\\i")); - assertEquals("a\\\\j", RegexpMaker.makeRegexp("a\\j")); - assertEquals("a\\\\k", RegexpMaker.makeRegexp("a\\k")); - assertEquals("a\\\\l", RegexpMaker.makeRegexp("a\\l")); - assertEquals("a\\\\m", RegexpMaker.makeRegexp("a\\m")); - assertEquals("a\\\\n", RegexpMaker.makeRegexp("a\\n")); - assertEquals("a\\\\o", RegexpMaker.makeRegexp("a\\o")); - assertEquals("a\\\\p", RegexpMaker.makeRegexp("a\\p")); - assertEquals("a\\\\q", RegexpMaker.makeRegexp("a\\q")); - assertEquals("a\\\\r", RegexpMaker.makeRegexp("a\\r")); - assertEquals("a\\\\s", RegexpMaker.makeRegexp("a\\s")); - assertEquals("a\\\\t", RegexpMaker.makeRegexp("a\\t")); - assertEquals("a\\\\u", RegexpMaker.makeRegexp("a\\u")); - assertEquals("a\\\\v", RegexpMaker.makeRegexp("a\\v")); - assertEquals("a\\\\w", RegexpMaker.makeRegexp("a\\w")); - assertEquals("a\\\\x", RegexpMaker.makeRegexp("a\\x")); - assertEquals("a\\\\y", RegexpMaker.makeRegexp("a\\y")); - assertEquals("a\\\\z", RegexpMaker.makeRegexp("a\\z")); - assertEquals("a\\]b", RegexpMaker.makeRegexp("a]b")); - assertEquals("a\\^b", RegexpMaker.makeRegexp("a^b")); - assertEquals("a\\_b", RegexpMaker.makeRegexp("a_b")); - assertEquals("a\\`b", RegexpMaker.makeRegexp("a`b")); - assertEquals("a\\{b", RegexpMaker.makeRegexp("a{b")); - assertEquals("a\\|b", RegexpMaker.makeRegexp("a|b")); - assertEquals("a\\}b", RegexpMaker.makeRegexp("a}b")); - assertEquals("a\\~b", RegexpMaker.makeRegexp("a~b")); - assertEquals("a\\\u007fb", RegexpMaker.makeRegexp("a\u007fb")); + assertEquals("a\\:b", makeRegexp("a:b")); + assertEquals("a\\;b", makeRegexp("a;b")); + assertEquals("a\\b", makeRegexp("a>b")); + assertEquals("a\\@b", makeRegexp("a@b")); + assertEquals("a\\[b", makeRegexp("a[b")); + assertEquals("a\\\\a", makeRegexp("a\\a")); + assertEquals("a\\\\b", makeRegexp("a\\b")); + assertEquals("a\\\\c", makeRegexp("a\\c")); + assertEquals("a\\\\d", makeRegexp("a\\d")); + assertEquals("a\\\\e", makeRegexp("a\\e")); + assertEquals("a\\\\f", makeRegexp("a\\f")); + assertEquals("a\\\\g", makeRegexp("a\\g")); + assertEquals("a\\\\h", makeRegexp("a\\h")); + assertEquals("a\\\\i", makeRegexp("a\\i")); + assertEquals("a\\\\j", makeRegexp("a\\j")); + assertEquals("a\\\\k", makeRegexp("a\\k")); + assertEquals("a\\\\l", makeRegexp("a\\l")); + assertEquals("a\\\\m", makeRegexp("a\\m")); + assertEquals("a\\\\n", makeRegexp("a\\n")); + assertEquals("a\\\\o", makeRegexp("a\\o")); + assertEquals("a\\\\p", makeRegexp("a\\p")); + assertEquals("a\\\\q", makeRegexp("a\\q")); + assertEquals("a\\\\r", makeRegexp("a\\r")); + assertEquals("a\\\\s", makeRegexp("a\\s")); + assertEquals("a\\\\t", makeRegexp("a\\t")); + assertEquals("a\\\\u", makeRegexp("a\\u")); + assertEquals("a\\\\v", makeRegexp("a\\v")); + assertEquals("a\\\\w", makeRegexp("a\\w")); + assertEquals("a\\\\x", makeRegexp("a\\x")); + assertEquals("a\\\\y", makeRegexp("a\\y")); + assertEquals("a\\\\z", makeRegexp("a\\z")); + assertEquals("a\\]b", makeRegexp("a]b")); + assertEquals("a\\^b", makeRegexp("a^b")); + assertEquals("a\\_b", makeRegexp("a_b")); + assertEquals("a\\`b", makeRegexp("a`b")); + assertEquals("a\\{b", makeRegexp("a{b")); + assertEquals("a\\|b", makeRegexp("a|b")); + assertEquals("a\\}b", makeRegexp("a}b")); + assertEquals("a\\~b", makeRegexp("a~b")); + assertEquals("a\\\u007fb", makeRegexp("a\u007fb")); - assertEquals("a\u0080b", RegexpMaker.makeRegexp("a\u0080b")); - assertEquals("a\u00c1b", RegexpMaker.makeRegexp("a\u00c1b")); + assertEquals("a\u0080b", makeRegexp("a\u0080b")); + assertEquals("a\u00c1b", makeRegexp("a\u00c1b")); - assertEquals("abc\\\\", RegexpMaker.makeRegexp("abc\\")); - assertEquals("\\\\\\\"", RegexpMaker.makeRegexp("\\\"")); - assertEquals("\\\\", RegexpMaker.makeRegexp("\\")); + assertEquals("abc\\\\", makeRegexp("abc\\")); + assertEquals("\\\\\\\"", makeRegexp("\\\"")); + assertEquals("\\\\", makeRegexp("\\")); assertEquals("\\

\\<\\/h3\\>", - RegexpMaker.makeRegexp("

")); + makeRegexp("

")); } @@ -300,7 +302,7 @@ String simpleExpr, String expectedMatch, boolean wholeWords) { - String regexp = RegexpMaker.makeRegexp(simpleExpr, wholeWords); + String regexp = makeRegexp(simpleExpr, wholeWords); Matcher matcher = Pattern.compile(regexp).matcher(testString); if (expectedMatch == null) { @@ -311,149 +313,46 @@ } } - public void testMakeMultiRegexp() { - assertEquals("", RegexpMaker.makeMultiRegexp("")); - assertEquals("a", RegexpMaker.makeMultiRegexp("a")); - assertEquals("ab", RegexpMaker.makeMultiRegexp("ab")); - assertEquals("abc", RegexpMaker.makeMultiRegexp("abc")); - assertEquals("a.", RegexpMaker.makeMultiRegexp("a?")); - assertEquals("a.*", RegexpMaker.makeMultiRegexp("a*")); - assertEquals(".a", RegexpMaker.makeMultiRegexp("?a")); - assertEquals(".*a", RegexpMaker.makeMultiRegexp("*a")); - assertEquals("a.b", RegexpMaker.makeMultiRegexp("a?b")); - assertEquals(".a.", RegexpMaker.makeMultiRegexp("?a?")); - assertEquals("a.*b.c", RegexpMaker.makeMultiRegexp("a*b?c")); - assertEquals("a...*b", RegexpMaker.makeMultiRegexp("a?*?b")); - assertEquals("a..*b", RegexpMaker.makeMultiRegexp("a*?*b")); - - assertEquals("a|b", RegexpMaker.makeMultiRegexp("a b")); - assertEquals("a\\!b", RegexpMaker.makeMultiRegexp("a!b")); - assertEquals("a\\\"b", RegexpMaker.makeMultiRegexp("a\"b")); - assertEquals("a\\#b", RegexpMaker.makeMultiRegexp("a#b")); - assertEquals("a\\$b", RegexpMaker.makeMultiRegexp("a$b")); - assertEquals("a\\%b", RegexpMaker.makeMultiRegexp("a%b")); - assertEquals("a\\&b", RegexpMaker.makeMultiRegexp("a&b")); - assertEquals("a\\'b", RegexpMaker.makeMultiRegexp("a'b")); - assertEquals("a\\(b", RegexpMaker.makeMultiRegexp("a(b")); - assertEquals("a\\)b", RegexpMaker.makeMultiRegexp("a)b")); - assertEquals("a\\+b", RegexpMaker.makeMultiRegexp("a+b")); - assertEquals("a|b", RegexpMaker.makeMultiRegexp("a,b")); - assertEquals("a\\-b", RegexpMaker.makeMultiRegexp("a-b")); - assertEquals("a\\.b", RegexpMaker.makeMultiRegexp("a.b")); - assertEquals("a\\/b", RegexpMaker.makeMultiRegexp("a/b")); - - assertEquals("a0b", RegexpMaker.makeMultiRegexp("a0b")); - assertEquals("a1b", RegexpMaker.makeMultiRegexp("a1b")); - assertEquals("a2b", RegexpMaker.makeMultiRegexp("a2b")); - assertEquals("a3b", RegexpMaker.makeMultiRegexp("a3b")); - assertEquals("a4b", RegexpMaker.makeMultiRegexp("a4b")); - assertEquals("a5b", RegexpMaker.makeMultiRegexp("a5b")); - assertEquals("a6b", RegexpMaker.makeMultiRegexp("a6b")); - assertEquals("a7b", RegexpMaker.makeMultiRegexp("a7b")); - assertEquals("a8b", RegexpMaker.makeMultiRegexp("a8b")); - assertEquals("a9b", RegexpMaker.makeMultiRegexp("a9b")); - - assertEquals("a\\:b", RegexpMaker.makeMultiRegexp("a:b")); - assertEquals("a\\;b", RegexpMaker.makeMultiRegexp("a;b")); - assertEquals("a\\b", RegexpMaker.makeMultiRegexp("a>b")); - assertEquals("a\\@b", RegexpMaker.makeMultiRegexp("a@b")); - assertEquals("a\\[b", RegexpMaker.makeMultiRegexp("a[b")); - assertEquals("ab", RegexpMaker.makeMultiRegexp("a\\b")); - assertEquals("a\\]b", RegexpMaker.makeMultiRegexp("a]b")); - assertEquals("a\\^b", RegexpMaker.makeMultiRegexp("a^b")); - assertEquals("a\\_b", RegexpMaker.makeMultiRegexp("a_b")); - assertEquals("a\\`b", RegexpMaker.makeMultiRegexp("a`b")); - assertEquals("a\\{b", RegexpMaker.makeMultiRegexp("a{b")); - assertEquals("a\\|b", RegexpMaker.makeMultiRegexp("a|b")); - assertEquals("a\\}b", RegexpMaker.makeMultiRegexp("a}b")); - assertEquals("a\\~b", RegexpMaker.makeMultiRegexp("a~b")); - assertEquals("a\\\u007fb", RegexpMaker.makeMultiRegexp("a\u007fb")); - - assertEquals("a\u0080b", RegexpMaker.makeMultiRegexp("a\u0080b")); - assertEquals("a\u00c1b", RegexpMaker.makeMultiRegexp("a\u00c1b")); - - assertEquals("abc\\\\", RegexpMaker.makeRegexp("abc\\")); - - assertEquals("", RegexpMaker.makeMultiRegexp("")); - assertEquals("", RegexpMaker.makeMultiRegexp(" ")); - assertEquals("", RegexpMaker.makeMultiRegexp(",")); - assertEquals("", RegexpMaker.makeMultiRegexp(", ")); - assertEquals("", RegexpMaker.makeMultiRegexp(" ,")); - assertEquals("a", RegexpMaker.makeMultiRegexp("a,")); - assertEquals("a", RegexpMaker.makeMultiRegexp("a ")); - assertEquals("a", RegexpMaker.makeMultiRegexp("a, ")); - assertEquals("a", RegexpMaker.makeMultiRegexp("a ,")); - assertEquals("a", RegexpMaker.makeMultiRegexp(",a")); - assertEquals("a", RegexpMaker.makeMultiRegexp(" a")); - assertEquals("a", RegexpMaker.makeMultiRegexp(", a")); - assertEquals("a", RegexpMaker.makeMultiRegexp(" ,a")); - assertEquals("a|b", RegexpMaker.makeMultiRegexp("a b")); - assertEquals("a|b", RegexpMaker.makeMultiRegexp("a,b")); - assertEquals("a|b", RegexpMaker.makeMultiRegexp("a, b")); - assertEquals("a|b", RegexpMaker.makeMultiRegexp("a ,b")); - assertEquals(" ", RegexpMaker.makeMultiRegexp("\\ ")); - assertEquals("\\,", RegexpMaker.makeMultiRegexp("\\,")); - assertEquals("\\,", RegexpMaker.makeMultiRegexp("\\, ")); - assertEquals(" ", RegexpMaker.makeMultiRegexp(",\\ ")); - assertEquals("\\, ", RegexpMaker.makeMultiRegexp("\\,\\ ")); - assertEquals(" ", RegexpMaker.makeMultiRegexp("\\ ,")); - assertEquals("\\,", RegexpMaker.makeMultiRegexp(" \\,")); - assertEquals(" \\,", RegexpMaker.makeMultiRegexp("\\ \\,")); - assertEquals("a", RegexpMaker.makeMultiRegexp("\\a,")); - assertEquals("a\\,", RegexpMaker.makeMultiRegexp("a\\,")); - assertEquals("a\\,", RegexpMaker.makeMultiRegexp("\\a\\,")); - assertEquals("a", RegexpMaker.makeMultiRegexp("\\a ")); - assertEquals("a ", RegexpMaker.makeMultiRegexp("a\\ ")); - assertEquals("a ", RegexpMaker.makeMultiRegexp("\\a\\ ")); - assertEquals("a|\\\\", RegexpMaker.makeMultiRegexp("a, \\")); - assertEquals("a| ", RegexpMaker.makeMultiRegexp("a,\\ ")); - assertEquals("a| \\\\", RegexpMaker.makeMultiRegexp("a,\\ \\")); - assertEquals("a\\,", RegexpMaker.makeMultiRegexp("a\\, ")); - assertEquals("a\\,|\\\\", RegexpMaker.makeMultiRegexp("a\\, \\")); - assertEquals("a\\, ", RegexpMaker.makeMultiRegexp("a\\,\\ ")); - assertEquals("a\\, \\\\", RegexpMaker.makeMultiRegexp("a\\,\\ \\")); - assertEquals("a", RegexpMaker.makeMultiRegexp("\\a, ")); - assertEquals("a|\\\\", RegexpMaker.makeMultiRegexp("\\a, \\")); - assertEquals("a| ", RegexpMaker.makeMultiRegexp("\\a,\\ ")); - assertEquals("a| \\\\", RegexpMaker.makeMultiRegexp("\\a,\\ \\")); - assertEquals("a\\,", RegexpMaker.makeMultiRegexp("\\a\\, ")); - assertEquals("a\\,|\\\\", RegexpMaker.makeMultiRegexp("\\a\\, \\")); - assertEquals("a\\, ", RegexpMaker.makeMultiRegexp("\\a\\,\\ ")); - assertEquals("a\\, \\\\", RegexpMaker.makeMultiRegexp("\\a\\,\\ \\")); - assertEquals("a|\\\\", RegexpMaker.makeMultiRegexp("a ,\\")); - assertEquals("a|\\,", RegexpMaker.makeMultiRegexp("a \\,")); - assertEquals("a|\\,\\\\", RegexpMaker.makeMultiRegexp("a \\,\\")); - assertEquals("a ", RegexpMaker.makeMultiRegexp("a\\ ,")); - assertEquals("a |\\\\", RegexpMaker.makeMultiRegexp("a\\ ,\\")); - assertEquals("a \\,", RegexpMaker.makeMultiRegexp("a\\ \\,")); - assertEquals("a \\,\\\\", RegexpMaker.makeMultiRegexp("a\\ \\,\\")); - assertEquals("a", RegexpMaker.makeMultiRegexp("\\a ,")); - assertEquals("a|\\\\", RegexpMaker.makeMultiRegexp("\\a ,\\")); - assertEquals("a|\\,", RegexpMaker.makeMultiRegexp("\\a \\,")); - assertEquals("a|\\,\\\\", RegexpMaker.makeMultiRegexp("\\a \\,\\")); - assertEquals("a ", RegexpMaker.makeMultiRegexp("\\a\\ ,")); - assertEquals("a |\\\\", RegexpMaker.makeMultiRegexp("\\a\\ ,\\")); - assertEquals("a \\,", RegexpMaker.makeMultiRegexp("\\a\\ \\,")); - assertEquals("a \\,\\\\", RegexpMaker.makeMultiRegexp("\\a\\ \\,\\")); - - assertEquals("a|b", RegexpMaker.makeMultiRegexp("a, b")); - assertEquals("a|.*b.", RegexpMaker.makeMultiRegexp("a,*b?")); - assertEquals("a|\\*b.", RegexpMaker.makeMultiRegexp("a,\\*b?")); - assertEquals("a|.*b\\?", RegexpMaker.makeMultiRegexp("a,*b\\?")); + public void testCanBeMultilinePattern() { + assertFalse(TextRegexpUtil.canBeMultilinePattern("a\\d\\d\\da")); + assertFalse(TextRegexpUtil.canBeMultilinePattern(".*")); + assertFalse(TextRegexpUtil.canBeMultilinePattern("(?m)^x.*y$")); + assertTrue(TextRegexpUtil.canBeMultilinePattern("(?ms-x)test.*test")); + assertTrue(TextRegexpUtil.canBeMultilinePattern("test\\ntest")); + assertTrue(TextRegexpUtil.canBeMultilinePattern("test\\rtest")); + assertTrue(TextRegexpUtil.canBeMultilinePattern("test\\r\\ntest")); + assertTrue(TextRegexpUtil.canBeMultilinePattern("test\\ftest")); + assertTrue(TextRegexpUtil.canBeMultilinePattern("test\\u000Btest")); + assertTrue(TextRegexpUtil.canBeMultilinePattern("test\\x85test")); } - - public void testCanBeMultilinePattern() { - assertFalse(RegexpMaker.canBeMultilinePattern("a\\d\\d\\da")); - assertFalse(RegexpMaker.canBeMultilinePattern(".*")); - assertFalse(RegexpMaker.canBeMultilinePattern("(?m)^x.*y$")); - assertTrue(RegexpMaker.canBeMultilinePattern("(?ms-x)test.*test")); - assertTrue(RegexpMaker.canBeMultilinePattern("test\\ntest")); - assertTrue(RegexpMaker.canBeMultilinePattern("test\\rtest")); - assertTrue(RegexpMaker.canBeMultilinePattern("test\\r\\ntest")); - assertTrue(RegexpMaker.canBeMultilinePattern("test\\ftest")); - assertTrue(RegexpMaker.canBeMultilinePattern("test\\u000Btest")); - assertTrue(RegexpMaker.canBeMultilinePattern("test\\x85test")); + + public void testLiteralMatches() { + SearchPattern sp = SearchPattern.create("a*b", false, false, + MatchType.LITERAL); + Pattern p = TextRegexpUtil.makeTextPattern(sp); + assertTrue(p.matcher("xxxa*byyy").find()); + assertFalse(p.matcher("xxxaSSbyyy").find()); + + sp = sp.changeSearchExpression("a?b"); + p = TextRegexpUtil.makeTextPattern(sp); + assertTrue(p.matcher("xxxa?byyy").find()); + assertFalse(p.matcher("xxxaSbyyy").find()); + + sp = sp.changeSearchExpression("a?b*c*d?e"); + p = TextRegexpUtil.makeTextPattern(sp); + assertTrue(p.matcher("xxxa?b*c*d?eyyy").find()); + assertFalse(p.matcher("xxxa?b*cudweyyy").find()); + } + + private String makeRegexp(String string) { + return TextRegexpUtil.makeTextPattern( + SearchPattern.create(string, false, false, MatchType.BASIC)) + .pattern(); + } + + private String makeRegexp(String string, boolean wholeWords) { + return TextRegexpUtil.makeTextPattern( + SearchPattern.create( + string, wholeWords, false, MatchType.BASIC)).pattern(); } }