KickJava   Java API By Example, From Geeks To Geeks.

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


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.beans.*;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.util.*;
25
26 import org.openide.nodes.*;
27 import org.openide.util.datatransfer.*;
28 import org.openide.src.ElementProperties;
29 import org.openide.loaders.DataObject;
30 import org.netbeans.jmi.javamodel.JavaClass;
31 import org.netbeans.jmi.javamodel.MultipartId;
32 import org.netbeans.jmi.javamodel.JavaModelPackage;
33 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
34 import org.netbeans.modules.java.ui.nodes.editors.IdentifierArrayEditor;
35 import org.netbeans.api.mdr.events.AttributeEvent;
36
37 import javax.jmi.reflect.JmiException;
38
39
40 /** Node representing a Java class.
41  * @see org.netbeans.jmi.javamodel.JavaClass
42  *
43  * @author Petr Hamernik, Jan Pokorsky
44  *
45  */

46 public final class ClassNode extends ElementNode {
47
48     /** Return value of getIconAffectingProperties method. */
49     private static final String JavaDoc[] ICON_AFFECTING_PROPERTIES = new String JavaDoc[] {
50                 PROP_MODIFIERS
51             };
52
53     private static final Map mapClassAttributeName;
54     private static final Map mapInterfaceAttributeName;
55     
56     static {
57         mapInterfaceAttributeName = new HashMap();
58         mapInterfaceAttributeName.put(PROP_MODIFIERS, PROP_MODIFIERS);
59         mapInterfaceAttributeName.put(ElementProperties.PROP_NAME, ElementProperties.PROP_NAME);
60         mapInterfaceAttributeName.put("interfaceNames", PROP_INTERFACES); // NOI18N
61

62         mapClassAttributeName = new HashMap(mapInterfaceAttributeName);
63         mapClassAttributeName.put("superClassName", PROP_SUPERCLASS); // NOI18N
64
}
65     
66     /**
67      * cached flag to not access jmi unnecessary
68      */

69     private boolean isInterface;
70     
71     /** is {@link org.openide.nodes.Sheet} initialized */
72     private boolean isSheetCreated = false;
73     
74     private DataObject sourceDO;
75     
76     /** Create a new class node.
77     * @param element class element to represent
78     * @param children node children
79     * @param writeable <code>true</code> to be writable
80     */

81     public ClassNode(JavaClass element, Children children, boolean writeable) {
82         super(element, children, writeable);
83         init(element);
84     }
85
86     private void init(JavaClass element) {
87         isInterface = element.isInterface();
88         setElementFormat0(getElementFormat(isInterface));
89         superSetName(element.getSimpleName());
90         setIconBase(resolveIconBase());
91         sourceDO = JavaMetamodel.getManager().getDataObject(element.getResource());
92     }
93
94     private JavaClass getJavaClass() {
95         return (JavaClass) this.element;
96     }
97     
98     /* Resolve the current icon base.
99     * @return icon base string.
100     */

101     protected String JavaDoc resolveIconBase() {
102         return IconResolver.getIconBaseForJavaClass(getJavaClass());
103     }
104
105     /* This method is used for resolving the names of the properties,
106     * which could affect the icon (such as "modifiers").
107     * @return the appropriate array.
108     */

109     protected String JavaDoc[] getIconAffectingProperties() {
110         return ICON_AFFECTING_PROPERTIES;
111     }
112
113     protected ElementFormat getElementFormatProperty() {
114         return getElementFormat(isInterface);
115     }
116
117     /* This method resolve the appropriate hint format for the type
118     * of the element. It defines the short description.
119     */

120     protected ElementFormat getHintElementFormat() {
121         return this.isInterface ?
122                getSourceOptions().getInterfaceElementLongFormat() :
123                getSourceOptions().getClassElementLongFormat();
124     }
125     
126     private static ElementFormat getElementFormat(boolean isInterface) {
127         return isInterface ?
128                getSourceOptions().getInterfaceElementFormat() :
129                getSourceOptions().getClassElementFormat();
130     }
131
132     protected Map getAttributeNameMap() {
133         return isInterface? mapInterfaceAttributeName: mapClassAttributeName;
134     }
135
136     protected ChangeDescriptor handleAttributeChange(AttributeEvent ae) {
137         ChangeDescriptor cd = super.handleAttributeChange(ae);
138         final Object JavaDoc src = ae.getSource();
139         if (src != element || !((JavaClass) src).isValid()) {
140             return cd;
141         }
142         String JavaDoc attrName = ae.getAttributeName();
143         JavaClass jc = getJavaClass();
144         if (PROP_MODIFIERS.equals(attrName) && jc.isInterface() != this.isInterface) {
145             this.isInterface = !this.isInterface;
146             this.elementFormat = getElementFormat(this.isInterface);
147             cd.iconBase = resolveIconBase();
148             cd.displayName = getElementFormat().format(jc);
149             cd.shortDescription = getShortDescription();
150             if (isSheetCreated) {
151                 cd.sheet = new Sheet();
152             }
153         }
154         return cd;
155     }
156
157     protected void processChange(ElementNode.ChangeDescriptor desc) {
158         if (desc.sheet != null) {
159             Sheet.Set ps = getSheet().get(Sheet.PROPERTIES);
160             configureSheetSet(ps, this.isInterface);
161             desc.sheet = null; // do not process again in ElementNode
162
}
163         super.processChange(desc);
164     }
165
166     /* Creates property set for this node */
167     protected Sheet createSheet () {
168         // do not change properties order without reviewing of recomputeSheet()
169
Sheet sheet = Sheet.createDefault();
170         Sheet.Set ps = sheet.get(Sheet.PROPERTIES);
171         configureSheetSet(ps, this.isInterface);
172         this.isSheetCreated = true;
173         return sheet;
174     }
175     
176     private void configureSheetSet(Sheet.Set ps, boolean isInterface) {
177         ps.put(createModifiersProperty(writeable));
178         ps.put(createNameProperty(getJavaClass()));
179         ps.put(createTypeParametersProperty());
180         if (isInterface) {
181             ps.remove(PROP_SUPERCLASS);
182         } else {
183             ps.put(createSuperclassProperty(writeable));
184         }
185         ps.put(createInterfacesProperty(writeable));
186     }
187
188     /** Create a node property for the superclass of this class.
189     * @param canW if <code>false</code>, property will be read-only
190     * @return the property
191     */

192     protected Node.Property createSuperclassProperty(boolean canW) {
193         return new ElementNode.ElementProp(PROP_SUPERCLASS, String JavaDoc.class, canW) {
194                    /** Gets the value */
195                    public Object JavaDoc getValue () {
196                        MultipartId mid = getJavaClass().getSuperClassName();
197                        return mid == null ? "" : IdentifierArrayEditor.multipartIdToName(mid); // NOI18N
198
}
199
200                    /** Sets the value */
201                    public void setValue(final Object JavaDoc val) throws IllegalArgumentException JavaDoc,
202                            IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
203                        super.setValue(val);
204                        if (!(val instanceof String JavaDoc))
205                            throw new IllegalArgumentException JavaDoc();
206                        String JavaDoc str = ((String JavaDoc) val).trim();
207                        if (str != null && !"".equals(str)) {
208                            boolean fail = true;
209                            try {
210                                JavaMetamodel.getDefaultRepository().beginTrans(true);
211                                try {
212                                    JavaModelPackage model = JavaMetamodel.getManager().getJavaExtent(getJavaClass());
213                                    MultipartId mid = model.getMultipartId().createMultipartId(str, null, null);
214                                    getJavaClass().setSuperClassName(mid);
215                                    fail = false;
216                                } finally {
217                                    JavaMetamodel.getDefaultRepository().endTrans(fail);
218                                }
219                            } catch (JmiException ex) {
220                                IllegalArgumentException JavaDoc iaex = new IllegalArgumentException JavaDoc();
221                                iaex.initCause(ex);
222                                throw ex;
223                            }
224 // Type t = Type.parse(str);
225
// if (!t.isClass())
226
// throw new IllegalArgumentException();
227
}
228
229                    }
230                };
231     }
232
233     /** Create a node property for the implemented interfaces of this class.
234     * (Or, extended interfaces if this is itself an interface.)
235     * @param canW if <code>false</code>, property will be read-only
236     * @return the property
237     */

