1 19 20 package org.netbeans.modules.javadoc.comments; 21 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.ref.Reference ; 24 import java.lang.ref.SoftReference ; 25 import java.beans.PropertyEditor ; 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 42 43 public class JavaDocPropertySupportFactory extends FilterSourceNodeFactory { 44 45 private static final String PROP_JAVADOCCOMMENT = "javadocComment"; 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 96 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 editorRef; 105 106 private AutoCommenter.Element getCommentable() throws InvocationTargetException { 107 if (acElement == null) { 108 ClassMember el = (ClassMember) node.getLookup().lookup(ClassMember.class); 109 if (el == null) 110 throw new IllegalStateException ("ClassMember missing in lookup: " + node); 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 (e); 120 } 121 } 122 return acElement; 123 } 124 125 public Object getValue() throws 126 IllegalAccessException , InvocationTargetException { 127 128 return getCommentable().getJavaDoc(); 129 } 130 131 public void setValue(Object val) throws IllegalArgumentException , 133 IllegalAccessException , InvocationTargetException { 134 135 super.setValue(val); 136 if (!(val instanceof JavaDoc)) 137 throw new IllegalArgumentException ("val: " + val); 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 (e); 150 } 151 } 152 153 public PropertyEditor getPropertyEditor() { 154 try { 155 PropertyEditor editor = editorRef == null? null: (PropertyEditor ) this.editorRef.get(); 156 if (editor == null) { 157 AutoCommenter.Element commentable = getCommentable(); 158 editor = new JavaDocEditor(commentable); 159 editorRef = new SoftReference (editor); 160 } 161 return editor; 162 } catch (InvocationTargetException 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 178 179 private static void addJavaDocProperty(final Node node) { 180 181 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 |