KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > JXFindDialog


1 /*
2  * $Id: JXFindDialog.java,v 1.2 2004/09/08 01:37:46 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing;
9
10 import java.awt.BorderLayout JavaDoc;
11 import java.awt.Component JavaDoc;
12 import java.awt.Frame JavaDoc;
13 import java.awt.GraphicsConfiguration JavaDoc;
14 import java.awt.GraphicsEnvironment JavaDoc;
15 import java.awt.Rectangle JavaDoc;
16
17 import java.awt.event.ActionEvent JavaDoc;
18 import java.awt.event.ActionListener JavaDoc;
19 import java.awt.event.KeyEvent JavaDoc;
20
21 import java.beans.EventHandler JavaDoc;
22
23 import java.util.regex.Pattern JavaDoc;
24 import java.util.regex.Matcher JavaDoc;
25
26 import javax.swing.*;
27
28 import javax.swing.event.DocumentEvent JavaDoc;
29 import javax.swing.event.DocumentListener JavaDoc;
30
31 public class JXFindDialog extends JDialog {
32
33     private Searchable searchable;
34
35     private JTextField findText;
36     private JCheckBox matchCheck;
37     private JCheckBox wrapCheck;
38     private JCheckBox backCheck;
39
40     private Pattern JavaDoc pattern;
41     private Matcher JavaDoc matcher;
42
43     private boolean DEBUG = true;
44
45     public JXFindDialog(Searchable searchable) {
46         super((Frame JavaDoc)SwingUtilities.getWindowAncestor((Component JavaDoc)searchable),
47               "Find in this component");
48         this.searchable = searchable;
49
50         GraphicsConfiguration JavaDoc gc =
51             GraphicsEnvironment.getLocalGraphicsEnvironment().
52             getDefaultScreenDevice().getDefaultConfiguration();
53         Rectangle JavaDoc bounds = gc.getBounds();
54         int x = bounds.x+bounds.width/3;
55         int y = bounds.y+bounds.height/3;
56
57         setLocation(x, y);
58
59         initUI();
60         pack();
61     }
62
63     /**
64      * Set the debug flag. Mostly for testing and diagnostics.
65      */

66     public void setDebug(boolean debug) {
67         this.DEBUG = debug;
68     }
69
70     private void initUI() {
71         getContentPane().add(createFieldPanel(), BorderLayout.CENTER);
72         getContentPane().add(createButtonPanel(), BorderLayout.SOUTH);
73
74         // Implementation of incremental search
75
/*
76           findText.getDocument().addDocumentListener(new DocumentListener() {
77           public void insertUpdate(DocumentEvent evt) {
78           doFind();
79           }
80
81           public void removeUpdate(DocumentEvent evt) {
82           doFind(true);
83           }
84
85           public void changedUpdate(DocumentEvent evt) {
86           }
87           });
88         */

89     }
90
91     /**
92      * TODO: Strings should be removed from the UI
93      */

94     private JComponent createFieldPanel() {
95
96         // Create components
97
JLabel label = new JLabel("Find Text: ");
98         label.setDisplayedMnemonicIndex(2);
99         label.setLabelFor(findText);
100
101         findText = new JTextField();
102         matchCheck = new JCheckBox(new MatchAction());
103         wrapCheck = new JCheckBox(new WrapAction());
104         backCheck = new JCheckBox(new BackwardAction());
105
106         Box lBox = Box.createVerticalBox();
107         lBox.add(label);
108         lBox.add(Box.createGlue());
109
110         Box rBox = Box.createVerticalBox();
111         rBox.add(findText);
112         rBox.add(matchCheck);
113         rBox.add(wrapCheck);
114         rBox.add(backCheck);
115
116         Box box = Box.createHorizontalBox();
117         box.add(lBox);
118         box.add(rBox);
119
120         box.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
121
122         return box;
123     }
124
125     private JPanel createButtonPanel() {
126         JPanel panel = new JPanel();
127
128         Action findAction = new FindAction();
129         Action closeAction = new CloseAction();
130
131         JButton findButton;
132         panel.add(findButton = new JButton(findAction));
133         panel.add(new JButton(closeAction));
134
135         // Bind the ESC key to CloseAction and ENTER to FindAction
136
String JavaDoc CANCEL_ACTION_KEY = "CANCEL_ACTION_KEY";
137         String JavaDoc ENTER_ACTION_KEY = "ENTER_ACTION_KEY";
138
139         KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
140         KeyStroke escapeKey = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
141
142         InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
143         inputMap.put(enterKey, ENTER_ACTION_KEY);
144         inputMap.put(escapeKey, CANCEL_ACTION_KEY);
145
146         getRootPane().setDefaultButton(findButton);
147
148         ActionMap actionMap = getRootPane().getActionMap();
149         actionMap.put(ENTER_ACTION_KEY, findAction);
150         actionMap.put(CANCEL_ACTION_KEY, closeAction);
151
152         return panel;
153     }
154
155     private int lastIndex = -1;
156
157     /**
158      * Action callback for Find action.
159      */

