KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.openide.util.NbBundle;
23
24 /**
25  * Base class for all conditions that have a list of options to choose
26  * from the mode of operation, e.g. {Contains, Equals,..},
27  * {Greater, Equal}, {True, NotTrue},etc.
28  *
29  * @author Tor Norbye
30  * @author tl
31  */

32 public abstract class OneOfFilterCondition extends FilterCondition {
33     
34     private String JavaDoc[] options = null;
35     private int id; // the selected option from nameKeys
36

37     /**
38      * Creates a condition with the given set of options and selected option.
39      *
40      * @param opts the set of options to choose from
41      * @param id one of the constants from this class
42      */

43     public OneOfFilterCondition(String JavaDoc [] opts, int id) {
44         this.options = opts;
45         this.id = id;
46     }
47     
48     public OneOfFilterCondition(final OneOfFilterCondition rhs) {
49         super(rhs);
50         this.options = rhs.options;
51         this.id = rhs.id;
52     }
53     
54     protected OneOfFilterCondition(String JavaDoc [] opts) {
55         this.options = opts;
56         this.id = -1;
57     }
58     
59     
60     public boolean sameType(FilterCondition fc) {
61         return super.sameType(fc) && this.id == ((OneOfFilterCondition)fc).id;
62     }
63     
64     protected String JavaDoc getDisplayName() {
65         return NbBundle.getMessage(this.getClass(), options[id]);
66     }
67     
68     protected int getId() { return id;}
69     
70     
71     protected abstract static class Convertor extends FilterCondition.Convertor {
72         private static final String JavaDoc ATTR_ID = "id";
73         
74         private String JavaDoc [] nameKeys;
75         
76         public Convertor(String JavaDoc root, String JavaDoc [] nameKeys) {
77             super(root);
78             this.nameKeys = nameKeys;
79         }
80         
81         /**
82          * Override if you want to use the default readElement and
83          * writeElement
84          */

85         protected OneOfFilterCondition createCondition() {
86             throw new UnsupportedOperationException JavaDoc();
87         }
88         
89         protected void readCondition(org.w3c.dom.Element JavaDoc element, OneOfFilterCondition cond)
90                 throws java.io.IOException JavaDoc, java.lang.ClassNotFoundException JavaDoc {
91             super.readCondition(element, cond);
92             cond.id = findIndex(element.getAttribute(ATTR_ID), nameKeys);
93         }
94         
95         protected Object JavaDoc readElement(org.w3c.dom.Element JavaDoc element)
96                 throws java.io.IOException JavaDoc, java.lang.ClassNotFoundException JavaDoc {
97             OneOfFilterCondition cond = createCondition();
98             this.readCondition(element, cond);
99             return cond;
100         }
101         
102         // write methods for supported condition types
103
protected void writeCondition(org.w3c.dom.Document JavaDoc document, org.w3c.dom.Element JavaDoc element, OneOfFilterCondition cond)
104                 throws java.io.IOException JavaDoc, org.w3c.dom.DOMException JavaDoc {
105             super.writeCondition(document, element, cond);
106             element.setAttribute(ATTR_ID, nameKeys[cond.id]);
107         }
108         
109         // write methods for supported condition types
110
protected void writeElement(org.w3c.dom.Document JavaDoc document, org.w3c.dom.Element JavaDoc element, Object JavaDoc obj)
111                 throws java.io.IOException JavaDoc, org.w3c.dom.DOMException JavaDoc {
112             this.writeCondition(document, element, (OneOfFilterCondition)obj);
113         }
114         
115         /** A utility function for searching for a string in an array of
116          * string.
117          * @param str String to find
118          * @param arr array to search
119          * @return the index if str in arr or throws IOException when no found
120          */

121         protected static int findIndex(String JavaDoc str, String JavaDoc[] arr) throws java.io.IOException JavaDoc {
122             for (int i = 0 ; i < arr.length; i++)
123                 if (arr[i].equals(str)) return i;
124             
125             throw new java.io.IOException JavaDoc("The value " + str + " not found in the array " + arr);
126         }
127     }
128 }
129
Popular Tags