KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.netbeans.jmi.javamodel.Attribute;
23 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
24 import org.netbeans.modules.java.ui.nodes.editors.TypeEditor;
25 import org.openide.nodes.Children;
26 import org.openide.nodes.Sheet;
27 import org.openide.nodes.Node;
28 import org.openide.src.ElementProperties;
29 import org.openide.ErrorManager;
30 import org.openide.util.NbBundle;
31
32 import javax.jmi.reflect.JmiException;
33 import java.lang.reflect.InvocationTargetException JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.HashMap JavaDoc;
36
37 /** Node for a annotation type method.
38  * @see org.netbeans.jmi.javamodel.Attribute
39  * @author Jan Pokorsky
40  */

41 public final class AnnotationTypeMethodNode 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_RETURN); // NOI18N
50
mapAttributeName.put("defaultValueText", PROP_DEFAULT_VALUE); // NOI18N
51
}
52     
53     /** Create a new method node.
54     * @param element method element to represent
55     * @param writeable <code>true</code> to be writable
56     */

57     public AnnotationTypeMethodNode(Attribute element, boolean writeable) {
58         super(element, Children.LEAF, writeable);
59         setElementFormat0(getElementFormatProperty());
60         superSetName(element.getName());
61     }
62
63     protected String JavaDoc resolveIconBase() {
64         return IconResolver.getIconBaseForAttribute(getMethod());
65     }
66
67     protected ElementFormat getElementFormatProperty() {
68         return getSourceOptions().getAnnTypeMethodElementFormat();
69     }
70
71     /* This method resolve the appropriate hint format for the type
72     * of the element. It defines the short description.
73     */

74     protected ElementFormat getHintElementFormat() {
75         return getSourceOptions().getAnnTypeMethodElementLongFormat();
76     }
77
78     protected Map JavaDoc getAttributeNameMap() {
79         return mapAttributeName;
80     }
81
82     /* Creates property set for this node */
83     protected Sheet createSheet () {
84         Sheet sheet = Sheet.createDefault();
85         Sheet.Set ps = sheet.get(Sheet.PROPERTIES);
86         ps.put(createModifiersProperty(writeable));
87         ps.put(createNameProperty(getMethod()));
88         ps.put(createReturnProperty(writeable));
89         ps.put(createDefaultValueProperty(writeable));
90         return sheet;
91     }
92
93     /** Create a property for the method default value.
94     * @param canW <code>false</code> to force property to be read-only
95     * @return the property
96     */

97     protected Node.Property createDefaultValueProperty(boolean canW) {
98         return new ElementNode.ElementProp(PROP_DEFAULT_VALUE, String JavaDoc.class, canW) {
99
100             public Object JavaDoc getValue () {
101                 String JavaDoc val = getMethod().getDefaultValueText();
102                 return val != null? val: ""; // NOI18N
103
}
104             
105             /** Sets the value */
106             public void setValue(final Object JavaDoc val) throws IllegalArgumentException JavaDoc,
107                    IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
108                
109                 super.setValue(val);
110                 if (!(val instanceof String JavaDoc)) throw new IllegalArgumentException JavaDoc();
111                 
112                 boolean fail = true;
113                 try {
114                     JavaMetamodel.getDefaultRepository().beginTrans(true);
115                     try {
116                         getMethod().setDefaultValueText((String JavaDoc) val);
117                         fail = false;
118                     } finally {
119                         JavaMetamodel.getDefaultRepository().endTrans(fail);
120                     }
121                 } catch (JmiException e) {
122                     IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc();
123                     iae.initCause(e);
124                     ErrorManager.getDefault().annotate(iae, ErrorManager.USER, null,
125                             NbBundle.getMessage(AnnotationTypeMethodNode.class, "MSG_InvalidDefaultVal"), null, null); // NOI18N
126
throw iae;
127                 }
128             }
129         };
130     }
131
132     /** Create a property for the method return value.
133     * @param canW <code>false</code> to force property to be read-only
134     * @return the property
135     */

136     private Node.Property createReturnProperty(boolean canW) {
137         Node.Property np = createTypeProperty(PROP_RETURN, getMethod(), canW);
138         np.setValue(TypeEditor.ANN_TYPE_EDITOR, Boolean.TRUE);
139         return np;
140     }
141     
142     private Attribute getMethod() {
143         return (Attribute) this.element;
144     }
145 }
146
Popular Tags