KickJava   Java API By Example, From Geeks To Geeks.

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


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.ref.WeakReference JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.awt.EventQueue JavaDoc;
27
28 import org.openide.nodes.*;
29 import org.openide.src.ElementProperties;
30 import org.openide.util.Utilities;
31 import org.openide.ErrorManager;
32 import org.netbeans.jmi.javamodel.Constructor;
33 import org.netbeans.jmi.javamodel.JavaClass;
34 import org.netbeans.jmi.javamodel.ClassDefinition;
35 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
36 import org.netbeans.api.mdr.events.MDRChangeListener;
37 import org.netbeans.api.mdr.events.MDRChangeEvent;
38 import org.netbeans.api.mdr.events.MDRChangeSource;
39 import org.netbeans.api.mdr.events.AttributeEvent;
40
41 import javax.jmi.reflect.JmiException;
42
43 /** Node representing a constructor.
44 * @see org.openide.src.ConstructorElement
45 * @author Petr Hamernik, Jan Pokorsky
46 */

47 public final class ConstructorNode extends ElementNode {
48
49     private static final Map JavaDoc mapAttributeName;
50     
51     static {
52         mapAttributeName = new HashMap JavaDoc();
53         mapAttributeName.put(PROP_MODIFIERS, PROP_MODIFIERS);
54         mapAttributeName.put(ElementProperties2.PROP_NAME, ElementProperties2.PROP_NAME);
55         mapAttributeName.put(PROP_PARAMETERS, PROP_PARAMETERS);
56         mapAttributeName.put("exceptionNames", PROP_EXCEPTIONS); // NOI18N
57
}
58     
59     /** Create a new constructor node.
60     * @param element constructor element to represent
61     * @param writeable <code>true</code> to be writable
62     */

63     public ConstructorNode(Constructor element, boolean writeable) {
64         super(element, Children.LEAF, writeable);
65         setElementFormat0(getElementFormatProperty());
66         superSetName(((JavaClass) element.getDeclaringClass()).getSimpleName());
67         NameListener.getInstance(this, element);
68     }
69
70     /* Resolve the current icon base.
71     * @return icon base string.
72     */

73     protected String JavaDoc resolveIconBase() {
74         return IconResolver.getIconBaseForConstructor(getConstructor());
75     }
76
77     protected ElementFormat getElementFormatProperty() {
78         return getSourceOptions().getConstructorElementFormat();
79     }
80
81     /* This method resolve the appropriate hint format for the type
82     * of the element. It defines the short description.
83     */

84     protected ElementFormat getHintElementFormat() {
85         return getSourceOptions().getConstructorElementLongFormat();
86     }
87
88     protected Map JavaDoc getAttributeNameMap() {
89         return mapAttributeName;
90     }
91
92     /* Creates property set for this node */
93     protected Sheet createSheet () {
94         Sheet sheet = Sheet.createDefault();
95         Sheet.Set ps = sheet.get(Sheet.PROPERTIES);
96         ps.put(createModifiersProperty(writeable));
97         ps.put(createNameProperty());
98         ps.put(createParametersProperty(false));
99         ps.put(createExceptionsProperty(writeable));
100         return sheet;
101     }
102
103     /** Indicate that this node cannot be renamed.
104     * An constructor must have the same name like class
105     * @return <code>false</code>
106     */

107     public boolean canRename() {
108         return false;
109     }
110     
111     private Node.Property createNameProperty() {
112         
113         return new ElementNode.ElementProp(ElementProperties.PROP_NAME, String JavaDoc.class, false) {
114             public Object JavaDoc getValue() throws
115                     IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
116
117                 try {
118                     JavaMetamodel.getDefaultRepository().beginTrans(false);
119                     try {
120                         String JavaDoc name;
121                         ClassDefinition cd = getConstructor().getDeclaringClass();
122                         name = cd instanceof JavaClass? ((JavaClass) cd).getSimpleName(): cd.getName();
123                         return name;
124                     } finally {
125                         JavaMetamodel.getDefaultRepository().endTrans();
126                     }
127                 } catch (JmiException e) {
128                     throw new InvocationTargetException JavaDoc(e);
129                 }
130             }
131         };
132     }
133
134     /** Create a node property for constructor parameters.
135     * @param canW <code>false</code> to force property to be read-only
136     * @return the property
137     */

138     private Node.Property createParametersProperty(boolean canW) {
139         // this should be read-olny -> refactoring job
140
Node.Property p = createParametersProperty(getConstructor(), canW);
141         p.setValue("changeImmediate" /* PropertyEnv.PROP_CHANGE_IMMEDIATE */,Boolean.FALSE); // NOI18N
142
return p;
143     }
144
145     /** Create a node property for constructor exceptions.
146     * @param canW <code>false</code> to force property to be read-only
147     * @return the property
148     */

149     private Node.Property createExceptionsProperty(boolean canW) {
150         Node.Property p = createExceptionsProperty(getConstructor(), canW);
151         p.setValue("changeImmediate" /* PropertyEnv.PROP_CHANGE_IMMEDIATE */,Boolean.FALSE); // NOI18N
152
return p;
153     }
154     
155     private Constructor getConstructor() {
156         return (Constructor) this.element;
157     }
158
159     /**
160      * Since {@link Constructor} does not notify about changes of its name
161      * it is necessary to add this listener to the declaring class.
162      */

163     private static final class NameListener extends WeakReference JavaDoc implements MDRChangeListener, Runnable JavaDoc {
164         
165         private final JavaClass declClass;
166         
167         public static NameListener getInstance(ConstructorNode node, Constructor c) {
168             JavaClass cd = (JavaClass) c.getDeclaringClass();
169             MDRChangeSource source = (MDRChangeSource) cd;
170             NameListener l = new NameListener(node, cd);
171             source.addListener(l, AttributeEvent.EVENTMASK_ATTRIBUTE);
172             return l;
173         }
174         
175         private NameListener(ConstructorNode referent, JavaClass declClass) {
176             super(referent, Utilities.activeReferenceQueue());
177             this.declClass = declClass;
178         }
179
180         public void change(MDRChangeEvent e) {
181             if (!(e instanceof AttributeEvent)) {
182                 return;
183             }
184             
185             final ConstructorNode node = (ConstructorNode) get();
186             AttributeEvent ae = (AttributeEvent) e;
187             String JavaDoc attrName = ae.getAttributeName();
188             ElementFormat format = null;
189             if (node != null && (attrName == null || ElementProperties2.PROP_NAME.equals(attrName)) &&
190                     (format = node.getElementFormat()).dependsOnProperty(attrName)) {
191                 final String JavaDoc[] names = new String JavaDoc[2]; // [displayName, name]
192
JavaMetamodel.getDefaultRepository().beginTrans(false);
193                 try {
194                     try {
195                         if (declClass.isValid()) {
196                             names[0] = format.format(node.element);
197                             names[1] = declClass.getSimpleName();
198                         }
199                     } finally {
200                         JavaMetamodel.getDefaultRepository().endTrans();
201                     }
202                 } catch (JmiException ex) {
203                     ErrorManager.getDefault().notify(ErrorManager.WARNING, ex);
204                 }
205                 if (names[0] != null) {
206                     EventQueue.invokeLater(new Runnable JavaDoc() {
207
208                         public void run() {
209                             node.setDisplayName(names[0]);
210                             node.superSetName(names[1]);
211                             node.superPropertyChange(ElementProperties2.PROP_NAME, null, null);
212                         }
213                     });
214                 }
215             }
216         }
217
218         public void run() {
219             ((MDRChangeSource) this.declClass).removeListener(this);
220         }
221     }
222 }
223
Popular Tags