KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.ca.directory.jxplorer.search;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.event.*;
6 import java.util.*;
7 import java.util.logging.Logger JavaDoc;
8
9
10 import com.ca.commons.cbutil.*;
11 import com.ca.directory.jxplorer.*;
12 import com.ca.directory.jxplorer.broker.StopMonitor;
13 import com.ca.commons.naming.DN;
14
15 public class SearchBar extends JToolBar
16 {
17
18     /**
19      * This sets up the quick search tool bar. It acts as a quick GUI to allow common, simple searches to be entered,
20      * and calls SearchExecute to do the real work.
21      */

22     JXplorer jxplorer;
23     StopMonitor stopMonitor;
24
25     int lastQuickSearchSelection = 0;
26
27     private static Logger JavaDoc log = Logger.getLogger(SearchBar.class.getName());
28
29     public SearchBar(JXplorer jxplorer)
30     {
31         super();
32         this.jxplorer = jxplorer;
33
34         setFloatable(false);
35
36         final String JavaDoc attFile = "quicksearch.txt";
37
38         setSize(750, 10);
39
40         final CBButton search = new CBButton(CBIntText.get("Quick Search"), CBIntText.get("Click here to perform the search."));
41         search.setPreferredSize(new Dimension(115, 20));
42
43         ButtonRegister br = JXplorer.getButtonRegister();
44         br.registerItem(br.SEARCH, search);
45
46         jxplorer.getRootPane().setDefaultButton(search); //TE: Sets the search button as the default - i.e for when the user hits the 'enter' key.
47

48         final CBJComboBox searchAttribute; // the attribute to search on
49
final CBJComboBox searchFtn; // the search function to use
50
final JTextField searchFilter; // the user's expression
51

52         //XXX read these from a parameter file
53
String JavaDoc[] selections = null;
54         try
55         {
56             selections = CBUtility.readStringArrayFile(attFile);
57         }
58         catch (Exception JavaDoc e)
59         {
60             selections = null;
61         }
62
63         if ((selections == null) || (selections.length == 0))
64             selections = new String JavaDoc[]{"cn", "sn", "description", "telephoneNumber", "postalCode", "address"};
65
66         searchAttribute = new CBJComboBox(selections);
67         searchAttribute.setEditable(true);
68         searchAttribute.setPreferredSize(new Dimension(150, 20));
69
70         searchAttribute.setToolTipText(CBIntText.get("Select a search attribute, or type in a new one (and press enter)."));
71         add(searchAttribute);
72
73         String JavaDoc[] ftns = new String JavaDoc[]{"=", "~=", ">=", "<=", "!(=)"};
74         searchFtn = new CBJComboBox(ftns);
75         searchFtn.setEditable(false);
76
77         searchFtn.setToolTipText(CBIntText.get("Specify the matching relationship for your search."));
78         add(searchFtn);
79
80         searchFilter = new JTextField();
81
82         searchFilter.setToolTipText(CBIntText.get("Place the value to match here (you can use wildcards such as '*')."));
83         add(searchFilter);
84
85         search.setToolTipText(CBIntText.get("Search from your currently selected node using the searchBar fields."));
86
87         add(search);
88
89 /*
90         stopMonitor = jxplorer.getStopMonitor();
91         CBButton stopMonitorButton = new CBButton(CBIntText.get("Stop"), new ImageIcon(jxplorer.getProperty("dir.images") + "stop.gif"));
92         stopMonitorButton.setDisabledSelectedIcon(new ImageIcon(jxplorer.getProperty("dir.images")+"stop_disabled.gif"));
93         stopMonitorButton.setEnabled(false);
94         stopMonitorButton.setToolTipText(CBIntText.get("Stop the current action"));
95         stopMonitorButton.setAlignmentY(Component.BOTTOM_ALIGNMENT);
96         add(stopMonitorButton);
97 */

98 //XXX jxplorer.jndiBroker.setStopMonitor(stopMonitor);
99
// jxplorer.mainPane.add(searchBar, BorderLayout.NORTH);
100

101         final JXplorer jx = jxplorer;
102
103         search.addActionListener(new ActionListener()
104         {
105             public void actionPerformed(ActionEvent e)
106             {
107                 String JavaDoc ftn = searchFtn.getSelectedItem().toString();
108                 String JavaDoc filter = "(" + searchAttribute.getSelectedItem();
109
110                 if ("!(=)".equals(ftn))
111                     filter = "(!" + filter + "=" + searchFilter.getText() + "))";
112                 else if ("rfc2254".equals(searchAttribute.getSelectedItem())) // Allows users to enter complex search strings in the Search Bar.
113
filter = searchFilter.getText();
114                 else
115                     filter += ftn + searchFilter.getText() + ")";
116
117                 DN base = jx.getTree().getCurrentDN();
118                 if (base == null)
119                     base = jx.getTree().getRootDN();
120
121                 String JavaDoc aliasOption = "always";
122                 log.info("Setting search alias option to: [" + aliasOption + "]");
123                 JXplorer.setProperty("option.ldap.searchAliasBehaviour", aliasOption);
124
125                 SearchExecute.run(jx.getSearchTree(), base, filter, new String JavaDoc[]{"objectClass"}, 2, jx.getSearchBroker());
126                 jx.getTreeTabPane().setSelectedComponent(jx.getResultsPanel());
127             }
128         });
129
130         searchAttribute.addActionListener(new ActionListener()
131         {
132             public void actionPerformed(ActionEvent e)
133             {
134                 boolean newItem = true;
135                 boolean itemDeleted = false;
136                 String JavaDoc selection = searchAttribute.getSelectedItem().toString();
137
138                 if (selection.length() == 0)
139                 {
140                     int removePos = (lastQuickSearchSelection < searchAttribute.getItemCount()) ? lastQuickSearchSelection : -1;
141                     if (removePos < 0) return;
142                     searchAttribute.removeItemAt(removePos);
143                 }
144                 lastQuickSearchSelection = searchAttribute.getSelectedIndex();
145                 String JavaDoc[] values = new String JavaDoc[searchAttribute.getItemCount() + 1];
146                 int j = 0;
147                 for (int i = 0; i < searchAttribute.getItemCount(); i++)
148                 {
149                     String JavaDoc searchAtt = searchAttribute.getItemAt(i).toString();
150                     if (selection.equals(searchAtt))
151                         newItem = false;
152
153                     if ((searchAtt != null) && (searchAtt.length() > 0))
154                     {
155                         if (isAttributeValid(searchAtt)) //TE: check that it doesn't contain any spaces.
156
values[j++] = searchAtt;
157                     }
158                     else
159                         itemDeleted = true;
160                 }
161
162                 if (newItem) // a new item has been added.
163
{
164                     if (isAttributeValid(selection)) //TE: check that it doesn't contain any spaces.
165
values[j++] = selection;
166                 }
167
168                 if (itemDeleted || newItem) // write updated list to disk and update combo box
169
{
170                     for (int i = 0; i < values.length; i++)
171                         if ((values[i] != null) && (values[i].toString().length() == 0))
172                             values[i] = null;
173
174                     Object JavaDoc[] trimmedArray = CBArray.trimNulls(values);
175                     String JavaDoc[] trimmedStrings = new String JavaDoc[trimmedArray.length];
176                     searchAttribute.removeAllItems();
177
178                     for (int i = 0; i < trimmedArray.length; i++)
179                         trimmedStrings[i] = trimmedArray[i].toString();
180
181                     Arrays.sort(trimmedStrings);
182
183                     for (int i = 0; i < trimmedArray.length; i++)
184                         searchAttribute.addItem(trimmedStrings[i]);
185
186                     CBUtility.writeStringArrayFile(attFile, trimmedStrings);
187
188                     searchAttribute.setSelectedItem(selection);
189                 }
190             }
191         });
192
193 /*
194         stopMonitorButton.addActionListener( new ActionListener()
195         {
196             public void actionPerformed(ActionEvent e)
197             {
198                 stopMonitor.setVisible(true);
199             }
200         });
201 */

202     }
203
204     /**
205      * Checks if a given attribute is valid by searching it for a space i.e. a string 'hello colour' would return false,
206      * whereas a string 'hello' would return true. Logs a message if the attribute is not valid.
207      * @param attr the string that is being check for a space.
208      */

209     public boolean isAttributeValid(String JavaDoc attr)
210     {
211         if (attr.indexOf(" ") > -1)
212         {
213             log.warning("The value '" + attr + "' in the Search Bar is not a valid attribute."
214                     + " An attribute cannot contain a space in it's name, therefore this value will not be saved in the 'quicksearch.txt' property file.");
215             return false;
216         }
217         else
218             return true;
219     }
220 }
Popular Tags