001: /*
002: * FindDialog.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: FindDialog.java,v 1.4 2003/10/13 23:52:56 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import gnu.regexp.REException;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027: import java.awt.event.TextEvent;
028: import java.awt.event.TextListener;
029: import javax.swing.Box;
030: import javax.swing.BoxLayout;
031: import javax.swing.JLabel;
032: import javax.swing.JPanel;
033:
034: public final class FindDialog extends AbstractDialog implements
035: ActionListener, TextListener {
036: private static final String patternKey = "find.pattern";
037: private static final String searchFromStartKey = "find.searchFromStart";
038:
039: private static boolean wholeWordsOnly;
040: private static boolean regularExpression;
041: private static boolean isMultilinePattern;
042:
043: private Search search;
044: private final Editor editor;
045: private HistoryTextField patternControl;
046: private History patternHistory;
047: private CheckBox ignoreCaseCheckBox;
048: private CheckBox wholeWordsCheckBox;
049: private CheckBox regularExpressionCheckBox;
050: private CheckBox multilinePatternCheckBox;
051: private CheckBox listOccurrencesCheckBox;
052: private CheckBox searchFromStartCheckBox;
053: private boolean listOccurrences;
054: private boolean searchFromStart;
055:
056: public FindDialog(Editor editor) {
057: super (editor, "Find", true);
058: this .editor = editor;
059: search = new Search();
060: patternControl = new HistoryTextField(20);
061: patternHistory = new History(patternKey);
062: patternControl.setHistory(patternHistory);
063: // Pre-fill pattern control.
064: String s = editor.getCurrentText();
065: if (s != null)
066: patternControl.setText(s);
067: else
068: patternControl.recallLast();
069: Label label = new Label("Pattern:");
070: label.setDisplayedMnemonic('P');
071: addLabelAndTextField(label, patternControl);
072: addVerticalStrut();
073: ignoreCaseCheckBox = new CheckBox("Ignore case");
074: ignoreCaseCheckBox.setMnemonic('I');
075: setIgnoreCaseDefault();
076: addCheckBox(ignoreCaseCheckBox);
077: wholeWordsCheckBox = new CheckBox("Whole words only",
078: wholeWordsOnly);
079: wholeWordsCheckBox.setMnemonic('W');
080: addCheckBox(wholeWordsCheckBox);
081: regularExpressionCheckBox = new CheckBox("Regular expression",
082: regularExpression);
083: regularExpressionCheckBox.setMnemonic('X');
084: regularExpressionCheckBox.addActionListener(this );
085: addCheckBox(regularExpressionCheckBox);
086: if (Editor.checkExperimental()) {
087: JPanel panel = new JPanel();
088: panel.setAlignmentX(LEFT_ALIGNMENT);
089: panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
090: panel.add(Box.createHorizontalStrut(17));
091: multilinePatternCheckBox = new CheckBox(
092: "Multiline pattern (experimental)",
093: isMultilinePattern);
094: multilinePatternCheckBox.setMnemonic('M');
095: panel.add(multilinePatternCheckBox);
096: multilinePatternCheckBox.addKeyListener(this );
097: multilinePatternCheckBox
098: .setEnabled(regularExpressionCheckBox.isSelected());
099: mainPanel.add(panel);
100: }
101: listOccurrencesCheckBox = new CheckBox("List occurrences",
102: listOccurrences);
103: listOccurrencesCheckBox.setMnemonic('L');
104: addCheckBox(listOccurrencesCheckBox);
105: searchFromStart = Editor.getSessionProperties()
106: .getBooleanProperty(searchFromStartKey, false);
107: searchFromStartCheckBox = new CheckBox(
108: "Search from start of document", searchFromStart);
109: searchFromStartCheckBox.setMnemonic('S');
110: addCheckBox(searchFromStartCheckBox);
111: addVerticalStrut();
112: addOKCancel();
113: patternControl.addTextListener(this );
114: patternControl.addActionListener(this );
115: pack();
116: patternControl.requestFocus();
117: }
118:
119: public Search getSearch() {
120: return search;
121: }
122:
123: public boolean getListOccurrences() {
124: return listOccurrences;
125: }
126:
127: public boolean searchFromStart() {
128: return searchFromStart;
129: }
130:
131: protected void ok() {
132: search.setPattern(patternControl.getText());
133: search.setIgnoreCase(ignoreCaseCheckBox.isSelected());
134: wholeWordsOnly = wholeWordsCheckBox.isSelected();
135: search.setWholeWordsOnly(wholeWordsOnly);
136: regularExpression = regularExpressionCheckBox.isSelected();
137: search.setRegularExpression(regularExpression);
138: if (multilinePatternCheckBox != null) {
139: isMultilinePattern = multilinePatternCheckBox.isSelected();
140: search.setMultiline(isMultilinePattern);
141: }
142: if (regularExpression) {
143: try {
144: search.setREFromPattern();
145: } catch (REException e) {
146: MessageDialog.showMessageDialog(editor, e.getMessage(),
147: "Error");
148: patternControl.requestFocus();
149: return;
150: }
151: }
152: listOccurrences = listOccurrencesCheckBox.isSelected();
153: searchFromStart = searchFromStartCheckBox.isSelected();
154: Editor.getSessionProperties().setBooleanProperty(
155: searchFromStartKey, searchFromStart);
156: patternHistory.append(search.getPattern());
157: patternHistory.save();
158: dispose();
159: }
160:
161: protected void cancel() {
162: cancelled = true;
163: search = null;
164: dispose();
165: }
166:
167: public void textValueChanged(TextEvent e) {
168: setIgnoreCaseDefault();
169: }
170:
171: public void actionPerformed(ActionEvent e) {
172: String cmd = e.getActionCommand();
173: if (cmd != null
174: && cmd.equals(regularExpressionCheckBox.getText())) {
175: if (multilinePatternCheckBox != null) {
176: boolean isRegExp = regularExpressionCheckBox
177: .isSelected();
178: if (!isRegExp)
179: multilinePatternCheckBox.setSelected(false);
180: multilinePatternCheckBox.setEnabled(isRegExp);
181: }
182: } else
183: super .actionPerformed(e);
184: }
185:
186: private void setIgnoreCaseDefault() {
187: String pattern = patternControl.getText();
188: ignoreCaseCheckBox.setSelected(pattern == null
189: || Utilities.isLowerCase(pattern));
190: }
191:
192: public static void find() {
193: find(Editor.currentEditor());
194: }
195:
196: // Also called from IncrementalTextFieldHandler.finish() if the user hits
197: // Enter with an empty search string.
198: public static void find(Editor editor) {
199: if (editor.getDot() == null)
200: return;
201: FindDialog d = new FindDialog(editor);
202: editor.centerDialog(d);
203: d.show();
204: Search search = d.getSearch();
205: if (search == null)
206: return;
207: editor.setLastSearch(search);
208: if (d.getListOccurrences()) {
209: ListOccurrences.listOccurrences(editor);
210: } else {
211: editor.setWaitCursor();
212: final Position start;
213: if (d.searchFromStart())
214: start = new Position(editor.getBuffer().getFirstLine(),
215: 0);
216: else
217: start = editor.getDot();
218: Position pos = search.find(editor.getBuffer(), start);
219: editor.setDefaultCursor();
220: if (pos != null) {
221: editor.moveDotTo(pos);
222: editor.markFoundPattern(search);
223: } else
224: search.notFound(editor);
225: }
226: }
227: }
|