KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > search > types > ModificationDateCustomizer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.search.types;
21
22
23 import java.awt.Color JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.awt.event.KeyEvent JavaDoc;
27 import java.awt.event.KeyListener JavaDoc;
28 import java.beans.Customizer JavaDoc;
29 import java.beans.PropertyChangeListener JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.ResourceBundle JavaDoc;
32 import javax.swing.ButtonGroup JavaDoc;
33 import javax.swing.JPanel JavaDoc;
34 import javax.swing.JTextField JavaDoc;
35 import javax.swing.border.CompoundBorder JavaDoc;
36 import javax.swing.border.TitledBorder JavaDoc;
37 import javax.swing.UIManager JavaDoc;
38
39 import org.openide.awt.Mnemonics;
40 import org.openide.util.HelpCtx;
41 import org.openide.util.NbBundle;
42
43
44 /**
45  * Customizer of criterion for testing date of primary file modification.
46  *
47  * @author Petr Kuzel
48  * @author Marian Petras
49  */

50 public class ModificationDateCustomizer extends JPanel JavaDoc
51                                         implements Customizer JavaDoc,
52                                                    KeyListener JavaDoc,
53                                                    ActionListener JavaDoc {
54
55     private ModificationDateType peer;
56     private String JavaDoc lastDays = "";
57     private String JavaDoc lastBefore = "";
58     private String JavaDoc lastAfter = "";
59
60     /** Creates new form ModificationDateCustomizer */
61     public ModificationDateCustomizer() {
62
63         initComponents ();
64         initAccessibility();
65         TitledBorder JavaDoc tb = new TitledBorder JavaDoc(
66             NbBundle.getBundle(ModificationDateCustomizer.class).getString("TEXT_LABEL_MODIFIED")); // NOI18N
67

68         tb.setBorder(new CompoundBorder JavaDoc());
69         setBorder (tb);
70         
71         ButtonGroup JavaDoc group = new ButtonGroup JavaDoc();
72         group.add(daysRadioButton);
73         group.add(betweenRadioButton);
74
75         HelpCtx.setHelpIDString(this, ModificationDateType.class.toString());
76     }
77
78     /** Initialize customizer state.
79     */

80     public void setObject(final Object JavaDoc obj) {
81
82         peer = (ModificationDateType) obj;
83
84         // set default coloring
85
daysTextField.setForeground(getForegroundColor());
86         afterTextField.setForeground(getForegroundColor());
87         beforeTextField.setForeground(getForegroundColor());
88
89         daysTextField.setText(""); // NOI18N
90
afterTextField.setText(""); // NOI18N
91
beforeTextField.setText(""); // NOI18N
92

93
94         // display object value in customizer
95
if(!"".equals(peer.getMatchAfter())) { // NOI18N
96
afterTextField.setText(peer.getMatchAfter());
97             if(!peer.getMatchAfter().equals(null))
98                 betweenRadioButton.setSelected(true);
99         }
100
101         if(!"".equals(peer.getMatchBefore())) { // NOI18N
102
beforeTextField.setText(peer.getMatchBefore());
103             if(!peer.getMatchAfter().equals(null))
104                 betweenRadioButton.setSelected(true);
105         }
106
107         if(!"".equals(peer.getDays())) { // NOI18N
108
daysTextField.setText(peer.getDays());
109             daysRadioButton.setSelected(true);
110         }
111
112     }
113
114     public void addPropertyChangeListener(final PropertyChangeListener JavaDoc p1) {
115     }
116
117     public void removePropertyChangeListener(final PropertyChangeListener JavaDoc p1) {
118     }
119
120
121      private void initAccessibility(){
122         ResourceBundle JavaDoc 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     /** This method is called from within the constructor to
140      * initialize the form.
141      * WARNING: Do NOT modify this code. The content of this method is
142      * always regenerated by the FormEditor.
143      */

144     private void initComponents() {//GEN-BEGIN:initComponents
145
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
146
147         daysLabel = new javax.swing.JLabel JavaDoc();
148         beginLabel = new javax.swing.JLabel JavaDoc();
149         jPanel1 = new javax.swing.JPanel JavaDoc();
150         egLabel = new javax.swing.JLabel JavaDoc();
151
152         setLayout(new java.awt.GridBagLayout JavaDoc());
153
154         daysRadioButton.setSelected(true);
155         Mnemonics.setLocalizedText(daysRadioButton, NbBundle.getMessage(ModificationDateCustomizer.class, "TEXT_BUTTON_PRECEDING")); //NOI18N
156
daysRadioButton.addActionListener(this);
157         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
158         gridBagConstraints.gridx = 0;
159         gridBagConstraints.gridy = 0;
160         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
161         gridBagConstraints.insets = new java.awt.Insets JavaDoc(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 JavaDoc();
166         gridBagConstraints.gridx = 2;
167         gridBagConstraints.gridy = 0;
168         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
169         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 4, 0, 0);
170         add(daysLabel, gridBagConstraints);
171
172         daysTextField.setPreferredSize(new java.awt.Dimension JavaDoc (50, daysTextField.getPreferredSize ().height));
173         daysTextField.addKeyListener(this);
174         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(4, 0, 0, 0);
181         add(daysTextField, gridBagConstraints);
182
183         Mnemonics.setLocalizedText(betweenRadioButton, NbBundle.getMessage(ModificationDateCustomizer.class, "TEXT_BUTTON_RANGE")); //NOI18N
184
betweenRadioButton.addActionListener(this);
185         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
186         gridBagConstraints.gridx = 0;
187         gridBagConstraints.gridy = 1;
188         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
189         gridBagConstraints.insets = new java.awt.Insets JavaDoc(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")); //NOI18N
194
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
195         gridBagConstraints.gridx = 3;
196         gridBagConstraints.gridy = 1;
197         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
198         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 4, 0, 4);
199         add(beginLabel, gridBagConstraints);
200
201         afterTextField.setPreferredSize(new java.awt.Dimension JavaDoc (100, afterTextField.getPreferredSize ().height));
202         afterTextField.addKeyListener(this);
203         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(4, 0, 0, 4);
211         add(afterTextField, gridBagConstraints);
212
213         beforeTextField.setPreferredSize(new java.awt.Dimension JavaDoc (100, beforeTextField.getPreferredSize ().height));
214         beforeTextField.addKeyListener(this);
215         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(4, 0, 0, 12);
222         add(beforeTextField, gridBagConstraints);
223
224         jPanel1.setLayout(new java.awt.GridBagLayout JavaDoc());
225
226         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc()).toString());
237         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
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 JavaDoc(11, 8, 0, 0);
244         add(egLabel, gridBagConstraints);
245
246     }//GEN-END:initComponents
247

