KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.PropertyEditor JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.text.ParseException JavaDoc;
26 import java.text.SimpleDateFormat JavaDoc;
27 import java.util.Date JavaDoc;
28 import javax.swing.*;
29 import javax.swing.event.DocumentListener JavaDoc;
30 import javax.swing.event.DocumentEvent JavaDoc;
31
32 import org.openide.ErrorManager;
33 import org.openide.nodes.Node;
34 import org.openide.util.NbBundle;
35
36 /**
37  * Basic condition class for string comparisons.
38  * This class can also be used to compare objects of classes other than
39  * strings. It uses getAsText() to convert properties to strings.
40  *
41  * @author Tor Norbye
42  */

43 public class StringFilterCondition extends OneOfFilterCondition {
44     public static final int EQUALS = 0;
45     public static final int NOTEQUALS = 1;
46     public static final int CONTAINS = 2;
47     public static final int DOESNOTCONTAIN = 3;
48     public static final int BEGINSWITH = 4;
49     public static final int ENDSWITH = 5;
50     public static final int CEQUALS = 6;
51     public static final int CCONTAINS = 7;
52     public static final int CDOESNOTCONTAIN = 8;
53     public static final int CBEGINSWITH = 9;
54     public static final int CENDSWITH = 10;
55     
56     /**
57      * Creates an array of filter conditions for the specified property
58      *
59      * @param index index of the property
60      */

61     public static StringFilterCondition[] createConditions() {
62         return new StringFilterCondition[] {
63             new StringFilterCondition(StringFilterCondition.CONTAINS),
64             new StringFilterCondition(StringFilterCondition.DOESNOTCONTAIN),
65             new StringFilterCondition(StringFilterCondition.BEGINSWITH),
66             new StringFilterCondition(StringFilterCondition.ENDSWITH),
67             new StringFilterCondition(StringFilterCondition.EQUALS),
68             new StringFilterCondition(StringFilterCondition.NOTEQUALS),
69             new StringFilterCondition(StringFilterCondition.CCONTAINS),
70             new StringFilterCondition(StringFilterCondition.CDOESNOTCONTAIN),
71             new StringFilterCondition(StringFilterCondition.CBEGINSWITH),
72             new StringFilterCondition(StringFilterCondition.CENDSWITH),
73             new StringFilterCondition(StringFilterCondition.CEQUALS)
74         };
75     }
76     
77     private static String JavaDoc[] NAME_KEYS = {
78         "Equals", // NOI18N
79
"NotEquals", // NOI18N
80
"Contains", // NOI18N
81
"DoesNotContain", // NOI18N
82
"BeginsWith", // NOI18N
83
"EndsWith", // NOI18N
84
"CEquals", // NOI18N
85
"CContains", // NOI18N
86
"CDoesNotContain", // NOI18N
87
"CBeginsWith", // NOI18N
88
"CEndsWith" // NOI18N
89
};
90     
91     /** saved constant for comparison */
92     private String JavaDoc constant = ""; // NOI18N
93

94     /**
95      * Creates a condition with the given name.
96      *
97      * @param id one of the constants from this class
98      */

99     public StringFilterCondition(int id) {
100         super(NAME_KEYS, id);
101     }
102
103     /**
104      * Creates a condition with the given name.
105      *
106      * @param id one of the constants from this class
107      * @param value the value to compare the property with
108      */

109     public StringFilterCondition(int id, String JavaDoc value) {
110         this(id);
111         this.constant = value;
112     }
113
114     
115     public StringFilterCondition(final StringFilterCondition rhs) {
116         super(rhs);
117         this.constant = rhs.constant;
118     }
119     
120     public Object JavaDoc clone() {
121         return new StringFilterCondition(this);
122     }
123
124     private StringFilterCondition() { super(NAME_KEYS); constant = null; }
125     
126     /** Return the value that Strings are compared with.
127      * @return the value that Strings are compared with */

128     public String JavaDoc getConstant() {
129         return constant;
130     }
131
132     public JComponent createConstantComponent() {
133         final JTextField tf = new JTextField();
134         tf.setText(constant);
135         tf.setToolTipText(Util.getString("string_desc"));
136         tf.getDocument().addDocumentListener(new DocumentListener JavaDoc() {
137             public void changedUpdate(DocumentEvent JavaDoc e) {
138                 Boolean JavaDoc valid = Boolean.valueOf("".equals(tf.getText()) == false);
139                 tf.putClientProperty(FilterCondition.PROP_VALUE_VALID, valid);
140             }
141
142             public void insertUpdate(DocumentEvent JavaDoc e) {
143                 Boolean JavaDoc valid = Boolean.valueOf("".equals(tf.getText()) == false);
144                 tf.putClientProperty(FilterCondition.PROP_VALUE_VALID, valid);
145             }
146
147             public void removeUpdate(DocumentEvent JavaDoc e) {
148                 Boolean JavaDoc valid = Boolean.valueOf("".equals(tf.getText()) == false);
149                 tf.putClientProperty(FilterCondition.PROP_VALUE_VALID, valid);
150             }
151
152         });
153         return tf;
154     }
155
156     public void getConstantFrom(JComponent cmp) {
157         JTextField tf = (JTextField) cmp;
158         constant = tf.getText();
159     }
160     
161     public boolean isTrue(Object JavaDoc obj) {
162         String JavaDoc s = obj == null ? "" : obj.toString();
163         switch (getId()) {
164             case EQUALS:
165                 return s.equalsIgnoreCase(constant);
166             case NOTEQUALS:
167                 return !s.equalsIgnoreCase(constant);
168             case CONTAINS:
169                 return s.toLowerCase().indexOf(constant.toLowerCase()) >= 0;
170             case DOESNOTCONTAIN:
171                 return s.toLowerCase().indexOf(constant.toLowerCase()) < 0;
172             case BEGINSWITH:
173                 return s.toLowerCase().startsWith(constant.toLowerCase());
174             case ENDSWITH:
175                 return s.toLowerCase().endsWith(constant.toLowerCase());
176             case CEQUALS:
177                 return s.equals(constant);
178             case CCONTAINS:
179                 return s.indexOf(constant) >= 0;
180             case CDOESNOTCONTAIN:
181                 return s.indexOf(constant) < 0;
182             case CBEGINSWITH:
183                 return s.startsWith(constant);
184             case CENDSWITH:
185                 return s.endsWith(constant);
186             default:
187                 throw new InternalError JavaDoc("wrong id");
188         }
189     }
190
191   private static class Convertor extends OneOfFilterCondition.Convertor {
192     private static final String JavaDoc ELEM_STRING_CONDITION = "StringCondition";
193     private static final String JavaDoc ATTR_CONSTANT = "constant";
194
195     public Convertor() { super(ELEM_STRING_CONDITION, NAME_KEYS);}
196     public static StringFilterCondition.Convertor create() {
197       return new StringFilterCondition.Convertor();
198     }
199
200     protected Object JavaDoc readElement(org.w3c.dom.Element JavaDoc element) throws java.io.IOException JavaDoc, java.lang.ClassNotFoundException JavaDoc {
201       StringFilterCondition cond = new StringFilterCondition();
202       super.readCondition(element, cond);
203       cond.constant = element.getAttribute(ATTR_CONSTANT);
204       return cond;
205     }
206    
207     // write methods for supported condition types
208
protected void writeElement(org.w3c.dom.Document JavaDoc document, org.w3c.dom.Element JavaDoc element, Object JavaDoc obj)
209       throws java.io.IOException JavaDoc, org.w3c.dom.DOMException JavaDoc
210     {
211       StringFilterCondition cond = (StringFilterCondition)obj;
212       super.writeCondition(document, element, cond);
213       element.setAttribute(ATTR_CONSTANT, cond.constant);
214
215     }
216   }
217
218 }
219
Popular Tags