1 19 20 package org.netbeans.modules.java.ui.nodes.elements; 21 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.ref.WeakReference ; 24 import java.util.Map ; 25 import java.util.HashMap ; 26 import java.awt.EventQueue ; 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 47 public final class ConstructorNode extends ElementNode { 48 49 private static final Map mapAttributeName; 50 51 static { 52 mapAttributeName = new HashMap (); 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); } 58 59 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 73 protected String resolveIconBase() { 74 return IconResolver.getIconBaseForConstructor(getConstructor()); 75 } 76 77 protected ElementFormat getElementFormatProperty() { 78 return getSourceOptions().getConstructorElementFormat(); 79 } 80 81 84 protected ElementFormat getHintElementFormat() { 85 return getSourceOptions().getConstructorElementLongFormat(); 86 } 87 88 protected Map getAttributeNameMap() { 89 return mapAttributeName; 90 } 91 92 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 107 public boolean canRename() { 108 return false; 109 } 110 111 private Node.Property createNameProperty() { 112 113 return new ElementNode.ElementProp(ElementProperties.PROP_NAME, String .class, false) { 114 public Object getValue() throws 115 IllegalAccessException , InvocationTargetException { 116 117 try { 118 JavaMetamodel.getDefaultRepository().beginTrans(false); 119 try { 120 String 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 (e); 129 } 130 } 131 }; 132 } 133 134 138 private Node.Property createParametersProperty(boolean canW) { 139 Node.Property p = createParametersProperty(getConstructor(), canW); 141 p.setValue("changeImmediate" ,Boolean.FALSE); return p; 143 } 144 145 149 private Node.Property createExceptionsProperty(boolean canW) { 150 Node.Property p = createExceptionsProperty(getConstructor(), canW); 151 p.setValue("changeImmediate" ,Boolean.FALSE); return p; 153 } 154 155 private Constructor getConstructor() { 156 return (Constructor) this.element; 157 } 158 159 163 private static final class NameListener extends WeakReference implements MDRChangeListener, Runnable { 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 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 [] names = new String [2]; 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 () { 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 |