KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > search > JoinFilterPanel


1 package com.ca.directory.jxplorer.search;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.util.*;
6 import java.util.logging.Logger JavaDoc;
7 import java.util.logging.Level JavaDoc;
8 import javax.swing.*;
9
10 import com.ca.commons.cbutil.*;
11
12
13
14 /**
15 * Creates a scrollable panel that can add and remove rows that are used to join raw filters or filters
16 * that are a combination of filter. Each row consists of a filter combo box that contains the names of
17 * all possible filters that can be joined (obtained from search_filter.txt) and an edit button which is used
18 * to display the filter of a particular row. If there are more than one row, an And/Or combo is added to the second
19 * row and and And/Or labels to all rows there after. The And/Or labels always show the value of the And/Or combo box.
20 * <p>
21 * This idea behind this class is that a user can join existing filters. The filters can be raw filters or filters
22 * consisting of already joined filters. This enables the user to construct complex LDAP filters.
23 * <p>
24 * This panel is controlled by the SearchGUI class. I.e. the SearchGUI class tells this class to add or remove a row or
25 * to edit or load a certain filter. It also feeds back to the SearchGUI the values of it's components when required.
26 * @author Trudi.
27 */

28
29 public class JoinFilterPanel extends CBPanel
30 {
31     CBPanel panel = new CBPanel();
32     JCheckBox notCheckBox;
33     static final String JavaDoc[] andOrArray = new String JavaDoc[] {CBIntText.get("And"), CBIntText.get("Or")};
34     static final int AND=0, OR=1;
35     JLabel[] andOrLabel = new JLabel[50];
36     CBJComboBox[] filterCombo = new CBJComboBox[50];
37     CBJComboBox andOrCombo;
38     CBButton[] btnEdit = new CBButton[50];
39     int rowCount=0, labelCount=0;
40     SearchModel searchModel = new SearchModel();
41     
42     private static Logger JavaDoc log = Logger.getLogger(JoinFilterPanel.class.getName());
43
44     
45    /**
46     * Constructor that sets up the Join tab with a not check box, and the first row e.g a filter combo and an edit button.
47     * @param btnEdit the edit button that is added to each row.
48     * .
49     */

50         
51     public JoinFilterPanel(CBButton btnEdit)
52     {
53         notCheckBox = new JCheckBox(CBIntText.get("Not"));
54         notCheckBox.setToolTipText(CBIntText.get("Not this filter"));
55         
56         makeLight();
57         add(notCheckBox);
58
59         newLine();
60     
61         makeHeavy();
62                 
63         add(new JScrollPane(panel));
64         makeLight();
65         
66         addFilterRow(btnEdit);
67     }
68     
69     
70     
71    /**
72     * <p>Adds a row or line to the Join tab. A line consists of a filter combo and an edit button. If the second
73     * row is added, an And/Or combo is added, if there are any more rows that are added after the second an And/Or
74     * label is added with the value of the And/Or combo. If the And/Or combo is changed by the user the And/Or
75     * labels are updated to reflect the change via a listener on the And/Or combo.</p>
76     *
77     * <p>The components in a row are added according to row number for example if the second row is being added, a
78     * filter combo is created and stored in the filter combo array at that row position.</p>
79     * @param btnEdit the edit button to be added to the row.
80     * .
81     */

82         
83     protected void addFilterRow(CBButton btnEdit)
84     {
85         if (rowCount < 50)
86         {
87             panel.newLine();
88             panel.makeLight();
89             
90             /*
91              * The first row has an 'invisible' combo box so the layout stays regular
92              */

93              
94             if (rowCount==0)
95             {
96                 panel.add(andOrCombo = new CBJComboBox(andOrArray)); //TE: combo box ('and', 'or').
97
andOrCombo.setPreferredSize(new Dimension(50, 20));
98                 andOrCombo.setVisible(false);
99             }
100             
101             /*
102              * The second row has the active combo box that the user can change.
103              */

104              
105             else if (rowCount==1)
106             {
107                 panel.add(andOrCombo= new CBJComboBox(andOrArray)); //TE: combo box ('and', 'or').
108

109                 andOrCombo.addActionListener(new ActionListener(){ //TE: add an action listener that updates the And/Or labels.
110
public void actionPerformed(ActionEvent e){
111                         try
112                         {
113                             for(int i=0; i<labelCount;i++)
114                                 andOrLabel[i].setText(andOrCombo.getSelectedItem().toString());
115                         }
116                         catch(Exception JavaDoc ee)
117                         {
118                             log.log(Level.WARNING, "Problem updating the 'And/Or' labels in the Join tab of the search dialog:", ee);
119                         }
120                 }});
121                 andOrCombo.setPreferredSize(new Dimension(50, 20));
122             }
123             
124             /*
125              * Any subsequent rows simply have labels that 'echo' the status of the combo box on the second row.
126              */

127              
128             else if (rowCount >1)
129             {
130                 andOrLabel[labelCount] = new JLabel(); //TE the and/or label.
131
try
132                 {
133                     andOrLabel[labelCount].setText(andOrCombo.getSelectedItem().toString());
134                 }
135                 catch(Exception JavaDoc e)
136                 {
137                     andOrLabel[labelCount].setText(" ");
138                     log.log(Level.WARNING, "Problem updating the 'And/Or' labels in the Join tab of the search dialog:", e);
139                 }
140                 panel.add(andOrLabel[labelCount]);
141                 labelCount++; //TE: keep the label counter up to date.
142
}
143
144             panel.makeWide();
145                 
146             ArrayList list = searchModel.getFilterNames(SearchModel.BUILDJOIN);
147             
148             Object JavaDoc listOb[] = list.toArray();
149             Arrays.sort(listOb, new SearchModel.StringComparator()); //TE: sort the list alphabetically.
150

151             panel.add(filterCombo[rowCount] = new CBJComboBox(listOb)); //TE: the attribute combo ('cn', 'sn' etc).
152
filterCombo[rowCount].setPreferredSize(new Dimension(140, 20));
153             filterCombo[rowCount].setRenderer(new CBBasicComboBoxRenderer(listOb));
154             
155             panel.makeLight();
156             
157             panel.add(btnEdit);
158
159             rowCount++; //TE: keep the row counter up to date.
160
panel.revalidate();
161         }
162     }
163     
164
165
166    /**
167     * Updates the all filter combo's and their renders with any new filters that have usually been added
168     * in the Build tab.
169     * @param item the name of the filter to add to the combo boxes.
170     * .
171     */

172
173     protected void updateFilterCombo(String JavaDoc item)
174     {
175         ArrayList list = searchModel.getFilterNames(SearchModel.ALLFILTERS);
176         list.add(item);
177         
178         for(int i=0; i<rowCount; i++)
179         {
180             filterCombo[i].setRenderer(new CBBasicComboBoxRenderer(list.toArray()));
181             filterCombo[i].addItem(item);
182         }
183     }
184
185
186     
187    /**
188     * Removes the last line from the join filter tab e.g the filter combo, edit button and andOr combo/label.
189     * @param btnEdit the edit button for the line or row that is being removed.
190     * .
191     */

192         
193     protected void removeFilterRow(CBButton btnEdit)
194     {
195         if (rowCount > 1)
196         {
197             rowCount--; //TE: keep the row counter up to date.
198

199             if(rowCount==1)
200                 panel.remove(andOrCombo);
201             
202             panel.remove(filterCombo[rowCount]);
203             panel.remove(btnEdit);
204             
205             if (rowCount>1)
206             {
207                 labelCount--; //TE: keep the label counter up to date.
208
panel.remove(andOrLabel[labelCount]);
209             }
210             
211             panel.repaint();
212         }
213
214         panel.revalidate();
215     }
216     
217     
218     
219    /**
220     * Checks if the filter is valid. A filter is not valid if any attribute or function
221     * combo contains an empty string or a null.
222     * returns true if all combo contain a value, false otherwise.
223     * .
224     */

225         
226     protected boolean isFilterValid()
227     {
228         try
229         {
230             for(int i=0; i<rowCount; i++)
231             {
232                 String JavaDoc name = filterCombo[i].getSelectedItem().toString();
233                 
234                 if(name.trim().length()<=0)
235                     return false; //TE: check if the attribute combo has a value.
236

237                 if (rowCount>=2)
238                 {
239                     String JavaDoc andOr = andOrCombo.getSelectedItem().toString();
240                     
241                     if(andOr.trim().length()<=0) //TE: check if the and/or combo has a value.
242
return false;
243                 }
244             }
245         }
246         catch(Exception JavaDoc e)
247         {
248             return false;
249         }
250         return true;
251     }
252         
253
254
255    /**
256     * Returns the current filter that is displayed in the Join tab by getting the values of
257     * each component.
258     * @return the filter value (not an LDAP filter, something more like: '&JXFilter.myFilter1JXFilter.myFilter2').
259     * .
260     */

261     
262     protected String JavaDoc getFilter()
263     {
264         StringBuffer JavaDoc buffy = new StringBuffer JavaDoc();
265
266         for(int i=0;i<rowCount;i++) //TE: append the value in each filter combo e.g. 'JXFilter.myFilter1JXFilter.myFilter2'.
267
buffy.append("JXFilter." + filterCombo[i].getSelectedItem().toString());
268             
269         if (rowCount>=2)
270         {
271             switch (andOrCombo.getSelectedIndex())
272             {
273                 case AND: { buffy.insert(0, "&"); break; } //TE: insert '&' (AND).
274
case OR : { buffy.insert(0, "|"); break; } //TE: insert '|' (OR).
275
}
276         }
277         
278         if (notCheckBox.isSelected())
279             buffy.insert(0, "!"); //TE: insert '!'.
280

281         return buffy.toString();
282     }
283         
284     
285     
286    /**
287     * Returns the value of the filter combo box at a specified row.
288     * @param row the row number of the combo box that the value is to be returned from.
289     * @return the value that is selected in the combo box.
290     * .
291     */

292     
293     protected String JavaDoc getFilterComboValue(int row)
294     {
295         try
296         {
297             return filterCombo[row].getSelectedItem().toString();
298         }
299         catch(Exception JavaDoc e)
300         {
301             return null;
302         }
303     }
304     
305
306     
307    /**
308     * Attempts to display a filter usually after someone has selected to load a filter via the load dialog
309     * or via the edit button in the search dialog. This method needs to set values for the andOr combo and
310     * labels, the not check box, and the filter combos.
311     * @param list a list of the names of the filters e.g.[myFilter1, myFilter2].
312     * @param value the value of the filter being displayed e.g. '&JXFilter.myFilter1JXFilter.myFilter2'.
313     * return true if the display was successful, false otherwise.
314     * .
315     */

316     
317     protected boolean displayFilter(ArrayList list, String JavaDoc value)
318     {
319         boolean not = value.startsWith("!"); //TE: do we need to tick the not check box?
320
int andOr=-1;
321         if(value.indexOf("&")<2 && value.indexOf("&")>-1) //TE: do we display AND in the and/or combo box or...
322
andOr=AND;
323         else if(value.indexOf("|")<2 && value.indexOf("|")>-1) //TE: ...do we display OR?
324
andOr=OR;
325         else
326             return false;
327         
328         setItems(not, andOr); //TE: set the check box and the and/or combo box.
329

330         String JavaDoc[] names = (String JavaDoc[])list.toArray(new String JavaDoc[list.size()]); //TE: convert the list array to a string array.
331

332         for(int i=0; i<names.length; i++) //TE: set the filter combo boxes with the appropriate filter names.
333
setItems(names[i], i);
334         
335         return true;
336     }
337         
338     
339     
340    /**
341     * Sets the filter combo with the supplied string (or filter) at the supplied row.
342     * @param filter the value to displayed in the filter combo.
343     * @param row the row number of the combo box that the value is to be set.
344     * .
345     */

346         
347     protected void setItems(String JavaDoc filter, int row)
348     {
349         filterCombo[row].setSelectedItem(filter);
350         panel.revalidate();
351     }
352     
353     
354     
355    /**
356     * Selects or de-selects the not check box depending on the value supplied and
357     * sets the and/or combo to the index supplied.
358     * @param not the state of the not check box.
359     * @param index the index that the and/or combo is to be set to.
360     * .
361     */

362     
363     protected void setItems(boolean not, int index)
364     {
365         notCheckBox.setSelected(not);
366         if(index==-1)
367             index=0; //TE: just in case something stuffed up, display AND.
368
andOrCombo.setSelectedIndex(index);
369         panel.revalidate();
370     }
371 }
372
373
Popular Tags