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 ; 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 22 JXplorer jxplorer; 23 StopMonitor stopMonitor; 24 25 int lastQuickSearchSelection = 0; 26 27 private static Logger 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 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); 48 final CBJComboBox searchAttribute; final CBJComboBox searchFtn; final JTextField searchFilter; 52 String [] selections = null; 54 try 55 { 56 selections = CBUtility.readStringArrayFile(attFile); 57 } 58 catch (Exception e) 59 { 60 selections = null; 61 } 62 63 if ((selections == null) || (selections.length == 0)) 64 selections = new String []{"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 [] ftns = new String []{"=", "~=", ">=", "<=", "!(=)"}; 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 98 101 final JXplorer jx = jxplorer; 102 103 search.addActionListener(new ActionListener() 104 { 105 public void actionPerformed(ActionEvent e) 106 { 107 String ftn = searchFtn.getSelectedItem().toString(); 108 String filter = "(" + searchAttribute.getSelectedItem(); 109 110 if ("!(=)".equals(ftn)) 111 filter = "(!" + filter + "=" + searchFilter.getText() + "))"; 112 else if ("rfc2254".equals(searchAttribute.getSelectedItem())) 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 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 []{"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 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 [] values = new String [searchAttribute.getItemCount() + 1]; 146 int j = 0; 147 for (int i = 0; i < searchAttribute.getItemCount(); i++) 148 { 149 String 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)) values[j++] = searchAtt; 157 } 158 else 159 itemDeleted = true; 160 } 161 162 if (newItem) { 164 if (isAttributeValid(selection)) values[j++] = selection; 166 } 167 168 if (itemDeleted || newItem) { 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 [] trimmedArray = CBArray.trimNulls(values); 175 String [] trimmedStrings = new String [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 202 } 203 204 209 public boolean isAttributeValid(String 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 |