KickJava   Java API By Example, From Geeks To Geeks.

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


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.BorderLayout JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import javax.swing.*;
26
27 import org.netbeans.modules.tasklist.client.SuggestionPriority;
28 import org.netbeans.modules.tasklist.core.Task;
29 import org.netbeans.modules.tasklist.core.checklist.CheckList;
30 import org.netbeans.modules.tasklist.core.checklist.CheckListModel;
31 import org.netbeans.modules.tasklist.core.checklist.DefaultCheckListModel;
32 import org.openide.util.NbBundle;
33
34 /**
35  * "Priority is" - condition
36  *
37  * @author tl
38  */

39 public class PriorityCondition extends FilterCondition {
40     /**
41      * Creates an array of filter conditions for the specified property
42      *
43      * @param index index of the property
44      */

45     public static PriorityCondition[] createConditions() {
46         return new PriorityCondition[] {
47             new PriorityCondition()
48         };
49     };
50     
51     
52     private static int NPRIORITIES = 5;
53     
54     // 5 = Number of different priorities
55
protected boolean[] priorities = new boolean[NPRIORITIES];
56     
57     /**
58      * Creates a new instance
59      *
60      * @param prop index of a property
61      */

62     public PriorityCondition() {
63         Arrays.fill(priorities, true);
64     }
65     
66         
67     public PriorityCondition(final PriorityCondition rhs) {
68         super(rhs);
69         // copy priorities
70
for (int i = 0; i < NPRIORITIES; i++) this.priorities[i] = rhs.priorities[i];
71     }
72     
73     public Object JavaDoc clone() {
74         return new PriorityCondition(this);
75     }
76     
77     public boolean isTrue(Object JavaDoc o1) {
78         SuggestionPriority k = (SuggestionPriority) o1;
79         return priorities[k.intValue() - 1];
80     }
81     
82     public JComponent createConstantComponent() {
83         CheckList list = new CheckList(
84             new DefaultCheckListModel(
85                 priorities, SuggestionPriority.getPriorityNames()
86             )
87         );
88         JPanel panel = new JPanel(new BorderLayout JavaDoc());
89         panel.setOpaque(false);
90         panel.setBorder(BorderFactory.createCompoundBorder(
91             UIManager.getBorder("TextField.border"), // NOI18N
92
BorderFactory.createEmptyBorder(2, 2, 2, 2)
93         ));
94         panel.add(list, BorderLayout.CENTER);
95         panel.setToolTipText(Util.getString("prio_desc"));
96
97         list.getAccessibleContext().setAccessibleName(NbBundle.getMessage(PriorityCondition.class, "LBL_PriorityCheckList"));
98         list.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(PriorityCondition.class, "LBL_PriorityCheckList"));
99         
100         return panel;
101     }
102     
103     public void getConstantFrom(JComponent cmp) {
104         // Nothing to do. The array of booleans will not be cloned in
105
// DefaultCheckListModel
106
}
107
108     protected String JavaDoc getDisplayName() {
109       return NbBundle.getMessage(PriorityCondition.class, "IsOneOf");
110     }
111
112
113   private static class Convertor extends FilterCondition.Convertor {
114     private static final String JavaDoc ELEM_PRIORITY_CONDITION = "PriorityCondition";
115     private static final String JavaDoc ATTR_PRIORITIES = "priorities";
116
117     public Convertor() { super(ELEM_PRIORITY_CONDITION);}
118     public static PriorityCondition.Convertor create() {
119       return new PriorityCondition.Convertor();
120     }
121
122     protected Object JavaDoc readElement(org.w3c.dom.Element JavaDoc element) throws java.io.IOException JavaDoc, java.lang.ClassNotFoundException JavaDoc {
123       PriorityCondition cond = new PriorityCondition();
124       super.readCondition(element, cond);
125
126       String JavaDoc spriorities = element.getAttribute(ATTR_PRIORITIES);
127       boolean bpriorities [] = new boolean[NPRIORITIES];
128       for (int i = 0; i < NPRIORITIES; i++) bpriorities[i] = (spriorities.charAt(i) == '+');
129       cond.priorities = bpriorities;
130
131       return cond;
132     }
133    
134     // write methods for supported condition types
135
protected void writeElement(org.w3c.dom.Document JavaDoc document, org.w3c.dom.Element JavaDoc element, Object JavaDoc obj)
136       throws java.io.IOException JavaDoc, org.w3c.dom.DOMException JavaDoc
137     {
138       PriorityCondition cond = (PriorityCondition)obj;
139       super.writeCondition(document, element, cond);
140
141       StringBuffer JavaDoc str = new StringBuffer JavaDoc(NPRIORITIES);
142       for (int i = 0 ; i < NPRIORITIES ; i++) str.append( cond.priorities[i] ? '+' : '-');
143       element.setAttribute(ATTR_PRIORITIES, str.toString());
144     }
145   }
146
147
148 }
149
Popular Tags