KickJava   Java API By Example, From Geeks To Geeks.

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


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.Level JavaDoc;
7 import java.util.logging.Logger JavaDoc;
8 import javax.swing.*;
9 import javax.swing.event.*;
10 import javax.swing.border.*;
11
12 import com.ca.directory.jxplorer.*;
13 import com.ca.commons.naming.*;
14 import com.ca.commons.cbutil.*;
15
16 /**
17 * This class creates a dialog that has currently three tabs on it. The first one is for
18 * creating or building filters, the second on is for joining already created filters and the third
19 * one allows the user to enter or paste in a text filter. This class acts as a
20 * a controller between the view (build, join & text tabs) and the model (SearchModel) as well as being a part of
21 * the view itself.
22 * <p>
23 * This class, in brief, sets up a search dialog that can be used to build, load, view, edit, join or save filters aswell
24 * as search an LDAP directory with the filter that the user has created.
25 * @author Trudi.
26 */

27 public class SearchGUI extends CBDialog
28 {
29     final JXplorer jx;
30     protected JTabbedPane tabbedPane;
31     JCheckBox aliasSearchCheckBox, aliasFindingCheckBox;
32     JTextField baseDNTextField, filterNameTextField;
33     CBJComboBox andOrCombo, searchLevelCombo, returnAttributesCombo;
34     static final String JavaDoc[] andOrArray = new String JavaDoc[] {CBIntText.get("And"), CBIntText.get("Or")};
35     static final String JavaDoc[] searchLevelArray = new String JavaDoc[] {CBIntText.get("Search Base Object"), CBIntText.get("Search Next Level"), CBIntText.get("Search Full Subtree"),};
36     static final int BASEOBJECTSEARCH=0, ONELEVELSEARCH=1, FULLSUBTREESEARCH=2;
37     CBButton btnSave, btnLoad, btnMore, btnLess, btnView;
38     BuildFilterPanel build;
39     JoinFilterPanel join;
40     TextFilterPanel text;
41     SearchModel searchModel;
42     String JavaDoc buildName = "Untitled", joinName = "Untitled", textName = "Untitled", dirImages;
43     CBButton[] btnEdit = new CBButton[50];
44     int buttonCounter = 0; //TE: a counter that keeps track of the number of created 'edit' buttons and is used when recalling buttons from the button array..
45
String JavaDoc[] returnAttrs = null;
46
47     private static Logger JavaDoc log = Logger.getLogger(SearchGUI.class.getName());
48
49     //private static ReturnAttributesDisplay rat = null;
50

51    /**
52     * Contructor that sets up the display and initiates the main search objects: SearchModel,
53     * BuildFilterPanel, JoinFilterPanel and TextFilterPanel.
54     * @param baseDN the DN of the currently selected entry (i.e. unless changed is where the search will be conducted from).
55     * @param jxplorer JXplorer.
56     */

57     public SearchGUI(DN baseDN, JXplorer jxplorer)
58     {
59         super(jxplorer, CBIntText.get("Search"), HelpIDs.SEARCH);
60         jx = jxplorer;
61
62         dirImages = JXplorer.getProperty("dir.images");
63         
64         build = new BuildFilterPanel(jx);
65         join = new JoinFilterPanel(getEditButton());
66         text = new TextFilterPanel();
67         
68         buttonCounter++;
69         searchModel = new SearchModel();
70         
71         CBPanel panel = getMainPanel(baseDN);
72         
73         tabbedPane = new JTabbedPane();
74
75         tabbedPane.addTab(CBIntText.get("Build Filter"), new ImageIcon(dirImages+"build.gif"), build, CBIntText.get("Build a filter from scratch."));
76         tabbedPane.addTab(CBIntText.get("Join Filters"), new ImageIcon(dirImages+"join.gif"), join, CBIntText.get("Join filters that have been made in the Build tab."));
77         tabbedPane.addTab(CBIntText.get("Text Filter"), new ImageIcon(dirImages+"text.gif"), text, CBIntText.get("Type or paste a filter into the field in plain text."));
78         
79         OK.setText(CBIntText.get("Search"));
80         
81         display.makeHeavy();
82         display.addln(panel);
83         display.add(tabbedPane);
84         display.makeLight();
85         display.add(getButtonPanel());
86
87         CBButton btnAttrs = new CBButton(CBIntText.get("Return Attrs"), CBIntText.get("Select Returning Attributes."));
88         btnAttrs.addActionListener(new ActionListener(){
89                 public void actionPerformed(ActionEvent e){
90                     ArrayList list = CBListSelector.getSelectedValues(jx, build.getAttributes(), CBIntText.get("Select Returning Attributes"), HelpIDs.SEARCH);
91                     if(list!=null)
92                         returnAttrs = (String JavaDoc[])list.toArray(new String JavaDoc[list.size()]);
93         }});
94                 
95         setSize(550, 400);
96         CBUtility.center(this, jx);
97         
98         /**
99          * This change listener is intended to listen for tab changes.
100          * Updates the filter name as the user flicks between tabs &
101          * enables or disables the controller buttons depending on which
102          * tab is visible.
103          */

104         tabbedPane.addChangeListener(new ChangeListener()
105         {
106             public void stateChanged(ChangeEvent e)
107             {
108                 int index = tabbedPane.getSelectedIndex();
109                 
110                 if (index == 0)
111                 {
112                     filterNameTextField.setText(buildName);
113                     setButtons(true);
114                 }
115                 else if (index == 1)
116                 {
117                     filterNameTextField.setText(joinName);
118                     setButtons(true);
119                 }
120                 else if (index ==2)
121                 {
122                     filterNameTextField.setText(textName);
123                     setButtons(false);
124                 }
125             }
126         });
127     }
128
129    /**
130     * Sets up a panel with the components which display the name of the filter, the base DN,
131     * aliase prefs, search level prefs and the button panel.
132     * @param baseDN the Distinguished Name where the searching is done from. This is added to a text field on this panel.
133     * @return the panel with the components added.
134     */

135     public CBPanel getMainPanel(DN baseDN)
136     {
137         CBPanel panel = new CBPanel();
138         
139         //TE: adds a label & text field for the name of the filter...
140
panel.add(new JLabel(CBIntText.get("Filter Name") + ": "));
141         panel.makeWide();
142         panel.add(filterNameTextField = new JTextField("Untitled"));
143         panel.makeLight();
144         panel.newLine();
145
146         //TE: adds a label & text field for the name of the DN being searched from...
147
panel.add(new JLabel(CBIntText.get("Start Searching From") + ": "));
148         panel.makeWide();
149         if(baseDN == null)
150             panel.add(baseDNTextField = new JTextField(""));
151         else
152             panel.add(baseDNTextField = new JTextField(baseDN.toString()));
153         panel.makeLight();
154         panel.newLine();
155         
156         CBPanel optionsPanel = new CBPanel(); //TE: panel for adding the alias & search level panels to (for layout).
157

158         //TE: alias check boxes...
159
CBPanel aliasPanel = new CBPanel();
160         aliasPanel.setBorder(new TitledBorder(CBIntText.get(" Alias Options ")));
161         
162         aliasPanel.makeWide();
163         aliasPanel.addln(aliasSearchCheckBox = new JCheckBox(CBIntText.get("Resolve aliases while searching.")));
164         aliasSearchCheckBox.setToolTipText(CBIntText.get("Resolve aliases while searching."));
165         aliasPanel.addln(aliasFindingCheckBox = new JCheckBox(CBIntText.get("Resolve aliases when finding base object.")));
166         aliasFindingCheckBox.setToolTipText(CBIntText.get("Resolve aliases when finding base object."));
167
168         //TE: search level combo...
169
CBPanel searchLevelPanel = new CBPanel();
170         searchLevelPanel.setBorder(new TitledBorder(CBIntText.get(" Search Level ")));
171         searchLevelPanel.addln(new JLabel(CBIntText.get("Select Search Level: ")));
172         searchLevelPanel.makeWide();
173         searchLevelPanel.addln(searchLevelCombo = new CBJComboBox(searchLevelArray));
174         searchLevelCombo.setSelectedIndex(FULLSUBTREESEARCH);
175         
176         //TE: put the alias & search level panels on the options panel then add the options panel to the main panel...
177
optionsPanel.add(aliasPanel);
178         optionsPanel.makeWide();
179         optionsPanel.addln(searchLevelPanel);
180         
181         panel.makeWide();
182         panel.addln(optionsPanel);
183     
184         //TE: return attributes combo...
185
CBPanel returnAttrsPanel = new CBPanel();
186         returnAttributesCombo = new CBJComboBox(ReturnAttributesDialog.getSavedListNames());
187         returnAttributesCombo.setSelectedItem(ReturnAttributesDialog.DEFAULT_RETURN_ATTRS);
188     
189         returnAttrsPanel.makeLight();
190         returnAttrsPanel.add(new JLabel(CBIntText.get("Information to retrieve: ")));
191         returnAttrsPanel.makeWide();
192         returnAttrsPanel.addln(returnAttributesCombo);
193         
194         panel.addln(returnAttrsPanel);
195         
196         return panel;
197     }
198
199     /**
200      * Sets the base DN in the base DN text field.
201      * @param baseDN the DN to search from.
202      */

203     public void setBaseDN(DN baseDN)
204     {
205         if(baseDN != null)
206             baseDNTextField.setText(baseDN.toString());
207     }
208
209    /**
210     * Returns a panel with four buttons on it: More, Less, Save & View. The buttons are set up
211     * with listeners and are placed one above the other.
212     * @return the panel with the buttons on it.
213     */

214     public CBPanel getButtonPanel()
215     {
216         CBPanel panel = new CBPanel();
217
218         btnMore = new CBButton(CBIntText.get("More"), CBIntText.get("Add a Line to the search window."));
219         btnMore.addActionListener(new ActionListener(){
220                 public void actionPerformed(ActionEvent e){
221                     if (isFilterValid())
222                     {
223                         if (tabbedPane.getSelectedIndex()==0) //TE: make sure that you are adding rows to the tab that is visible, not to both tabs!
224
{
225                             build.addFilterRow();
226                         }
227                         else if (tabbedPane.getSelectedIndex()==1 && buttonCounter<50)
228                         {
229                             join.addFilterRow(getEditButton());
230                             buttonCounter++; //TE: a counter that keeps track of the number of created 'edit' buttons.
231
}
232                     }
233                     else
234                     {
235                         showMessage(CBIntText.get("There is an error in the filter; there appears to be missing information.\nPlease make sure you have entered all the information for the filter correctly,\nthen try to add more rows."), CBIntText.get("Missing Information"));
236                     }
237         }});
238         
239         btnLess = new CBButton(CBIntText.get("Less"), CBIntText.get("Remove a Line from the search window."));
240         btnLess.addActionListener(new ActionListener(){
241                 public void actionPerformed(ActionEvent e){
242                     if (tabbedPane.getSelectedIndex()==0) //TE: make sure that you are removing rows from the tab that is visible, not from both tabs!
243
{
244                         build.removeFilterRow();
245                     }
246                     else if (tabbedPane.getSelectedIndex()==1 && buttonCounter>1)
247                     {
248                         buttonCounter--;
249                         join.removeFilterRow(btnEdit[buttonCounter]);
250                     }
251         }});
252         
253         btnSave = new CBButton(CBIntText.get("Save"), CBIntText.get("Save this filter."));
254         btnSave.addActionListener(new ActionListener(){
255                 public void actionPerformed(ActionEvent e){
256                     if (isFilterValid())
257                         save();
258                     else
259                         showMessage(CBIntText.get("The filter cannot be constructed; there appears to be missing information.\nPlease make sure you have entered all the information for the filter correctly."), CBIntText.get("Missing Information"));
260         }});
261         
262         btnLoad = new CBButton(CBIntText.get("Load"), CBIntText.get("Load a previously saved filter."));
263         btnLoad.addActionListener(new ActionListener(){
264                 public void actionPerformed(ActionEvent e){
265                     if(tabbedPane.getSelectedIndex()==0)
266                         loadBuild();
267                     else if(tabbedPane.getSelectedIndex()==1)
268                         loadJoin();
269                     else if(tabbedPane.getSelectedIndex()==2)
270                         loadText();
271         }});
272         
273         btnView = new CBButton(CBIntText.get("View"), CBIntText.get("View this search filter as text."));
274         btnView.addActionListener(new ActionListener(){
275                 public void actionPerformed(ActionEvent e){
276                     if (!isFilterValid())
277                         showMessage(CBIntText.get("The filter cannot be constructed; there appears to be missing information.\nPlease make sure you have entered all the information for the filter correctly."), CBIntText.get("Missing Information"));
278                     else if(tabbedPane.getSelectedIndex()==1 && recursiveFilterCheck(null, join.getFilter(), "View")) // We don't care about the name of this filter b/c we are just viewing it.
279
return;
280                     else
281                         showDialog(CBIntText.get("Current Filter"), getLDAPFilter());
282         }});
283
284         panel.makeHigh(); //TE: add a label that it takes up the any remaining space above the buttons, so that the buttons are at the bottom of the panel.
285
panel.addln(new JLabel(" "));
286         panel.makeLight();
287         panel.addln(btnMore);
288         panel.addln(btnLess);
289         panel.addln(btnSave);
290         panel.addln(btnLoad);
291         panel.addln(btnView);
292         
293         return panel;
294     }
295
296    /**
297     * Enables or disables the More, Less and View buttons. We basically don't
298     * want these buttons enabled if the user has selected the Text Filter tab.
299     * @param state the state of the buttons (enabled=true or disabled=false).
300     */

301     protected void setButtons(boolean state)
302     {
303         btnMore.setEnabled(state);
304         btnLess.setEnabled(state);
305         btnView.setEnabled(state);
306     }
307
308    /**
309     * Displays a JOptionPane message with a text area containing the current filter.
310     * The text area is set to do line wrapping and only vertical scrolling.
311     * @param title the heading of the dialog that appears in the title bar.
312     * @param filter the current LDAP filter (or any string to be displayed).
313     */

314     protected void showDialog(String JavaDoc title, String JavaDoc filter)
315     {
316         JTextArea area = new JTextArea(filter);
317         area.setLineWrap(true);
318         area.setWrapStyleWord(true);
319         JScrollPane scrollPane = new JScrollPane(area);
320         scrollPane.setPreferredSize(new Dimension(300,60));
321         scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
322         JOptionPane.showMessageDialog(this, scrollPane, title, JOptionPane.INFORMATION_MESSAGE);
323     }
324
325    /**
326     * Returns an edit button, i.e. a button labelled "Edit", has a dimension of 55x21, has a tooltip "Edit
327     * this filter.", and has an action listener that calls the edit method with it's position (or row number).
328     * @return the configured button.
329     */

330     protected CBButton getEditButton()
331     {
332         btnEdit[buttonCounter] = new CBButton(CBIntText.get("Edit"), CBIntText.get("Edit this filter."));
333
334         btnEdit[buttonCounter].setPreferredSize(new Dimension(55,21));
335         btnEdit[buttonCounter].addActionListener(new ActionListener()
336         {
337             public void actionPerformed(ActionEvent e)
338             {
339                 CBButton item = (CBButton)e.getSource();
340
341                 for(int i=0; i<buttonCounter; i++)
342                 {
343                     if (item == btnEdit[i])
344                         edit(i);
345                 }
346             }
347         });
348         
349         return btnEdit[buttonCounter];
350     }
351     
352    /**
353     * Displays the filter(s) corresponding to the edit button that the user has selected.
354     * The edit buttons live on the join panel. If a user wants to edit a raw filter the raw filter
355     * is displayed in the build panel otherwise if the user wants to edit a filter that is a combination
356     * of filters, the filter is displayed in the join panel.
357     * @param row the row number that the filter name is to be taken from.
358     */

359     protected void edit(int row)
360     {
361         ArrayList list = searchModel.getFilterNames(SearchModel.BUILDFILTER); //TE: get all the raw filter names.
362
String JavaDoc filter = join.getFilterComboValue(row);
363         
364         if (filter==null) //TE: will equal null if nothing to edit.
365
{
366             showMessage(CBIntText.get("There are no filters available to edit"), CBIntText.get("Nothing to Edit"));
367             return;
368         }
369         else
370         {
371             try
372             {
373                 if (list.contains(filter)) //TE: the filter is a raw filter which needs to be displayed in the build filter panel.
374
{
375                     buildName = filter; //TE: set the global variable so that the correct name of the filter is displayed when tabs are changed.
376
tabbedPane.setSelectedIndex(0); //TE: change tabs.
377

378                     build.displayFilter(searchModel.getFilter(filter)); //TE: send the raw filter off to get displayed.
379
}
380                 else //TE: the filter is a combination of filters which need to be displayed in the join filter panel.
381
{
382                     String JavaDoc value = searchModel.getFilter(filter); //TE: get the value of the filter.
383

384                     setNumberOfRows(build.getOccurrences(searchModel.getFilter(filter), "JXFilter")); //TE: ...set up that many number of rows.
385

386                     ArrayList aList = searchModel.getJoinFilterNames(value); //TE: get the names of these filters.
387

388                     if (!join.displayFilter(aList, value))
389                     {
390                         showMessage(CBIntText.get("Your filter cannot be edited."), CBIntText.get("Edit Unsuccessful"));
391                     }
392                     else
393                     {
394                         joinName = filter;
395                         filterNameTextField.setText(joinName); //TE: set the global variable so that the correct name of the filter is displayed.
396
}
397                 }
398             }
399             catch(Exception JavaDoc e) //TE: the user has somehow selected nothing in either the attribute or the function combo.
400
{
401                 showMessage(CBIntText.get("The filter cannot be constructed; there appears to be missing information.\nPlease make sure you have entered all the information for the filter correctly."), CBIntText.get("Missing Information"));
402                 return;
403             }
404         }
405     }
406         
407    /**
408     * Currently used by the loadJoin() and edit() methods to prompt the JoinFilterPanel class to either add or remove
409     * rows. If adding rows is required, the extra number of edit buttons are created before the hand-balling goes off
410     * to the JoinFilterPanel class.
411     * @param rows the number of rows that need to be added.
412     */

413     protected void setNumberOfRows(int rows)
414     {
415         if (buttonCounter > rows) //TE: if there are more buttons than rows required...
416
{
417             while (buttonCounter > rows) //TE: ...delete these buttons and any other components on that row until the correct number of rows are displayed.
418
{
419                 buttonCounter--; //TE: make sure the button counter is updated.
420
join.removeFilterRow(btnEdit[buttonCounter]);
421             }
422         }
423         else if (buttonCounter < rows) //TE: if there are less buttons than rows required...
424
{
425             while (buttonCounter < rows) //TE: ...create and add these buttons and any other required components on that row until the correct number of rows are displayed.
426
{
427                 join.addFilterRow(getEditButton());
428                 buttonCounter++; //TE: make sure the button counter is updated.
429
}
430         }
431     }
432
433    /**
434     * Save the filter to the property file 'search_filter.txt". Checks first to see if the filter exists. If
435     * it does a JOptionPane message asks the user if they want to replace it. If the filter is saved successfully
436     * a confirmation message is displayed again using JOptionPane. This also updates the filter combo of the join
437     * tab so that a user can see changes straight away.
438     */

439     protected void save()
440     {
441         String JavaDoc name = filterNameTextField.getText(); //TE: get the user defined name for the filter.
442
boolean exists = false; //TE: true if the name exists, false otherwise.
443

444         if (name == null || name.trim().length()<=0 || name.equalsIgnoreCase("Untitled"))
445         {
446             showMessage(CBIntText.get("Please enter a name in the 'Filter Name' field for the filter that you are trying to save."), CBIntText.get("Name Required"));
447             return;
448         }
449         else if (name.startsWith("JXFilter"))
450         {
451             showMessage(CBIntText.get("The name ''{0}'' is a reserved name. Please enter a different name.", new String JavaDoc[] {name}), CBIntText.get("Naming Error"));
452             return;
453         }
454         
455         if (searchModel.exists(name)) //TE: if the name exists ask the user if they want to replace it...
456
{
457             int response = JOptionPane.showConfirmDialog(this, CBIntText.get("The name ''{0}'' already exists. Do you want to replace it?", new String JavaDoc[] { name }),
458                 CBIntText.get("Select Filter"), JOptionPane.YES_NO_OPTION );
459
460             if(response != JOptionPane.YES_OPTION)
461                 return;
462             
463             exists = true;
464         }
465         
466         try
467         {
468             if (tabbedPane.getSelectedIndex()==0)
469             {
470                 buildName = name; //TE: update the name of the build filter so the tab change listener knows the correct name of the filter.
471
searchModel.saveFilter(name, getLDAPFilter());
472             }
473             else if (tabbedPane.getSelectedIndex()==1)
474             {
475                 String JavaDoc filter = join.getFilter();
476                 
477                 if(recursiveFilterCheck(name, filter, "Save")) //TE: stop the user from constructing a filter that is recursive.
478
return;
479                                     
480                 joinName = name; //TE: update the name of the join filter so the tab change listener knows the correct name of the filter.
481
searchModel.saveFilter(name, filter);
482             }
483             else if (tabbedPane.getSelectedIndex()==2)
484             {
485                 textName = name; //TE: update the name of the text filter so the tab change listener knows the correct name of the filter.
486
searchModel.saveTextFilter(name, text.getFilter());
487             }
488         }
489         catch(Exception JavaDoc e) //TE: the user has somehow selected nothing in either the attribute or the function combo.
490
{
491             showMessage(CBIntText.get("The filter cannot be constructed; there appears to be missing information.\nPlease make sure you have entered all the information for the filter correctly."), CBIntText.get("Missing Information"));
492             return;
493         }
494             
495         save(name); //TE: save search levels, baseDN etc.
496

497         if(!exists) //TE: update the filter combo if the filter doesn't exist. Otherwise you would need to close the search dialog to see any changes??
498
join.updateFilterCombo(name);
499         
500         jx.getMainMenu().updateSearchMenu(); //TE: updates the Search menu items.
501

502         showMessage(CBIntText.get("Your filter ''{0}'' was saved successfully.", new String JavaDoc[] {name}), CBIntText.get("Successful Save."));
503     }
504
505    /**
506     * Saves the base DN, return attribute list name, the search level and the alias
507     * state to the property file so they can be loaded similar to the filter.
508     * @param name the name of the filter, will be used in the key (e.g. name.baseDN=whatever).
509     */

510     protected void save(String JavaDoc name)
511     {
512         String JavaDoc baseDN = ((baseDNTextField.getText()).trim()).length()<=0 ? ((jx.getTree()).getRootDN()).toString() : baseDNTextField.getText();
513         searchModel.saveValue(name, SearchModel.BASEDN, baseDN);
514         searchModel.saveValue(name, SearchModel.RETATTRS, (returnAttributesCombo.getSelectedItem()).toString());
515         searchModel.saveSearchLevel(name, searchLevelCombo.getSelectedIndex());
516         searchModel.saveAlias(name, SearchModel.FIND, aliasFindingCheckBox.isSelected());
517         searchModel.saveAlias(name, SearchModel.SEARCH, aliasSearchCheckBox.isSelected());
518     }
519
520    /**
521     * Displays a JOptionPane message which has a combo box containing all the possible filters that can be
522     * loaded into the build display. Gets the user selection and hand-balls the loading off to the BuildFilterPanel class.
523     */

524     protected void loadBuild()
525     {
526         ArrayList list = searchModel.getFilterNames(SearchModel.BUILDFILTER); //TE: get the names of raw filters (i.e. (cn=f*)).
527

528         if (list.size()==0)
529         {
530             showMessage(CBIntText.get("There are no filters available to load.") , CBIntText.get("Nothing to Load"));
531             return;
532         }
533         
534         Object JavaDoc listOb[] = list.toArray();
535         Arrays.sort(listOb, new SearchModel.StringComparator()); //TE: sort the list alphabetically.
536

537         CBJComboBox loadCombo = new CBJComboBox(listOb);
538         loadCombo.setRenderer(new CBBasicComboBoxRenderer(listOb));
539         loadCombo.setPreferredSize(new Dimension(140, 20));
540         int response = JOptionPane.showConfirmDialog(this, loadCombo, CBIntText.get("Select Filter"), JOptionPane.OK_CANCEL_OPTION);
541             
542         if (response != JOptionPane.OK_OPTION)
543             return; //TE: the user has probably decided not to load a filter i.e. has clicked 'cancel'.
544

545         if (loadCombo.getSelectedItem()!=null)
546         {
547             String JavaDoc filter=null;
548             try
549             {
550                 filter = searchModel.getFilter(loadCombo.getSelectedItem().toString()); //TE: gets the filter that the user selected.
551
}
552             catch(Exception JavaDoc e) //TE: the user has somehow selected nothing in either the attribute or the function combo.
553
{
554                 showMessage(CBIntText.get("The filter cannot be constructed; there appears to be missing information.\nPlease make sure you have entered all the information for the filter correctly."), CBIntText.get("Missing Information"));
555                 return;
556             }
557             
558             if (!build.displayFilter(filter)) //TE: display the filter. If unsuccessful show message.
559
{
560                 showMessage(CBIntText.get("Your filter cannot be displayed."), CBIntText.get("Load Unsuccessful"));
561             }
562             else
563             {
564                 buildName = loadCombo.getSelectedItem().toString(); //TE: update the name of the join filter so the tab change listener knows the correct name of the filter.
565
filterNameTextField.setText(buildName); //TE: set the name of the filter in the filter name field.
566
}
567         }
568         else
569             showMessage(CBIntText.get("Problem loading; there are no filters selected.") , CBIntText.get("Nothing to Load"));
570         
571         load(loadCombo.getSelectedItem().toString());
572     }
573     
574    /**
575     * Displays a JOptionPane message which has a combo box containing all the possible filters that can be
576     * loaded into the join display. Gets the user selection and creates the edit buttons before hand-balling
577     * the loading off to the JoinFilterPanel class.
578     */

579     protected void loadJoin()
580     {
581         ArrayList list = searchModel.getFilterNames(SearchModel.JOINFILTER); //TE: get the names of filters that are made up of other filters (i.e. not raw filters).
582

583         if (list.size()==0)
584         {
585             showMessage(CBIntText.get("There are no filters available to load.") , CBIntText.get("Nothing to Load"));
586             return;
587         }
588         
589         CBJComboBox loadCombo = new CBJComboBox(list.toArray());
590         loadCombo.setRenderer(new CBBasicComboBoxRenderer(list.toArray()));
591         loadCombo.setPreferredSize(new Dimension(140, 20));
592         int response = JOptionPane.showConfirmDialog(this, loadCombo,
593                                     CBIntText.get("Select Filter"), JOptionPane.OK_CANCEL_OPTION);
594             
595         if (response != JOptionPane.OK_OPTION)
596             return; //TE: the user has probably decided not to load a filter i.e. has clicked 'cancel'.
597

598         if (loadCombo.getSelectedItem()!=null)
599         {
600             String JavaDoc filter = loadCombo.getSelectedItem().toString(); //TE: get the user selected filter from the combo box.
601

602             try
603             {
604                 setNumberOfRows(build.getOccurrences(searchModel.getFilter(filter), "JXFilter")); //TE: add the right number of rows for displaying the filter.
605

606                 list = searchModel.getJoinFilterNames(searchModel.getFilter(filter)); //TE: get a list of the subfilter names.
607

608                 if(!join.displayFilter(list, searchModel.getFilter(filter))) //TE: display the filter. If unsuccessful show message.
609
showMessage(CBIntText.get("Your filter cannot be displayed."), CBIntText.get("Load Unsuccessful"));
610                 else
611                 {
612                     joinName = loadCombo.getSelectedItem().toString(); //TE: update the name of the join filter so the tab change listener knows the correct name of the filter.
613
filterNameTextField.setText(joinName); //TE: set the name of the filter in the filter name field.
614
}
615             }
616             catch(Exception JavaDoc e) //TE: the user has somehow selected nothing in either the attribute or the function combo.
617
{
618                 showMessage(CBIntText.get("The filter cannot be constructed; there appears to be missing information.\nPlease make sure you have entered all the information for the filter correctly."), CBIntText.get("Missing Information"));
619                 return;
620             }
621         }
622         else
623             showMessage(CBIntText.get("Problem loading; there are no filters selected.") , CBIntText.get("Nothing to Load"));
624         
625         load(loadCombo.getSelectedItem().toString());
626     }
627
628    /**
629     * Displays a JOptionPane message which has a combo box containing all the possible filters
630     * that can be loaded into the text display. Gets the user selection and hand-balls
631     * the loading off to the JoinFilterPanel class.
632     */

633     //TE: Perhaps allow the user to load any filter into the text area, not just text filters???
634
protected void loadText()
635     {
636         ArrayList list = searchModel.getFilterNames(SearchModel.TEXTFILTER); //TE: get the names of filters that are made up of other filters (i.e. not raw filters).
637

638         if (list.size()==0)
639         {
640             showMessage(CBIntText.get("There are no filters available to load.") , CBIntText.get("Nothing to Load"));
641             return;
642         }
643         
644         CBJComboBox loadCombo = new CBJComboBox(list.toArray());
645         loadCombo.setRenderer(new CBBasicComboBoxRenderer(list.toArray()));
646         loadCombo.setPreferredSize(new Dimension(140, 20));
647         int response = JOptionPane.showConfirmDialog(this, loadCombo, CBIntText.get("Select Filter"), JOptionPane.OK_CANCEL_OPTION);
648             
649         if (response != JOptionPane.OK_OPTION)
650             return;
651             
652         if (loadCombo.getSelectedItem()!=null)
653         {
654             textName = loadCombo.getSelectedItem().toString(); //TE: get the user selected filter from the combo box.
655
text.displayFilter(searchModel.getTextFilter(textName));
656             filterNameTextField.setText(textName); //TE: set the name of the filter in the filter name field.
657
}
658         else
659             showMessage(CBIntText.get("Problem loading; there are no filters selected.") , CBIntText.get("Nothing to Load"));
660         
661         load(loadCombo.getSelectedItem().toString());
662     }
663     
664    /**
665     * Gets the values of the base dn, search level, return attributes and alias state from the
666     * property file that pertain to the filter being loaded and sets these values in
667     * the GUI.
668     * @param name the name of the filter that is being loaded.
669     */

670     protected void load(String JavaDoc name)
671     {
672         //TE: load search level...
673
int searchLevel = 2;
674         try
675         {
676             searchLevel = Integer.parseInt(searchModel.getValue(name + "." + SearchModel.SEARCHLEVEL));
677             searchLevelCombo.setSelectedIndex(searchLevel);
678         }
679         catch(NumberFormatException JavaDoc e)
680         {
681             searchLevelCombo.setSelectedIndex(2);
682         }
683                 
684         
685         //TE: load base dn...
686
String JavaDoc dn = searchModel.getValue(name + "." + SearchModel.BASEDN);
687         if(dn!=null)
688             baseDNTextField.setText(dn);
689
690         //TE: get the name of the return attribute list. Check that the list name exists then
691
// set it in the combo...
692
String JavaDoc retAttrs = searchModel.getValue(name + "." + SearchModel.RETATTRS);
693         if (retAttrs!=null)
694         {
695             Object JavaDoc temp[] = ReturnAttributesDialog.getSavedListNames();
696             
697             for(int i=0; i<temp.length;i++)
698             {
699                 if (((String JavaDoc)temp[i]).equalsIgnoreCase(retAttrs))
700                 {
701                     returnAttributesCombo.setSelectedItem(temp[i]);
702                     break;
703                 }
704             }
705         }
706
707         //TE: select finding alias checkboxes...
708
String JavaDoc find = searchModel.getValue(name + "." + SearchModel.FIND);
709         if (find!=null)
710         {
711             if(find.equalsIgnoreCase("true"))
712                 aliasFindingCheckBox.setSelected(true);
713             else
714                 aliasFindingCheckBox.setSelected(false);
715         }
716         
717         //TE: select searching alias checkboxes...
718
String JavaDoc search = searchModel.getValue(name + "." + SearchModel.SEARCH);
719         if (search!=null)
720         {
721             if(search.equalsIgnoreCase("true"))
722                 aliasSearchCheckBox.setSelected(true);
723             else
724                 aliasSearchCheckBox.setSelected(false);
725         }
726     }
727     
728    /**
729     * Does a check to see if the filter is valid i.e. it check if there is a value in
730     * each of the combo boxes.
731     * @return false if one or more of the combo boxes does not contain a value, true otherwise.
732     */

733     protected boolean isFilterValid()
734     {
735         if (tabbedPane.getSelectedIndex()==0)
736             return build.isFilterValid();
737         else if (tabbedPane.getSelectedIndex()==1)
738             return join.isFilterValid();
739         else if(tabbedPane.getSelectedIndex()==2)
740             return text.isFilterValid();
741             
742         return false; //TE: something weird with the tabbed panes.
743
}
744
745    /**
746     * Returns the LDAP filter of the currently displayed tabbed pane. This assumes that the build tab is at
747     * position 0, the join tab is at position 1 and the text tab is at position 2.
748     * @return the LDAP filter for example (cn=f*)
749     */

750     protected String JavaDoc getLDAPFilter()
751     {
752         if (tabbedPane.getSelectedIndex()==0)
753             return build.getFilter();
754         else if(tabbedPane.getSelectedIndex()==1)
755             return searchModel.getJoinFilter(join.getFilter());
756         else if(tabbedPane.getSelectedIndex()==2)
757             return text.getFilter();
758             
759         return "";
760     }
761
762    /**
763     * Displays a information message dialog with the text that is supplied and titles the dialog with the title supplied.
764     * @param message the text to be displayed.
765     * @param title the title of this information message dialog.
766     */

767     protected void showMessage(String JavaDoc message, String JavaDoc title)
768     {
769         JOptionPane.showMessageDialog(this, message, title, JOptionPane.INFORMATION_MESSAGE );
770     }
771
772    /**
773     * Sets the search Alias Behaviour in the property file, gets the filter and executes the search & display
774     * before closing the window.
775     */

776     public void doOK()
777     {
778         if (isFilterValid())
779         {
780             // Check if the filter is safe to use (we don't care about the name to begin with b/c we aren't saving)...
781
if(tabbedPane.getSelectedIndex()==1 && recursiveFilterCheck(null, join.getFilter(), "Search"))
782                 return;
783             setAliasOptions();
784             
785             String JavaDoc returnAttrs = (returnAttributesCombo.getSelectedItem()).toString();
786             if (!returnAttrs.equalsIgnoreCase(ReturnAttributesDialog.DEFAULT_RETURN_ATTRS))
787             {
788                 closeSearchGUI();
789                 String JavaDoc[] attrNames = ReturnAttributesDialog.getReturnAttributes(returnAttrs);
790                 searchModel.openRetAttrDisplay(jx, attrNames, (jx.getSearchTree()).getDataSource());
791                 SearchExecute.run(jx.getSearchTree(), new DN(baseDNTextField.getText()), getLDAPFilter(), attrNames, searchLevelCombo.getSelectedIndex(), jx.getSearchBroker()); //TE: the search details.
792
}
793             else
794             {
795                 closeSearchGUI();
796                 SearchExecute.run(jx.getSearchTree(), new DN(baseDNTextField.getText()), getLDAPFilter(),
797                         new String JavaDoc[] {"objectClass"}, searchLevelCombo.getSelectedIndex(), jx.getSearchBroker()); //TE: the search details. SearchExecute.run(jx.getSearchTree(), new DN(baseDNTextField.getText()), getLDAPFilter(), attrNames, searchLevelCombo.getSelectedIndex(), jx.getSearchBroker()); //TE: the search details.
798
}
799             jx.getTreeTabPane().setSelectedComponent(jx.getResultsPanel());
800         }
801         else
802         {
803             showMessage(CBIntText.get("The filter cannot be constructed; there appears to be missing information.\nPlease make sure you have entered all the information for the filter correctly."), CBIntText.get("Missing Information"));
804         }
805     }
806
807     /**
808      * Over writes the parent method to call the closeSearchGUI method.
809      */

810     public void doCancel()
811     {
812         closeSearchGUI();
813     }
814
815     /**
816      * Sets the filter name text to 'Untitled' then
817      * sets the visibility of the dialog to false.
818      */

819     public void closeSearchGUI()
820     {
821         filterNameTextField.setText("Untitled"); //TE: bug 4896.
822
setVisible(false);
823     }
824
825    /**
826     * Determines which search alias behaviour the user has selected in the search GUI.
827     * The combinations are:
828     * <p>
829     * <b>always</b> - both alias check boxes are checked.<br>
830     * <b>never</b> - neither alias check boxes are checked.<br>
831     * <b>finding</b> - only the 'Resolve aliases when finding base object' check box is checked.<br>
832     * <b>searching</b> - only the 'Resolve aliases while searching' check box is checked.
833     * </p>
834     * The search alias option is set in JXplorer's property file under 'option.ldap.searchAliasBehaviour'.
835     */

836     public void setAliasOptions()
837     {
838         String JavaDoc aliasOption = "always";
839         
840         if (!aliasSearchCheckBox.isSelected() && !aliasFindingCheckBox.isSelected())
841             aliasOption = "never";
842         else if (aliasSearchCheckBox.isSelected() && !aliasFindingCheckBox.isSelected())
843             aliasOption = "searching";
844         else if (!aliasSearchCheckBox.isSelected() && aliasFindingCheckBox.isSelected())
845             aliasOption = "finding";
846         
847         log.fine("Setting search alias option to: ["+aliasOption+"]");
848         JXplorer.setProperty("option.ldap.searchAliasBehaviour", aliasOption);
849     }
850
851    /**
852     * Checks if the user is trying to save a copy of the filter into itself, for example
853     * if a filter named t3 contains the filter t3 as part of a join. This check applies only
854     * for the join tab. Constructs an info dialog if the filter is recursive.
855     * @param filterName the user specified name for the filter that is currently being saved.
856     * @param filter the value of the filter for example 'JXFilter.t1JXFilter.t1'.
857     * @param type the source of the filter being constructed for example: view, save or search (used in the info dialog).
858     * @return true if the filter contains itself, false otherwise.
859     */

860     protected boolean recursiveFilterCheck(String JavaDoc filterName, String JavaDoc filter, String JavaDoc type)
861     {
862         if(recursiveFilterCheck(filterName, filter)) //TE: stop the user from constructing a filter that is recursive.
863
{
864             showMessage(CBIntText.get("The filter you are trying to {0} is not valid. You may be trying to construct a filter within itself which will cause an infinite loop.", new String JavaDoc[] {type.toLowerCase()}), CBIntText.get(type + " Error"));
865             return true;
866         }
867         return false;
868     }
869     
870    /**
871     * Checks if the user is trying to save a copy of the filter into itself, for example
872     * if a filter named t3 contains the filter t3 as part of a join. This check applies only
873     * for the join tab.
874     * @param filterName the user specified name for the filter that is currently being saved.
875     * @param filter the value of the filter for example 'JXFilter.t1JXFilter.t1'.
876     * @return true if the filter contains itself, false otherwise.
877     */

878     protected boolean recursiveFilterCheck(String JavaDoc filterName, String JavaDoc filter)
879     {
880         boolean recursive = false;
881         String JavaDoc temp;
882         ArrayList list = searchModel.getJoinFilterNames(filter);
883
884         for(int i=0;i<list.size();i++)
885         {
886             temp = list.get(i).toString();
887             
888             if(filterName != null && filterName.compareTo(temp) == 0)
889             {
890                 recursive = true;
891                 break;
892             }
893             
894             // Go down each level that check there is the same name in the filter to be saved and the children fiters...
895
String JavaDoc value = searchModel.getFilter(temp);
896
897             // check if it is joint filter or not...
898
if(value != null && value.indexOf("JXFilter") != -1)
899                 recursive = recursiveFilterCheck(filterName, value);
900
901             if(recursive)
902                 return true;
903         }
904                     
905         return recursive;
906     }
907 }
908
909
910 //TE: ONE DAY..........
911

912 // class TreeFilterPanel extends CBPanel
913
// {
914
// public TreeFilterPanel()
915
// {
916
// setBackground(new Color(235,255,255));
917
// }
918
// }
919

920
921     
922
Popular Tags