KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javadoc > comments > JavaDocPropertySupportFactory


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.javadoc.comments;
21
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.lang.ref.Reference JavaDoc;
24 import java.lang.ref.SoftReference JavaDoc;
25 import java.beans.PropertyEditor JavaDoc;
26
27 import org.openide.nodes.*;
28 import org.openide.ErrorManager;
29 import org.openide.src.JavaDoc;
30 import org.netbeans.modules.java.ui.nodes.FilterSourceNodeFactory;
31 import org.netbeans.jmi.javamodel.*;
32
33 import javax.jmi.reflect.JmiException;
34 import org.netbeans.modules.javacore.api.JavaModel;
35
36
37 /** This FilterFactory descendant adds the JavaDoc property to every Method, Constructor, Field and
38  * class node create for JavaDataObjects
39  *
40  * @author Petr Hrebejk
41  */

42
43 public class JavaDocPropertySupportFactory extends FilterSourceNodeFactory {
44     
45     private static final String JavaDoc PROP_JAVADOCCOMMENT = "javadocComment"; // NOI18N
46

47     public Node createMethodNode(Method element) {
48         Node node = super.createMethodNode( element );
49         JavaDocPropertySupportFactory.addJavaDocProperty( node );
50         return node;
51     }
52
53     public Node createConstructorNode(Constructor element) {
54         Node node = super.createConstructorNode( element );
55         JavaDocPropertySupportFactory.addJavaDocProperty( node );
56         return node;
57     }
58
59     public Node createFieldNode(Field element) {
60         Node node = super.createFieldNode( element );
61         JavaDocPropertySupportFactory.addJavaDocProperty( node );
62         return node;
63     }
64
65     public Node createClassNode(final JavaClass element) {
66         Node node = super.createClassNode( element );
67         JavaDocPropertySupportFactory.addJavaDocProperty( node );
68         return node;
69     }
70
71     public Node createAnnotationTypeMethodNode(Attribute element) {
72         Node node = super.createAnnotationTypeMethodNode(element);
73         JavaDocPropertySupportFactory.addJavaDocProperty( node );
74         return node;
75     }
76
77     public Node createAnnotationTypeNode(AnnotationType element) {
78         Node node = super.createAnnotationTypeNode(element);
79         JavaDocPropertySupportFactory.addJavaDocProperty( node );
80         return node;
81     }
82
83     public Node createEnumConstantNode(EnumConstant element) {
84         Node node = super.createEnumConstantNode(element);
85         JavaDocPropertySupportFactory.addJavaDocProperty( node );
86         return node;
87     }
88
89     public Node createEnumNode(JavaEnum element) {
90         Node node = super.createEnumNode(element);
91         JavaDocPropertySupportFactory.addJavaDocProperty( node );
92         return node;
93     }
94     // UTILITY FUNCTIONS ------------------------------------------------------------------------------
95

96     /** Create a node property for javadoc comment for class element
97     * @param canW if <code>false</code>, property will be read-only
98     * @return the property
99     */

100     private static Node.Property createJavaDocProperty(boolean canW, final Node node ) {
101         JavaDocPropertySupport prop = new JavaDocPropertySupport(PROP_JAVADOCCOMMENT, JavaDoc.class, canW) {
102
103             private AutoCommenter.Element acElement;
104             private Reference JavaDoc editorRef;
105             
106             private AutoCommenter.Element getCommentable() throws InvocationTargetException JavaDoc {
107                 if (acElement == null) {
108                     ClassMember el = (ClassMember) node.getLookup().lookup(ClassMember.class);
109                     if (el == null)
110                       throw new IllegalStateException JavaDoc("ClassMember missing in lookup: " + node); // NOI18N
111
try {
112                         JavaModel.getJavaRepository().beginTrans(false);
113                         try {
114                             acElement = AutoCommenter.createAutoCommenterElement(el);
115                         } finally {
116                             JavaModel.getJavaRepository().endTrans();
117                         }
118                     } catch (JmiException e) {
119                         throw new InvocationTargetException JavaDoc(e);
120                     }
121                 }
122                 return acElement;
123             }
124             
125             public Object JavaDoc getValue() throws
126                     IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
127                 
128                 return getCommentable().getJavaDoc();
129             }
130
131             // Sets the value
132
public void setValue(Object JavaDoc val) throws IllegalArgumentException JavaDoc,
133                     IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
134             
135                 super.setValue(val);
136                 if (!(val instanceof JavaDoc))
137                     throw new IllegalArgumentException JavaDoc("val: " + val); //NOI18N
138

139                 try {
140                     JavaModel.getJavaRepository().beginTrans(true);
141                     boolean fail = true;
142                     try {
143                         getCommentable().setJavaDoc((JavaDoc) val);
144                         fail = false;
145                     } finally {
146                         JavaModel.getJavaRepository().endTrans(fail);
147                     }
148                 } catch (JmiException e) {
149                     throw new InvocationTargetException JavaDoc(e);
150                 }
151             }
152             
153             public PropertyEditor JavaDoc getPropertyEditor() {
154                 try {
155                     PropertyEditor JavaDoc editor = editorRef == null? null: (PropertyEditor JavaDoc) this.editorRef.get();
156                     if (editor == null) {
157                         AutoCommenter.Element commentable = getCommentable();
158                         editor = new JavaDocEditor(commentable);
159                         editorRef = new SoftReference JavaDoc(editor);
160                     }
161                     return editor;
162                 } catch (InvocationTargetException JavaDoc e) {
163                     ErrorManager.getDefault().notify(ErrorManager.WARNING, e.getCause());
164                     return null;
165                 }
166             }
167         
168         };
169
170         return prop;
171     }
172
173
174
175
176     /** Adds JavaDoc property to node
177      */

178
179     private static void addJavaDocProperty(final Node node) {
180
181         // PENDING: THIS IS A WORKAROUND FOR ADDING NEW PROPERTIES ONLY
182
// TO JAVA NODES NOT TO CLAZZ NODES
183

184         Node.PropertySet [] ps = node.getPropertySets();
185
186         for ( int i = 0; i < ps.length; i++ ) {
187             if ( ps[i].getName().equals( Sheet.PROPERTIES ) && ps[i] instanceof Sheet.Set ) {
188                 ((Sheet.Set)ps[i]).put(createJavaDocProperty( true, node ));
189             }
190         }
191     }
192
193
194 }
195
Popular Tags