1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import javax.jmi.reflect.RefObject; 25 import org.netbeans.jmi.javamodel.Annotation; 26 import org.netbeans.jmi.javamodel.AnnotationType; 27 import org.netbeans.jmi.javamodel.Attribute; 28 import org.netbeans.jmi.javamodel.AttributeValue; 29 import org.netbeans.jmi.javamodel.Element; 30 import org.netbeans.modules.javacore.parser.AnnotationValueInfo; 31 import org.netbeans.jmi.javamodel.ElementReference; 32 import org.netbeans.jmi.javamodel.InitialValue; 33 import org.netbeans.jmi.javamodel.JavaModelPackage; 34 import org.netbeans.jmi.javamodel.NamedElement; 35 import org.netbeans.lib.java.parser.ASTree; 36 import org.netbeans.mdr.storagemodel.StorableObject; 37 import org.netbeans.modules.javacore.JMManager; 38 import org.netbeans.modules.javacore.parser.AnnotationInfo; 39 import org.netbeans.modules.javacore.parser.AnnotationValueInfo; 40 import org.netbeans.modules.javacore.parser.ElementInfo; 41 42 43 47 public abstract class AttributeValueImpl extends SemiPersistentElement implements AttributeValue, ElementReference { 48 private static final ElementInfo DEFAULT_INFO = new AnnotationValueInfo(null, AnnotationValueInfo.ANNOTATIONVALUE_STRING, null, null); 49 50 private String attributeValueText; 51 private InitialValue attributeValue; 52 53 54 55 public AttributeValueImpl(StorableObject o) { 56 super(o); 57 } 58 59 public InitialValue getValue() { 60 if (!childrenInited) { 61 initChildren(); 62 } 63 return attributeValue; 64 } 65 66 public void setValue(InitialValue newValue) { 67 objectChanged(CHANGED_INITIAL_VALUE); 68 changeChild(getValue(), newValue); 69 attributeValue = newValue; 70 } 71 72 public Attribute getDefinition() { 73 String name=getName(); 74 75 if (name!=null) { 76 return (Attribute)getElement(); 77 } else { 78 Annotation ann=(Annotation)refImmediateComposite(); 79 AnnotationType annType=ann.getType(); 80 81 if (annType!=null) { 82 int valIndex=ann.getAttributeValues().indexOf(this); 83 Object [] features=annType.getContents().toArray(); 84 int attrIndex=0; 85 86 for (int i=0;i<features.length;i++) { 87 Object obj = features[i]; 88 if (obj instanceof Attribute) { 89 if (valIndex==attrIndex) 90 return (Attribute)obj; 91 attrIndex++; 92 } 93 } 94 } 95 } 96 return null; 97 } 98 99 public void setDefinition(Attribute newValue) { 100 throw new UnsupportedOperationException ("setDefinition()"); } 102 103 public List getChildren() { 104 List list = new ArrayList (1); 105 addIfNotNull(list, getValue()); 106 return list; 107 } 108 109 public String getSourceText() { 111 String origElem; 112 if ((origElem = checkChange()) != null) 113 return origElem; 114 115 StringBuffer buf = new StringBuffer (); 116 String name = getName(); 117 if (name != null && name.length() > 0) { 118 buf.append(name); 119 buf.append(formatElementPart(FIELD_EQUALS)); 120 } 121 buf.append(((MetadataElement) getValue()).getSourceText()); 122 return buf.toString(); 123 } 124 125 public void getDiff(List diff) { 126 if (isChanged(CHANGED_NAME)) { 127 replaceNode(diff, getParser(), getASTree().getSubTrees()[0], getName(), 0, null); 128 } 129 if (isChanged(CHANGED_INITIAL_VALUE)) { 130 getChildDiff(diff, getParser(), getASTree().getSubTrees()[1], (MetadataElement) attributeValue, CHANGED_INITIAL_VALUE); 131 } else if (isChanged(CHANGED_CHILDREN)) { 132 if (attributeValue != null) { 133 ((MetadataElement) attributeValue).getDiff(diff); 134 } 135 } 136 } 137 138 void setData(InitialValue attributeValue, String attributeValueText) { 139 changeChild(null, attributeValue); 140 this.attributeValue = attributeValue; 141 this.attributeValueText = attributeValueText; 142 } 143 144 protected void initChildren() { 145 if (childrenInited) { 146 JMManager.getTransactionMutex().addBFeatureToInitQueue(this); 147 } else { 148 initInitValue(); 149 } 150 } 151 152 public void initInitValue() { 153 childrenInited = false; 154 RefObject parent = (RefObject) refImmediateComposite(); 155 while (parent != null && !(parent instanceof AnnotationImpl)) { 156 parent = (RefObject) parent.refImmediateComposite(); 157 } 158 if (parent != null) { 159 AnnotationImpl annParent = (AnnotationImpl) parent; 160 AnnotationInfo initValInfo = (AnnotationInfo) annParent.getElementInfo(); 161 if (initValInfo != null) { 162 AnnotationValueInfo info = (AnnotationValueInfo) getElementInfo(); 163 ASTree tree = getASTree(); 164 if (tree != null) { 165 initValInfo.doAttribution(annParent); 166 } 167 if (info.infoType == AnnotationValueInfo.ANNOTATIONVALUE_ANNOTATION) { 168 InitialValue oldInit = attributeValue; 169 attributeValue = (Annotation) createElement((AnnotationInfo)info.value); 170 changeChild(oldInit,attributeValue); 171 if (oldInit != null) { 172 oldInit.refDelete(); 173 } 174 } else { 175 attributeValue = (InitialValue) initOrCreate(attributeValue, extractAttributeValue()); 176 } 177 } 178 } 179 childrenInited = true; 180 } 181 182 private ASTree extractAttributeValue() { 183 return getASTree().getSubTrees()[1]; 184 } 185 186 public Element duplicate(JavaModelPackage targetExtent) { 187 return targetExtent.getAttributeValue().createAttributeValue( 188 getName(), (InitialValue) duplicateElement(getValue(), targetExtent)); 189 } 190 191 public NamedElement getElement() { 192 String name=getName(); 193 194 if (name!=null) { 195 AnnotationType annType=((Annotation)refImmediateComposite()).getType(); 196 197 if (annType!=null) { 198 Iterator iter = annType.getFeatures().iterator(); 199 while (iter.hasNext()) { 200 Object obj = iter.next (); 201 if (obj instanceof Attribute) { 202 Attribute attrib = (Attribute) obj; 203 if (name.equals(attrib.getName())) 204 return attrib; 205 } 206 } 207 } 208 } 209 return null; 210 } 211 212 protected ElementInfo getDefaultInfo() { 213 return DEFAULT_INFO; 214 } 215 216 protected void matchPersistent(ElementInfo info) { 217 super.matchPersistent(info); 218 if (!isPersisted()) { 219 setPersisted(true); 220 persist(); 221 } else { 223 } 225 } 226 227 protected void matchElementInfo(ElementInfo newInfo) { 228 super.matchElementInfo(newInfo); 229 resetChildren(); 230 } 231 232 protected void resetChildren() { 233 if (childrenInited) { 234 InitialValue temp = attributeValue; 235 attributeValue = null; 236 changeChild(temp, null); 237 temp.refDelete(); 238 childrenInited = false; 239 } 240 } 241 242 public void replaceChild(Element oldElement, Element newElement) { 243 if (childrenInited && oldElement.equals(attributeValue)) { 244 setValue((InitialValue) newElement); 245 return; 246 } 247 super.replaceChild(oldElement, newElement); 248 } 249 250 public boolean isPersisted() { 251 return _getDelegate().getSlot1() != null; 252 } 253 254 public void setPersisted(boolean persisted) { 255 _getDelegate().setSlot1(persisted ? "" : null); 256 } 257 258 protected void _delete() { 259 if (childrenInited) { 260 deleteChild(attributeValue); 261 } 262 super._delete(); 263 } 264 } 265 | Popular Tags |