248     /**
249      * This method is called when one of the two radio buttons is selected.
250      * It updates the search type according to the change of the selection.
251      */

252     public void actionPerformed(ActionEvent JavaDoc e) {
253         Object JavaDoc source = e.getSource();
254         
255         if (source == daysRadioButton) {
256             setDays();
257         } else if (source == betweenRadioButton) {
258             setBetween();
259         } else {
260             
261             /* This should never happen. */
262             assert false;
263         }
264     }
265     
266     /**
267      * This method is called when a key is released in some of this customizer's
268      * text fields.
269      * It checks whether the text within the text field has changed and updates
270      * the search type if so.
271      */

272     public void keyReleased(KeyEvent JavaDoc e) {
273         Object JavaDoc 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             /* This should never happen. */
284             assert false;
285         }
286     }
287     
288     /**
289      * This method is called when a key is pressed in some of this customizer's
290      * text fields.
291      * It does nothing and it is here just because this class declares that
292      * it implements the <code>KeyListener</code> interface.
293      *
294      * @see #keyReleased(KeyEvent)
295      */

296     public void keyPressed(KeyEvent JavaDoc e) {
297         //does nothing
298
}
299     
300     /**
301      * This method is called when a key is typed in some of this customizer's
302      * text fields.
303      * It does nothing and it is here just because this class declares that
304      * it implements the <code>KeyListener</code> interface.
305      *
306      * @see #keyReleased(KeyEvent)
307      */

