1 19 20 package org.netbeans.modules.search.types; 21 22 23 import java.awt.Color ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.awt.event.KeyEvent ; 27 import java.awt.event.KeyListener ; 28 import java.beans.Customizer ; 29 import java.beans.PropertyChangeListener ; 30 import java.util.Date ; 31 import java.util.ResourceBundle ; 32 import javax.swing.ButtonGroup ; 33 import javax.swing.JPanel ; 34 import javax.swing.JTextField ; 35 import javax.swing.border.CompoundBorder ; 36 import javax.swing.border.TitledBorder ; 37 import javax.swing.UIManager ; 38 39 import org.openide.awt.Mnemonics; 40 import org.openide.util.HelpCtx; 41 import org.openide.util.NbBundle; 42 43 44 50 public class ModificationDateCustomizer extends JPanel 51 implements Customizer , 52 KeyListener , 53 ActionListener { 54 55 private ModificationDateType peer; 56 private String lastDays = ""; 57 private String lastBefore = ""; 58 private String lastAfter = ""; 59 60 61 public ModificationDateCustomizer() { 62 63 initComponents (); 64 initAccessibility(); 65 TitledBorder tb = new TitledBorder ( 66 NbBundle.getBundle(ModificationDateCustomizer.class).getString("TEXT_LABEL_MODIFIED")); 68 tb.setBorder(new CompoundBorder ()); 69 setBorder (tb); 70 71 ButtonGroup group = new ButtonGroup (); 72 group.add(daysRadioButton); 73 group.add(betweenRadioButton); 74 75 HelpCtx.setHelpIDString(this, ModificationDateType.class.toString()); 76 } 77 78 80 public void setObject(final Object obj) { 81 82 peer = (ModificationDateType) obj; 83 84 daysTextField.setForeground(getForegroundColor()); 86 afterTextField.setForeground(getForegroundColor()); 87 beforeTextField.setForeground(getForegroundColor()); 88 89 daysTextField.setText(""); afterTextField.setText(""); beforeTextField.setText(""); 93 94 if(!"".equals(peer.getMatchAfter())) { afterTextField.setText(peer.getMatchAfter()); 97 if(!peer.getMatchAfter().equals(null)) 98 betweenRadioButton.setSelected(true); 99 } 100 101 if(!"".equals(peer.getMatchBefore())) { beforeTextField.setText(peer.getMatchBefore()); 103 if(!peer.getMatchAfter().equals(null)) 104 betweenRadioButton.setSelected(true); 105 } 106 107 if(!"".equals(peer.getDays())) { daysTextField.setText(peer.getDays()); 109 daysRadioButton.setSelected(true); 110 } 111 112 } 113 114 public void addPropertyChangeListener(final PropertyChangeListener p1) { 115 } 116 117 public void removePropertyChangeListener(final PropertyChangeListener p1) { 118 } 119 120 121 private void initAccessibility(){ 122 ResourceBundle bundle = NbBundle.getBundle(ModificationDateCustomizer.class); 123 this.getAccessibleContext().setAccessibleName(bundle.getString("ACS_Dates")); 124 daysTextField.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_DURING_DAYS")); 125 daysTextField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_DURING_DAYS")); 126 daysTextField.setToolTipText(bundle.getString("ACSD_DURING_DAYS")); 127 afterTextField.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_AFTER")); 128 afterTextField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_AFTER")); 129 afterTextField.setToolTipText(bundle.getString("TOOLTIP_AFTER")); 130 beforeTextField.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_BEFORE")); 131 beforeTextField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_BEFORE")); 132 beforeTextField.setToolTipText(bundle.getString("ACSD_BEFORE")); 133 daysRadioButton.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_TEXT_BUTTON_PRECEDING")); 134 daysRadioButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_TEXT_BUTTON_PRECEDING")); 135 betweenRadioButton.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_TEXT_BUTTON_RANGE")); 136 betweenRadioButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_TEXT_BUTTON_RANGE")); 137 } 138 139 144 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 146 147 daysLabel = new javax.swing.JLabel (); 148 beginLabel = new javax.swing.JLabel (); 149 jPanel1 = new javax.swing.JPanel (); 150 egLabel = new javax.swing.JLabel (); 151 152 setLayout(new java.awt.GridBagLayout ()); 153 154 daysRadioButton.setSelected(true); 155 Mnemonics.setLocalizedText(daysRadioButton, NbBundle.getMessage(ModificationDateCustomizer.class, "TEXT_BUTTON_PRECEDING")); daysRadioButton.addActionListener(this); 157 gridBagConstraints = new java.awt.GridBagConstraints (); 158 gridBagConstraints.gridx = 0; 159 gridBagConstraints.gridy = 0; 160 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 161 gridBagConstraints.insets = new java.awt.Insets (4, 8, 0, 4); 162 add(daysRadioButton, gridBagConstraints); 163 164 daysLabel.setText(NbBundle.getBundle(ModificationDateCustomizer.class).getString("TEXT_LABEL_DURING_LAST")); 165 gridBagConstraints = new java.awt.GridBagConstraints (); 166 gridBagConstraints.gridx = 2; 167 gridBagConstraints.gridy = 0; 168 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 169 gridBagConstraints.insets = new java.awt.Insets (0, 4, 0, 0); 170 add(daysLabel, gridBagConstraints); 171 172 daysTextField.setPreferredSize(new java.awt.Dimension (50, daysTextField.getPreferredSize ().height)); 173 daysTextField.addKeyListener(this); 174 gridBagConstraints = new java.awt.GridBagConstraints (); 175 gridBagConstraints.gridx = 1; 176 gridBagConstraints.gridy = 0; 177 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 178 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 179 gridBagConstraints.weightx = 0.5; 180 gridBagConstraints.insets = new java.awt.Insets (4, 0, 0, 0); 181 add(daysTextField, gridBagConstraints); 182 183 Mnemonics.setLocalizedText(betweenRadioButton, NbBundle.getMessage(ModificationDateCustomizer.class, "TEXT_BUTTON_RANGE")); betweenRadioButton.addActionListener(this); 185 gridBagConstraints = new java.awt.GridBagConstraints (); 186 gridBagConstraints.gridx = 0; 187 gridBagConstraints.gridy = 1; 188 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 189 gridBagConstraints.insets = new java.awt.Insets (4, 8, 0, 4); 190 add(betweenRadioButton, gridBagConstraints); 191 192 beginLabel.setLabelFor(beforeTextField); 193 Mnemonics.setLocalizedText(beginLabel, NbBundle.getMessage(ModificationDateCustomizer.class, "TEXT_LABEL_BEGIN_DATE")); gridBagConstraints = new java.awt.GridBagConstraints (); 195 gridBagConstraints.gridx = 3; 196 gridBagConstraints.gridy = 1; 197 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 198 gridBagConstraints.insets = new java.awt.Insets (0, 4, 0, 4); 199 add(beginLabel, gridBagConstraints); 200 201 afterTextField.setPreferredSize(new java.awt.Dimension (100, afterTextField.getPreferredSize ().height)); 202 afterTextField.addKeyListener(this); 203 gridBagConstraints = new java.awt.GridBagConstraints (); 204 gridBagConstraints.gridx = 1; 205 gridBagConstraints.gridy = 1; 206 gridBagConstraints.gridwidth = 2; 207 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 208 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 209 gridBagConstraints.weightx = 0.5; 210 gridBagConstraints.insets = new java.awt.Insets (4, 0, 0, 4); 211 add(afterTextField, gridBagConstraints); 212 213 beforeTextField.setPreferredSize(new java.awt.Dimension (100, beforeTextField.getPreferredSize ().height)); 214 beforeTextField.addKeyListener(this); 215 gridBagConstraints = new java.awt.GridBagConstraints (); 216 gridBagConstraints.gridx = 4; 217 gridBagConstraints.gridy = 1; 218 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 219 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 220 gridBagConstraints.weightx = 0.5; 221 gridBagConstraints.insets = new java.awt.Insets (4, 0, 0, 12); 222 add(beforeTextField, gridBagConstraints); 223 224 jPanel1.setLayout(new java.awt.GridBagLayout ()); 225 226 gridBagConstraints = new java.awt.GridBagConstraints (); 227 gridBagConstraints.gridx = 0; 228 gridBagConstraints.gridy = 3; 229 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 230 gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER; 231 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 232 gridBagConstraints.weightx = 1.0; 233 gridBagConstraints.weighty = 1.0; 234 add(jPanel1, gridBagConstraints); 235 236 egLabel.setText(NbBundle.getBundle(ModificationDateCustomizer.class).getString("TEXT_DATE_EXAMPLE") + new ModificationDateType.FormattedDate(new Date ()).toString()); 237 gridBagConstraints = new java.awt.GridBagConstraints (); 238 gridBagConstraints.gridx = 0; 239 gridBagConstraints.gridy = 2; 240 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 241 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 242 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 243 gridBagConstraints.insets = new java.awt.Insets (11, 8, 0, 0); 244 add(egLabel, gridBagConstraints); 245 246 } 248 252 public void actionPerformed(ActionEvent e) { 253 Object source = e.getSource(); 254 255 if (source == daysRadioButton) { 256 setDays(); 257 } else if (source == betweenRadioButton) { 258 setBetween(); 259 } else { 260 261 262 assert false; 263 } 264 } 265 266 272 public void keyReleased(KeyEvent e) { 273 Object source = e.getSource(); 274 275 if (source == daysTextField) { 276 daysTextFieldKeyReleased(); 277 } else if (source == afterTextField) { 278 afterTextFieldKeyReleased(); 279 } else if (source == beforeTextField) { 280 beforeTextFieldKeyReleased(); 281 } else { 282 283 284 assert false; 285 } 286 } 287 288 296 public void keyPressed(KeyEvent e) { 297 } 299 300 308 public void keyTyped(KeyEvent e) { 309 } 311 312 318 private void daysTextFieldKeyReleased() { 319 String text = daysTextField.getText(); 320 if (!text.equals(lastDays)) { 321 setDays(); 322 daysRadioButton.setSelected(true); 323 lastDays = text; 324 } 325 } 326 327 333 private void afterTextFieldKeyReleased() { 334 String text = afterTextField.getText(); 335 if (!text.equals(lastAfter)) { 336 setBetween(); 337 betweenRadioButton.setSelected(true); 338 lastAfter = text; 339 } 340 } 341 342 348 private void beforeTextFieldKeyReleased() { 349 String text = beforeTextField.getText(); 350 if (!text.equals(lastBefore)) { 351 setBetween(); 352 betweenRadioButton.setSelected(true); 353 lastBefore = text; 354 } 355 } 356 357 private void setDays() { 358 359 String text = daysTextField.getText(); 360 try { 361 peer.setDays(text); 362 daysTextField.setForeground(getForegroundColor()); 363 } catch (IllegalArgumentException ex) { 364 daysTextField.setForeground(getErrorForegroundColor()); 365 } 366 } 367 368 private void setAfter () { 369 370 String after = afterTextField.getText(); 371 372 try { 373 peer.setMatchAfter(after); 374 afterTextField.setForeground(getForegroundColor()); 375 } catch (IllegalArgumentException ex) { 376 afterTextField.setForeground(getErrorForegroundColor()); 377 } 378 379 } 380 381 private void setBefore () { 382 383 String before = beforeTextField.getText(); 384 385 try { 386 peer.setMatchBefore(before); 387 beforeTextField.setForeground(getForegroundColor()); 388 } catch (IllegalArgumentException ex) { 389 beforeTextField.setForeground(getErrorForegroundColor()); 390 } 391 392 } 393 394 private void setBetween () { 395 String after = afterTextField.getText(); 396 if ( after.length() != 0 ) { 397 setAfter(); 398 } 399 setBefore(); 400 } 401 402 private final javax.swing.JTextField afterTextField = new javax.swing.JTextField (); 404 private final javax.swing.JTextField beforeTextField = new javax.swing.JTextField (); 405 private javax.swing.JLabel beginLabel; 406 private final javax.swing.JRadioButton betweenRadioButton = new javax.swing.JRadioButton (); 407 private javax.swing.JLabel daysLabel; 408 private final javax.swing.JRadioButton daysRadioButton = new javax.swing.JRadioButton (); 409 private final javax.swing.JTextField daysTextField = new javax.swing.JTextField (); 410 private javax.swing.JLabel egLabel; 411 private javax.swing.JPanel jPanel1; 412 414 public void requestFocus() { 415 JTextField tf = betweenRadioButton.isSelected() ? 416 afterTextField : daysTextField; 417 int n = tf.getText().length(); 418 if (n > 0) { 419 tf.setCaretPosition(0); 420 tf.moveCaretPosition(n); 421 } 422 tf.requestFocus(); 423 } 424 425 426 428 private Color findColor (String key, Color defCol) { 429 Color color = UIManager.getDefaults().getColor (key); 430 if ( color != null ) { 431 return color; 432 } 433 return defCol; 434 } 435 436 private Color getForegroundColor () { 437 return findColor ("TextField.foreground", Color.black); 438 } 439 440 private Color getErrorForegroundColor () { 441 return findColor ("TextField.errorForeground", Color.red); 442 } 443 444 } 445 | Popular Tags |