KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > search > SearchEntryForm


1 package net.suberic.pooka.gui.search;
2 import javax.swing.*;
3 import javax.swing.event.*;
4 import javax.mail.search.*;
5 import java.util.*;
6 import net.suberic.pooka.Pooka;
7 import net.suberic.pooka.SearchTermManager;
8 import net.suberic.util.VariableBundle;
9
10 /**
11  * This is a gui component which lets you configure a
12  * javax.mail.search.SearchTerm. It can be used either to configure mail
13  * filters, or to search folders.
14  */

15 public class SearchEntryForm implements java.awt.event.ItemListener JavaDoc {
16     // ui objects
17
private Box panel;
18     private JComboBox searchFieldCombo;
19     private JPanel selectionPanel;
20
21     private Box stringMatchPanel;
22     private JComboBox operationCombo;
23     private JTextField textField;
24
25     private Box booleanPanel;
26     private JComboBox booleanValueCombo;
27
28     private Box datePanel;
29     private JComboBox dateComparisonCombo;
30     private JTextField dateField;
31     private java.awt.CardLayout JavaDoc layout;
32
33     private Box headerMatchPanel;
34     private JComboBox headerOperationCombo;
35     private JTextField headerNameTextField;
36     private JTextField headerValueTextField;
37
38     // the source SearchTermManager.
39
SearchTermManager manager;
40
41     /**
42      * Constructs a new SearchEntryForm.
43      */

44     public SearchEntryForm(SearchTermManager newManager) {
45     manager = newManager;
46     panel = new Box(BoxLayout.X_AXIS);
47
48     searchFieldCombo = new JComboBox(manager.getTermLabels());
49     searchFieldCombo.setPreferredSize(searchFieldCombo.getMinimumSize());
50     searchFieldCombo.setMaximumSize(searchFieldCombo.getMinimumSize());
51
52     searchFieldCombo.addItemListener( this );
53
54     createPanels();
55
56     selectionPanel = new JPanel();
57     layout = new java.awt.CardLayout JavaDoc();
58     selectionPanel.setLayout(layout);
59     selectionPanel.add(stringMatchPanel, SearchTermManager.STRING_MATCH);
60     selectionPanel.add(booleanPanel, SearchTermManager.BOOLEAN_MATCH);
61     selectionPanel.add(datePanel, SearchTermManager.DATE_MATCH);
62     selectionPanel.add(headerMatchPanel, SearchTermManager.HEADER_MATCH);
63     panel.add(searchFieldCombo);
64     panel.add(selectionPanel);
65
66     }
67
68     /**
69      * Constructs a new SearchEntryForm using the given property and
70      * VariableBundle.
71      */

72     public SearchEntryForm(SearchTermManager newManager, String JavaDoc rootProperty, VariableBundle bundle) {
73     this(newManager);
74     setTermValue(rootProperty, bundle);
75     }
76   
77   /**
78    * Creates all of the selection panels.
79    */

80   public void createPanels() {
81
82     Vector operationFields = manager.getOperationLabels(SearchTermManager.STRING_MATCH);
83
84     headerMatchPanel = new Box(BoxLayout.X_AXIS);
85     headerOperationCombo = new JComboBox(operationFields);
86     headerOperationCombo.setPreferredSize(headerOperationCombo.getMinimumSize());
87     headerOperationCombo.setMaximumSize(headerOperationCombo.getMinimumSize());
88
89     headerNameTextField = new JTextField(10);
90     headerNameTextField.setMaximumSize(new java.awt.Dimension JavaDoc(500, headerNameTextField.getPreferredSize().height));
91
92     headerValueTextField = new JTextField(15);
93     headerValueTextField.setMaximumSize(new java.awt.Dimension JavaDoc(1000, headerValueTextField.getPreferredSize().height));
94
95     headerMatchPanel.add(headerNameTextField);
96     headerMatchPanel.add(headerOperationCombo);
97     headerMatchPanel.add(headerValueTextField);
98     headerMatchPanel.add(Box.createGlue());
99
100     stringMatchPanel = new Box(BoxLayout.X_AXIS);
101     operationCombo = new JComboBox(operationFields);
102     operationCombo.setPreferredSize(operationCombo.getMinimumSize());
103     operationCombo.setMaximumSize(operationCombo.getMinimumSize());
104     textField = new JTextField(20);
105     textField.setMaximumSize(new java.awt.Dimension JavaDoc(1000, textField.getPreferredSize().height));
106     
107     stringMatchPanel.add(operationCombo);
108     stringMatchPanel.add(textField);
109     stringMatchPanel.add(Box.createGlue());
110     
111     booleanPanel = new Box(BoxLayout.X_AXIS);
112     Vector booleanFields = manager.getOperationLabels(SearchTermManager.BOOLEAN_MATCH);
113     booleanValueCombo = new JComboBox(booleanFields);
114     booleanValueCombo.setPreferredSize(booleanValueCombo.getMinimumSize());
115     booleanValueCombo.setMaximumSize(booleanValueCombo.getMinimumSize());
116     booleanPanel.add(booleanValueCombo);
117     booleanPanel.add(Box.createGlue());
118     
119     datePanel = new Box(BoxLayout.X_AXIS);
120     
121     Vector dateFields = manager.getOperationLabels(SearchTermManager.DATE_MATCH);
122     dateComparisonCombo = new JComboBox(dateFields);
123     dateComparisonCombo.setPreferredSize(dateComparisonCombo.getMinimumSize());
124     dateComparisonCombo.setMaximumSize(dateComparisonCombo.getMinimumSize());
125     dateField = new JTextField(10);
126     dateField.setMaximumSize(new java.awt.Dimension JavaDoc(1000, dateField.getPreferredSize().height));
127     
128     JLabel dateFormatLabel = new JLabel(Pooka.getProperty("Search.dateFormat", "mm/dd/yyyy"));
129     datePanel.add(dateComparisonCombo);
130     datePanel.add(dateField);
131     datePanel.add(dateFormatLabel);
132     datePanel.add(Box.createGlue());
133   }
134
135     /**
136      * Returns the Box which shows the SearchEntryForm.
137      */

138     public Box getPanel() {
139     return panel;
140     }
141
142     /**
143      * This generates a SearchTerm from the information on the Box, and
144      * returns that value.
145      */

146     public SearchTerm generateSearchTerm() throws java.text.ParseException JavaDoc {
147     if (Pooka.isDebug())
148         System.out.println("SearchEntryForm: generating SearchTerm from " + searchFieldCombo.getSelectedItem() + " and " + operationCombo.getSelectedItem());
149
150     String JavaDoc searchProperty = (String JavaDoc)(manager.getLabelToPropertyMap().get(searchFieldCombo.getSelectedItem()));
151     String JavaDoc selectedType = Pooka.getProperty(searchProperty + ".type", "");
152     String JavaDoc operationProperty = null;
153     String JavaDoc pattern = null;
154     String JavaDoc header = null;
155
156     if (selectedType.equalsIgnoreCase(SearchTermManager.HEADER_MATCH)) {
157       operationProperty = (String JavaDoc)(manager.getLabelToOperationMap().get(headerOperationCombo.getSelectedItem()));
158       header = headerNameTextField.getText();
159       pattern = headerValueTextField.getText();
160       if (Pooka.isDebug())
161         System.out.println("using " + searchProperty + ", " + operationProperty + ", " + header + ", " + pattern);
162
163     } else if (selectedType.equalsIgnoreCase(SearchTermManager.STRING_MATCH)) {
164         operationProperty = (String JavaDoc)(manager.getLabelToOperationMap().get(operationCombo.getSelectedItem()));
165         pattern = textField.getText();
166         if (Pooka.isDebug())
167         System.out.println("using " + searchProperty + ", " + operationProperty + ", " + pattern);
168
169     } else if (selectedType.equalsIgnoreCase(SearchTermManager.BOOLEAN_MATCH)) {
170         operationProperty = (String JavaDoc)(manager.getLabelToOperationMap().get(booleanValueCombo.getSelectedItem()));
171         if (Pooka.isDebug())
172         System.out.println("using " + searchProperty + ", " + operationProperty);
173         pattern = null;
174
175     } else if (selectedType.equalsIgnoreCase(SearchTermManager.DATE_MATCH)) {
176         operationProperty = (String JavaDoc)(manager.getLabelToOperationMap().get(dateComparisonCombo.getSelectedItem()));
177         if (Pooka.isDebug())
178         System.out.println("using " + searchProperty + ", " + operationProperty);
179         pattern = dateField.getText();
180     }
181     
182     return manager.generateSearchTerm(searchProperty, operationProperty, pattern, header);
183     }
184
185     /**
186      * This generates the Properties for the given SearchTerm.
187      */

188     public java.util.Properties JavaDoc generateSearchTermProperties(String JavaDoc rootProperty) {
189       if (Pooka.isDebug())
190     System.out.println("SearchEntryForm: generating SearchTerm property from " + searchFieldCombo.getSelectedItem() + " and " + operationCombo.getSelectedItem() + " using rootProperty " + rootProperty);
191       
192       String JavaDoc searchProperty = (String JavaDoc)(manager.getLabelToPropertyMap().get(searchFieldCombo.getSelectedItem()));
193       String JavaDoc selectedType = Pooka.getProperty(searchProperty + ".type", "");
194       String JavaDoc operationProperty = null;
195       String JavaDoc pattern = null;
196       String JavaDoc header = null;
197       
198       if (selectedType.equalsIgnoreCase(SearchTermManager.HEADER_MATCH)) {
199     operationProperty = (String JavaDoc)(manager.getLabelToOperationMap().get(headerOperationCombo.getSelectedItem()));
200     if (Pooka.isDebug())
201       System.out.println("using " + searchProperty + ", " + operationProperty);
202     pattern = headerValueTextField.getText();
203     header = headerNameTextField.getText();
204     
205       } else if (selectedType.equalsIgnoreCase(SearchTermManager.STRING_MATCH)) {
206     operationProperty = (String JavaDoc)(manager.getLabelToOperationMap().get(operationCombo.getSelectedItem()));
207     if (Pooka.isDebug())
208       System.out.println("using " + searchProperty + ", " + operationProperty);
209         pattern = textField.getText();
210
211     } else if (selectedType.equalsIgnoreCase(SearchTermManager.BOOLEAN_MATCH)) {
212         operationProperty = (String JavaDoc)(manager.getLabelToOperationMap().get(booleanValueCombo.getSelectedItem()));
213         if (Pooka.isDebug())
214         System.out.println("using " + searchProperty + ", " + operationProperty);
215         pattern = null;
216
217     } else if (selectedType.equalsIgnoreCase(SearchTermManager.DATE_MATCH)) {
218         operationProperty = (String JavaDoc)(manager.getLabelToOperationMap().get(dateComparisonCombo.getSelectedItem()));
219         if (Pooka.isDebug())
220         System.out.println("using " + searchProperty + ", " + operationProperty);
221         pattern = dateField.getText();
222     }
223     
224     Properties returnValue = new java.util.Properties JavaDoc();
225     if (searchProperty != null)
226         returnValue.setProperty(rootProperty + ".searchTerm", searchProperty);
227     if (operationProperty != null)
228         returnValue.setProperty(rootProperty + ".operation", operationProperty);
229     if (pattern != null)
230         returnValue.setProperty(rootProperty + ".pattern", pattern);
231
232     if (header != null)
233         returnValue.setProperty(rootProperty + ".header", header);
234     returnValue.setProperty(rootProperty + ".type", "single");
235
236     return returnValue;
237     }
238
239     /**
240      * Sets the value of this SearchTerm to the one defined by the
241      * given property.
242      */

243     public void setTermValue(String JavaDoc rootProperty, VariableBundle bundle) {
244     if (Pooka.isDebug())
245         System.out.println("SearchEntryForm: setting SearchTerm value to that defined by " + rootProperty);
246     
247     String JavaDoc searchProperty = bundle.getProperty(rootProperty + ".searchTerm", "Search.searchTerms.Subject");
248     if (Pooka.isDebug())
249         System.out.println("getting searchProperty from " + rootProperty + ".searchTerm; value is " + searchProperty);
250     
251     String JavaDoc selectedType = Pooka.getProperty(searchProperty + ".type", "");
252     if (Pooka.isDebug())
253         System.out.println("getting selectedType from " + searchProperty + ".type; value is " + selectedType);
254     String JavaDoc typeLabel = Pooka.getProperty(searchProperty + ".label", "Subject");
255     if (Pooka.isDebug())
256         System.out.println("getting typeLabel from " + searchProperty + ".label; value is " + typeLabel);
257     String JavaDoc operationProperty = bundle.getProperty(rootProperty + ".operation", "Search.operations.Contains");
258     if (Pooka.isDebug())
259         System.out.println("getting operationProperty from " + rootProperty + ".operation; value is " + operationProperty);
260     String JavaDoc operationLabel = Pooka.getProperty(operationProperty + ".label");
261     if (Pooka.isDebug())
262         System.out.println("got operationLabel: " + operationLabel);
263     
264     String JavaDoc pattern = bundle.getProperty(rootProperty + ".pattern", "");
265     if (Pooka.isDebug())
266         System.out.println("got pattern from " + rootProperty + ".pattern; value is " + pattern);
267
268     String JavaDoc header = bundle.getProperty(rootProperty + ".header", "");
269     if (Pooka.isDebug())
270         System.out.println("got header from " + rootProperty + ".header; value is " + header);
271     
272     if (selectedType.equalsIgnoreCase(SearchTermManager.HEADER_MATCH)) {
273         if (Pooka.isDebug())
274         System.out.println("selectedType is header_match.");
275         headerOperationCombo.setSelectedItem(operationLabel);
276         headerValueTextField.setText(pattern);
277         headerNameTextField.setText(header);
278
279     } else if (selectedType.equalsIgnoreCase(SearchTermManager.STRING_MATCH)) {
280         if (Pooka.isDebug())
281         System.out.println("selectedType is string_match.");
282         operationCombo.setSelectedItem(operationLabel);
283         textField.setText(pattern);
284     } else if (selectedType.equalsIgnoreCase(SearchTermManager.BOOLEAN_MATCH)) {
285         if (Pooka.isDebug())
286         System.out.println("selectedType is boolean_match.");
287         booleanValueCombo.setSelectedItem(operationLabel);
288     } else if (selectedType.equalsIgnoreCase(SearchTermManager.DATE_MATCH)) {
289         if (Pooka.isDebug())
290         System.out.println("selectedType is date_match.");
291         dateComparisonCombo.setSelectedItem(operationLabel);
292         dateField.setText(pattern);
293     }
294     
295     searchFieldCombo.setSelectedItem(typeLabel);
296     
297     }
298
299     /**
300      * This enables or disables the SearchEntryForm.
301      */

302     public void setEnabled(boolean newValue) {
303     searchFieldCombo.setEnabled(newValue);
304
305     stringMatchPanel.setEnabled(newValue);
306     operationCombo.setEnabled(newValue);
307     textField.setEnabled(newValue);
308
309     booleanValueCombo.setEnabled(newValue);
310
311     dateComparisonCombo.setEnabled(newValue);
312     dateField.setEnabled(newValue);
313
314     headerMatchPanel.setEnabled(true);
315     headerOperationCombo.setEnabled(newValue);
316     headerNameTextField.setEnabled(newValue);
317     headerValueTextField.setEnabled(newValue);
318     }
319
320     /**
321      * This handles the switch of the selectionPanel when the searchFieldCombo
322      * value changes.
323      */

324     public void itemStateChanged(java.awt.event.ItemEvent JavaDoc e) {
325     String JavaDoc selectedString = (String JavaDoc)(manager.getLabelToPropertyMap().get(searchFieldCombo.getSelectedItem()));
326     
327     String JavaDoc selectedType = Pooka.getProperty(selectedString + ".type", "");
328     layout.show(selectionPanel, selectedType);
329     }
330 }
331
332
333
334
335
Popular Tags