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.Expression; 24 import org.netbeans.jmi.javamodel.JavaModelPackage; 25 import org.netbeans.jmi.javamodel.StatementBlock; 26 import org.netbeans.jmi.javamodel.SynchronizedStatement; 27 import org.netbeans.lib.java.parser.ASTree; 28 import org.netbeans.mdr.storagemodel.StorableObject; 29 import org.netbeans.modules.javacore.parser.ASTProvider; 30 31 35 public abstract class SynchronizedStatementImpl extends StatementImpl implements SynchronizedStatement { 36 private Expression expression = null; 37 private StatementBlock body = null; 38 39 40 public SynchronizedStatementImpl(StorableObject o) { 41 super(o); 42 } 43 44 public void setLock(Expression expression) { 45 objectChanged(CHANGED_LOCK); 46 changeChild(getLock(), expression); 47 this.expression = expression; 48 } 49 50 public Expression getLock() { 51 if (!childrenInited) { 52 initChildren(); 53 } 54 return expression; 55 } 56 57 public void setBody(StatementBlock body) { 58 objectChanged(CHANGED_BODY); 59 changeChild(getBody(), body); 60 this.body = body; 61 } 62 63 public StatementBlock getBody() { 64 if (!childrenInited) { 65 initChildren(); 66 } 67 return body; 68 } 69 70 public List getChildren() { 71 List list = new ArrayList(2); 72 addIfNotNull(list, getLock()); 73 addIfNotNull(list, getBody()); 74 return list; 75 } 76 77 protected void initChildren() { 78 childrenInited = false; 79 ASTree tree = getASTree(); 80 if (tree != null) { 81 expression = (Expression) initOrCreate(expression, tree.getSubTrees()[0]); 82 body = (StatementBlock) initOrCreate(body, tree.getSubTrees()[1]); 83 } 84 childrenInited = true; 85 } 86 87 public String getSourceText() { 88 String origElem; 89 if ((origElem = checkChange()) != null) 90 return origElem; 91 StringBuffer buf = new StringBuffer (); 92 StatementImpl body = (StatementImpl) getBody(); 93 StatementImpl lock = (StatementImpl) getLock(); 94 buf.append('\n'); 95 formatElementPart(SYNCHRONIZED_KEYWORD, buf); 96 formatElementPart(STMT_OPEN_BRACKET, buf); 97 buf.append(lock.getSourceText()); 98 formatElementPart(STMT_CLOSE_BRACKET, buf); 99 buf.append(body.getSourceText()); 100 return buf.toString(); 101 } 102 103 public void getDiff(List diff) { 104 ASTProvider parser = getParser(); 105 ASTree tree = getASTree(); 106 ASTree[] children = tree.getSubTrees(); 107 108 getChildDiff(diff, parser, children[0], (MetadataElement) getLock(), CHANGED_LOCK); 109 getChildDiff(diff, parser, children[1], (MetadataElement) getBody(), CHANGED_BODY); 110 } 111 112 void setData(Expression expression, StatementBlock body) { 113 changeChild(null, expression); 114 this.expression = expression; 115 changeChild(null, body); 116 this.body = body; 117 } 118 119 protected void _delete() { 120 if (childrenInited) { 122 deleteChild(expression); 123 deleteChild(body); 124 } 125 super._delete(); 129 } 130 131 public void replaceChild(Element oldElement,Element newElement) { 132 if (childrenInited) { 133 if (oldElement.equals(expression)) { 134 setLock((Expression)newElement); 135 } else if (oldElement.equals(body)) { 136 setBody((StatementBlock)newElement); 137 } 138 } 139 } 140 141 public Element duplicate(JavaModelPackage targetExtent) { 142 return targetExtent.getSynchronizedStatement().createSynchronizedStatement( 143 (Expression) duplicateElement(getLock(), targetExtent), 144 (StatementBlock) duplicateElement(getBody(), targetExtent)); 145 } 146 } 147 | Popular Tags |