KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > filter > DurationFilterCondition


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.usertasks.filter;
21
22 import javax.swing.BorderFactory JavaDoc;
23 import javax.swing.JComponent JavaDoc;
24 import javax.swing.UIManager JavaDoc;
25
26 import org.netbeans.modules.tasklist.core.filter.OneOfFilterCondition;
27 import org.netbeans.modules.tasklist.usertasks.DurationPanel;
28 import org.openide.util.NbBundle;
29
30 /**
31  * Condition class for comparisons of durations.
32  *
33  * @author tl
34  */

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

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

77     public DurationFilterCondition(int id) {
78         super(NAME_KEYS, id);
79     }
80     
81     /**
82      * Copy constructor.
83      */

84     public DurationFilterCondition( final DurationFilterCondition rhs) {
85         super(rhs);
86         this.constant = rhs.constant;
87     }
88     
89     public Object JavaDoc clone() {
90         return new DurationFilterCondition(this);
91     }
92     
93     public JComponent JavaDoc createConstantComponent() {
94         DurationPanel dp = new DurationPanel();
95         dp.setOpaque(false);
96         dp.setDuration(constant);
97         dp.setToolTipText(NbBundle.getMessage(
98                 DurationFilterCondition.class, "duration_desc")); // NOI18N
99
return dp;
100     }
101     
102     public void getConstantFrom(JComponent JavaDoc cmp) {
103         DurationPanel dp = (DurationPanel) cmp;
104         constant = dp.getDuration();
105     }
106     
107     public boolean isTrue(Object JavaDoc obj) {
108         int n = ((Integer JavaDoc) obj).intValue();
109         switch (getId()) {
110         case EQUALS:
111             return constant == n;
112         case NOTEQUALS:
113             return constant != n;
114         case LESSTHAN:
115             return n < constant;
116         case LESSOREQUALS:
117             return n <= constant;
118         case GREATERTHAN:
119             return n > constant;
120         case GREATEROREQUALS:
121             return n >= constant;
122         default:
123             throw new InternalError JavaDoc("wrong id"); // NOI18N
124
}
125     }
126     
127     private static class Convertor extends OneOfFilterCondition.Convertor {
128         private static final String JavaDoc ELEM_DATE_CONDITION = "DurationCondition";
129         private static final String JavaDoc ATTR_DATE = "duration";
130         
131         public Convertor() {
132             super(ELEM_DATE_CONDITION, NAME_KEYS);
133         }
134         
135         public static DurationFilterCondition.Convertor create() {
136             return new DurationFilterCondition.Convertor();
137         }
138         
139         protected Object JavaDoc readElement(org.w3c.dom.Element JavaDoc element)
140                 throws java.io.IOException JavaDoc, java.lang.ClassNotFoundException JavaDoc {
141             DurationFilterCondition cond = new DurationFilterCondition(EQUALS);
142             super.readCondition(element, cond);
143             cond.constant = Integer.parseInt(element.getAttribute(ATTR_DATE));
144             return cond;
145         }
146         
147         protected void writeElement(org.w3c.dom.Document JavaDoc document,
148                 org.w3c.dom.Element JavaDoc element, Object JavaDoc obj)
149                 throws java.io.IOException JavaDoc, org.w3c.dom.DOMException JavaDoc {
150             DurationFilterCondition cond = (DurationFilterCondition)obj;
151             super.writeElement(document, element, cond);
152             element.setAttribute(ATTR_DATE, Integer.toString(cond.constant));
153         }
154     }
155 }
156
Popular Tags