001: /*
002: * FindInFilesDialog.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: FindInFilesDialog.java,v 1.7 2003/07/27 01:12:55 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.RE;
025: import gnu.regexp.REException;
026: import java.awt.Dimension;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.awt.event.FocusEvent;
030: import java.awt.event.FocusListener;
031: import java.awt.event.TextEvent;
032: import java.awt.event.TextListener;
033: import java.util.ArrayList;
034: import java.util.Iterator;
035: import java.util.StringTokenizer;
036: import javax.swing.Box;
037: import javax.swing.BoxLayout;
038: import javax.swing.JComboBox;
039: import javax.swing.JPanel;
040:
041: public class FindInFilesDialog extends AbstractDialog implements
042: Constants, ActionListener, FocusListener, TextListener {
043: private static final String patternKey = "find.pattern";
044: private static final String replacementKey = "replace.replacement";
045: private static final String filesKey = "findInFiles.files";
046: private static final String wholeWordsOnlyKey = "findInFiles.wholeWordsOnly";
047: private static final String regExpKey = "findInFiles.regularExpression";
048: private static final String includeSubdirsKey = "findInFiles.includeSubdirs";
049: private static final String searchFilesInMemoryKey = "findInFiles.searchFilesInMemory";
050: private static final String listOccurrencesKey = "findInFiles.listOccurrences";
051:
052: private final SessionProperties sessionProperties = Editor
053: .getSessionProperties();
054:
055: private FindInFiles findInFiles;
056:
057: private final Editor editor;
058: private final boolean replace;
059:
060: private HistoryTextField patternControl;
061: private HistoryTextField replacementControl;
062: private HistoryTextField filesControl;
063:
064: private History patternHistory;
065: private History replacementHistory;
066: private History filesHistory;
067:
068: private CheckBox ignoreCaseCheckBox;
069: private CheckBox wholeWordsOnlyCheckBox;
070: private CheckBox regExpCheckBox;
071: private CheckBox confirmChangesCheckBox;
072: private CheckBox includeSubdirsCheckBox;
073: private CheckBox searchFilesInMemoryCheckBox;
074: private CheckBox listOccurrencesCheckBox;
075:
076: private Label modeLabel;
077: private JComboBox modeComboBox;
078:
079: private ModeListEntry[] permissibleModes;
080:
081: public FindInFilesDialog(Editor editor, boolean replace) {
082: super (editor, replace ? "Replace In Files" : "Find In Files",
083: true);
084:
085: this .editor = editor;
086: this .replace = replace;
087:
088: patternControl = new HistoryTextField(20);
089: patternHistory = new History(patternKey);
090: patternControl.setHistory(patternHistory);
091:
092: // Pre-fill pattern control.
093: String s;
094: if (editor.getBuffer() instanceof Directory)
095: // It's not very likely that we want to search for the text at dot
096: // in a directory buffer.
097: s = null;
098: else
099: s = editor.getCurrentText();
100: if (s != null)
101: patternControl.setText(s);
102: else
103: patternControl.recallLast();
104:
105: Label label = new Label("Pattern:");
106: label.setDisplayedMnemonic('P');
107: addLabelAndTextField(label, patternControl);
108: patternControl.addTextListener(this );
109:
110: addVerticalStrut();
111:
112: if (replace) {
113: replacementControl = new HistoryTextField(20);
114: replacementHistory = new History(replacementKey);
115: replacementControl.setHistory(replacementHistory);
116: replacementControl.recallLast();
117: label = new Label("Replace with:");
118: label.setDisplayedMnemonic('E');
119: addLabelAndTextField(label, replacementControl);
120: addVerticalStrut();
121: }
122:
123: filesControl = new HistoryTextField(20);
124: filesHistory = new History(filesKey);
125: filesControl.setHistory(filesHistory);
126: filesControl.recallLast();
127: label = new Label("Files:");
128: label.setDisplayedMnemonic('F');
129: addLabelAndTextField(label, filesControl);
130:
131: filesControl.addFocusListener(this );
132:
133: addVerticalStrut();
134:
135: ignoreCaseCheckBox = new CheckBox("Ignore case");
136: ignoreCaseCheckBox.setMnemonic('I');
137: setIgnoreCaseDefault();
138: addCheckBox(ignoreCaseCheckBox);
139:
140: wholeWordsOnlyCheckBox = new CheckBox("Whole words only",
141: sessionProperties.getBooleanProperty(wholeWordsOnlyKey,
142: false));
143: wholeWordsOnlyCheckBox.setMnemonic('W');
144: wholeWordsOnlyCheckBox.addActionListener(this );
145: addCheckBox(wholeWordsOnlyCheckBox);
146:
147: // Mode combo box.
148: modeComboBox = new JComboBox(getPermissibleModes());
149: Dimension dim = modeComboBox.getPreferredSize();
150: modeComboBox.setMinimumSize(dim);
151: modeComboBox.setMaximumSize(dim);
152:
153: JPanel panel = new JPanel();
154: panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
155: panel.setAlignmentX(LEFT_ALIGNMENT);
156: panel.add(Box.createHorizontalStrut(22));
157: modeLabel = new Label("Mode:");
158: panel.add(modeLabel);
159: modeLabel.setDisplayedMnemonic('O');
160: modeLabel.setLabelFor(modeComboBox);
161: panel.add(Box.createHorizontalStrut(5));
162: panel.add(modeComboBox);
163: updateModeControl();
164: modeComboBox.addKeyListener(this );
165: mainPanel.add(panel);
166:
167: regExpCheckBox = new CheckBox(replace ? "Regular expressions"
168: : "Regular expression", sessionProperties
169: .getBooleanProperty(regExpKey, false));
170: regExpCheckBox.setMnemonic('X');
171: addCheckBox(regExpCheckBox);
172:
173: if (replace) {
174: confirmChangesCheckBox = new CheckBox("Confirm changes",
175: true);
176: confirmChangesCheckBox.setMnemonic('C');
177: addCheckBox(confirmChangesCheckBox);
178: }
179:
180: includeSubdirsCheckBox = new CheckBox("Include subdirectories",
181: sessionProperties.getBooleanProperty(includeSubdirsKey,
182: false));
183: includeSubdirsCheckBox.setMnemonic('S');
184: addCheckBox(includeSubdirsCheckBox);
185:
186: // Always search files in memory for replace in files.
187: // Otherwise it's up to the user.
188: if (!replace) {
189: searchFilesInMemoryCheckBox = new CheckBox(
190: "Search files in memory", sessionProperties
191: .getBooleanProperty(searchFilesInMemoryKey,
192: true));
193: searchFilesInMemoryCheckBox.setMnemonic('M');
194: addCheckBox(searchFilesInMemoryCheckBox);
195: listOccurrencesCheckBox = new CheckBox("List occurrences",
196: sessionProperties.getBooleanProperty(
197: listOccurrencesKey, true));
198: listOccurrencesCheckBox.setMnemonic('L');
199: addCheckBox(listOccurrencesCheckBox);
200: }
201:
202: addVerticalStrut();
203:
204: addOKCancel();
205:
206: pack();
207:
208: patternControl.requestFocus();
209: }
210:
211: private ModeListEntry[] getPermissibleModes() {
212: if (permissibleModes == null) {
213: ModeList modeList = Editor.getModeList();
214: ArrayList list = new ArrayList();
215: synchronized (modeList) {
216: Iterator it = modeList.iterator();
217: while (it.hasNext()) {
218: ModeListEntry entry = (ModeListEntry) it.next();
219: if (entry.isSelectable()
220: && entry.getId() != BINARY_MODE)
221: list.add(entry);
222: }
223: }
224: permissibleModes = (ModeListEntry[]) list
225: .toArray(new ModeListEntry[list.size()]);
226: }
227: return permissibleModes;
228: }
229:
230: public FindInFiles getFindInFiles() {
231: return findInFiles;
232: }
233:
234: protected void ok() {
235: findInFiles = new FindInFiles(editor);
236:
237: findInFiles.setPattern(patternControl.getText());
238:
239: if (replacementControl != null) {
240: findInFiles.setReplaceWith(replacementControl.getText());
241: replacementHistory.append(findInFiles.getReplaceWith());
242: replacementHistory.save();
243: }
244:
245: findInFiles.setIgnoreCase(ignoreCaseCheckBox.isSelected());
246: findInFiles.setWholeWordsOnly(wholeWordsOnlyCheckBox
247: .isSelected());
248: findInFiles.setRegularExpression(regExpCheckBox.isSelected());
249:
250: if (confirmChangesCheckBox != null)
251: findInFiles.setConfirmChanges(confirmChangesCheckBox
252: .isSelected());
253:
254: findInFiles.setIncludeSubdirs(includeSubdirsCheckBox
255: .isSelected());
256:
257: if (searchFilesInMemoryCheckBox != null)
258: findInFiles
259: .setSearchFilesInMemory(searchFilesInMemoryCheckBox
260: .isSelected());
261: else
262: findInFiles.setSearchFilesInMemory(true);
263:
264: if (listOccurrencesCheckBox != null
265: && listOccurrencesCheckBox.isSelected())
266: findInFiles.setListEachOccurrence(true);
267:
268: patternHistory.append(findInFiles.getPattern());
269: patternHistory.save();
270:
271: sessionProperties.setBooleanProperty(wholeWordsOnlyKey,
272: findInFiles.wholeWordsOnly());
273: sessionProperties.setBooleanProperty(regExpKey, findInFiles
274: .isRegularExpression());
275: sessionProperties.setBooleanProperty(includeSubdirsKey,
276: findInFiles.getIncludeSubdirs());
277: sessionProperties.setBooleanProperty(searchFilesInMemoryKey,
278: findInFiles.getSearchFilesInMemory());
279: if (!replace)
280: sessionProperties.setBooleanProperty(listOccurrencesKey,
281: findInFiles.getListEachOccurrence());
282:
283: if (findInFiles.isRegularExpression()) {
284: if (findInFiles.getRE() == null) {
285: try {
286: int flags = RE.REG_MULTILINE;
287: if (findInFiles.ignoreCase())
288: flags |= RE.REG_ICASE;
289: findInFiles.setRE(new RE(findInFiles.getPattern(),
290: flags));
291: } catch (REException e) {
292: findInFiles = null;
293: MessageDialog.showMessageDialog(editor, e
294: .getMessage(), "Error");
295: patternControl.requestFocus();
296: return;
297: }
298: }
299: }
300:
301: final String files = filesControl.getText();
302: try {
303: findInFiles.setFiles(files);
304: } catch (Exception e) {
305: findInFiles = null;
306: filesControl.requestFocus();
307: MessageDialog.showMessageDialog(editor, e.getMessage(),
308: "Error");
309: return;
310: }
311: filesHistory.append(files);
312: filesHistory.save();
313:
314: if (modeComboBox != null) {
315: ModeListEntry entry = (ModeListEntry) modeComboBox
316: .getSelectedItem();
317: findInFiles.setMode(entry.getMode(true));
318: }
319:
320: dispose();
321: }
322:
323: public void textValueChanged(TextEvent e) {
324: setIgnoreCaseDefault();
325: }
326:
327: public void actionPerformed(ActionEvent e) {
328: String cmd = e.getActionCommand();
329: if (cmd != null && cmd.equals(wholeWordsOnlyCheckBox.getText()))
330: updateModeControl();
331: else
332: super .actionPerformed(e);
333: }
334:
335: public void focusGained(FocusEvent e) {
336: }
337:
338: public void focusLost(FocusEvent e) {
339: if (e.getComponent() == filesControl)
340: updateModeControl();
341: }
342:
343: private void updateModeControl() {
344: if (modeComboBox != null) {
345: String files = filesControl.getText();
346: StringTokenizer st = new StringTokenizer(files, ";");
347: if (st.hasMoreTokens()) {
348: String token = st.nextToken().trim();
349: if (token.length() > 0) {
350: int id = Editor.getModeList().getModeIdForFileName(
351: token);
352: if (id >= 0) {
353: ModeListEntry[] modes = getPermissibleModes();
354: for (int i = modes.length; i-- > 0;) {
355: if (modes[i].getId() == id) {
356: modeComboBox.setSelectedItem(modes[i]);
357: break;
358: }
359: }
360: }
361: }
362: }
363: boolean b = wholeWordsOnlyCheckBox.isSelected();
364: modeComboBox.setEnabled(b);
365: modeLabel.setEnabled(b);
366: }
367: }
368:
369: private void setIgnoreCaseDefault() {
370: String pattern = patternControl.getText();
371: ignoreCaseCheckBox.setSelected(pattern == null
372: || Utilities.isLowerCase(pattern));
373: }
374: }
|