001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.beaninfo.editors;
043:
044: import java.beans.*;
045: import java.util.Enumeration;
046:
047: import org.openide.ServiceType;
048: import org.openide.explorer.propertysheet.*;
049: import org.openide.util.Lookup;
050:
051: /** Support for property editor for Executor.
052: *
053: * @author Jaroslav Tulach
054: */
055: @SuppressWarnings("deprecation")
056: public class ServiceTypeEditor extends java.beans.PropertyEditorSupport
057: implements ExPropertyEditor {
058:
059: /** Name of the custom property that can be passed in PropertyEnv. */
060: private static final String PROPERTY_NEW_TYPE = "createNew"; // NOI18N
061: /** Name of the custom property that can be passed in PropertyEnv. */
062: private static final String PROPERTY_SUPERCLASS = "superClass"; // NOI18N
063:
064: /** tagx */
065: private String[] tags;
066:
067: /** class to work on */
068: private Class<? extends ServiceType> clazz;
069:
070: /** message key to be used in custom editor */
071: private String message;
072:
073: /** Environment passed to the ExPropertyEditor*/
074: private PropertyEnv env;
075:
076: /**
077: * This variable can be read in attachEnv. Defaults to false,
078: * false - we are selecting from the registered service types, true - creating
079: * new instances of the services
080: */
081: private boolean createNewInstance = false;
082:
083: /** constructs new property editor.
084: */
085: public ServiceTypeEditor() {
086: this (ServiceType.class, "LAB_ChooseServiceType"); // NOI18N
087: }
088:
089: /** constructs new property editor.
090: * @param clazz the class to use
091: * @param message the message for custom editor
092: */
093: public ServiceTypeEditor(Class<?> clazz, String message) {
094: this .clazz = clazz.asSubclass(ServiceType.class);
095: this .message = message;
096: }
097:
098: /**
099: * This method is called by the IDE to pass
100: * the environment to the property editor.
101: * @param env Environment passed by the ide.
102: */
103: public void attachEnv(PropertyEnv env) {
104: this .env = env;
105: Object newObj = env.getFeatureDescriptor().getValue(
106: PROPERTY_NEW_TYPE);
107: if (newObj instanceof Boolean) {
108: createNewInstance = ((Boolean) newObj).booleanValue();
109: }
110: Object sup = env.getFeatureDescriptor().getValue(
111: PROPERTY_SUPERCLASS);
112: if (sup instanceof Class) {
113: @SuppressWarnings("unchecked")
114: Class<? extends ServiceType> c = (Class<? extends ServiceType>) sup;
115: clazz = c;
116: }
117: }
118:
119: /** Updates the list of executors.
120: */
121: private void updateTags() {
122: java.util.LinkedList<String> names = new java.util.LinkedList<String>();
123: ServiceType.Registry registry = Lookup.getDefault().lookup(
124: ServiceType.Registry.class);
125: Enumeration ee = registry.services(clazz);
126: while (ee.hasMoreElements()) {
127: ServiceType e = (ServiceType) ee.nextElement();
128: names.add(e.getName());
129: }
130: names.toArray(tags = new String[names.size()]);
131: }
132:
133: //----------------------------------------------------------------------
134:
135: /**
136: * @return The property value as a human editable string.
137: * <p> Returns null if the value can't be expressed as an editable string.
138: * <p> If a non-null value is returned, then the PropertyEditor should
139: * be prepared to parse that string back in setAsText().
140: */
141: public String getAsText() {
142: if (createNewInstance) {
143: return null;
144: }
145: ServiceType s = (ServiceType) getValue();
146: if (s == null) {
147: return getString("LAB_DefaultServiceType");
148: } else {
149: return s.getName();
150: }
151: }
152:
153: /** Set the property value by parsing a given String. May raise
154: * java.lang.IllegalArgumentException if either the String is
155: * badly formatted or if this kind of property can't be expressed
156: * as text.
157: * @param text The string to be parsed.
158: */
159: public void setAsText(String text) {
160: if (createNewInstance) {
161: // new instance cannot be entered as a text
162: throw new IllegalArgumentException();
163: }
164:
165: ServiceType.Registry registry = Lookup.getDefault().lookup(
166: ServiceType.Registry.class);
167: Enumeration en = registry.services(clazz);
168: while (en.hasMoreElements()) {
169: ServiceType t = (ServiceType) en.nextElement();
170: if (text.equals(t.getName())) {
171: setValue(t);
172: return;
173: }
174: }
175: setValue(null);
176: }
177:
178: /** @return tags */
179: public String[] getTags() {
180: if (!createNewInstance) {
181: updateTags();
182: return tags;
183: }
184: return null;
185: }
186:
187: public boolean supportsCustomEditor() {
188: return true;
189: }
190:
191: public java.awt.Component getCustomEditor() {
192: final ServiceTypePanel s = new ServiceTypePanel(clazz,
193: getString(message), /*none*/null, createNewInstance);
194:
195: s.setServiceType((ServiceType) getValue());
196: // [PENDING] why is this here? Cancel does not work correctly because of this, I think:
197: s.addPropertyChangeListener(new PropertyChangeListener() {
198: public void propertyChange(PropertyChangeEvent ev) {
199: if ("serviceType".equals(ev.getPropertyName())) {
200: setValue(s.getServiceType());
201: }
202: }
203: });
204: return s;
205: }
206:
207: private static String getString(String s) {
208: return org.openide.util.NbBundle.getBundle(
209: ServiceTypeEditor.class).getString(s);
210: }
211: }
|