KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > ui > nodes > 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.java.ui.nodes.editors;
21
22 import java.awt.*;
23 import java.lang.reflect.Modifier JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import javax.swing.*;
27
28 import org.openide.src.ElementProperties;
29 import org.openide.explorer.propertysheet.ExPropertyEditor;
30 import org.openide.explorer.propertysheet.PropertyEnv;
31 import org.openide.ErrorManager;
32 import org.openide.util.NbBundle;
33
34 /** Property editors for java modifiers.
35 *
36 * @author Petr Hamernik
37 */

38 public final class ModifierEditor extends JPanel implements ExPropertyEditor {
39
40     /**
41      * attribute name for the feature descriptor of the property environment
42      * @see #ACCESS_MODIFIERS_CUSTOM_EDITOR
43      * @see #OTHERS_MODIFIERS_CUSTOM_EDITOR
44      */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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