KickJava   Java API By Example, From Geeks To Geeks.

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

41 public class ModifierEditor extends JPanel JavaDoc implements ExPropertyEditor {
42
43     /** Instance of custom property editor - visual panel. */
44     private ModifierPanel panel;
45
46     /** Serial version UID */
47     static final long serialVersionUID = 6324048239020120791L;
48     
49     /** Current mask */
50     private int mask;
51
52     /** Current value */
53     private int modifier;
54     
55     private PropertyEnv env;
56
57     /** Creates new modifiers editor with full mask.
58     */

59     public ModifierEditor() {
60         this(ModifierPanel.EDITABLE_MASK);
61     }
62
63     /** Creates new modifiers editor.
64     * @param mask The mask of modifier values which should be possible to change.
65     */

66     public ModifierEditor(int mask) {
67         modifier = 0;
68         setMask(mask & ModifierPanel.EDITABLE_MASK);
69
70         getAccessibleContext().setAccessibleDescription(getString("ACSD_ModifierPanel"));
71     }
72     
73     public void addNotify() {
74         setLayout(new BorderLayout());
75         panel = new ModifierPanel(this);
76         panel.setMnemonics(env);
77         add(panel, BorderLayout.CENTER);
78         
79         super.addNotify();
80     }
81
82     public void removeNotify() {
83         super.removeNotify();
84         if (panel != null) {
85             remove(panel);
86             panel = null;
87         }
88     }
89     
90     /** Getter for property mask.
91      *@return Value of property mask.
92      */

93     int getMask() {
94         return mask;
95     }
96
97     /** Set the mask of editable modifiers.
98      * @param mask new value of the mask.
99      */

100     public void setMask(int mask) {
101         if (this.mask != mask) {
102             int oldMask = this.mask;
103             this.mask = mask & ModifierPanel.EDITABLE_MASK;
104             firePropertyChange (ModifierPanel.PROP_MASK, new Integer JavaDoc (oldMask), new Integer JavaDoc (mask));
105             setModifier(modifier & mask);
106         }
107     }
108     
109     /** Getter for property modifier.
110      *@return Value of property modifier.
111      */

112     int getModifier() {
113         return modifier;
114     }
115
116     /** Setter for property modifier.
117      *@param modifier New value of property modifier.
118      */

119     void setModifier(int modifier) {
120         if (this.modifier != modifier) {
121             int oldModifier = this.modifier;
122             this.modifier = modifier;
123             // for our panel
124
firePropertyChange (ModifierPanel.PROP_MODIFIER, new Integer JavaDoc (oldModifier), new Integer JavaDoc (modifier));
125             // for the outside world
126
firePropertyChange(ElementProperties.PROP_MODIFIERS, new Integer JavaDoc (oldModifier), new Integer JavaDoc (modifier));
127         }
128     }
129
130     /** Set new value */
131     public void setValue(Object JavaDoc object) throws IllegalArgumentException JavaDoc {
132         if (object == null) {
133             setModifier(0);
134             return;
135         }
136         if (object instanceof Integer JavaDoc) {
137             setModifier(((Integer JavaDoc) object).intValue());
138         }
139         else {
140             throw new IllegalArgumentException JavaDoc();
141         }
142     }
143
144     /** @return the java source code representation
145     * of the current value.
146     */

147     public String JavaDoc getJavaInitializationString() {
148         return new Integer JavaDoc(getModifier()).toString();
149     }
150
151     /** Get the value */
152     public Object JavaDoc getValue() {
153         return new Integer JavaDoc(getModifier());
154     }
155
156     /** @return <CODE>false</CODE> */
157     public boolean isPaintable() {
158         return false;
159     }
160
161     /** Does nothing. */
162     public void paintValue(Graphics g, Rectangle rectangle) {
163     }
164
165     /** @return textual representition of current value of the modifiers. */
166     public String JavaDoc getAsText() {
167         return Modifier.toString(getModifier());
168     }
169
170     /** Parse the text and sets the modifier editor value */
171     public void setAsText(String JavaDoc string) throws IllegalArgumentException JavaDoc {
172         int newValue = 0;
173         int oldValue = modifier;
174
175         StringTokenizer JavaDoc tukac = new StringTokenizer JavaDoc(string, ", ", false); // NOI18N
176
while (tukac.hasMoreTokens()) {
177             String JavaDoc token = tukac.nextToken();
178             boolean known = false;
179             for (int i = 0; i < ModifierPanel.MODIFIER_COUNT; i++) {
180                 if ((ModifierPanel.MODIFIER_VALUES[i] & mask) != 0) {
181                     if (token.equals(ModifierPanel.MODIFIER_NAMES[i])) {
182                         if (((ModifierPanel.MODIFIER_VALUES[i] == Modifier.FINAL) && ((newValue & Modifier.ABSTRACT) != 0)) ||
183                                 ((ModifierPanel.MODIFIER_VALUES[i] == Modifier.ABSTRACT) && ((newValue & Modifier.FINAL) != 0)))
184                             break;
185                         newValue |= ModifierPanel.MODIFIER_VALUES[i];
186                         known = true;
187                         break;
188                     }
189                 }
190             }
191             if ((newValue & ModifierPanel.ACCESS_MASK) == 0) {
192                 for (int i = 1; i <= 3; i++) {
193                     if ((ModifierPanel.ACCESS_VALUES[i] & mask) != 0) {
194                         if (token.equals(ModifierPanel.ACCESS_NAMES[i])) {
195                             newValue |= ModifierPanel.ACCESS_VALUES[i];
196                             known = true;
197                             break;
198                         }
199                     }
200                 }
201             }
202             if (!known) {
203                 IllegalArgumentException JavaDoc x = new IllegalArgumentException JavaDoc(
204                     "Invalid modifier: " + token); // NOI18N
205
String JavaDoc message = java.text.MessageFormat.format(
206                     getString("MSG_IllegalModifierString"), // NOI18N
207
new Object JavaDoc[] { token });
208                 ErrorManager.getDefault().annotate(x,
209             ErrorManager.USER, null, message, null, null);
210                 throw x;
211             }
212         }
213         if (oldValue != newValue) {
214             modifier = newValue;
215             firePropertyChange(ModifierPanel.PROP_MODIFIER,
216                     new Integer JavaDoc(oldValue), new Integer JavaDoc(modifier));
217         }
218     }
219
220     /** @return <CODE>null</CODE> */
221     public String JavaDoc[] getTags() {
222         return null;
223     }
224
225     /** @return <CODE>this</CODE> */
226     public Component getCustomEditor() {
227         return this;
228     }
229
230     /** @return <CODE>true</CODE> */
231     public boolean supportsCustomEditor() {
232         return true;
233     }
234
235     /** Get the customized property value.
236      * @return the property value
237      * @exception InvalidStateException when the custom property editor does not contain a valid property value
238      * (and thus it should not be set)
239      */

240     public Object JavaDoc getPropertyValue() throws IllegalStateException JavaDoc {
241         return getValue();
242     }
243
244     /**
245      * This method is called by the IDE to pass
246      * the environment to the property editor.
247      */

248     public void attachEnv(PropertyEnv env) {
249         this.env = env;
250     }
251      
252     private static String JavaDoc getString(String JavaDoc key) {
253         return NbBundle.getBundle("org.openide.explorer.propertysheet.editors.Bundle2", Locale.getDefault(), ModifierEditor.class.getClassLoader()).getString(key);
254     }
255 }
256
Popular Tags