KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: JXSearchPanel.java,v 1.1 2004/07/28 21:21:11 aim 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.util.regex.Pattern JavaDoc;
11
12 import java.awt.Dimension JavaDoc;
13 import java.awt.FlowLayout JavaDoc;
14 import java.awt.LayoutManager JavaDoc;
15 import java.awt.event.ActionEvent JavaDoc;
16 import java.awt.event.ActionListener JavaDoc;
17 import javax.swing.ComboBoxModel JavaDoc;
18 import javax.swing.DefaultComboBoxModel JavaDoc;
19 import javax.swing.JButton JavaDoc;
20 import javax.swing.JCheckBox JavaDoc;
21 import javax.swing.JComboBox JavaDoc;
22 import javax.swing.JComponent JavaDoc;
23 import javax.swing.JLabel JavaDoc;
24 import javax.swing.JPanel JavaDoc;
25 import javax.swing.JTextField JavaDoc;
26 import javax.swing.event.DocumentEvent JavaDoc;
27 import javax.swing.event.DocumentListener JavaDoc;
28
29 import org.jdesktop.swing.decorator.FilterPipeline;
30 import org.jdesktop.swing.decorator.HighlighterPipeline;
31 import org.jdesktop.swing.decorator.PatternFilter;
32 import org.jdesktop.swing.decorator.PatternHighlighter;
33 import org.jdesktop.swing.decorator.PatternMatcher;
34 import org.jdesktop.swing.decorator.PipelineListener;
35 import org.jdesktop.swing.decorator.Sorter;
36
37 /**
38  * Search panel.
39  *
40  * @author Ramesh Gupta
41  */

