1 19 20 21 package org.netbeans.modules.search.types; 22 23 24 import java.awt.BorderLayout ; 25 import java.awt.Color ; 26 import java.awt.Component ; 27 import java.awt.GridLayout ; 28 import java.awt.event.ItemEvent ; 29 import java.awt.event.ItemListener ; 30 import java.awt.event.KeyEvent ; 31 import java.awt.event.KeyListener ; 32 import java.beans.Customizer ; 33 import java.beans.PropertyChangeListener ; 34 import java.util.HashSet ; 35 import java.util.Iterator ; 36 import java.util.ResourceBundle ; 37 import java.util.List ; 38 import java.util.Set ; 39 import java.util.Vector ; 40 import javax.swing.BorderFactory ; 41 import javax.swing.Box ; 42 import javax.swing.BoxLayout ; 43 import javax.swing.DefaultComboBoxModel ; 44 import javax.swing.JButton ; 45 import javax.swing.JCheckBox ; 46 import javax.swing.JComboBox ; 47 import javax.swing.JComponent ; 48 import javax.swing.JPanel ; 49 import javax.swing.JRootPane ; 50 import javax.swing.JTextField ; 51 import javax.swing.SwingUtilities ; 52 import javax.swing.border.CompoundBorder ; 53 import javax.swing.border.TitledBorder ; 54 import javax.swing.UIManager ; 55 import javax.swing.event.DocumentEvent ; 56 import javax.swing.event.DocumentListener ; 57 import org.netbeans.modules.search.DialogLifetime; 58 import org.netbeans.modules.search.FindDialogMemory; 59 60 import org.openide.awt.Mnemonics; 61 import org.openide.util.NbBundle; 62 import org.openidex.search.SearchPattern; 63 64 65 71 public abstract class TextCustomizer extends JPanel 72 implements Customizer , DialogLifetime, ItemListener , 73 KeyListener 74 { 75 76 private final JComboBox substringComboBox = new JComboBox (); 77 private final JCheckBox caseSensitiveCheckBox = new JCheckBox (); 78 private final JCheckBox regexpCheckBox = new JCheckBox (); 79 private final JCheckBox wholeWordsCheckBox = new JCheckBox (); 80 private final JPanel contentPanel = new JPanel (); 81 85 private int replacePanelIndex = -1; 86 87 private boolean replacePanelPresent = false; 88 89 protected TextType peer; 90 91 92 public TextCustomizer() { 93 initComponents (); 94 initAccessibility (); 95 initTextFieldListeners(); 96 TitledBorder tb = new TitledBorder (getBorderLabel()); 97 tb.setBorder(new CompoundBorder ()); 98 setBorder (tb); 99 100 initHistory(); 101 } 102 103 104 106 protected String getBorderLabel() { 107 return null; 108 } 109 110 private void initAccessibility(){ 111 ResourceBundle bundle = NbBundle.getBundle(TextCustomizer.class); 112 substringComboBox.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_PROP_SUBSTRING")); 113 substringComboBox.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_PROP_SUBSTRING")); 114 caseSensitiveCheckBox.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_TEXT_LABEL_CASE_SENSITIVE")); 115 wholeWordsCheckBox.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_TEXT_LABEL_WHOLE_WORDS")); 116 regexpCheckBox.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_TEXT_LABEL_RE")); 117 } 118 119 121 private void initTextFieldListeners() { 122 class TextChangeListener implements DocumentListener { 123 private JTextField textField; 124 TextChangeListener(JTextField textField) { 125 this.textField = textField; 126 } 127 public void changedUpdate(DocumentEvent e) { 128 documentChanged(); 129 } 130 public void insertUpdate(DocumentEvent e) { 131 documentChanged(); 132 } 133 public void removeUpdate(DocumentEvent e) { 134 documentChanged(); 135 } 136 private void documentChanged() { 137 stringChanged(); 138 } 139 } 140 ((JTextField )substringComboBox.getEditor().getEditorComponent()).getDocument().addDocumentListener( 141 new TextChangeListener((JTextField )substringComboBox.getEditor().getEditorComponent())); 142 } 143 144 148 private void initHistory() { 149 final List patterns = getSearchPatterns(); 150 if (!patterns.isEmpty()) { 151 final Set inserted = new HashSet (patterns.size()); 152 final Vector itemsList = new Vector (patterns.size()); 153 154 final Iterator it = patterns.iterator(); 155 while (it.hasNext()) { 156 String str = ((SearchPattern)it.next()).getSearchExpression(); 157 if (inserted.add(str)) { 158 itemsList.add(str); 159 } 160 } 161 substringComboBox.setModel(new DefaultComboBoxModel (itemsList)); 162 substringComboBox.setSelectedIndex(-1); 163 } 164 } 165 166 169 public void initFromHistory() { 170 Object topmostItem = getSearchPatterns().get(0); 171 if (topmostItem != null) { 172 putCurrentSearchPattern((SearchPattern) topmostItem); 173 } 174 } 175 176 178 private void initComponents() { 179 substringComboBox.setEditable(true); 180 substringComboBox.addItemListener(this); 181 activateEnterKeyBypass(substringComboBox); 182 183 Mnemonics.setLocalizedText( 184 wholeWordsCheckBox, 185 NbBundle.getMessage(TextCustomizer.class, 186 "TEXT_LABEL_WHOLE_WORDS")); wholeWordsCheckBox.setBorder(null); 188 wholeWordsCheckBox.addItemListener(this); 189 190 Mnemonics.setLocalizedText( 191 caseSensitiveCheckBox, 192 NbBundle.getMessage(TextCustomizer.class, 193 "TEXT_LABEL_CASE_SENSITIVE")); caseSensitiveCheckBox.setBorder(null); 195 caseSensitiveCheckBox.addItemListener(this); 196 197 Mnemonics.setLocalizedText( 198 regexpCheckBox, 199 NbBundle.getMessage(TextCustomizer.class, 200 "TEXT_LABEL_RE")); regexpCheckBox.setBorder(null); 202 regexpCheckBox.addItemListener(this); 203 204 JPanel checkBoxesPanel = new JPanel (new GridLayout (0, 1, 0, 2)); 205 checkBoxesPanel.add(wholeWordsCheckBox); 206 checkBoxesPanel.add(caseSensitiveCheckBox); 207 checkBoxesPanel.add(regexpCheckBox); 208 checkBoxesPanel.setBorder(BorderFactory.createEmptyBorder(0, 7, 0, 0)); 209 210 contentPanel.setLayout(new BoxLayout (contentPanel, BoxLayout.Y_AXIS)); 211 contentPanel.add(substringComboBox); 212 contentPanel.add(Box.createVerticalStrut(6)); 213 replacePanelIndex = contentPanel.getComponentCount(); 214 contentPanel.add(checkBoxesPanel); 215 substringComboBox.setAlignmentX(0f); 216 checkBoxesPanel.setAlignmentX(0f); 217 218 setLayout(new BorderLayout ()); 219 add(contentPanel, BorderLayout.NORTH); 220 221 setBorder(BorderFactory.createEmptyBorder(6, 6, 0, 6)); 222 } 223 224 226 protected void addReplacePanel(Box replaceBox) { 227 if (replacePanelPresent) { 228 throw new IllegalStateException (); 229 } 230 231 replacePanelPresent = true; 232 233 assert replacePanelIndex >= 0; 234 int insertIndex = replacePanelIndex; 235 for (Component c : replaceBox.getComponents()) { 236 contentPanel.add(c, insertIndex++); 237 if (c instanceof JComponent ) { 238 ((JComponent ) c).setAlignmentX(0f); 239 } 240 } 241 contentPanel.add(Box.createVerticalStrut(6), insertIndex); 242 } 243 244 251 protected void activateEnterKeyBypass(JComboBox comboBox) { 252 Component editor = comboBox.getEditor().getEditorComponent(); 253 if (!(editor instanceof JTextField )) { 254 assert false; 255 return; 256 } 257 ((JTextField ) editor).addKeyListener(this); 258 } 259 260 265 public void keyPressed(KeyEvent e) { 266 if ((e.getKeyCode() == KeyEvent.VK_ENTER) && (e.getModifiersEx() == 0)){ 267 268 JRootPane rootPane = SwingUtilities.getRootPane(this); 269 if (rootPane != null) { 270 JButton button = rootPane.getDefaultButton(); 271 if ((button != null) && button.isEnabled()) { 272 e.consume(); 273 button.doClick(); 274 } 275 } 276 } 277 } 278 279 281 public void keyReleased(KeyEvent e) { 282 } 284 285 287 public void keyTyped(KeyEvent e) { 288 } 290 291 293 protected String getComboText(JComboBox comboBox) { 294 return ((JTextField ) comboBox.getEditor().getEditorComponent()).getText(); 295 } 296 297 300 private void regexpChkBoxChanged() { 301 enableUI(); 302 303 if (peer == null) { 304 return; 305 } 306 307 final String text = getComboText(substringComboBox); 308 if ((text == null) || (text.length() == 0)) { 309 310 315 return; 316 } 317 318 if (!regexpCheckBox.isSelected()) { 319 peer.setMatchString(text); 320 substringComboBox.getEditor().getEditorComponent().setForeground(getForegroundColor()); 321 } else { 322 try { 323 peer.setRe(text); 324 substringComboBox.getEditor().getEditorComponent().setForeground(getForegroundColor()); 325 } catch (IllegalArgumentException ex) { 326 substringComboBox.getEditor().getEditorComponent().setForeground(getErrorForegroundColor()); 327 } 328 } 329 } 330 331 334 private void stringChanged() { 335 if (peer == null) { 336 return; 337 } 338 339 final String text = getComboText(substringComboBox); 340 if (!regexpCheckBox.isSelected()) { 341 peer.setMatchString(text); 342 substringComboBox.getEditor().getEditorComponent().setForeground(getForegroundColor()); 343 } else { 344 try { 345 peer.setRe(text); 346 substringComboBox.getEditor().getEditorComponent().setForeground(getForegroundColor()); 347 } catch (IllegalArgumentException ex) { 348 substringComboBox.getEditor().getEditorComponent().setForeground(getErrorForegroundColor()); 349 } 350 } 351 } 352 353 355 public void setObject(final Object obj) { 356 357 peer = (TextType) obj; 358 359 substringComboBox.setForeground(getForegroundColor()); 361 362 String text; 363 Boolean isRegexp = null; 364 365 if ((text = peer.getRe()).length() != 0) { 366 isRegexp = Boolean.TRUE; 367 } else if ((text = peer.getMatchString()).length() != 0) { 368 isRegexp = Boolean.FALSE; 369 } else { 370 text = getComboText(substringComboBox); 371 } 372 373 if (isRegexp != null) { 374 375 376 substringComboBox.setSelectedItem(text); 377 378 regexpCheckBox.setSelected(isRegexp.booleanValue()); 379 caseSensitiveCheckBox.setSelected(peer.isCaseSensitive()); 380 wholeWordsCheckBox.setSelected(peer.getWholeWords()); 381 382 } else if ((text != null) && (text.length() != 0)) { 383 384 385 stringChanged(); 386 } 387 388 enableUI(); 389 } 390 391 public void addPropertyChangeListener(final PropertyChangeListener p1) { 392 } 393 394 public void removePropertyChangeListener(final PropertyChangeListener p1) { 395 } 396 397 public void requestFocus() { 398 JTextField tf = (JTextField )substringComboBox.getEditor().getEditorComponent(); 399 int n = tf.getText().length(); 400 if (n > 0) { 401 tf.setCaretPosition(0); 402 tf.moveCaretPosition(n); 403 } 404 tf.requestFocus(); 405 } 406 407 408 410 private Color findColor (String key, Color defCol) { 411 Color color = UIManager.getDefaults().getColor (key); 412 if ( color != null ) { 413 return color; 414 } 415 return defCol; 416 } 417 418 private Color getForegroundColor () { 419 return findColor ("TextField.foreground", Color.black); 420 } 421 422 private Color getErrorForegroundColor () { 423 return findColor ("TextField.errorForeground", Color.red); 424 } 425 426 430 private void enableUI() { 431 boolean r = regexpCheckBox.isSelected(); 432 regexpCheckBox.setEnabled(true); 433 caseSensitiveCheckBox.setEnabled(!r); 434 wholeWordsCheckBox.setEnabled(!r); 435 } 436 437 440 public void itemStateChanged(ItemEvent e) { 441 Object source = e.getSource(); 442 443 if (source == substringComboBox) { 444 stringChanged(); 445 446 } else if (source == wholeWordsCheckBox) { 447 peer.setWholeWords(wholeWordsCheckBox.isSelected()); 448 449 } else if (source == caseSensitiveCheckBox) { 450 peer.setCaseSensitive(caseSensitiveCheckBox.isSelected()); 451 452 } else if (source == regexpCheckBox) { 453 regexpChkBoxChanged(); 454 455 } else { 456 457 assert false; 458 } 459 } 460 461 466 private SearchPattern getCurrentSearchPattern() { 467 return SearchPattern. 468 create( 469 getComboText(substringComboBox), 470 wholeWordsCheckBox.isSelected(), 471 caseSensitiveCheckBox.isSelected(), 472 regexpCheckBox.isSelected()); 473 } 474 475 480 private void putCurrentSearchPattern(final SearchPattern pat) { 481 assert pat != null; 482 483 substringComboBox.setSelectedItem(pat.getSearchExpression()); 484 wholeWordsCheckBox.setSelected(pat.isWholeWords()); 485 caseSensitiveCheckBox.setSelected(pat.isMatchCase()); 486 regexpCheckBox.setSelected(pat.isRegExp()); 487 488 enableUI(); 489 } 490 491 private void initCheckBoxes(SearchPattern pat) { 492 wholeWordsCheckBox.setSelected(pat.isWholeWords()); 493 caseSensitiveCheckBox.setSelected(pat.isMatchCase()); 494 regexpCheckBox.setSelected(pat.isRegExp()); 495 enableUI(); 496 } 497 498 499 503 504 510 abstract protected List getSearchPatterns(); 511 512 515 abstract protected void addSearchPattern(SearchPattern pattern); 516 517 518 public void onOk() { 519 final SearchPattern searchPattern = getCurrentSearchPattern(); 520 521 if (searchPattern != null) { 522 addSearchPattern(searchPattern); 523 524 String expr = searchPattern.getSearchExpression(); 525 if ((expr != null) && (expr.length() != 0)) { 526 FindDialogMemory.getDefault() 527 .setSearchTypeUsed(peer.getClass().getName(), true); 528 } 529 } 530 } 531 532 public void onCancel() { 533 } 534 535 } 536 | Popular Tags |