1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.util.*; 22 import org.netbeans.jmi.javamodel.Case; 23 import org.netbeans.jmi.javamodel.Element; 24 import org.netbeans.jmi.javamodel.Expression; 25 import org.netbeans.jmi.javamodel.JavaModelPackage; 26 import org.netbeans.lib.java.parser.ASTree; 27 import org.netbeans.lib.java.parser.ASTreeTypes; 28 import org.netbeans.mdr.storagemodel.StorableObject; 29 import org.netbeans.modules.javacore.parser.ASTProvider; 30 31 35 public abstract class CaseImpl extends TransientElement implements Case { 36 private Expression value = null; 37 private LightAttrList statements = null; 38 39 40 public CaseImpl(StorableObject o) { 41 super(o); 42 } 43 44 public Expression getValue() { 45 if (!childrenInited) { 46 initChildren(); 47 } 48 return value; 49 } 50 51 public void setValue(Expression value) { 52 objectChanged(CHANGED_VALUE); 53 changeChild(getValue(), value); 54 this.value = value; 55 } 56 57 public List getStatements() { 58 if (!childrenInited) { 59 initChildren(); 60 } 61 return statements; 62 } 63 64 public List getChildren() { 65 List list = new ArrayList(1 + getStatements().size()); 66 addIfNotNull(list, getValue()); 67 list.addAll(getStatements()); 68 return list; 69 } 70 71 protected void initChildren() { 72 childrenInited = false; 73 ASTree tree = getASTree(); 74 if (tree != null) { 75 value = (Expression) initOrCreate(value, getLabelAST(tree)); 76 statements = createChildrenList(statements, "statements", getStatementsAST(tree), CHANGED_STATEMENTS, false); } 78 childrenInited = true; 79 } 80 81 private static ASTree getLabelAST(ASTree tree) { 82 if (tree.getType() == ASTreeTypes.SWITCH_BLOCK_STATEMENT_GROUP) { 83 ASTree[] cases = tree.getSubTrees(); 84 ASTree[] labels = cases[0].getSubTrees(); 85 return cases[0].getType() == ASTreeTypes.SWITCH_LABELS ? 86 labels[labels.length - 1].getSubTrees()[0] : labels[0]; 87 } else { 88 return tree.getSubTrees()[0]; 89 } 90 } 91 92 private static ASTree[] getStatementsAST(ASTree tree) { 93 if (tree.getType() == ASTreeTypes.SWITCH_BLOCK_STATEMENT_GROUP) { 94 return tree.getSubTrees()[1].getSubTrees(); 95 } else { 96 return null; 97 } 98 } 99 100 public String getSourceText() { 101 StringBuffer buf = new StringBuffer (); 105 List statements = getStatements(); 106 TransientElement value = (TransientElement)getValue(); 107 buf.append('\n'); 108 if (value != null) { 109 formatElementPart(CASE_KEYWORD, buf); 110 buf.append(value.getSourceText()); 111 } else { 112 formatElementPart(DEFAULT_KEYWORD, buf); 113 } 114 buf.append(':'); 115 for (Iterator iter = statements.iterator(); iter.hasNext();) { 116 buf.append('\n'); 117 StatementImpl stat = (StatementImpl) iter.next(); 118 buf.append(stat.getSourceText()); 119 } 120 return buf.toString(); 121 } 122 123 public void getDiff(List diff) { 124 ASTProvider parser = getParser(); 125 ASTree tree = getASTree(); 126 127 getChildDiff(diff, parser, getLabelAST(tree), (MetadataElement) getValue(), CHANGED_VALUE); 128 getCollectionDiff(diff, parser, CHANGED_STATEMENTS, getStatementsAST(tree), getStatements(), getEndOffset(parser, tree), "\n"); } 130 131 void setData(Expression value, List statements) { 132 changeChild(null, value); 134 this.value = value; 135 this.statements = createChildrenList("statements", statements, CHANGED_STATEMENTS); } 138 139 protected void _delete() { 140 if (childrenInited) { 142 deleteChild(value); 143 deleteChildren(statements); 144 } 145 super._delete(); 149 } 150 151 public void replaceChild(Element oldElement,Element newElement){ 152 if (childrenInited) { 153 if (oldElement.equals(value)) { 154 setValue((Expression)newElement); 155 } else 156 replaceObject(getStatements(),oldElement,newElement); 157 } 158 } 159 160 public Element duplicate(JavaModelPackage targetExtent) { 161 return targetExtent.getCase().createCase( 162 (Expression) duplicateElement(getValue(), targetExtent), duplicateList(getStatements(), targetExtent)); 163 } 164 } 165 | Popular Tags |