KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > search > types > ObjectTypeType


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
21 package org.netbeans.modules.search.types;
22
23
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.io.ObjectStreamField JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.openide.loaders.DataObject;
33 import org.openide.util.HelpCtx;
34 import org.openide.util.NbBundle;
35
36
37 /**
38  * Test DataObject loader match.
39  *
40  * @author Petr Kuzel
41  * @author Marian Petras
42  */

43 public class ObjectTypeType extends DataObjectType {
44
45     private static final long serialVersionUID = 1L;
46     //private static final long serialVersionUID = -8014986485098211562L;
47

48     /** stream replacing. */
49     private static final String JavaDoc NAMES_FIELD = "classNames"; //NOI18N
50

51     /** Serial persistent fields. */
52     private static final ObjectStreamField JavaDoc[] serialPersistentFields
53                          = {new ObjectStreamField JavaDoc(NAMES_FIELD, List JavaDoc.class)};
54
55     /** Which classes to search. */
56     private transient Class JavaDoc[] mask = new Class JavaDoc[0];
57                          
58
59     /**
60      */

61     protected String JavaDoc displayName() {
62         /*
63          * For all non-default search types, display name is taken from
64          * the search type's bean descriptor. But this search type has
65          * no bean descriptor so we must override this method.
66          */

67         
68         return NbBundle.getMessage(ObjectTypeType.class,
69                                    "TEXT_OBJECTTYPE_CRITERION"); //NOI18N
70
}
71
72     /** Test whether this data object meets criteria. */
73     public boolean testDataObject(DataObject dataObject) {
74         Class JavaDoc ld = dataObject.getLoader().getClass();
75
76         for (int i = 0; i < mask.length; i++) {
77             if (mask[i].isAssignableFrom(ld)) {
78                 return true;
79             }
80         }
81
82         return false;
83     }
84
85     /** Setter for mask property. */
86     public void setMask(Class JavaDoc[] mask) {
87         this.mask = mask;
88         firePropertyChange("mask", null, null); // NOI18N
89

90         setValid(this.mask.length != 0);
91     }
92
93     /** Getter for mask property. */
94     public Class JavaDoc[] getMask() {
95         return mask;
96     }
97     
98     /** Setter for mask property. */
99     public void setMask(int i, Class JavaDoc m) {
100         mask[i] = m;
101         firePropertyChange("mask", null, null); // NOI18N
102
setValid(true);
103     }
104     
105     /** Getter for mask property. */
106     public Class JavaDoc getMask(int i) {
107         return mask[i];
108     }
109
110     /** Gets help context for this search type.
111      * Implements superclass abstract method. */

112     public HelpCtx getHelpCtx() {
113         return new HelpCtx(ObjectTypeType.class);
114     }
115     
116     /** Store itself as sequence of class names. */
117     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
118         List JavaDoc classNames = new ArrayList JavaDoc(mask == null ? 0 : mask.length);
119         
120         if (mask != null) {
121             for (int i = 0; i < mask.length; i++) {
122                 if (mask[i] == null) {
123                     continue;
124                 }
125                 
126                 classNames.add(mask[i].getName());
127             }
128         }
129         
130         out.putFields().put(NAMES_FIELD, classNames);
131         out.writeFields();
132     }
133
134     /** Restore itself from sequence of class names. */
135     private void readObject(ObjectInputStream JavaDoc in)
136             throws IOException JavaDoc, ClassNotFoundException JavaDoc {
137         List JavaDoc classNames = (List JavaDoc) in.readFields().get(NAMES_FIELD, new ArrayList JavaDoc());
138
139         // fill mask array
140
List JavaDoc classes = new ArrayList JavaDoc(classNames.size());
141         ClassLoader JavaDoc classLoader = (ClassLoader JavaDoc)
142                                   org.openide.util.Lookup.getDefault()
143                                   .lookup(ClassLoader JavaDoc.class);
144         
145         Iterator JavaDoc it = classNames.iterator();
146         while (it.hasNext()) {
147             try {
148                 classes.add(Class.forName((String JavaDoc) it.next(),
149                                           false,
150                                           classLoader));
151             } catch (ClassNotFoundException JavaDoc cnfe) {
152                 cnfe.printStackTrace();
153             }
154         }
155         
156         mask = new Class JavaDoc[classes.size()];
157         classes.toArray(mask);
158
159         setValid(false);
160     }
161     
162 }
163
Popular Tags