KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > core > filter > DateFilterCondition


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.tasklist.core.filter;
21
22 import java.text.ParseException JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.Calendar JavaDoc;
25 import java.util.Date JavaDoc;
26 import javax.swing.*;
27
28
29 /**
30  * Basic condition class for date comparisons
31  *
32  * @author Tor Norbye
33  * @author tl
34  */

35 public class DateFilterCondition extends OneOfFilterCondition {
36     public static final int EQUALS = 0;
37     public static final int NOTEQUALS = 1;
38     public static final int EARLIERTHAN = 2;
39     public static final int LATERTHAN = 3;
40     public static final int ISTODAY = 4;
41     public static final int ISUNDEFINED = 5;
42        
43     private static String JavaDoc[] NAME_KEYS = {
44         "Equals", // NOI18N
45
"NotEquals", // NOI18N
46
"EarlierThan", // NOI18N
47
"LaterThan", // NOI18N
48
"IsToday", // NOI18N
49
"IsUndefined" // NOI18N
50
};
51     
52     /**
53      * Creates an array of filter conditions for the specified property
54      *
55      * @param index index of the property
56      */

57     public static DateFilterCondition[] createConditions() {
58         return new DateFilterCondition[] {
59             new DateFilterCondition(DateFilterCondition.EARLIERTHAN),
60             new DateFilterCondition(DateFilterCondition.LATERTHAN),
61             new DateFilterCondition(DateFilterCondition.EQUALS),
62             new DateFilterCondition(DateFilterCondition.NOTEQUALS),
63             new DateFilterCondition(DateFilterCondition.ISTODAY),
64             new DateFilterCondition(DateFilterCondition.ISUNDEFINED)
65         };
66     };
67     
68     /** saved constant for comparison */
69     private Date JavaDoc constant = new Date JavaDoc();
70     
71     /** Date format */
72     private static SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc();
73     
74     /** contains today's date 00:00 or -1 if not initialized */
75     private long today = -1;
76     
77     /**
78      * Creates a condition with the given name.
79      *
80      * @param prop index of the property this condition uses
81      * @param id one of the constants from this class
82      */

83     public DateFilterCondition(int id) {
84         super(NAME_KEYS, id);
85     }
86
87
88     public DateFilterCondition(final DateFilterCondition rhs) {
89         super(rhs);
90         this.constant = (rhs.constant == null) ? null : (Date JavaDoc)rhs.constant.clone();
91     }
92
93   
94     public Object JavaDoc clone() {
95         return new DateFilterCondition(this);
96     }
97     
98     /** for deconvertization **/
99     DateFilterCondition() {
100         super(NAME_KEYS);
101         this.constant = null;
102     }
103     
104     public JComponent createConstantComponent() {
105         if (getId() != ISTODAY && getId() != ISUNDEFINED) {
106             JTextField tf = new JTextField();
107             tf.setText(sdf.format(constant));
108             tf.setToolTipText(Util.getString("date_desc"));
109             return tf;
110         } else {
111             return null;
112         }
113     }
114
115     public void getConstantFrom(JComponent cmp) {
116         JTextField tf = (JTextField) cmp;
117         try {
118             constant = sdf.parse(tf.getText());
119         } catch (ParseException JavaDoc e) {
120             // ignore TODO
121
}
122     }
123     
124     public boolean isTrue(Object JavaDoc obj) {
125         int c;
126         if (obj != null)
127             c = ((Date JavaDoc) obj).compareTo(constant);
128         else
129             c = 0;
130         switch (getId()) {
131             case EQUALS:
132                 return obj != null && c == 0;
133             case NOTEQUALS:
134                 return obj != null && c != 0;
135             case EARLIERTHAN:
136                 return obj != null && c < 0;
137             case LATERTHAN:
138                 return obj != null && c > 0;
139             case ISTODAY: {
140                 if (obj == null)
141                     return false;
142                 
143                 if (today < 0 || System.currentTimeMillis() - today >=
144                     24 * 60 * 60 * 1000) {
145                     Calendar JavaDoc cal = Calendar.getInstance();
146                     cal.set(Calendar.HOUR_OF_DAY, 0);
147                     cal.set(Calendar.MINUTE, 0);
148                     cal.set(Calendar.SECOND, 0);
149                     cal.set(Calendar.MILLISECOND, 0);
150                     today = cal.getTimeInMillis();
151                 }
152                 long diff = ((Date JavaDoc) obj).getTime() - today;
153                 return diff >= 0 && diff < 24 * 60 * 60 * 1000;
154             }
155             case ISUNDEFINED:
156                 return obj == null;
157             default:
158                 throw new InternalError JavaDoc("wrong id");
159         }
160     }
161
162     private static class Convertor extends OneOfFilterCondition.Convertor {
163         private static final String JavaDoc ELEM_DATE_CONDITION = "DateCondition";
164         private static final String JavaDoc ATTR_DATE = "date";
165         
166         public Convertor() {
167             super(ELEM_DATE_CONDITION, NAME_KEYS);
168         }
169         
170         public static DateFilterCondition.Convertor create() {
171             return new DateFilterCondition.Convertor();
172         }
173         
174         protected Object JavaDoc readElement(org.w3c.dom.Element JavaDoc element) throws java.io.IOException JavaDoc, java.lang.ClassNotFoundException JavaDoc {
175             DateFilterCondition cond = new DateFilterCondition();
176             super.readCondition(element, cond);
177             cond.constant = new Date JavaDoc(Long.parseLong(element.getAttribute(ATTR_DATE)));
178             return cond;
179         }
180         
181         // write methods for supported condition types
182
protected void writeElement(org.w3c.dom.Document JavaDoc document, org.w3c.dom.Element JavaDoc element, Object JavaDoc obj)
183         throws java.io.IOException JavaDoc, org.w3c.dom.DOMException JavaDoc {
184             DateFilterCondition cond = (DateFilterCondition)obj;
185             super.writeElement(document, element, cond);
186             element.setAttribute(ATTR_DATE, Long.toString(cond.constant.getTime()));
187             
188         }
189     }
190 }
191
Popular Tags