KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.pooka.gui.search;
2 import net.suberic.pooka.*;
3 import net.suberic.util.VariableBundle;
4 import javax.swing.*;
5 import java.util.*;
6 import java.awt.event.*;
7 import java.awt.FlowLayout JavaDoc;
8 import javax.mail.search.SearchTerm JavaDoc;
9
10 /**
11  * This is a full panel for making search options. This panel combines lots
12  * of SearchEntryForms with some controls for adding and removing
13  * SearchEntryForms. getSearchTerm() returns the composite SearchTerm
14  * made from all of the SearchEntryForms.
15  */

16 public class SearchEntryPanel extends JPanel {
17
18     public static int FIRST = -1;
19     public static int AND = 0;
20     public static int OR = 1;
21
22     public static String JavaDoc AND_LABEL = Pooka.getProperty("Search.button.and.label", "And");
23     public static String JavaDoc OR_LABEL = Pooka.getProperty("Search.button.or.label", "Or");
24     
25     JPanel conditionPanel;
26     JPanel entryPanel;
27     JScrollPane entryScrollPane;
28     JButton buttonOne, buttonTwo;
29     Vector searchTerms = new Vector();
30     SearchTermManager manager;
31
32     class SearchEntryPair {
33     SearchEntryForm form;
34     SearchConnector connector;
35
36     public SearchEntryPair(SearchEntryForm newForm, int newType) {
37         form = newForm;
38         connector = new SearchConnector(newType);
39     }
40     }
41
42     class SearchConnector {
43     
44     JComboBox list;
45     SearchConnector(int newType) {
46         String JavaDoc[] choices = new String JavaDoc[2];
47         choices[0] = AND_LABEL;
48         choices[1] = OR_LABEL;
49         list = new JComboBox(choices);
50         
51         if (newType < 2) {
52         
53         list.setSelectedIndex(newType);
54         } else
55         list.setSelectedIndex(0);
56         
57     }
58     
59     public int getType() {
60         return list.getSelectedIndex();
61     }
62
63     public JComboBox getCombo() {
64         return list;
65     }
66
67     }
68
69     /**
70      * Creates a new SearchEntryPanel.
71      */

72     public SearchEntryPanel(SearchTermManager newManager) {
73     manager = newManager;
74     populatePanel();
75     }
76
77     /**
78      * Creates a new SearchEntryPanel populated by the SearchTerm defined
79      * by the given Property.
80      */

81     public SearchEntryPanel(SearchTermManager newManager, String JavaDoc property) {
82     this(newManager, property, Pooka.getResources());
83     }
84
85     /**
86      * Creates a new SearchEntryPanel populated by the SearchTerm defined
87      * by the given Property.
88      */

89     public SearchEntryPanel(SearchTermManager newManager, String JavaDoc property, VariableBundle bundle) {
90     this(newManager);
91     setSearchTerm(property, bundle);
92     }
93
94     
95     /**
96      * Populates the panel with all the appropriate widgets. Called by
97      * the constructor.
98      */

99     public void populatePanel() {
100     this.setLayout(new java.awt.BorderLayout JavaDoc());
101     entryPanel = new JPanel();
102     entryPanel.setLayout(new BoxLayout(entryPanel,BoxLayout.Y_AXIS));
103
104     addSearchEntryForm(FIRST);
105
106     createConditionPanel();
107
108     entryScrollPane = new JScrollPane(entryPanel);
109     int defaultHeight = entryPanel.getPreferredSize().height;
110     entryScrollPane.setPreferredSize(new java.awt.Dimension JavaDoc(entryPanel.getPreferredSize().width + 15, defaultHeight * 5));
111
112     this.add(conditionPanel, java.awt.BorderLayout.SOUTH);
113     this.add(entryScrollPane, java.awt.BorderLayout.CENTER);
114
115     }
116
117     /**
118      * This creates the conditionPanel. This is always the bottom panel,
119      * and contains the and / or buttons. Pressing one of these buttons
120      * will create a new SearchEntryForm.
121      */

122     private void createConditionPanel() {
123     JPanel jp = new JPanel();
124     jp.setLayout(new FlowLayout JavaDoc());
125     
126     buttonOne = new JButton(Pooka.getProperty("Search.button.and.label", "And"));
127     buttonOne.addActionListener(new ActionListener() {
128         public void actionPerformed(ActionEvent e) {
129             addSearchEntryForm(AND);
130         }
131         });
132
133     buttonTwo = new JButton(Pooka.getProperty("Search.button.or.label", "Or"));
134     buttonTwo.addActionListener(new ActionListener() {
135         public void actionPerformed(ActionEvent e) {
136             addSearchEntryForm(OR);
137         }
138         });
139
140     jp.add(buttonOne);
141     jp.add(buttonTwo);
142
143     conditionPanel=jp;
144     }
145
146     public void addSearchEntryForm(int type) {
147     if (type == FIRST) {
148         SearchEntryForm sef = new SearchEntryForm(manager);
149         SearchConnector sc = new SearchConnector(AND);
150         //java.awt.Component blankPanel = Box.createRigidArea(sc.getCombo().getPreferredSize());
151
//blankPanel.setSize(sc.getCombo().getSize());
152
//blankPanel.setPreferredSize(sc.getCombo().getPreferredSize());
153
Box fullPanel = new Box(BoxLayout.X_AXIS);
154         searchTerms.add(new SearchEntryPair(sef, AND));
155         //fullPanel.add(blankPanel);
156
fullPanel.add(sef.getPanel());
157         entryPanel.add(fullPanel);
158     } else {
159         SearchEntryForm sef = new SearchEntryForm(manager);
160         SearchEntryPair pair = new SearchEntryPair(sef, type);
161         searchTerms.add(pair);
162
163         JPanel newSearchPanel = new JPanel();
164         newSearchPanel.add(pair.connector.getCombo());
165         newSearchPanel.add(sef.getPanel());
166         entryPanel.add(newSearchPanel);
167         //entryPanel.add(pair.connector.getCombo());
168
//entryPanel.add(sef.getPanel());
169

170         entryPanel.revalidate();
171         Runnable JavaDoc runMe = new Runnable JavaDoc() {
172             public void run() {
173             JScrollBar vsb = entryScrollPane.getVerticalScrollBar();
174             vsb.setValue(vsb.getMaximum());
175             //entryPanel.repaint();
176
}
177         };
178
179         SwingUtilities.invokeLater(runMe);
180     }
181
182     }
183
184     /**
185      * Returns the SearchTerm specified by this SearchEntryPanel.
186      */

187     public SearchTerm JavaDoc getSearchTerm() throws java.text.ParseException JavaDoc {
188     if (Pooka.isDebug())
189         System.out.println("calling SearchEntryPanel.getSearchTerm()");
190     if (searchTerms.size() > 0) {
191         if (Pooka.isDebug())
192         System.out.println("SearchEntryPanel: searchTerms.size() > 0.");
193         SearchEntryPair pair = (SearchEntryPair) searchTerms.elementAt(0);
194         SearchTerm JavaDoc term = pair.form.generateSearchTerm();
195         if (Pooka.isDebug())
196         System.out.println("SearchEntryPanel: setting term to " + term);
197         for (int i = 1; i < searchTerms.size(); i++) {
198         SearchEntryPair newPair = (SearchEntryPair) searchTerms.elementAt(i);
199         SearchTerm JavaDoc newTerm = newPair.form.generateSearchTerm();
200         if (newPair.connector.getType() == AND) {
201             term = new javax.mail.search.AndTerm JavaDoc(term, newTerm);
202         } else if (newPair.connector.getType() == OR) {
203             term = new javax.mail.search.OrTerm JavaDoc(term, newTerm);
204         }
205         }
206
207         return term;
208     } else
209         return null;
210     }
211
212     /**
213      * This sets the currently depicted SearchTerm to the one defined by
214      * the rootProperty in the given VariableBundle.
215      */

216     public void setSearchTerm(String JavaDoc rootProperty, VariableBundle bundle) {
217     searchTerms = new Vector();
218     entryPanel = new JPanel();
219     entryPanel.setLayout(new BoxLayout(entryPanel,BoxLayout.Y_AXIS));
220     addSearchTermProperty(rootProperty, bundle, FIRST);
221     entryScrollPane.setViewportView(entryPanel);
222     entryPanel.revalidate();
223     }
224
225     /**
226      * Adds a SearchTerm property. This actually goes through and divides
227      * up single vs. compound search terms. addSignleSearchTerm() actually
228      * adds the editor.
229      */

230     private void addSearchTermProperty(String JavaDoc rootProperty, VariableBundle bundle, int type) {
231     String JavaDoc termType = bundle.getProperty(rootProperty + ".type", "simple");
232     if (termType.equalsIgnoreCase("compound")) {
233         Vector subProps = bundle.getPropertyAsVector(rootProperty + ".subTerms", "");
234         int subType = AND;
235         String JavaDoc operation = bundle.getProperty(rootProperty + ".operation", "and");
236         if (operation.equalsIgnoreCase("and"))
237         subType = AND;
238         else
239         subType = OR;
240
241         for (int i = 0; i < subProps.size(); i++) {
242         String JavaDoc nextTerm = (String JavaDoc) subProps.elementAt(i);
243         if (i == 0)
244             addSearchTermProperty(nextTerm, bundle, type);
245         else
246             addSearchTermProperty(nextTerm, bundle, subType);
247         }
248     } else {
249         addSingleSearchTerm(rootProperty, bundle, type);
250     }
251     }
252     
253     /**
254      * Adds a single search term.
255      */

256     private void addSingleSearchTerm(String JavaDoc rootProperty, VariableBundle bundle, int type) {
257     if (type == FIRST) {
258         SearchEntryForm sef = new SearchEntryForm(manager, rootProperty, bundle);
259         SearchConnector sc = new SearchConnector(AND);
260         Box fullPanel = new Box(BoxLayout.X_AXIS);
261         searchTerms.add(new SearchEntryPair(sef, AND));
262         fullPanel.add(sef.getPanel());
263         entryPanel.add(fullPanel);
264     } else {
265         SearchEntryForm sef = new SearchEntryForm(manager, rootProperty, bundle);
266         SearchEntryPair pair = new SearchEntryPair(sef, type);
267         searchTerms.add(pair);
268         
269         JPanel newSearchPanel = new JPanel();
270         newSearchPanel.add(pair.connector.getCombo());
271         newSearchPanel.add(sef.getPanel());
272         entryPanel.add(newSearchPanel);
273     }
274     }
275
276     /**
277      * This returns the defined SearchTerm as a set of Properties, with the
278      * given rootProperty as the root.
279      */

280     public java.util.Properties JavaDoc generateSearchTermProperties(String JavaDoc rootProperty) {
281     java.util.Properties JavaDoc returnValue = new java.util.Properties JavaDoc();
282
283     Vector v = new Vector (searchTerms);
284     addToProperties(v, rootProperty, returnValue);
285     return returnValue;
286     }
287
288     /**
289      * Adds the given properties to the list.
290      */

291     private void addToProperties(SearchEntryPair pair, String JavaDoc rootProperty, java.util.Properties JavaDoc props) {
292     Properties tmpProperties = pair.form.generateSearchTermProperties(rootProperty);
293     Enumeration keys = tmpProperties.keys();
294     while (keys.hasMoreElements()) {
295         String JavaDoc current = (String JavaDoc) keys.nextElement();
296         props.setProperty(current, tmpProperties.getProperty(current));
297     }
298     }
299
300     /**
301      * Adds the searchTerms given by the pairList to the properties list, with
302      * rootProperty as the root property.
303      */

304     private void addToProperties(Vector pairList, String JavaDoc rootProperty, Properties props) {
305     if (pairList.size() == 1) {
306         addToProperties((SearchEntryPair) pairList.remove(0), rootProperty, props);
307     } else {
308         addToProperties((SearchEntryPair) pairList.remove(0), rootProperty + ".term1", props);
309         int type = ((SearchEntryPair) pairList.elementAt(0)).connector.getType();
310         addToProperties(pairList, rootProperty + ".term2", props);
311         props.setProperty(rootProperty + ".type", "compound");
312         props.setProperty(rootProperty + ".subTerms", rootProperty + ".term1:" + rootProperty + ".term2");
313         if (type == AND) {
314         props.setProperty(rootProperty + ".operation", "and");
315         } else {
316         props.setProperty(rootProperty + ".operation", "or");
317         }
318     }
319     }
320
321     /**
322      * Sets whether or not this panel is enabled.
323      */

324     public void setEnabled(boolean newValue) {
325     for (int i = 0; i < searchTerms.size(); i++) {
326         SearchEntryPair currentPair = (SearchEntryPair) searchTerms.elementAt(i);
327         currentPair.form.setEnabled(newValue);
328         currentPair.connector.getCombo().setEnabled(newValue);
329     }
330     buttonOne.setEnabled(newValue);
331     buttonTwo.setEnabled(newValue);
332
333     }
334 }
335
Popular Tags