1 19 20 package org.netbeans.modules.java.ui.nodes.elements; 21 22 import java.lang.reflect.InvocationTargetException ; 23 import java.util.Map ; 24 import java.util.HashMap ; 25 26 import org.openide.nodes.*; 27 import org.openide.ErrorManager; 28 import org.openide.src.ElementProperties; 29 import org.openide.util.NbBundle; 30 import org.netbeans.jmi.javamodel.*; 31 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 32 33 import javax.jmi.reflect.JmiException; 34 35 41 public final class FieldNode extends ElementNode { 42 43 private static final Map mapAttributeName; 44 45 static { 46 mapAttributeName = new HashMap (); 47 mapAttributeName.put(PROP_MODIFIERS, PROP_MODIFIERS); 48 mapAttributeName.put(ElementProperties.PROP_NAME, ElementProperties.PROP_NAME); 49 mapAttributeName.put("typeName", PROP_TYPE); mapAttributeName.put("initialValueText", PROP_INIT_VALUE); } 52 53 57 public FieldNode(Field element, boolean writeable) { 58 super(element, Children.LEAF, writeable); 59 setElementFormat0(getElementFormatProperty()); 60 superSetName(element.getName()); 61 } 62 63 66 protected String resolveIconBase() { 67 return IconResolver.getIconBaseForField(getField()); 68 } 69 70 protected ElementFormat getElementFormatProperty() { 71 return getSourceOptions().getFieldElementFormat(); 72 } 73 74 77 protected ElementFormat getHintElementFormat() { 78 return getSourceOptions().getFieldElementLongFormat(); 79 } 80 81 protected Map getAttributeNameMap() { 82 return mapAttributeName; 83 } 84 85 86 protected Sheet createSheet () { 87 Sheet sheet = Sheet.createDefault(); 88 Sheet.Set ps = sheet.get(Sheet.PROPERTIES); 89 ps.put(createModifiersProperty(writeable)); 90 ps.put(createNameProperty(getField())); 91 ps.put(createTypeProperty(writeable)); 92 ps.put(createInitValueProperty(writeable)); 93 return sheet; 94 } 95 96 100 protected Node.Property createTypeProperty(boolean canW) { 101 Node.Property prop = createTypeProperty(PROP_TYPE, getField(), canW); 102 prop.setValue("acceptVoidType", Boolean.FALSE); return prop; 104 } 105 106 110 protected Node.Property createInitValueProperty(boolean canW) { 111 return new ElementNode.ElementProp(PROP_INIT_VALUE, String .class, canW) { 112 113 public Object getValue () { 114 String val = getField().getInitialValueText(); 115 return val != null? val: ""; } 117 118 119 public void setValue(final Object val) throws IllegalArgumentException , 120 IllegalAccessException , InvocationTargetException { 121 122 super.setValue(val); 123 if (!(val instanceof String )) throw new IllegalArgumentException (); 124 125 boolean fail = true; 126 try { 127 JavaMetamodel.getDefaultRepository().beginTrans(true); 128 try { 129 getField().setInitialValueText((String ) val); 130 fail = false; 131 } finally { 132 JavaMetamodel.getDefaultRepository().endTrans(fail); 133 } 134 } catch (JmiException e) { 135 IllegalArgumentException iae = new IllegalArgumentException (); 136 iae.initCause(e); 137 ErrorManager.getDefault().annotate(iae, ErrorManager.USER, null, 138 NbBundle.getMessage(FieldNode.class, "MSG_InvalidInitVal"), null, null); throw iae; 140 } 141 } 142 }; 143 } 144 145 private Field getField() { 146 return (Field) this.element; 147 } 148 149 } 150 | Popular Tags |