KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.*;
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.Modifier JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26
27 import org.openide.src.*;
28 import org.openide.nodes.*;
29
30 /** Node for a method element.
31 * @see MethodElement
32 * @author Petr Hamernik
33 */

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

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

52     protected String JavaDoc resolveIconBase() {
53         int modif = ((MethodElement)element).getModifiers();
54         if (Modifier.isStatic(modif)) {
55             // static method...
56
if (Modifier.isPrivate(modif))
57                 return METHOD_ST_PRIVATE;
58             else if (Modifier.isProtected(modif))
59                 return METHOD_ST_PROTECTED;
60             else if (Modifier.isPublic(modif))
61                 return METHOD_ST_PUBLIC;
62             else
63                 return METHOD_ST_PACKAGE;
64         }
65         else {
66             // non-static method...
67
if (Modifier.isPrivate(modif))
68                 return METHOD_PRIVATE;
69             else if (Modifier.isProtected(modif))
70                 return METHOD_PROTECTED;
71             else if (Modifier.isPublic(modif))
72                 return METHOD_PUBLIC;
73             else
74                 return METHOD_PACKAGE;
75         }
76     }
77
78     /** Indicate that this node cannot be renamed.
79     * An constructor must have the same name like class
80     * @return <code>true</code>
81     */

82     public boolean canRename() {
83         return true;
84     }
85
86
87     /* This method resolve the appropriate hint format for the type
88     * of the element. It defines the short description.
89     */

90     protected ElementFormat getHintElementFormat() {
91         return sourceOptions.getMethodElementLongFormat();
92     }
93
94     /* Creates property set for this node */
95     protected Sheet createSheet () {
96         Sheet sheet = Sheet.createDefault();
97         Sheet.Set ps = sheet.get(Sheet.PROPERTIES);
98         ps.put(createModifiersProperty(writeable));
99         ps.put(createNameProperty(writeable));
100         ps.put(createParametersProperty(writeable));
101         ps.put(createReturnProperty(writeable));
102         ps.put(createExceptionsProperty(writeable));
103         return sheet;
104     }
105
106     /* Removes the element from the class and calls superclass.
107     *
108     * @exception IOException if SourceException is thrown
109     * from the underlayed Element.
110     */

111     public void destroy() throws IOException JavaDoc {
112         SourceEditSupport.invokeAtomicAsUser(element, new SourceEditSupport.ExceptionalRunnable() {
113                                                  public void run() throws SourceException {
114                                                      MethodElement el = (MethodElement) element;
115                                                      el.getDeclaringClass().removeMethod(el);
116                                                  }
117                                              });
118         super.destroy();
119     }
120
121     /** Create a property for the method return value.
122     * @param canW <code>false</code> to force property to be read-only
123     * @return the property
124     */

125     protected Node.Property createReturnProperty(boolean canW) {
126         return new ElementProp(PROP_RETURN, Type.class, canW) {
127                    /** Gets the value */
128                    public Object JavaDoc getValue () {
129                        return ((MethodElement)element).getReturn();
130                    }
131
132                    /** Sets the value */
133                    public void setValue(final Object JavaDoc val) throws IllegalArgumentException JavaDoc,
134                        IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
135                        super.setValue(val);
136                        if (!(val instanceof Type))
137                            throw new IllegalArgumentException JavaDoc();
138
139                        runAtomic(element, new SourceEditSupport.ExceptionalRunnable() {
140                                      public void run() throws SourceException {
141                                          ((MethodElement)element).setReturn((Type)val);
142                                      }
143                                  });
144                    }
145                };
146     }
147 }
148
Popular Tags