KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > ui > nodes > elements > FieldNode


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.elements;
21
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.HashMap JavaDoc;
25
26 import org.openide.nodes.*;
27 import org.openide.ErrorManager;
28 import org.openide.src.ElementProperties;
29 import org.openide.util.NbBundle;
30 import org.netbeans.jmi.javamodel.*;
31 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
32
33 import javax.jmi.reflect.JmiException;
34
35 /** Node representing a field (variable).
36 * @see org.netbeans.jmi.javamodel.Field
37 * @author Petr Hamernik, Jan Pokorsky
38  *
39  * XXX help ids
40 */

41 public final class FieldNode extends ElementNode {
42
43     private static final Map JavaDoc mapAttributeName;
44     
45     static {
46         mapAttributeName = new HashMap JavaDoc();
47         mapAttributeName.put(PROP_MODIFIERS, PROP_MODIFIERS);
48         mapAttributeName.put(ElementProperties.PROP_NAME, ElementProperties.PROP_NAME);
49         mapAttributeName.put("typeName", PROP_TYPE); // NOI18N
50
mapAttributeName.put("initialValueText", PROP_INIT_VALUE); // NOI18N
51
}
52     
53     /** Create a new field node.
54     * @param element field element to represent
55     * @param writeable <code>true</code> to be writable
56     */

57     public FieldNode(Field element, boolean writeable) {
58         super(element, Children.LEAF, writeable);
59         setElementFormat0(getElementFormatProperty());
60         superSetName(element.getName());
61     }
62
63     /* Resolve the current icon base.
64     * @return icon base string.
65     */

66     protected String JavaDoc resolveIconBase() {
67         return IconResolver.getIconBaseForField(getField());
68     }
69
70     protected ElementFormat getElementFormatProperty() {
71         return getSourceOptions().getFieldElementFormat();
72     }
73
74     /* This method resolve the appropriate hint format for the type
75     * of the element. It defines the short description.
76     */

77     protected ElementFormat getHintElementFormat() {
78         return getSourceOptions().getFieldElementLongFormat();
79     }
80
81     protected Map JavaDoc getAttributeNameMap() {
82         return mapAttributeName;
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(getField()));
91         ps.put(createTypeProperty(writeable));
92         ps.put(createInitValueProperty(writeable));
93         return sheet;
94     }
95
96     /** Create a property for the field type.
97     * @param canW <code>false</code> to force property to be read-only
98     * @return the property
99     */

100     protected Node.Property createTypeProperty(boolean canW) {
101         Node.Property prop = createTypeProperty(PROP_TYPE, getField(), canW);
102         prop.setValue("acceptVoidType", Boolean.FALSE); // NOI18N
103
return prop;
104     }
105
106     /** Create a property for the field init value.
107     * @param canW <code>false</code> to force property to be read-only
108     * @return the property
109     */

110     protected Node.Property createInitValueProperty(boolean canW) {
111         return new ElementNode.ElementProp(PROP_INIT_VALUE, String JavaDoc.class, canW) {
112
113             public Object JavaDoc getValue () {
114                 String JavaDoc val = getField().getInitialValueText();
115                 return val != null? val: ""; // NOI18N
116
}
117             
118             /** Sets the value */
119             public void setValue(final Object JavaDoc val) throws IllegalArgumentException JavaDoc,
120                    IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
121                
122                 super.setValue(val);
123                 if (!(val instanceof String JavaDoc)) throw new IllegalArgumentException JavaDoc();
124                 
125                 boolean fail = true;
126                 try {
127                     JavaMetamodel.getDefaultRepository().beginTrans(true);
128                     try {
129                         getField().setInitialValueText((String JavaDoc) val);
130                         fail = false;
131                     } finally {
132                         JavaMetamodel.getDefaultRepository().endTrans(fail);
133                     }
134                 } catch (JmiException e) {
135                     IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc();
136                     iae.initCause(e);
137                     ErrorManager.getDefault().annotate(iae, ErrorManager.USER, null,
138                             NbBundle.getMessage(FieldNode.class, "MSG_InvalidInitVal"), null, null); // NOI18N
139
throw iae;
140                 }
141             }
142         };
143     }
144     
145     private Field getField() {
146         return (Field) this.element;
147     }
148
149 }
150
Popular Tags