KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 package org.netbeans.modules.search.types;
22
23
24 import java.text.DateFormat JavaDoc;
25 import java.text.DecimalFormat JavaDoc;
26 import java.text.ParseException JavaDoc;
27 import java.text.SimpleDateFormat JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.GregorianCalendar JavaDoc;
30
31 import org.openide.filesystems.FileObject;
32 import org.openide.loaders.DataObject;
33 import org.openide.util.HelpCtx;
34 import org.openide.util.NbBundle;
35
36
37 /**
38  * Test DataObject primaryFile for modificaion date.
39  * <p>There are mutually exclusive criteria: between and days.
40  * Between represents absolute date interval. Days represents
41  * relative interval related to today.
42  *
43  * <p>Internally uses null as wildcard. It is presented as an empty string.
44  * One of between or days must be null.
45  *
46  * @author Petr Kuzel
47  * @author Marian Petras
48  */

49 public class ModificationDateType extends DataObjectType {
50
51     private static final long serialVersionUID = 4L;
52     //private static final long serialVersionUID = -5372364935321919998L;
53

54     /** Holds value of property matchBefore. */
55     private Date JavaDoc matchBefore;
56
57     /** Holds value of property matchAfter. */
58     private Date JavaDoc matchAfter;
59
60     /** Holds value of property day. */
61     private Short JavaDoc days;
62
63     
64     /**
65      */

66     protected String JavaDoc displayName() {
67         /*
68          * For all non-default search types, display name is taken from
69          * the search type's bean descriptor. But this search type has
70          * no bean descriptor so we must override this method.
71          */

72         
73         return NbBundle.getMessage(ModificationDateType.class,
74                                    "TEXT_DATE_CRITERION"); //NOI18N
75
}
76
77     /**
78      * Does the DataObject pass this criterion?
79      * @param dataobject to be examined
80      * @return true if pass */

81     public boolean testDataObject(DataObject dobj) {
82
83         FileObject fo = dobj.getPrimaryFile();
84
85         // it is strange
86
if(fo == null)
87             return false;
88
89         // Primary File Modification Date
90
Date JavaDoc date = fo.lastModified();
91
92         boolean hit = testDays(date) && testAfter(date) && testBefore(date);
93
94         if(hit)
95             return true;
96         else
97             return false;
98     }
99
100     /** Is within range? */
101     private boolean testAfter(Date JavaDoc date) {
102         if (matchAfter == null) return true;
103         return date.compareTo(matchAfter)>=0;
104     }
105
106     /** Is within range? */
107     private boolean testBefore(Date JavaDoc date) {
108         if (matchBefore == null) return true;
109         return date.compareTo(matchBefore)<=0;
110     }
111
112     /** Is within 24 hours range? */
113     private boolean testDays(Date JavaDoc date) {
114         if (days == null) return true;
115         return (System.currentTimeMillis() - date.getTime()) < days.shortValue()*1000L*60L*60L*24L;
116     }
117
118     // -------------- before -------------------
119
/** Getter for property matchBefore.
120      *@return Value of property matchBefore. */

121     public Date JavaDoc getMatchBeforeAsDate() {
122         return new FormattedDate(matchBefore);
123     }
124
125     /** Getter of <code>matchBefore</code> property. */
126     public String JavaDoc getMatchBefore() {
127         if (matchBefore == null)
128             return ""; // NOI18N
129
else
130             return getMatchBeforeAsDate().toString();
131     }
132
133     /** Setter for property <code>matchBefore</code>.
134      * @exception IllegalArgumentException if null passed */

135     public void setMatchBefore(String JavaDoc before) {
136         try {
137             setMatchBeforeImpl(before);
138
139             setValid (this.matchBefore != null);
140         } catch (IllegalArgumentException JavaDoc ex) {
141             setValid(false);
142             throw ex;
143         }
144     }
145
146     /** Helper method for setting <code>matchBefore</code> property. */
147     private void setMatchBeforeImpl(String JavaDoc matchBefore) {
148         if(matchBefore == null) throw new IllegalArgumentException JavaDoc();
149
150         if(matchBefore.equals("")) { // NOI18N
151
setMatchBeforeByDate(null);
152             return;
153         }
154
155         try {
156             setMatchBeforeByDate(new FormattedDate(matchBefore));
157
158         } catch (ParseException JavaDoc ex) {
159             throw new IllegalArgumentException JavaDoc();
160         }
161
162     }
163
164     /** Sets property <code>matchBefore</code> according specified date.
165      * @param matchBefore new value of property matchBefore */

166     public void setMatchBeforeByDate(Date JavaDoc matchBefore) {
167         //let date represent one milisecond before midnight
168
if (matchBefore != null) {
169
170             GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc();
171             cal.setTime(matchBefore);
172
173             cal.set(cal.HOUR_OF_DAY, cal.getActualMaximum(cal.HOUR_OF_DAY));
174             cal.set(cal.MINUTE, cal.getActualMaximum(cal.MINUTE));
175             cal.set(cal.SECOND, cal.getActualMaximum(cal.SECOND));
176             cal.set(cal.MILLISECOND, cal.getActualMaximum(cal.MILLISECOND));
177
178             matchBefore = cal.getTime();
179         }
180
181         //do it
182

183         this.matchBefore = matchBefore;
184         days = null;
185     }
186
187
188     // --------------- after ---------------
189
/** Gets for property <code>matchAfter</code> according specified date.
190      * @return value of property matchAfter */

191     public Date JavaDoc getMatchAfterAsDate() {
192         return new FormattedDate(matchAfter);
193     }
194
195     /** Getter for property <code>matchAfter</code>. */
196     public String JavaDoc getMatchAfter() {
197
198         if (matchAfter == null)
199             return ""; // NOI18N
200
else
201             return getMatchAfterAsDate().toString();
202     }
203
204     /** Setter for property <code>matchAfter</code>.
205      * @exception IllegalArgumentException if null passed */

206     public void setMatchAfter(String JavaDoc after) {
207         try {
208             setMatchAfterImpl(after);
209
210             setValid (this.matchAfter != null);
211         } catch (IllegalArgumentException JavaDoc ex) {
212             setValid(false);
213             throw ex;
214         }
215     }
216
217     /** Helper method which sets <code>matchAfter</code> property. */
218     private void setMatchAfterImpl(String JavaDoc matchAfter) {
219
220         if (matchAfter == null) throw new IllegalArgumentException JavaDoc();
221
222         if (matchAfter.equals("")) { // NOI18N
223
setMatchAfterByDate(null);
224             return;
225         }
226
227         try {
228             setMatchAfterByDate(new FormattedDate(matchAfter));
229
230         } catch (ParseException JavaDoc ex) {
231             throw new IllegalArgumentException JavaDoc();
232         }
233
234     }
235
236     /** Setter for property matchAfter.
237      *@param matchAfter New value of property matchAfter.
238      */

239     public void setMatchAfterByDate(Date JavaDoc matchAfter) {
240
241         //let date represent midnight
242

243         if (matchAfter != null) {
244
245             GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc();
246             cal.setTime(matchAfter);
247
248             cal.set(cal.HOUR_OF_DAY, cal.getActualMinimum(cal.HOUR_OF_DAY));
249             cal.set(cal.MINUTE, cal.getActualMinimum(cal.MINUTE));
250             cal.set(cal.SECOND, cal.getActualMinimum(cal.SECOND));
251             cal.set(cal.MILLISECOND, cal.getActualMinimum(cal.MILLISECOND));
252
253             matchAfter = cal.getTime();
254         }
255
256         //do it
257

258         this.matchAfter = matchAfter;
259         days = null;
260     }
261
262     // ------------ days handling ----------------------
263
/** Gets property <code>days</code> ad short.
264      * @return value of property day */

265     public Short JavaDoc getDaysAsShort() {
266         return days;
267     }
268
269     /** Getter for property <code>days</code>. */
270     public String JavaDoc getDays() {
271         if(days == null) {
272             return ""; // NOI18N
273
} else {
274             return days.toString();
275         }
276     }
277
278     /** Setter for property <code>days</code>.
279      * @param String to be parsed as short */

280     public void setDays(String JavaDoc days) {
281         try {
282             setDaysImpl(days);
283
284             setValid (this.days != null);
285         } catch (IllegalArgumentException JavaDoc ex) {
286             setValid(false);
287             throw ex;
288         }
289     }
290
291     /** Sets <code>days</code> property. Helper method. */
292     private void setDaysImpl(String JavaDoc days) {
293
294         if("".equals(days)) { // NOI18N
295
setDaysByShort(null);
296             return;
297         }
298
299         try {
300             DecimalFormat JavaDoc format = new DecimalFormat JavaDoc();
301             setDaysByShort(new Short JavaDoc(format.parse(days).shortValue()));
302
303         } catch (ParseException JavaDoc ex) {
304             throw new IllegalArgumentException JavaDoc();
305         }
306
307     }
308
309     /** Setter for property day.
310      * @param day new value of property day */

311     private void setDaysByShort(Short JavaDoc days) {
312         this.days = days;
313
314         matchAfter = null;
315         matchBefore = null;
316     }
317
318     /** Gets help context for this search type.
319      * Implements superclass abstract method. */

320     public HelpCtx getHelpCtx() {
321         return new HelpCtx(ModificationDateType.class);
322     }
323     
324     
325     /**
326      * Date using default locale formatting.
327      * Provide overridden constructor and toString() method. */

328     static class FormattedDate extends Date JavaDoc {
329
330         /** Default locale formattor. */
331         private static DateFormat JavaDoc format;
332         /** isNull property */
333         private transient boolean isNull = true;
334
335         /** Init static fields */
336         static {
337             format = new SimpleDateFormat JavaDoc().getDateInstance();
338         }
339
340         /** Create new object.
341         * @param date null is ambiguous -> today.
342         */

343         public FormattedDate(Date JavaDoc date) {
344             super(date == null ? new Date JavaDoc().getTime() : date.getTime() );
345             isNull = date == null;
346         }
347
348         /** Create new object.
349          * @param Use default locale formatting while parsing date */

350         public FormattedDate(String JavaDoc date) throws ParseException JavaDoc {
351             super( format.parse(date).getTime() );
352             isNull = date == null;
353         }
354
355         /** Use defalt locale formatting. */
356         public String JavaDoc toString() {
357             return format.format(this);
358         }
359
360         /**
361          * Extra handling of equals(null). Return true if
362          * this Date represents empty string value. */

363         public boolean equals(Object JavaDoc obj){
364             if (obj == null && isNull) return true;
365             else return super.equals(obj);
366         }
367     }
368     
369 }
370
Popular Tags