KickJava   Java API By Example, From Geeks To Geeks.

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


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 constructor.
32 * @see ConstructorElement
33 * @author Petr Hamernik
34 */

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

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

52     protected String JavaDoc resolveIconBase() {
53         int modif = ((ConstructorElement)element).getModifiers();
54         if (Modifier.isPrivate(modif))
55             return CONSTRUCTOR_PRIVATE;
56         else if (Modifier.isProtected(modif))
57             return CONSTRUCTOR_PROTECTED;
58         else if (Modifier.isPublic(modif))
59             return CONSTRUCTOR_PUBLIC;
60         else
61             return CONSTRUCTOR_PACKAGE;
62     }
63
64     /* This method resolve the appropriate hint format for the type
65     * of the element. It defines the short description.
66     */

67     protected ElementFormat getHintElementFormat() {
68         return sourceOptions.getConstructorElementLongFormat();
69     }
70
71     /* Creates property set for this node */
72     protected Sheet createSheet () {
73         Sheet sheet = Sheet.createDefault();
74         Sheet.Set ps = sheet.get(Sheet.PROPERTIES);
75         ps.put(createModifiersProperty(writeable));
76         ps.put(createNameProperty(false));
77         ps.put(createParametersProperty(writeable));
78         ps.put(createExceptionsProperty(writeable));
79         return sheet;
80     }
81
82     /* Removes the element from the class and calls superclass.
83     *
84     * @exception IOException if SourceException is thrown
85     * from the underlayed Element.
86     */

87     public void destroy() throws IOException JavaDoc {
88         if (!(element instanceof MethodElement)) {
89             SourceEditSupport.invokeAtomicAsUser(element, new SourceEditSupport.ExceptionalRunnable() {
90                                                      public void run() throws SourceException {
91                                                          ConstructorElement el = (ConstructorElement) element;
92                                                          el.getDeclaringClass().removeConstructor(el);
93                                                      }
94                                                  });
95         }
96         super.destroy();
97     }
98
99     /** Indicate that this node cannot be renamed.
100     * An constructor must have the same name like class
101     * @return <code>false</code>
102     */

103     public boolean canRename() {
104         return false;
105     }
106
107     public Component JavaDoc getCustomizer() {
108         return new MethodCustomizer((ConstructorElement)element);
109     }
110
111     public boolean hasCustomizer() {
112         return isWriteable();
113     }
114
115     /** Create a node property for constructor parameters.
116     * @param canW <code>false</code> to force property to be read-only
117     * @return the property
118     */

119     protected Node.Property createParametersProperty(boolean canW) {
120         Node.Property p = new ElementProp(PROP_PARAMETERS, MethodParameter[].class, canW) {
121                    /** Gets the value */
122                    public Object JavaDoc getValue () {
123                        return ((ConstructorElement)element).getParameters();
124                    }
125
126                    /** Sets the value */
127                    public void setValue(final Object JavaDoc val) throws IllegalArgumentException JavaDoc,
128                        IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
129                        super.setValue(val);
130                        if (!(val instanceof MethodParameter[]))
131                            throw new IllegalArgumentException JavaDoc();
132
133                        runAtomic(element, new SourceEditSupport.ExceptionalRunnable() {
134                                      public void run() throws SourceException {
135                                          ((ConstructorElement)element).setParameters((MethodParameter[])val);
136                                      }
137                                  });
138                    }
139                };
140         p.setValue("changeImmediate" /* PropertyEnv.PROP_CHANGE_IMMEDIATE */,Boolean.FALSE); // NOI18N
141
return p;
142     }
143
144     /** Create a node property for constructor exceptions.
145     * @param canW <code>false</code> to force property to be read-only
146     * @return the property
147     */

148     protected Node.Property createExceptionsProperty(boolean canW) {
149         Node.Property p = new ElementProp(PROP_EXCEPTIONS, Identifier[].class, canW) {
150                    /** Gets the value */
151                    public Object JavaDoc getValue () {
152                        return ((ConstructorElement)element).getExceptions();
153                    }
154
155                    /** Sets the value */
156                    public void setValue(final Object JavaDoc val) throws IllegalArgumentException JavaDoc,
157                        IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
158                        super.setValue(val);
159                        if (!(val instanceof Identifier[]))
160                            throw new IllegalArgumentException JavaDoc();
161
162                        runAtomic(element, new SourceEditSupport.ExceptionalRunnable() {
163                                      public void run() throws SourceException {
164                                          ((ConstructorElement)element).setExceptions((Identifier[])val);
165                                      }
166                                  });
167                    }
168                };
169         p.setValue("changeImmediate" /* PropertyEnv.PROP_CHANGE_IMMEDIATE */,Boolean.FALSE); // NOI18N
170
return p;
171     }
172 }
173
Popular Tags