KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Modifier JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27 import org.openide.nodes.*;
28 import org.openide.src.ElementProperties;
29 import org.netbeans.jmi.javamodel.Initializer;
30 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
31
32 import javax.jmi.reflect.JmiException;
33
34 /** Node representing an initializer (static or nonstatic).
35  * @see org.netbeans.jmi.javamodel.Initializer
36  *
37  * @author Petr Hamernik, Jan Pokorsky
38  *
39  * XXX help ids
40  */

41 public final class InitializerNode extends ElementNode {
42     /** Return value of getIconAffectingProperties method. */
43     private static final String JavaDoc[] ICON_AFFECTING_PROPERTIES = new String JavaDoc[] {
44                 PROP_MODIFIERS
45             };
46     
47     private static final Map JavaDoc mapAttributeName;
48     
49     static {
50         mapAttributeName = new HashMap JavaDoc();
51         mapAttributeName.put(PROP_MODIFIERS, PROP_MODIFIERS);
52     }
53
54     /** Create a new initializer node.
55     * @param element initializer element to represent
56     * @param writeable <code>true</code> to be writable
57     */

58     public InitializerNode(Initializer element, boolean writeable) {
59         super(element, Children.LEAF, writeable);
60         setElementFormat0(getElementFormatProperty());
61         superSetName("<initializer>"); // NOI18N
62
}
63
64     protected String JavaDoc resolveIconBase() {
65         return IconResolver.getIconBaseForInitializer(getInitializer());
66     }
67
68     protected String JavaDoc[] getIconAffectingProperties() {
69         return ICON_AFFECTING_PROPERTIES;
70     }
71
72     protected ElementFormat getElementFormatProperty() {
73         return getSourceOptions().getInitializerElementFormat();
74     }
75
76     protected ElementFormat getHintElementFormat() {
77         return getSourceOptions().getInitializerElementLongFormat();
78     }
79
80     protected Map JavaDoc getAttributeNameMap() {
81         return mapAttributeName;
82     }
83
84     protected Sheet createSheet () {
85         Sheet sheet = Sheet.createDefault();
86         Sheet.Set ps = sheet.get(Sheet.PROPERTIES);
87         ps.put(createStaticProperty(writeable));
88         return sheet;
89     }
90
91     /** Indicate that this node cannot be renamed.
92     * An initializer has no name.
93     * @return <code>false</code>
94     */

95     public boolean canRename() {
96         return false;
97     }
98
99     /** Create a property for whether or not the initializer is static.
100     * @param canW <code>false</code> to force property to be read-only
101     * @return the property
102     */

103     protected Node.Property createStaticProperty(boolean canW) {
104         return new ElementNode.ElementProp(ElementProperties.PROP_STATIC, Boolean.TYPE, canW) {
105                    /** Gets the value */
106                    public Object JavaDoc getValue () {
107                        return Modifier.isStatic(getInitializer().getModifiers())? Boolean.TRUE : Boolean.FALSE;
108                    }
109
110                    /** Sets the value */
111                    public void setValue(final Object JavaDoc val) throws IllegalArgumentException JavaDoc,
112                            IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
113                        super.setValue(val);
114
115                        if (!(val instanceof Boolean JavaDoc))
116                            throw new IllegalArgumentException JavaDoc();
117                        
118                        boolean fail = true;
119                        try {
120                            JavaMetamodel.getDefaultRepository().beginTrans(true);
121                            try {
122                                int m = getInitializer().getModifiers();
123                                
124                                boolean oldV = Modifier.isStatic(m);
125                                boolean newV = ((Boolean JavaDoc) val).booleanValue();
126                                if (oldV == newV) {
127                                    // no change
128
} else if (oldV) {
129                                    m -= Modifier.STATIC;
130                                } else {
131                                    m += Modifier.STATIC;
132                                }
133                                
134                                getInitializer().setModifiers(m);
135                                fail = false;
136                            } finally {
137                                JavaMetamodel.getDefaultRepository().endTrans(fail);
138                            }
139                        } catch (JmiException ex) {
140                            IllegalArgumentException JavaDoc iaex = new IllegalArgumentException JavaDoc();
141                            iaex.initCause(ex);
142                            throw iaex;
143                        }
144                    }
145                };
146     }
147     
148     private Initializer getInitializer() {
149         return (Initializer) this.element;
150     }
151 }
152
Popular Tags