KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Component JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.text.ParseException JavaDoc;
25 import java.text.SimpleDateFormat JavaDoc;
26 import java.util.Date JavaDoc;
27 import javax.swing.*;
28
29 import org.openide.ErrorManager;
30 import org.openide.nodes.Node;
31 import org.openide.util.NbBundle;
32
33 /**
34  * Basic condition class for comparisons of integer values.
35  *
36  * @author Tor Norbye
37  * @author tl
38  */

39 public class IntegerFilterCondition extends OneOfFilterCondition {
40     public static final int EQUALS = 0;
41     public static final int NOTEQUALS = 1;
42     public static final int LESSTHAN = 2;
43     public static final int LESSOREQUALS = 3;
44     public static final int GREATERTHAN = 4;
45     public static final int GREATEROREQUALS = 5;
46        
47     /**
48      * Creates an array of filter conditions for the specified property
49      *
50      * @param index index of the property
51      */

52     public static IntegerFilterCondition[] createConditions() {
53         return new IntegerFilterCondition[] {
54             new IntegerFilterCondition(IntegerFilterCondition.EQUALS),
55             new IntegerFilterCondition(IntegerFilterCondition.NOTEQUALS),
56             new IntegerFilterCondition(IntegerFilterCondition.LESSTHAN),
57             new IntegerFilterCondition(IntegerFilterCondition.LESSOREQUALS),
58             new IntegerFilterCondition(IntegerFilterCondition.GREATERTHAN),
59             new IntegerFilterCondition(IntegerFilterCondition.GREATEROREQUALS)
60         };
61     };
62     
63     private static String JavaDoc[] NAME_KEYS = {
64         "Equals", // NOI18N
65
"NotEquals", // NOI18N
66
"LessThan", // NOI18N
67
"LessEquals", // NOI18N
68
"GreaterThan", // NOI18N
69
"GreaterEquals", // NOI18N
70
};
71     
72     /** saved constant for comparison */
73     private int constant;
74     
75     /**
76      * Creates a condition with the given name.
77      *
78      * @param prop index of the property this condition uses
79      * @param id one of the constants from this class
80      */

81     public IntegerFilterCondition(int id) {
82         super(NAME_KEYS, id);
83     }
84     
85     public IntegerFilterCondition(final IntegerFilterCondition rhs) {
86         super(rhs);
87         this.constant = rhs.constant;
88     }
89     
90     public Object JavaDoc clone() {
91         return new IntegerFilterCondition(this);
92     }
93
94     /** for deconvertization **/
95     private IntegerFilterCondition() {super(NAME_KEYS); this.constant = -1; }
96
97     public JComponent createConstantComponent() {
98         JTextField tf = new JTextField();
99         tf.setText(String.valueOf(constant));
100         tf.setToolTipText(Util.getString("int_desc"));
101         return tf;
102     }
103
104     public void getConstantFrom(JComponent cmp) {
105         JTextField tf = (JTextField) cmp;
106         try {
107             constant = Integer.parseInt(tf.getText());
108         } catch (NumberFormatException JavaDoc e) {
109             // ignore TODO
110
}
111     }
112     
113     public boolean isTrue(Object JavaDoc obj) {
114         int n = ((Integer JavaDoc) obj).intValue();
115         switch (getId()) {
116             case EQUALS:
117                 return constant == n;
118             case NOTEQUALS:
119                 return constant != n;
120             case LESSTHAN:
121                 return n < constant;
122             case LESSOREQUALS:
123                 return n <= constant;
124             case GREATERTHAN:
125                 return n > constant;
126             case GREATEROREQUALS:
127                 return n >= constant;
128             default:
129                 throw new InternalError JavaDoc("wrong id");
130         }
131     }
132
133   private static class Convertor extends OneOfFilterCondition.Convertor {
134     private static final String JavaDoc ELEM_INTEGER_CONDITION = "IntegerCondition";
135     private static final String JavaDoc ATTR_CONSTANT = "constant";
136
137     public Convertor() { super(ELEM_INTEGER_CONDITION, NAME_KEYS);}
138     public static IntegerFilterCondition.Convertor create() {
139       return new IntegerFilterCondition.Convertor();
140     }
141
142     protected Object JavaDoc readElement(org.w3c.dom.Element JavaDoc element)
143       throws java.io.IOException JavaDoc, java.lang.ClassNotFoundException JavaDoc
144     {
145       IntegerFilterCondition cond = new IntegerFilterCondition();
146       super.readCondition(element, cond);
147       cond.constant = Integer.parseInt(element.getAttribute(ATTR_CONSTANT));
148       return cond;
149     }
150    
151     // write methods for supported condition types
152
protected void writeElement(org.w3c.dom.Document JavaDoc document, org.w3c.dom.Element JavaDoc element, Object JavaDoc obj)
153       throws java.io.IOException JavaDoc, org.w3c.dom.DOMException JavaDoc
154     {
155       IntegerFilterCondition cond = (IntegerFilterCondition)obj;
156       super.writeCondition(document, element, cond);
157       element.setAttribute(ATTR_CONSTANT, Integer.toString(cond.constant));
158     }
159   }
160
161 }
162
Popular Tags