KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > src > nodes > FieldElementNode


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.src.nodes;
21
22 import java.awt.Component JavaDoc;
23 import java.beans.*;
24 import java.io.IOException JavaDoc;
25 import java.lang.reflect.Modifier JavaDoc;
26 import java.lang.reflect.InvocationTargetException JavaDoc;
27
28 import org.openide.src.*;
29 import org.openide.nodes.*;
30
31 /** Node representing a field (variable).
32 * @see FieldElement
33 * @author Petr Hamernik
34 */

35 public class FieldElementNode extends MemberElementNode {
36     /** Create a new field node.
37     * @param element field element to represent
38     * @param writeable <code>true</code> to be writable
39     */

40     public FieldElementNode(FieldElement element, boolean writeable) {
41         super(element, Children.LEAF, writeable);
42         setElementFormat0(sourceOptions.getFieldElementFormat());
43     }
44
45     public org.openide.util.HelpCtx getHelpCtx () {
46         return new org.openide.util.HelpCtx ("org.openide.src.nodes.FieldNode"); // NOI18N
47
}
48
49     /* Resolve the current icon base.
50     * @return icon base string.
51     */

52     protected String JavaDoc resolveIconBase() {
53         int modif = ((FieldElement)element).getModifiers();
54         if (!Modifier.isStatic(modif)) {
55             // non-static field...
56
if (Modifier.isPrivate(modif))
57                 return FIELD_PRIVATE;
58             else if (Modifier.isProtected(modif))
59                 return FIELD_PROTECTED;
60             else if (Modifier.isPublic(modif))
61                 return FIELD_PUBLIC;
62             else
63                 return FIELD_PACKAGE;
64         }
65         else {
66             // static field...
67
if (Modifier.isPrivate(modif))
68                 return FIELD_ST_PRIVATE;
69             else if (Modifier.isProtected(modif))
70                 return FIELD_ST_PROTECTED;
71             else if (Modifier.isPublic(modif))
72                 return FIELD_ST_PUBLIC;
73             else
74                 return FIELD_ST_PACKAGE;
75         }
76     }
77
78     /* This method resolve the appropriate hint format for the type
79     * of the element. It defines the short description.
80     */

81     protected ElementFormat getHintElementFormat() {
82         return sourceOptions.getFieldElementLongFormat();
83     }
84
85     /* Creates property set for this node */
86     protected Sheet createSheet () {
87         Sheet sheet = Sheet.createDefault();
88         Sheet.Set ps = sheet.get(Sheet.PROPERTIES);
89         ps.put(createModifiersProperty(writeable));
90         ps.put(createNameProperty(writeable));
91         ps.put(createTypeProperty(writeable));
92         ps.put(createInitValueProperty(writeable));
93         return sheet;
94     }
95
96     /* Removes the element from the class and calls superclass.
97     *
98     * @exception IOException if SourceException is thrown
99     * from the underlayed Element.
100     */

101     public void destroy() throws IOException JavaDoc {
102         SourceEditSupport.invokeAtomicAsUser(element, new SourceEditSupport.ExceptionalRunnable() {
103                                                  public void run() throws SourceException {
104                                                      FieldElement el = (FieldElement) element;
105                                                      el.getDeclaringClass().removeField(el);
106                                                  }
107                                              });
108         super.destroy();
109     }
110
111     public Component JavaDoc getCustomizer() {
112         return new FieldCustomizer((FieldElement)element);
113     }
114
115     public boolean hasCustomizer() {
116         return isWriteable();
117     }
118
119     /** Create a property for the field type.
120     * @param canW <code>false</code> to force property to be read-only
121     * @return the property
122     */

123     protected Node.Property createTypeProperty(boolean canW) {
124         Node.Property prop = new ElementProp(PROP_TYPE, Type.class, canW) {
125                    /** Gets the value */
126                    public Object JavaDoc getValue () {
127                        return ((FieldElement)element).getType();
128                    }
129
130                    /** Sets the value */
131                    public void setValue(final Object JavaDoc val) throws IllegalArgumentException JavaDoc,
132                        IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
133                        super.setValue(val);
134                        if (!(val instanceof Type))
135                            throw new IllegalArgumentException JavaDoc();
136
137                        runAtomic(element, new SourceEditSupport.ExceptionalRunnable() {
138                                      public void run() throws SourceException {
139                                          ((FieldElement)element).setType((Type)val);
140                                      }
141                                  });
142                    }
143                };
144        prop.setValue("acceptVoidType", Boolean.FALSE); // NOI18N
145
return prop;
146     }
147
148     /** Create a property for the field init value.
149     * @param canW <code>false</code> to force property to be read-only
150     * @return the property
151     */

152     protected Node.Property createInitValueProperty(boolean canW) {
153         return new ElementProp(PROP_INIT_VALUE, String JavaDoc.class, canW) {
154                    /** Gets the value */
155                    public Object JavaDoc getValue () {
156                        return ((FieldElement)element).getInitValue();
157                    }
158
159                    /** Sets the value */
160                    public void setValue(final Object JavaDoc val) throws IllegalArgumentException JavaDoc,
161                        IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
162                        super.setValue(val);
163                        if (!(val instanceof String JavaDoc))
164                            throw new IllegalArgumentException JavaDoc();
165
166                        runAtomic(element, new SourceEditSupport.ExceptionalRunnable() {
167                                      public void run() throws SourceException {
168                                          ((FieldElement)element).setInitValue((String JavaDoc)val);
169                                      }
170                                  });
171                    }
172                };
173     }
174 }
175
Popular Tags