1 19 20 package org.netbeans.modules.java.ui.nodes.elements; 21 22 import java.beans.*; 23 import java.lang.reflect.InvocationTargetException ; 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 46 public final class ClassNode extends ElementNode { 47 48 49 private static final String [] ICON_AFFECTING_PROPERTIES = new String [] { 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); 62 mapClassAttributeName = new HashMap(mapInterfaceAttributeName); 63 mapClassAttributeName.put("superClassName", PROP_SUPERCLASS); } 65 66 69 private boolean isInterface; 70 71 72 private boolean isSheetCreated = false; 73 74 private DataObject sourceDO; 75 76 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 101 protected String resolveIconBase() { 102 return IconResolver.getIconBaseForJavaClass(getJavaClass()); 103 } 104 105 109 protected String [] getIconAffectingProperties() { 110 return ICON_AFFECTING_PROPERTIES; 111 } 112 113 protected ElementFormat getElementFormatProperty() { 114 return getElementFormat(isInterface); 115 } 116 117 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 src = ae.getSource(); 139 if (src != element || !((JavaClass) src).isValid()) { 140 return cd; 141 } 142 String 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; } 163 super.processChange(desc); 164 } 165 166 167 protected Sheet createSheet () { 168 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 192 protected Node.Property createSuperclassProperty(boolean canW) { 193 return new ElementNode.ElementProp(PROP_SUPERCLASS, String .class, canW) { 194 195 public Object getValue () { 196 MultipartId mid = getJavaClass().getSuperClassName(); 197 return mid == null ? "" : IdentifierArrayEditor.multipartIdToName(mid); } 199 200 201 public void setValue(final Object val) throws IllegalArgumentException , 202 IllegalAccessException , InvocationTargetException { 203 super.setValue(val); 204 if (!(val instanceof String )) 205 throw new IllegalArgumentException (); 206 String str = ((String ) 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 iaex = new IllegalArgumentException (); 221 iaex.initCause(ex); 222 throw ex; 223 } 224 } 228 229 } 230 }; 231 } 232 233 238 protected Node.Property createInterfacesProperty(boolean canW) { 239 Node.Property prop = createInterfacesProperty(getJavaClass(), canW); 240 241 if (isInterface) { 242 prop.setDisplayName(getString("PROP_superInterfaces")); prop.setShortDescription(getString("HINT_superInterfaces")); } 245 prop.setValue("changeImmediate" , Boolean.FALSE); 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" , Boolean.FALSE); 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 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 getValue () { 298 return element.getInterfaceNames().toArray(new MultipartId[0]); 299 } 300 301 public void setValue(final Object val) throws IllegalArgumentException , 302 IllegalAccessException , InvocationTargetException { 303 super.setValue(val); 304 if (!(val instanceof MultipartId[])) 305 throw new IllegalArgumentException (); 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 iaex = new IllegalArgumentException (); 320 iaex.initCause(ex); 321 throw ex; 322 } 323 } 324 } 325 } 326 | Popular Tags |