001: /*
002: * Copyright (c) 2000, Jacob Smullyan.
003: *
004: * This is part of SkunkDAV, a WebDAV client. See http://skunkdav.sourceforge.net/
005: * for the latest version.
006: *
007: * SkunkDAV is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License as published
009: * by the Free Software Foundation; either version 2, or (at your option)
010: * any later version.
011: *
012: * SkunkDAV 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 GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with SkunkDAV; see the file COPYING. If not, write to the Free
019: * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
020: * 02111-1307, USA.
021: */
022:
023: package org.skunk.dav.client.gui.editor;
024:
025: import java.awt.Font;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.ActionListener;
028: import java.awt.event.KeyAdapter;
029: import java.awt.event.KeyEvent;
030: import java.util.ArrayList;
031: import java.util.Enumeration;
032: import javax.swing.AbstractButton;
033: import javax.swing.ButtonGroup;
034: import javax.swing.JCheckBox;
035: import javax.swing.JComponent;
036: import javax.swing.JRadioButton;
037: import javax.swing.JTextField;
038: import javax.swing.JPanel;
039: import javax.swing.event.DocumentEvent;
040: import javax.swing.event.DocumentListener;
041: import org.skunk.dav.client.gui.ResourceManager;
042: import org.skunk.trace.Debug;
043:
044: public abstract class AbstractSearchPanel extends JPanel implements
045: ISearchPanel {
046: private ArrayList actionListeners;
047: public static final int ENTRY_FIELD_LENGTH = 20;
048: protected JTextField entryField, replaceField;
049: protected JCheckBox reverseButton, incrementalButton,
050: wrappedButton;
051: protected JRadioButton literalButton, caseButton, regexpButton;
052: private int searchMode;
053:
054: public AbstractSearchPanel() {
055: super ();
056: actionListeners = new ArrayList();
057: initComponents();
058: }
059:
060: protected abstract void initComponents();
061:
062: protected void createReverseCheckBox() {
063: reverseButton = new JCheckBox(ResourceManager
064: .getMessage(ResourceManager.REVERSE_OPTION), false);
065: }
066:
067: protected void createIncrementalCheckBox() {
068: incrementalButton = new JCheckBox(ResourceManager
069: .getMessage(ResourceManager.INCREMENTAL_OPTION), true);
070: }
071:
072: protected void createWrappedCheckBox() {
073: wrappedButton = new JCheckBox(ResourceManager
074: .getMessage(ResourceManager.WRAPPED_OPTION), false);
075: }
076:
077: protected void createSearchModeButtonGroup()
078: {
079: literalButton=new JRadioButton(ResourceManager.getMessage(ResourceManager.LITERAL_OPTION));
080: literalButton.setActionCommand(ResourceManager.LITERAL_OPTION);
081:
082: caseButton=new JRadioButton(ResourceManager.getMessage(ResourceManager.CASE_OPTION));
083: caseButton.setActionCommand(ResourceManager.CASE_OPTION);
084:
085: regexpButton=new JRadioButton(ResourceManager.getMessage(ResourceManager.REGEXP_OPTION));
086: regexpButton.setActionCommand(ResourceManager.REGEXP_OPTION);
087:
088: final ButtonGroup group=new ButtonGroup();
089: group.add(literalButton);
090: group.add(caseButton);
091: group.add(regexpButton);
092:
093: ActionListener groupListener=new ActionListener()
094: {
095: public void actionPerformed(ActionEvent ae)
096: {
097: int mode=getSearchModeForActionCommand(group.getSelection().getActionCommand());
098: AbstractSearchPanel.this .searchMode=mode;
099: }
100: };
101: for (Enumeration enum=group.getElements();enum.hasMoreElements();)
102: {
103: ((AbstractButton)enum.nextElement()).addActionListener(groupListener);
104: }
105: group.setSelected(literalButton.getModel(), true);
106: }
107:
108: protected void createEntryField() {
109: entryField = new JTextField(ENTRY_FIELD_LENGTH);
110: entryField.setFont(new Font("Serif", Font.PLAIN, 10));
111: entryField.setMaximumSize(entryField.getPreferredSize());
112: entryField.getDocument().addDocumentListener(
113: new DocumentListener() {
114: public void changedUpdate(DocumentEvent donkey) {
115: }
116:
117: public void insertUpdate(DocumentEvent donkey) {
118: if (isIncremental())
119: fireActionEvent(isReverse() ? SEARCH_FROM_SELECTION
120: : SEARCH_FROM_CARET);
121: }
122:
123: public void removeUpdate(DocumentEvent donkey) {
124: if (isIncremental())
125: fireActionEvent(SEARCH_FROM_START_OR_END);
126: }
127: });
128:
129: entryField.addKeyListener(new KeyAdapter() {
130: public void keyPressed(KeyEvent ke) {
131: if (ke.getKeyCode() == KeyEvent.VK_ESCAPE)
132: fireActionEvent(END_SEARCH);
133: }
134: });
135:
136: entryField.addActionListener(new ActionListener() {
137: public void actionPerformed(ActionEvent ae) {
138: fireActionEvent(isReverse() ? SEARCH_FROM_CARET
139: : SEARCH_FROM_SELECTION);
140: }
141: });
142: }
143:
144: public void createReplaceField() {
145: replaceField = new JTextField(ENTRY_FIELD_LENGTH);
146: replaceField.setFont(new Font("Serif", Font.PLAIN, 10));
147: replaceField.setMaximumSize(replaceField.getPreferredSize());
148:
149: replaceField.addKeyListener(new KeyAdapter() {
150: public void keyPressed(KeyEvent ke) {
151: if (ke.getKeyCode() == KeyEvent.VK_ESCAPE)
152: fireActionEvent(END_SEARCH);
153: }
154: });
155:
156: replaceField.addActionListener(new ActionListener() {
157: public void actionPerformed(ActionEvent ae) {
158: fireActionEvent(isReverse() ? SEARCH_FROM_CARET
159: : SEARCH_FROM_SELECTION);
160: }
161: });
162: }
163:
164: public JComponent getComponent() {
165: return this ;
166: }
167:
168: public void addActionListener(ActionListener listener) {
169: actionListeners.add(listener);
170: }
171:
172: public void removeActionListener(ActionListener listener) {
173: actionListeners.remove(listener);
174: }
175:
176: public void fireActionEvent(String command) {
177: Debug.trace(this , Debug.DP3, "in fireActionEvent()");
178: ActionEvent ae = new ActionEvent(this ,
179: ActionEvent.ACTION_PERFORMED, command);
180: Object[] listenerArray = actionListeners.toArray();
181: for (int i = 0; i < listenerArray.length; i++) {
182: ((ActionListener) listenerArray[i]).actionPerformed(ae);
183: }
184: }
185:
186: protected int getSearchModeForActionCommand(String actionCommand) {
187: if (ResourceManager.LITERAL_OPTION.equals(actionCommand))
188: return LITERAL_MODE;
189: else if (ResourceManager.CASE_OPTION.equals(actionCommand))
190: return CASE_MODE;
191: else if (ResourceManager.REGEXP_OPTION.equals(actionCommand))
192: return REGEXP_MODE;
193: throw new IllegalArgumentException(
194: "unrecognized action command");
195: }
196:
197: public boolean isReverse() {
198: if (reverseButton != null)
199: return reverseButton.isSelected();
200: else
201: return false;
202: }
203:
204: public boolean isIncremental() {
205: if (incrementalButton != null)
206: return incrementalButton.isSelected();
207: else
208: return false;
209: }
210:
211: public boolean isWrapped() {
212: if (wrappedButton != null)
213: return wrappedButton.isSelected();
214: else
215: return false;
216: }
217:
218: public int getSearchMode() {
219: return this .searchMode;
220: }
221:
222: public String getSearchText() {
223: if (entryField != null)
224: return entryField.getText();
225: else
226: return null;
227: }
228:
229: public String getReplaceText() {
230: if (replaceField != null)
231: return replaceField.getText();
232: else
233: return null;
234: }
235:
236: public void focus() {
237: entryField.requestFocus();
238: }
239: }
|