238     protected Node.Property createInterfacesProperty(boolean canW) {
239         Node.Property prop = createInterfacesProperty(getJavaClass(), canW);
240
241         if (isInterface) {
242             prop.setDisplayName(getString("PROP_superInterfaces")); // NOI18N
243
prop.setShortDescription(getString("HINT_superInterfaces")); // NOI18N
244
}
245         prop.setValue("changeImmediate" /* PropertyEnv.PROP_CHANGE_IMMEDIATE */, Boolean.FALSE); // NOI18N
246
setModel(getJavaClass(), prop);
247         
248         return prop;
249     }
250     
251     Node.Property createTypeParametersProperty() {
252         Node.Property np = createTypeParametersProperty(PROP_TYPE_PARAMETERS, getJavaClass(), false);
253         np.setValue("changeImmediate" /* PropertyEnv.PROP_CHANGE_IMMEDIATE */, Boolean.FALSE); // NOI18N
254
return np;
255     }
256
257     public NewType[] getNewTypes() {
258         if (writeable) {
259             boolean jdk15 = sourceDO != null?
260                     SourceEditSupport.isJDK15Supported(sourceDO.getPrimaryFile()):
261                     false;
262             if (isInterface) {
263                 return SourceEditSupport.createInterfaceNewTypes(this.getJavaClass(), jdk15);
264             } else {
265                 return SourceEditSupport.createClassNewTypes(this.getJavaClass(), jdk15);
266             }
267         } else {
268             return super.getNewTypes();
269         }
270     }
271     
272     /** Create a node property for the implemented interfaces of this class.
273      * (Or, extended interfaces if this is itself an interface.)
274      * @param element element implementing or extending interfaces
275      * @param canW <code>false</code> to force property to be read-only
276      * @return the property
277      */

