KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.MessageFormat JavaDoc;
24 import java.util.*;
25 import org.netbeans.core.UIExceptions;
26 import org.openide.explorer.propertysheet.ExPropertyEditor;
27 import org.openide.explorer.propertysheet.PropertyEnv;
28
29 import org.openide.loaders.ExtensionList;
30 import org.openide.util.NbBundle;
31 import org.openide.nodes.Node;
32
33 /** Property editor for {@link ExtensionList}s.
34 *
35 * @see ExtensionList
36 *
37 * @author Jaroslav Tulach
38 * @version 0.11 November 11, 1997
39 */

40 public class ExtensionListEditor extends Object JavaDoc implements PropertyEditor,
41         ExPropertyEditor {
42     /** value to edit */
43     private ExtensionList value;
44     private PropertyChangeSupport support = new PropertyChangeSupport (this);
45     
46     private boolean editable=true;
47
48     /*
49     * Set (or change) the object that is to be edited. Builtin types such
50     * as "int" must be wrapped as the corresponding object type such as
51     * "java.lang.Integer".
52     *
53     * @param value The new target object to be edited. Note that this
54     * object should not be modified by the PropertyEditor, rather
55     * the PropertyEditor should create a new object to hold any
56     * modified value.
57     */

58     public void setValue(Object JavaDoc value) {
59         if ( value != null )
60             this.value = (ExtensionList)((ExtensionList)value).clone ();
61         else
62             this.value = null;
63         support.firePropertyChange (null, null, null);
64     }
65
66     /*
67     * @return The value of the property. Builtin types such as "int" will
68     * be wrapped as the corresponding object type such as "java.lang.Integer".
69     */

70     public Object JavaDoc getValue() {
71         return value;
72     }
73
74     /*
75     * @return True if the class will honor the paintValue method.
76     */

77     public boolean isPaintable() {
78         return false;
79     }
80
81     /*
82     * Paint a representation of the value into a given area of screen
83     * real estate. Note that the propertyEditor is responsible for doing
84     * its own clipping so that it fits into the given rectangle.
85     *
86
87     * If the PropertyEditor doesn't honor paint requests (see isPaintable)
88     * this method should be a silent noop.
89     *
90
91     * The given Graphics object will have the default font, color, etc of
92     * the parent container. The PropertyEditor may change graphics attributes
93     * such as font and color and doesn't need to restore the old values.
94     *
95     * @param gfx Graphics object to paint into.
96     * @param box Rectangle within graphics object into which we should paint.
97     */

98     public void paintValue (java.awt.Graphics JavaDoc gfx, java.awt.Rectangle JavaDoc box) {
99     }
100
101     /*
102     * This method is intended for use when generating Java code to set
103     * the value of the property. It should return a fragment of Java code
104     * that can be used to initialize a variable with the current property
105     * value.
106     *
107
108     * Example results are "2", "new Color(127,127,34)", "Color.orange", etc.
109     *
110     * @return A fragment of Java code representing an initializer for the
111     * current value.
112     */

113     public String JavaDoc getJavaInitializationString() {
114         return null;
115     }
116
117     /*
118     * @return The property value as a human editable string.
119     * Returns null if the value can't be expressed as an editable string.
120     * If a non-null value is returned, then the PropertyEditor should
121     * be prepared to parse that string back in setAsText().
122     */

123     public String JavaDoc getAsText () {
124         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
125         String JavaDoc[] exts = getStringArray ();
126         for (int i = 0; i < exts.length; i++) {
127             if (i > 0) sb.append (", "); // NOI18N
128
sb.append (exts[i]);
129         }
130         return sb.toString ();
131     }
132
133     /*
134     * Set the property value by parsing a given String. May raise
135     * java.lang.IllegalArgumentException if either the String is
136     * badly formatted or if this kind of property can't be expressed
137     * as text.
138     * @param text The string to be parsed.
139     */

140     public void setAsText (String JavaDoc text) throws java.lang.IllegalArgumentException JavaDoc {
141         try {
142         if ( NbBundle.getMessage ( org.openide.explorer.propertysheet.PropertyPanel.class, "CTL_Different_Values").equals( text ) ) {
143             // XXX huh?!
144
setValue( new String JavaDoc[] { text } );
145         }
146         else {
147             StringTokenizer st = new StringTokenizer (text, ",. \n\t"); // NOI18N
148
setAs (st);
149         }
150         } catch (Exception JavaDoc e) {
151             IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc (e.getMessage());
152             String JavaDoc msg = e.getLocalizedMessage();
153             if (msg == null) {
154                 msg = MessageFormat.format(
155                 NbBundle.getMessage(
156                     ExtensionListEditor.class, "FMT_EXC_GENERIC_BAD_VALUE"), //NOI18N
157
new Object JavaDoc[] {text});
158             }
159             UIExceptions.annotateUser(iae, iae.getMessage(), msg, e, new Date());
160             throw iae;
161         }
162     }
163
164     /*
165     * If the property value must be one of a set of known tagged values,
166     * then this method should return an array of the tags. This can
167     * be used to represent (for example) enum values. If a PropertyEditor
168     * supports tags, then it should support the use of setAsText with
169     * a tag value as a way of setting the value and the use of getAsText
170     * to identify the current value.
171     *
172     * @return The tag values for this property. May be null if this
173     * property cannot be represented as a tagged value.
174     *
175     */

176     public String JavaDoc[] getTags() {
177         return null;
178     }
179
180     /*
181     * A PropertyEditor may choose to make available a full custom Component
182     * that edits its property value. It is the responsibility of the
183     * PropertyEditor to hook itself up to its editor Component itself and
184     * to report property value changes by firing a PropertyChange event.
185     *
186
187     * The higher-level code that calls getCustomEditor may either embed
188     * the Component in some larger property sheet, or it may put it in
189     * its own individual dialog, or ...
190     *
191     * @return A java.awt.Component that will allow a human to directly
192     * edit the current property value. May be null if this is
193     * not supported.
194     */

195
196     public java.awt.Component JavaDoc getCustomEditor() {
197         return new ExtensionListCustomEditor(this);
198     }
199
200     /*
201     * @return True if the propertyEditor can provide a custom editor.
202     */

203     public boolean supportsCustomEditor() {
204         return true;
205     }
206
207     /*
208     * Register a listener for the PropertyChange event. When a
209     * PropertyEditor changes its value it should fire a PropertyChange
210     * event on all registered PropertyChangeListeners, specifying the
211     * null value for the property name and itself as the source.
212     *
213     * @param listener An object to be invoked when a PropertyChange
214     * event is fired.
215     */

216     public void addPropertyChangeListener(PropertyChangeListener listener) {
217         support.addPropertyChangeListener (listener);
218     }
219
220     /*
221     * Remove a listener for the PropertyChange event.
222     *
223     * @param listener The PropertyChange listener to be removed.
224     */

225     public void removePropertyChangeListener(PropertyChangeListener listener) {
226         support.removePropertyChangeListener (listener);
227     }
228
229     /** Used to acquire the current value from the PropertyEditor
230      * @return the current value of the property
231      */

232     @SuppressWarnings JavaDoc("unchecked") public String JavaDoc[] getStringArray() {
233         if (value == null) {
234             return new String JavaDoc[0];
235         }
236         List<String JavaDoc> l = new ArrayList<String JavaDoc> ();
237         Enumeration<String JavaDoc> e = (Enumeration<String JavaDoc>)value.extensions ();
238         while (e.hasMoreElements ())
239             l.add (e.nextElement ());
240         
241         
242         e = (Enumeration<String JavaDoc>)value.mimeTypes ();
243         while (e.hasMoreElements ())
244             l.add (e.nextElement ());
245         
246         Collections.sort (l);
247         return l.toArray (new String JavaDoc[l.size ()]);
248     }
249
250     /** Used to modify the current value in the PropertyEditor
251      * @param value the new value of the property
252      */

253     public void setStringArray(String JavaDoc[] nue) {
254         setAs (org.openide.util.Enumerations.array (nue));
255     }
256
257     
258     /** Sets the value as enumeration.
259      * @param en enumeration of strings
260      */

261     private void setAs (Enumeration en) {
262         ExtensionList list = new ExtensionList ();
263
264         while (en.hasMoreElements()) {
265             String JavaDoc tok = (String JavaDoc)en.nextElement ();
266             if (tok.indexOf('/') >= 0) {
267                 // mime type!?
268
list.addMimeType(tok);
269             } else {
270                 list.addExtension (tok);
271             }
272         }
273         value = list;
274         
275         support.firePropertyChange (null, null, null);
276     }
277
278     /** gets information if the text in editor should be editable or not */
279     public boolean isEditable(){
280         return (editable);
281     }
282     
283     public void attachEnv(PropertyEnv env) {
284         FeatureDescriptor desc = env.getFeatureDescriptor();
285         if (desc instanceof Node.Property){
286             Node.Property prop = (Node.Property)desc;
287             editable = prop.canWrite();
288         }
289     }
290 }
291
Popular Tags