KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > beaninfo > editors > ServiceTypeEditor


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.beaninfo.editors;
21
22 import java.beans.*;
23 import java.util.Enumeration JavaDoc;
24
25 import org.openide.ServiceType;
26 import org.openide.explorer.propertysheet.*;
27 import org.openide.util.Lookup;
28
29
30 /** Support for property editor for Executor.
31 *
32 * @author Jaroslav Tulach
33 */

34 @SuppressWarnings JavaDoc("deprecation")
35 public class ServiceTypeEditor extends java.beans.PropertyEditorSupport JavaDoc implements ExPropertyEditor {
36
37     /** Name of the custom property that can be passed in PropertyEnv. */
38     private static final String JavaDoc PROPERTY_NEW_TYPE = "createNew"; // NOI18N
39
/** Name of the custom property that can be passed in PropertyEnv. */
40     private static final String JavaDoc PROPERTY_SUPERCLASS = "superClass"; // NOI18N
41

42     /** tagx */
43     private String JavaDoc[] tags;
44
45     /** class to work on */
46     private Class JavaDoc<? extends ServiceType> clazz;
47
48     /** message key to be used in custom editor */
49     private String JavaDoc message;
50     
51     /** Environment passed to the ExPropertyEditor*/
52     private PropertyEnv env;
53     
54     /**
55      * This variable can be read in attachEnv. Defaults to false,
56      * false - we are selecting from the registered service types, true - creating
57      * new instances of the services
58      */

59     private boolean createNewInstance = false;
60
61     /** constructs new property editor.
62     */

63     public ServiceTypeEditor() {
64         this (ServiceType.class, "LAB_ChooseServiceType"); // NOI18N
65
}
66
67     /** constructs new property editor.
68      * @param clazz the class to use
69      * @param message the message for custom editor
70      */

71     public ServiceTypeEditor(Class JavaDoc<?> clazz, String JavaDoc message) {
72         this.clazz = clazz.asSubclass(ServiceType.class);
73         this.message = message;
74     }
75
76     /**
77      * This method is called by the IDE to pass
78      * the environment to the property editor.
79      * @param env Environment passed by the ide.
80      */

81     public void attachEnv(PropertyEnv env) {
82         this.env = env;
83         Object JavaDoc newObj = env.getFeatureDescriptor().getValue(PROPERTY_NEW_TYPE);
84         if (newObj instanceof Boolean JavaDoc) {
85             createNewInstance = ((Boolean JavaDoc)newObj).booleanValue();
86         }
87         Object JavaDoc sup = env.getFeatureDescriptor().getValue(PROPERTY_SUPERCLASS);
88         if (sup instanceof Class JavaDoc) {
89             @SuppressWarnings JavaDoc("unchecked") Class JavaDoc<? extends ServiceType> c = (Class JavaDoc<? extends ServiceType>)sup;
90         clazz = c;
91         }
92     }
93     
94     /** Updates the list of executors.
95      */

96     private void updateTags () {
97         java.util.LinkedList JavaDoc<String JavaDoc> names = new java.util.LinkedList JavaDoc<String JavaDoc> ();
98         ServiceType.Registry registry = Lookup.getDefault ()
99                 .lookup (ServiceType.Registry.class);
100         Enumeration JavaDoc ee = registry.services (clazz);
101         while (ee.hasMoreElements()) {
102             ServiceType e = (ServiceType) ee.nextElement();
103             names.add(e.getName());
104         }
105         names.toArray(tags = new String JavaDoc[names.size()]);
106     }
107
108     //----------------------------------------------------------------------
109

110     /**
111     * @return The property value as a human editable string.
112     * <p> Returns null if the value can't be expressed as an editable string.
113     * <p> If a non-null value is returned, then the PropertyEditor should
114     * be prepared to parse that string back in setAsText().
115     */

116     public String JavaDoc getAsText() {
117         if (createNewInstance) {
118             return null;
119         }
120         ServiceType s = (ServiceType)getValue ();
121         if (s == null) {
122             return getString ("LAB_DefaultServiceType");
123         } else {
124             return s.getName();
125         }
126     }
127
128     /** Set the property value by parsing a given String. May raise
129     * java.lang.IllegalArgumentException if either the String is
130     * badly formatted or if this kind of property can't be expressed
131     * as text.
132     * @param text The string to be parsed.
133     */

134     public void setAsText(String JavaDoc text) {
135         if (createNewInstance) {
136             // new instance cannot be entered as a text
137
throw new IllegalArgumentException JavaDoc();
138         }
139
140         ServiceType.Registry registry = Lookup.getDefault().lookup(ServiceType.Registry.class);
141         Enumeration JavaDoc en = registry.services (clazz);
142         while (en.hasMoreElements ()) {
143             ServiceType t = (ServiceType)en.nextElement ();
144             if (text.equals (t.getName ())) {
145                 setValue (t);
146                 return;
147             }
148         }
149         setValue (null);
150     }
151
152     /** @return tags */
153     public String JavaDoc[] getTags() {
154         if (!createNewInstance) {
155             updateTags ();
156             return tags;
157         }
158         return null;
159     }
160
161     public boolean supportsCustomEditor () {
162         return true;
163     }
164
165     public java.awt.Component JavaDoc getCustomEditor () {
166         final ServiceTypePanel s = new ServiceTypePanel (clazz, getString(message), /*none*/ null, createNewInstance);
167
168         s.setServiceType ((ServiceType)getValue ());
169         // [PENDING] why is this here? Cancel does not work correctly because of this, I think:
170
s.addPropertyChangeListener (new PropertyChangeListener () {
171                                          public void propertyChange (PropertyChangeEvent ev) {
172                                              if ("serviceType".equals (ev.getPropertyName ())) {
173                                                  setValue (s.getServiceType ());
174                                              }
175                                          }
176                                      });
177         return s;
178     }
179
180     private static String JavaDoc getString (String JavaDoc s) {
181         return org.openide.util.NbBundle.getBundle (ServiceTypeEditor.class).getString (s);
182     }
183 }
184
Popular Tags