1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.util.*; 22 import org.netbeans.jmi.javamodel.Element; 23 import org.netbeans.jmi.javamodel.JavaModelPackage; 24 import org.netbeans.jmi.javamodel.LabeledStatement; 25 import org.netbeans.jmi.javamodel.Statement; 26 import org.netbeans.lib.java.parser.ASTree; 27 import org.netbeans.mdr.storagemodel.StorableObject; 28 import org.netbeans.modules.javacore.parser.ASTProvider; 29 import org.netbeans.modules.javacore.parser.ASTUtil; 30 31 35 public abstract class LabeledStatementImpl extends StatementImpl implements LabeledStatement { 36 private String label = null; 37 private Statement body = null; 38 39 40 public LabeledStatementImpl(StorableObject o) { 41 super(o); 42 } 43 44 public void setLabel(String label) { 45 objectChanged(CHANGED_LABEL); 46 this.label = label; 47 } 48 49 public String getLabel() { 50 if (isChanged(CHANGED_LABEL)) { 51 return label; 52 } else { 53 return ASTUtil.getIdentifier(getASTree().getSubTrees()[0]); 54 } 55 } 56 57 public void setBody(Statement body) { 58 objectChanged(CHANGED_BODY); 59 changeChild(getBody(), body); 60 this.body = body; 61 } 62 63 public List getChildren() { 64 List list = new ArrayList(1); 65 addIfNotNull(list, getBody()); 66 return list; 67 } 68 69 protected void initChildren() { 70 childrenInited = false; 71 ASTree tree = getASTree(); 72 if (tree != null) { 73 body = (Statement) initOrCreate(body, getASTree().getSubTrees()[1]); 74 } 75 childrenInited = true; 76 } 77 78 public Statement getBody() { 79 if (!childrenInited) { 80 initChildren(); 81 } 82 return body; 83 } 84 85 String getRawText() { 86 StringBuffer buf = new StringBuffer (); 87 StatementImpl body = (StatementImpl)getBody(); 88 String label = getLabel(); 89 buf.append(label); 90 buf.append(": "); buf.append(body.getSourceText()); 92 return buf.toString(); 93 } 94 95 public void getDiff(List diff) { 96 ASTProvider parser = getParser(); 97 ASTree tree = getASTree(); 98 ASTree[] children = tree.getSubTrees(); 99 100 if (isChanged(CHANGED_LABEL)) { 101 replaceNode(diff, parser, children[0], getLabel(), 0, null); 102 } 103 getChildDiff(diff, parser, children[1], (MetadataElement) getBody(), CHANGED_BODY); 104 } 105 106 void setData(String label, Statement body) { 107 this.label = label; 108 changeChild(null, body); 109 this.body = body; 110 } 111 112 protected void _delete() { 113 if (childrenInited) { 115 deleteChild(body); 116 } 117 super._delete(); 121 } 122 123 public void replaceChild(Element oldElement,Element newElement) { 124 if (childrenInited) { 125 if (oldElement.equals(body)) { 126 setBody((Statement)newElement); 127 } 128 } 129 } 130 131 public Element duplicate(JavaModelPackage targetExtent) { 132 return targetExtent.getLabeledStatement().createLabeledStatement( 133 getLabel(), 134 (Statement) duplicateElement(getBody(), targetExtent) 135 ); 136 } 137 } 138 | Popular Tags |