42 public class JXSearchPanel extends JPanel JavaDoc implements DocumentListener JavaDoc, ActionListener JavaDoc {
43     //public final static String MEDIATOR_KEY = "jfc:searchPanel";
44

45     // colleagues mediated by this JXSearchPanel
46
private final JLabel JavaDoc fieldName = new JLabel JavaDoc();
47     private final JCheckBox JavaDoc matchCase = new JCheckBox JavaDoc();
48     private final JComboBox JavaDoc searchCriteria = new JComboBox JavaDoc();
49     private final JButton JavaDoc searchButton = new JButton JavaDoc();
50     private final JTextField JavaDoc searchField = new JTextField JavaDoc();
51     // JXSearchPanel is the mediator for all colleagues
52
private final JComponent JavaDoc[] colleagues = {
53     matchCase,
54     searchCriteria,
55     searchButton,
56     searchField
57     };
58
59     private PatternFilter patternFilter = null;
60     private PatternHighlighter patternHighlighter = null;
61     private JComponent JavaDoc targetComponent = null;
62
63     public JXSearchPanel() {
64         //super(new FlowLayout());
65
/** @todo Remove hard-coded strings */
66         searchButton.setText("Search");
67         matchCase.setText("Match Case");
68         ComboBoxModel JavaDoc model = new DefaultComboBoxModel JavaDoc(new String JavaDoc[] {
69             "begins with",
70             "contains",
71             "ends with",
72             "equals"
73         });
74         searchCriteria.setModel(model);
75     searchField.setPreferredSize(new Dimension JavaDoc(80, 20));
76     /*
77       for (int i = 0; i < colleagues.length; i++) {
78       colleagues[i].putClientProperty(MEDIATOR_KEY, this);
79       }
80     */

81         add(fieldName);
82         add(searchCriteria);
83         add(searchField);
84         add(matchCase);
85         //add(searchButton); /** @todo remove searchButton entirely! */
86
addActionListener();
87         addEditListener();
88     }
89
90     public int getMatchFlags() {
91         return matchCase.isSelected() ? 0 : Pattern.CASE_INSENSITIVE;
92     }
93
94     public Pattern JavaDoc getPattern() {
95         String JavaDoc searchString = searchField.getText();
96         if (searchString.length() == 0) {
97             return Pattern.compile(".*", getMatchFlags());
98         }
99
100         String JavaDoc patternString;
101         int criteria = searchCriteria.getSelectedIndex();
102         /** @todo Remove hard-coded integers */
103         switch (criteria) {
104     case 0: {
105         patternString = new String JavaDoc(searchString + ".*");
106         break;
107     }
108     case 1: {
109         patternString = new String JavaDoc(".*" + searchString + ".*");
110         break;
111     }
112     case 2: {
113         patternString = new String JavaDoc(".*" + searchString);
114         break;
115     }
116     default: {
117         patternString = searchString;
118         break;
119     }
120         }
121         return Pattern.compile(patternString, getMatchFlags());
122     }
123
124     private void addActionListener() {
125         /** @todo Define ActionSource interface for all components that
126          * support addActionListener and removeActionListener
127          * for (int i = 0; i < colleagues.length; i++) {
128          * colleagues[i].addActionListener(this);
129          * }
130          */

131         // In the absence of ActionSource, add listener to each colleage...
132
matchCase.addActionListener(this);
133         searchButton.addActionListener(this);
134         searchField.addActionListener(this);
135         searchCriteria.addActionListener(this);
136     }
137
138     private void removeActionListener() {
139         /** @todo Define ActionSource interface for all components that
140          * support addActionListener and removeActionListener
141          * for (int i = 0; i < colleagues.length; i++) {
142          * colleagues[i].removeActionListener(listener);
143          * }
144          */

145         // In the absence of ActionSource, remove listener from each colleage...
146
matchCase.removeActionListener(this);
147         searchButton.removeActionListener(this);
148         searchField.removeActionListener(this);
149         searchCriteria.removeActionListener(this);
150     }
151
152     private void addEditListener() {
153         searchField.getDocument().addDocumentListener(this);
154     }
155
156     private void removeEditListener() {
157         searchField.getDocument().removeDocumentListener(this);
158     }
159
160     public void setPatternFilter(PatternFilter filter) {
161         patternFilter = filter;
162         if (filter == null) {
163             fieldName.setText("Field");
164         }
165         else {
166             fieldName.setText(filter.getColumnName());
167         }
168     }
169
170     public PatternFilter getPatternFilter() {
171         return patternFilter;
172     }
173
174     public void setPatternHighlighter(PatternHighlighter highlighter) {
175         patternHighlighter = highlighter;
176         if (fieldName.getText().length() == 0) { // ugly hack
177
fieldName.setText("Field"); /** @todo Remove this hack!!! */
178         }
179     }
180
181     public PatternHighlighter getPatternHighlighter() {
182         return patternHighlighter;
183     }
184
185     public void setTargetComponent(JComponent JavaDoc target) {
186         /** @todo Obsolete this method.
187          * Use event listener to target more than one component. */

188         this.targetComponent = target;
189     }
190
191     public JComponent JavaDoc getTargetComponent() {
192         return targetComponent;
193     }
194
195     public void setFieldName(String JavaDoc name) {
196         fieldName.setText(name);
197     }
198
199     public String JavaDoc getFieldName() {
200         return fieldName.getText();
201     }
202
203     /*
204       JTextField getSearchTextField() {
205       return searchField;
206       }
207
208       JComboBox getSearchCriteriaComboBox() {
209       return searchCriteria;
210       }
211
212       JCheckBox getCaseCheckBox() {
213       return matchCase;
214       }
215     */

216
217     public void changedUpdate(DocumentEvent JavaDoc ev) {
218         refresh();
219     }
220
221     public void insertUpdate(DocumentEvent JavaDoc ev) {
222         refresh();
223     }
224
225     public void removeUpdate(DocumentEvent JavaDoc ev) {
226         refresh();
227     }
228
229     public void actionPerformed(ActionEvent JavaDoc ev) {
230         refresh();
231     }
232
233     protected void refresh() {
234         Pattern JavaDoc pattern = getPattern();
235
236         PatternMatcher filter = getPatternFilter();
237         if (filter != null) {
238             filter.setPattern(pattern); // will repaint target automatically!
239
}
240
241         PatternMatcher highlighter = getPatternHighlighter();
242         if (highlighter != null) {
243             highlighter.setPattern(pattern);
244
245         if (filter == null) {
246                 // Repaint explicitly only if there is no filter
247
JComponent JavaDoc target = getTargetComponent();
248                 if (target != null) {
249                     target.repaint();
250                 }
251             }
252         }
253     }
254
255     /*
256       public static JXSearchPanel getMediator(Object colleague) {
257       JXSearchPanel searchPanel = null;
258       try {
259       // colleague could be a JTextField, JButton, JCheckBox, or JComboBox
260       JComponent component = (JComponent) colleague;
261       //searchPanel = (JXSearchPanel) component.getClientProperty(MEDIATOR_KEY);
262       }
263       catch (ClassCastException ex) {
264       // Perhaps colleague is not a JComponent?
265       }
266       return searchPanel; // OK to return null
267       }
268     */

269 }
270
271
Popular Tags