KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > editors > ModifierEditor


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.form.editors;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Graphics JavaDoc;
25 import java.awt.Rectangle JavaDoc;
26 import java.lang.reflect.Modifier JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28 import javax.swing.JPanel JavaDoc;
29 import org.openide.ErrorManager;
30 import org.openide.explorer.propertysheet.ExPropertyEditor;
31 import org.openide.explorer.propertysheet.PropertyEnv;
32 import org.openide.util.NbBundle;
33
34 /** Property editors for java modifiers.
35 *
36 * @author Petr Hamernik
37 */

38 public class ModifierEditor extends JPanel JavaDoc implements ExPropertyEditor {
39
40     public static final String JavaDoc PROP_MODIFIERS = "modifiers"; // NOI18N
41
/**
42      * attribute name for the feature descriptor of the property environment
43      * @see #ACCESS_MODIFIERS_CUSTOM_EDITOR
44      * @see #OTHERS_MODIFIERS_CUSTOM_EDITOR
45      */

46     public static final String JavaDoc CUSTOM_EDITOR_TYPE = "customEditorType"; // NOI18N
47
/** if it is set as feature descriptor's attribute value in the environment then the getCustomComponent returns
48      * the combo box for access modifiers
49      */

50     public static final Integer JavaDoc ACCESS_MODIFIERS_CUSTOM_EDITOR = new Integer JavaDoc(0);
51     /** if it is set as feature descriptor's attribute value in the environment then the getCustomComponent returns
52      * the panel containing other modifiers than the access modifiers.
53      */

54     public static final Integer JavaDoc OTHERS_MODIFIERS_CUSTOM_EDITOR = new Integer JavaDoc(1);
55     /** if it is set as feature descriptor's attribute value in the environment then the getCustomComponent returns
56      * the panel containing full range of modifiers
57      */

58     public static final Integer JavaDoc FULL_CUSTOM_EDITOR = new Integer JavaDoc(2);
59
60     /** Instance of custom property editor - visual panel. */
61     private ModifierPanel panel;
62
63     /** Current mask */
64     private int mask;
65
66     /** Current value */
67     private int modifier;
68     
69     private PropertyEnv env;
70     
71     /**
72      * @see #getType
73      */

74     private Object JavaDoc type;
75     
76     /** Creates new modifiers editor with full mask.
77     */

78     public ModifierEditor() {
79         this(ModifierPanel.EDITABLE_MASK);
80     }
81
82     /** Creates new modifiers editor.
83     * @param mask The mask of modifier values which should be possible to change.
84     */

85     public ModifierEditor(int mask) {
86         modifier = 0;
87         setMask(mask & ModifierPanel.EDITABLE_MASK);
88     }
89     
90     private Component JavaDoc customComponent;
91     
92     public void addNotify() {
93         setLayout(new BorderLayout JavaDoc());
94         panel = new ModifierPanel(this);
95         Object JavaDoc type = getType();
96         if (ACCESS_MODIFIERS_CUSTOM_EDITOR == type) {
97             customComponent = panel.getAccessComponent();
98         } else if (OTHERS_MODIFIERS_CUSTOM_EDITOR == type) {
99             customComponent = panel.getModifiersComponent();
100         } else {
101             customComponent = panel.getCompactComponent();
102         }
103         add(customComponent, BorderLayout.CENTER);
104         
105         super.addNotify();
106     }
107
108     public void removeNotify() {
109         super.removeNotify();
110         if (panel != null) {
111             remove(customComponent);
112             panel = null;
113         }
114     }
115     
116     /** Getter for property mask.
117      *@return Value of property mask.
118      */

119     int getMask() {
120         return mask;
121     }
122
123     /** Set the mask of editable modifiers.
124      * @param mask new value of the mask.
125      */

126     public void setMask(int mask) {
127         if (this.mask != mask) {
128             int oldMask = this.mask;
129             this.mask = mask & ModifierPanel.EDITABLE_MASK;
130             firePropertyChange (ModifierPanel.PROP_MASK, new Integer JavaDoc (oldMask), new Integer JavaDoc (mask));
131             setModifier(modifier & mask);
132         }
133     }
134     
135     /** Getter for property modifier.
136      *@return Value of property modifier.
137      */

138     int getModifier() {
139         return modifier;
140     }
141
142     /** Setter for property modifier.
143      *@param modifier New value of property modifier.
144      */

145     void setModifier(int modifier) {
146         if (this.modifier != modifier) {
147             int oldModifier = this.modifier;
148             this.modifier = modifier;
149             // for our panel
150
firePropertyChange (ModifierPanel.PROP_MODIFIER, new Integer JavaDoc (oldModifier), new Integer JavaDoc (modifier));
151             // for the outside world
152
firePropertyChange(PROP_MODIFIERS, new Integer JavaDoc (oldModifier), new Integer JavaDoc (modifier));
153         }
154     }
155     
156     /**
157      * @return type of the editor
158      * @see #ACCESS_MODIFIERS_CUSTOM_EDITOR
159      * @see #OTHERS_MODIFIERS_CUSTOM_EDITOR
160      * @see #FULL_CUSTOM_EDITOR
161      */

162     Object JavaDoc getType() {
163         return type;
164     }
165
166     /** Set new value */
167     public void setValue(Object JavaDoc object) throws IllegalArgumentException JavaDoc {
168         if (object == null) {
169             setModifier(0);
170             return;
171         }
172         if (object instanceof Integer JavaDoc) {
173             setModifier(((Integer JavaDoc) object).intValue());
174         }
175         else {
176             throw new IllegalArgumentException JavaDoc();
177         }
178     }
179
180     /** @return the java source code representation
181     * of the current value.
182     */

183     public String JavaDoc getJavaInitializationString() {
184         return new Integer JavaDoc(getModifier()).toString();
185     }
186
187     /** Get the value */
188     public Object JavaDoc getValue() {
189         return new Integer JavaDoc(getModifier());
190     }
191
192     /** @return <CODE>false</CODE> */
193     public boolean isPaintable() {
194         return false;
195     }
196
197     /** Does nothing. */
198     public void paintValue(Graphics JavaDoc g, Rectangle JavaDoc rectangle) {
199     }
200
201     /** @return textual representition of current value of the modifiers. */
202     public String JavaDoc getAsText() {
203         return Modifier.toString(getModifier());
204     }
205
206     /** Parse the text and sets the modifier editor value */
207     public void setAsText(String JavaDoc string) throws IllegalArgumentException JavaDoc {
208         int newValue = 0;
209         int oldValue = modifier;
210
211         StringTokenizer JavaDoc tukac = new StringTokenizer JavaDoc(string, ", ", false); // NOI18N
212
while (tukac.hasMoreTokens()) {
213             String JavaDoc token = tukac.nextToken();
214             boolean known = false;
215             for (int i = 0; i < ModifierPanel.MODIFIER_COUNT; i++) {
216                 if ((ModifierPanel.MODIFIER_VALUES[i] & mask) != 0) {
217                     if (token.equals(ModifierPanel.MODIFIER_NAMES[i])) {
218                         if (((ModifierPanel.MODIFIER_VALUES[i] == Modifier.FINAL) && ((newValue & Modifier.ABSTRACT) != 0)) ||
219                                 ((ModifierPanel.MODIFIER_VALUES[i] == Modifier.ABSTRACT) && ((newValue & Modifier.FINAL) != 0)))
220                             break;
221                         newValue |= ModifierPanel.MODIFIER_VALUES[i];
222                         known = true;
223                         break;
224                     }
225                 }
226             }
227             if ((newValue & ModifierPanel.ACCESS_MASK) == 0) {
228                 for (int i = 1; i <= 3; i++) {
229                     if ((ModifierPanel.ACCESS_VALUES[i] & mask) != 0) {
230                         if (token.equals(ModifierPanel.ACCESS_NAMES[i])) {
231                             newValue |= ModifierPanel.ACCESS_VALUES[i];
232                             known = true;
233                             break;
234                         }
235                     }
236                 }
237             }
238             if (!known) {
239                 IllegalArgumentException JavaDoc x = new IllegalArgumentException JavaDoc(
240                     "Invalid modifier: " + token); // NOI18N
241
String JavaDoc message = java.text.MessageFormat.format(
242                     getString("MSG_IllegalModifierString"), // NOI18N
243
new Object JavaDoc[] { token });
244                 ErrorManager.getDefault().annotate(x,
245             ErrorManager.USER, null, message, null, null);
246                 throw x;
247             }
248         }
249         if (oldValue != newValue) {
250             modifier = newValue;
251             firePropertyChange(ModifierPanel.PROP_MODIFIER,
252                     new Integer JavaDoc(oldValue), new Integer JavaDoc(modifier));
253         }
254     }
255
256     /** @return <CODE>null</CODE> */
257     public String JavaDoc[] getTags() {
258         return null;
259     }
260
261     /** @return <CODE>this</CODE> */
262     public Component JavaDoc getCustomEditor() {
263         return this;
264     }
265     
266     /** @return <CODE>true</CODE> */
267     public boolean supportsCustomEditor() {
268         return true;
269     }
270
271     /** Get the customized property value.
272      * @return the property value
273      * @exception IllegalStateException when the custom property editor does not contain a valid property value
274      * (and thus it should not be set)
275      */

276     public Object JavaDoc getPropertyValue() throws IllegalStateException JavaDoc {
277         return getValue();
278     }
279
280     /**
281      * This method is called by the IDE to pass
282      * the environment to the property editor.
283      */

284     public void attachEnv(PropertyEnv env) {
285         this.env = env;
286         type = env.getFeatureDescriptor().getValue(CUSTOM_EDITOR_TYPE);
287         if (type == null) {
288             type = FULL_CUSTOM_EDITOR;
289         } else if (ACCESS_MODIFIERS_CUSTOM_EDITOR.equals(type)) {
290             type = ACCESS_MODIFIERS_CUSTOM_EDITOR;
291         } else if (OTHERS_MODIFIERS_CUSTOM_EDITOR.equals(type)) {
292             type = OTHERS_MODIFIERS_CUSTOM_EDITOR;
293         } else {
294             type = FULL_CUSTOM_EDITOR;
295         }
296         
297     }
298      
299     private static String JavaDoc getString(String JavaDoc key) {
300         return NbBundle.getMessage(ModifierEditor.class, key);
301     }
302 }
303
Popular Tags