278     public static Node.Property createInterfacesProperty(JavaClass element, boolean canW) {
279         Node.Property prop = new InterfacesProperty(element, canW);
280         setModel(element, prop);
281         return prop;
282     }
283     
284     private static final class InterfacesProperty extends ElementNode.ElementProp {
285         
286         private final JavaClass element;
287         
288         public InterfacesProperty(JavaClass element, boolean canW) {
289             super(PROP_INTERFACES, MultipartId[].class, canW);
290             this.element = element;
291         }
292
293         protected PropertyEditor createPropertyEditor() {
294             return new IdentifierArrayEditor();
295         }
296
297         public Object JavaDoc getValue () {
298             return element.getInterfaceNames().toArray(new MultipartId[0]);
299         }
300
301         public void setValue(final Object JavaDoc val) throws IllegalArgumentException JavaDoc,
302                 IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
303             super.setValue(val);
304             if (!(val instanceof MultipartId[]))
305                 throw new IllegalArgumentException JavaDoc();
306                                    
307             boolean fail = true;
308             try {
309                 JavaMetamodel.getDefaultRepository().beginTrans(true);
310                 try {
311                     List l = element.getInterfaceNames();
312                     l.clear();
313                     l.addAll(Arrays.asList((MultipartId[]) val));
314                     fail = false;
315                 } finally {
316                     JavaMetamodel.getDefaultRepository().endTrans(fail);
317                 }
318             } catch (JmiException ex) {
319                 IllegalArgumentException JavaDoc iaex = new IllegalArgumentException JavaDoc();
320                 iaex.initCause(ex);
321                 throw ex;
322             }
323         }
324     }
325 }
326
Popular Tags