1 19 20 package org.netbeans.modules.xml.xam.ui.search; 21 22 import java.awt.Color ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.awt.event.FocusEvent ; 26 import java.awt.event.FocusListener ; 27 import java.awt.event.KeyEvent ; 28 import java.awt.event.KeyListener ; 29 import java.awt.event.MouseEvent ; 30 import java.awt.event.MouseListener ; 31 import java.util.Collection ; 32 import java.util.Collections ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.Map ; 37 import javax.swing.AbstractButton ; 38 import javax.swing.ButtonModel ; 39 import javax.swing.JCheckBoxMenuItem ; 40 import javax.swing.JMenuItem ; 41 import javax.swing.JPanel ; 42 import javax.swing.JRadioButtonMenuItem ; 43 import javax.swing.JSeparator ; 44 import javax.swing.UIManager ; 45 import javax.swing.event.DocumentEvent ; 46 import javax.swing.event.DocumentListener ; 47 import javax.swing.event.EventListenerList ; 48 import javax.swing.event.PopupMenuEvent ; 49 import javax.swing.event.PopupMenuListener ; 50 import org.openide.util.NbBundle; 51 import org.openide.util.WeakListeners; 52 53 58 public class SearchFieldPanel extends JPanel 59 implements ActionListener , DocumentListener , FocusListener , 60 KeyListener , MouseListener , PopupMenuListener { 61 62 private static final Color MATCH_FAILED_COLOR = new Color (255, 102, 102); 63 64 static final long serialVersionUID = 1L; 65 66 private Map <String , SearchProvider> buttonProviderMap; 67 69 private boolean expectingInput; 70 71 private JCheckBoxMenuItem fromSelectedMenuItem; 72 73 private JCheckBoxMenuItem regexMenuItem; 74 75 private EventListenerList searchListeners = new EventListenerList (); 76 77 80 public SearchFieldPanel() { 81 initComponents(); 82 typesButton.addActionListener(this); 83 searchTextField.addKeyListener(this); 84 searchTextField.addActionListener(this); 85 searchTextField.addFocusListener(this); 86 searchTextField.addMouseListener(this); 87 searchTextField.getDocument().addDocumentListener(this); 88 typesPopupMenu.addPopupMenuListener(this); 89 buttonProviderMap = new HashMap <String , SearchProvider>(); 90 } 91 92 public void actionPerformed(ActionEvent e) { 93 Object src = e.getSource(); 94 if (src == typesButton) { 95 typesPopupMenu.show(typesButton, 0, typesButton.getHeight()); 96 } else if (src == searchTextField) { 97 fireSearchEvent(SearchEvent.Type.COMMENCED); 98 String text = searchTextField.getText(); 100 if (text.length() > 0) { 101 ButtonModel model = typeButtonGroup.getSelection(); 102 SearchProvider provider = buttonProviderMap.get( 103 model.getActionCommand()); 104 boolean selected = fromSelectedMenuItem.isSelected(); 105 boolean regex = regexMenuItem.isSelected(); 106 Query query = new Query(text, selected, regex); 107 try { 108 List <Object > results = provider.search(query); 109 if (results.isEmpty()) { 110 indicateFailure(); 111 } else { 112 clearFailure(false); 113 } 114 fireSearchComplete(results); 115 } catch (SearchException se) { 116 fireSearchFailed(se); 117 } 118 } else { 119 List <Object > empty = Collections.emptyList(); 121 fireSearchComplete(empty); 122 } 123 } else if (src instanceof JMenuItem ) { 124 if (!expectingInput) { 125 indicateSearchType(); 128 } else { 129 ButtonModel bm = typeButtonGroup.getSelection(); 131 SearchProvider sp = buttonProviderMap.get(bm.getActionCommand()); 132 searchTextField.setToolTipText(sp.getInputDescription()); 133 } 134 } 135 } 136 137 143 public void addSearchListener(SearchListener l) { 144 searchListeners.add(SearchListener.class, l); 145 } 146 147 public void changedUpdate(DocumentEvent e) { 148 } 149 150 155 private void clearFailure(boolean clear) { 156 Color color = UIManager.getDefaults().getColor("TextField.background"); 157 setBackground(color); 158 searchTextField.setBackground(color); 159 color = UIManager.getDefaults().getColor("TextField.foreground"); 160 searchTextField.setForeground(color); 161 if (clear) { 162 searchTextField.setText(""); 163 } 164 } 165 166 171 protected void fireSearchComplete(List <Object > results) { 172 fireSearchEvent(new SearchEvent(this, SearchEvent.Type.FINISHED, results)); 173 } 174 175 180 private void fireSearchEvent(SearchEvent.Type type) { 181 fireSearchEvent(new SearchEvent(this, type)); 182 } 183 184 189 protected void fireSearchFailed(SearchException error) { 190 fireSearchEvent(new SearchEvent(this, SearchEvent.Type.FAILED, error)); 191 } 192 193 196 private void fireSearchEvent(SearchEvent event) { 197 Object [] listeners = searchListeners.getListenerList(); 198 SearchEvent.Type type = event.getType(); 199 for (int ii = listeners.length - 2; ii >= 0; ii -= 2) { 200 if (listeners[ii] == SearchListener.class) { 201 type.fireEvent(event, (SearchListener) listeners[ii + 1]); 202 } 203 } 204 } 205 206 public void focusGained(FocusEvent e) { 207 if (e.getSource() == searchTextField && !expectingInput) { 209 clearFailure(true); 212 expectingInput = true; 213 } 214 } 215 216 public void focusLost(FocusEvent e) { 217 if (e.getSource() == searchTextField && 218 searchTextField.getText().length() == 0) { 219 clearFailure(false); 220 indicateSearchType(); 221 } 222 } 223 224 227 private void indicateFailure() { 228 setBackground(MATCH_FAILED_COLOR); 229 searchTextField.setBackground(MATCH_FAILED_COLOR); 230 searchTextField.setForeground(Color.white); 231 } 232 233 236 private void indicateSearchType() { 237 expectingInput = false; 239 ButtonModel bm = typeButtonGroup.getSelection(); 240 if (bm == null) { 241 return; 244 } 245 SearchProvider provider = buttonProviderMap.get(bm.getActionCommand()); 246 String name = provider.getDisplayName(); 247 Color color = UIManager.getDefaults().getColor("textInactiveText"); 248 searchTextField.setForeground(color); 249 searchTextField.setText(name); 250 searchTextField.setToolTipText(provider.getInputDescription()); 251 } 252 253 public void insertUpdate(DocumentEvent e) { 254 } 258 259 public void keyPressed(KeyEvent e) { 260 } 261 262 public void keyReleased(KeyEvent e) { 263 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { 264 if (!expectingInput) { 265 clearFailure(true); 266 indicateSearchType(); 267 } 268 fireSearchEvent(SearchEvent.Type.DISMISSED); 269 typesButton.requestFocusInWindow(); 271 } 272 } 273 274 public void keyTyped(KeyEvent e) { 275 } 276 277 public void mouseClicked(MouseEvent e) { 278 } 279 280 public void mouseEntered(MouseEvent e) { 281 } 282 283 public void mouseExited(MouseEvent e) { 284 } 285 286 public void mousePressed(MouseEvent e) { 287 if (!expectingInput) { 291 clearFailure(true); 292 expectingInput = true; 293 } 294 } 295 296 public void mouseReleased(MouseEvent e) { 297 } 298 299 public void popupMenuWillBecomeVisible(PopupMenuEvent e) { 300 if (!expectingInput) { 301 clearFailure(true); 302 } 303 } 304 305 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { 306 } 307 308 public void popupMenuCanceled(PopupMenuEvent e) { 309 } 310 311 316 public void prepareForInput(boolean clear) { 317 clearFailure(false); 318 searchTextField.requestFocusInWindow(); 319 if (clear) { 320 searchTextField.setText(""); 321 } else { 322 searchTextField.selectAll(); 323 } 324 } 325 326 331 public void removeSearchListener(SearchListener l) { 332 searchListeners.remove(SearchListener.class, l); 333 } 334 335 public void removeUpdate(DocumentEvent e) { 336 if (e.getDocument().getLength() == 0) { 337 clearFailure(false); 339 } 340 } 341 342 public void setEnabled(boolean enabled) { 343 super.setEnabled(enabled); 344 typesButton.setEnabled(enabled); 345 searchTextField.setEnabled(enabled); 346 if (!expectingInput) { 347 clearFailure(true); 348 indicateSearchType(); 349 } 350 } 351 352 357 public void setProviders(Collection providers) { 358 if (providers.size() == 0) { 359 throw new IllegalArgumentException ("providers must be non-empty"); 360 } 361 buttonProviderMap.clear(); 362 typesPopupMenu.removeAll(); 363 Iterator iter = providers.iterator(); 364 while (iter.hasNext()) { 365 SearchProvider provider = (SearchProvider) iter.next(); 366 String name = provider.getDisplayName(); 367 JRadioButtonMenuItem item = new JRadioButtonMenuItem (name); 368 item.addActionListener((ActionListener ) WeakListeners.create( 369 ActionListener .class, this, item)); 370 item.setActionCommand(name); 371 item.setToolTipText(provider.getShortDescription()); 372 buttonProviderMap.put(name, provider); 373 typeButtonGroup.add(item); 374 typesPopupMenu.add(item); 375 } 376 AbstractButton button = (AbstractButton ) typesPopupMenu.getComponent(0); 378 button.setSelected(true); 379 if (!expectingInput) { 380 clearFailure(true); 381 indicateSearchType(); 382 } 383 384 typesPopupMenu.add(new JSeparator ()); 385 386 if (fromSelectedMenuItem == null) { 387 fromSelectedMenuItem = new JCheckBoxMenuItem (); 388 fromSelectedMenuItem.addActionListener((ActionListener ) WeakListeners.create( 389 ActionListener .class, this, fromSelectedMenuItem)); 390 fromSelectedMenuItem.setText(NbBundle.getMessage(SearchFieldPanel.class, 391 "LBL_SearchField_StartFromSelected")); 392 } 393 typesPopupMenu.add(fromSelectedMenuItem); 394 395 if (regexMenuItem == null) { 396 regexMenuItem = new JCheckBoxMenuItem (); 397 regexMenuItem.addActionListener((ActionListener ) WeakListeners.create( 398 ActionListener .class, this, regexMenuItem)); 399 regexMenuItem.setText(NbBundle.getMessage(SearchFieldPanel.class, 400 "LBL_SearchField_RegularExpression")); 401 } 402 typesPopupMenu.add(regexMenuItem); 403 } 404 405 410 private void initComponents() { 412 typesPopupMenu = new javax.swing.JPopupMenu (); 413 typeButtonGroup = new javax.swing.ButtonGroup (); 414 typesButton = new javax.swing.JButton (); 415 searchTextField = new javax.swing.JTextField (); 416 417 setBackground(javax.swing.UIManager.getDefaults().getColor("TextField.background")); 418 setBorder(javax.swing.BorderFactory.createEtchedBorder()); 419 typesButton.setBackground(javax.swing.UIManager.getDefaults().getColor("TextField.background")); 420 typesButton.setIcon(new javax.swing.ImageIcon (getClass().getResource("/org/netbeans/modules/xml/xam/ui/search/search_types.png"))); 421 typesButton.setToolTipText(java.util.ResourceBundle.getBundle("org/netbeans/modules/xml/xam/ui/search/Bundle").getString("HINT_SearchFieldPanel_Types")); 422 typesButton.setBorderPainted(false); 423 typesButton.setContentAreaFilled(false); 424 typesButton.setDisabledIcon(new javax.swing.ImageIcon (getClass().getResource("/org/netbeans/modules/xml/xam/ui/search/search_types_disabled.png"))); 425 typesButton.setMargin(new java.awt.Insets (0, 0, 0, 0)); 426 427 searchTextField.setColumns(15); 428 searchTextField.setBorder(null); 429 430 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 431 this.setLayout(layout); 432 layout.setHorizontalGroup( 433 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 434 .add(layout.createSequentialGroup() 435 .add(typesButton) 436 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 437 .add(searchTextField)) 438 ); 439 layout.setVerticalGroup( 440 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 441 .add(typesButton) 442 .add(searchTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE) 443 ); 444 } 446 private javax.swing.JTextField searchTextField; 448 private javax.swing.ButtonGroup typeButtonGroup; 449 private javax.swing.JButton typesButton; 450 private javax.swing.JPopupMenu typesPopupMenu; 451 } 453 | Popular Tags |