308     public void keyTyped(KeyEvent JavaDoc e) {
309         //does nothing
310
}
311     
312     /**
313      * This method is called when a key is released in the
314      * <em>Within the Past:</em> text field.
315      * It checks whether the text within the text field has changed and updates
316      * the search type if so.
317      */

318     private void daysTextFieldKeyReleased() {
319         String JavaDoc text = daysTextField.getText();
320         if (!text.equals(lastDays)) {
321             setDays();
322             daysRadioButton.setSelected(true);
323             lastDays = text;
324         }
325     }
326     
327     /**
328      * This method is called when a key is released in the
329      * <em>Before:</em> text field.
330      * It checks whether the text within the text field has changed and updates
331      * the search type if so.
332      */

333     private void afterTextFieldKeyReleased() {
334         String JavaDoc text = afterTextField.getText();
335         if (!text.equals(lastAfter)) {
336             setBetween();
337             betweenRadioButton.setSelected(true);
338             lastAfter = text;
339         }
340     }
341     
342     /**
343      * This method is called when a key is released in the
344      * <em>(Before ...) and</em> text field.
345      * It checks whether the text within the text field has changed and updates
346      * the search type if so.
347      */

348     private void beforeTextFieldKeyReleased() {
349         String JavaDoc 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 JavaDoc text = daysTextField.getText();
360         try {
361             peer.setDays(text);
362             daysTextField.setForeground(getForegroundColor());
363         } catch (IllegalArgumentException JavaDoc ex) {
364             daysTextField.setForeground(getErrorForegroundColor());
365         }
366     }
367
368     private void setAfter () {
369
370         String JavaDoc after = afterTextField.getText();
371
372         try {
373             peer.setMatchAfter(after);
374             afterTextField.setForeground(getForegroundColor());
375         } catch (IllegalArgumentException JavaDoc ex) {
376             afterTextField.setForeground(getErrorForegroundColor());
377         }
378
379     }
380
381     private void setBefore () {
382
383         String JavaDoc before = beforeTextField.getText();
384
385         try {
386             peer.setMatchBefore(before);
387             beforeTextField.setForeground(getForegroundColor());
388         } catch (IllegalArgumentException JavaDoc ex) {
389             beforeTextField.setForeground(getErrorForegroundColor());
390         }
391
392     }
393
394     private void setBetween () {
395         String JavaDoc after = afterTextField.getText();
396         if ( after.length() != 0 ) {
397             setAfter();
398         }
399         setBefore();
400     }
401
402     // Variables declaration - do not modify//GEN-BEGIN:variables
403
private final javax.swing.JTextField JavaDoc afterTextField = new javax.swing.JTextField JavaDoc();
404     private final javax.swing.JTextField JavaDoc beforeTextField = new javax.swing.JTextField JavaDoc();
405     private javax.swing.JLabel JavaDoc beginLabel;
406     private final javax.swing.JRadioButton JavaDoc betweenRadioButton = new javax.swing.JRadioButton JavaDoc();
407     private javax.swing.JLabel JavaDoc daysLabel;
408     private final javax.swing.JRadioButton JavaDoc daysRadioButton = new javax.swing.JRadioButton JavaDoc();
409     private final javax.swing.JTextField JavaDoc daysTextField = new javax.swing.JTextField JavaDoc();
410     private javax.swing.JLabel JavaDoc egLabel;
411     private javax.swing.JPanel JavaDoc jPanel1;
412     // End of variables declaration//GEN-END:variables
413

414     public void requestFocus() {
415         JTextField JavaDoc 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     // colors
427

428     private Color JavaDoc findColor (String JavaDoc key, Color JavaDoc defCol) {
429         Color JavaDoc color = UIManager.getDefaults().getColor (key);
430         if ( color != null ) {
431             return color;
432         }
433         return defCol;
434     }
435
436     private Color JavaDoc getForegroundColor () {
437         return findColor ("TextField.foreground", Color.black);
438     }
439
440     private Color JavaDoc getErrorForegroundColor () {
441         return findColor ("TextField.errorForeground", Color.red);
442     }
443
444 }
445
Popular Tags