160     public void doFind() {
161         doFind(getBackwardsFlag());
162     }
163
164     public void doFind(boolean backwards) {
165         Pattern JavaDoc pattern = getPattern();
166         // XXX
167
// System.out.println("doFind: " + findText.getText() + ", " + backwards);
168
lastIndex = searchable.search(getPattern(), lastIndex, backwards);
169         if (lastIndex == -1) {
170             JOptionPane.showMessageDialog(this, "Value not found");
171         }
172     }
173
174     /**
175      * Action callback for Close action.
176      */

177     public void doClose() {
178         JXFindDialog.this.dispose();
179     }
180
181     public boolean getMatchFlag() {
182         return matchCheck.isSelected();
183     }
184
185     /**
186      * Public method for testing.
187      * <p>
188      * TODO: The state should probably be encapsulated by a model rather
189      * that within the UI components.
190      */

191     public void setMatchFlag(boolean flag) {
192         matchCheck.setSelected(flag);
193     }
194
195     public boolean getWrapFlag() {
196         return wrapCheck.isSelected();
197     }
198
199     public void setWrapFlag(boolean flag) {
200         wrapCheck.setSelected(flag);
201     }
202
203     public boolean getBackwardsFlag() {
204         return backCheck.isSelected();
205     }
206
207     public void setBackwardsFlag(boolean flag) {
208         backCheck.setSelected(flag);
209     }
210
211     private Pattern JavaDoc getPattern() {
212         String JavaDoc searchString = findText.getText();
213         if (searchString.length() == 0) {
214             return null;
215         }
216         if (pattern == null || !pattern.pattern().equals(searchString)) {
217             // TODO: check to see if the existing pattern.flags() state matches
218
// getMatchFlag
219
pattern = Pattern.compile(searchString,
220                                       getMatchFlag() ? 0 : Pattern.CASE_INSENSITIVE);
221             // Start from the beginning.
222
lastIndex = -1;
223         }
224         return pattern;
225     }
226
227     private class FindAction extends AbstractAction {
228         public FindAction() {
229             super("Find");
230         }
231         public void actionPerformed(ActionEvent JavaDoc evt) {
232             doFind();
233         }
234     }
235
236     private class CloseAction extends AbstractAction {
237         public CloseAction() {
238             super("Close");
239         }
240
241         public void actionPerformed(ActionEvent JavaDoc evt) {
242             if (DEBUG) {
243                 System.err.println(this.getValue(Action.NAME));
244             }
245             doClose();
246         }
247     }
248
249     private abstract class CheckAction extends AbstractAction {
250
251         public CheckAction(String JavaDoc name) {
252             super(name);
253         }
254
255         public void actionPerformed(ActionEvent JavaDoc evt) {
256             if (DEBUG) {
257                 System.err.println(this.getValue(Action.NAME));
258             }
259         }
260     }
261
262     private class MatchAction extends CheckAction {
263         public MatchAction() {
264             super("Match upper/lower case");
265             putValue(Action.MNEMONIC_KEY, new Integer JavaDoc('M'));
266         }
267     }
268
269     private class WrapAction extends CheckAction {
270         public WrapAction() {
271             super("Wrap around");
272             putValue(Action.MNEMONIC_KEY, new Integer JavaDoc('W'));
273         }
274     }
275
276     private class BackwardAction extends CheckAction {
277         public BackwardAction() {
278             super("Search Backwards");
279             putValue(Action.MNEMONIC_KEY, new Integer JavaDoc('B'));
280         }
281     }
282
283 }
284
Popular Tags