1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import org.netbeans.jmi.javamodel.Element; 22 import org.netbeans.jmi.javamodel.Expression; 23 import org.netbeans.jmi.javamodel.SwitchStatement; 24 import org.netbeans.lib.java.parser.ASTree; 25 import org.netbeans.lib.java.parser.ASTreeTypes; 26 import org.netbeans.mdr.storagemodel.StorableObject; 27 import org.netbeans.modules.javacore.parser.ASTProvider; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import org.netbeans.jmi.javamodel.JavaModelPackage; 32 33 37 public abstract class SwitchStatementImpl extends ConditionImpl implements SwitchStatement { 38 private LightAttrList cases = null; 39 40 41 public SwitchStatementImpl(StorableObject o) { 42 super(o); 43 } 44 45 public List getCases() { 46 if (!childrenInited) { 47 initChildren(); 48 } 49 return cases; 50 } 51 52 public List getChildren() { 53 List list = super.getChildren(); 54 list.addAll(getCases()); 55 return list; 56 } 57 58 protected void initChildren() { 59 childrenInited = false; 60 ASTree tree = getASTree(); 61 if (tree != null) { 62 ASTree[] parts = tree.getSubTrees(); 63 expression = (Expression) initOrCreate(expression, parts[0]); 64 ASTree[] block = parts[1].getSubTrees(); 65 ArrayList children = new ArrayList (); 66 if (block[0] != null) { 67 ASTree[] groups = block[0].getSubTrees(); 68 for (int i = 0; i < groups.length; i++) { 69 ASTree[] cases = groups[i].getSubTrees(); 70 if (cases[0].getType() == ASTreeTypes.SWITCH_LABELS) { 71 ASTree[] labels = cases[0].getSubTrees(); 72 for (int j = 0; j < labels.length - 1; j++) { 73 children.add(labels[j]); 74 } 75 } 76 children.add(groups[i]); 77 } 78 } 79 if (block[1] != null) { 80 if (block[1].getType() == ASTreeTypes.SWITCH_LABELS) { 81 ASTree[] labels = block[1].getSubTrees(); 82 for (int j = 0; j < labels.length; j++) { 83 children.add(labels[j]); 84 } 85 } 86 } 87 cases = createChildrenList(cases, "cases", (ASTree[]) children.toArray(new ASTree[children.size()]), CHANGED_CASES, false); } 89 childrenInited = true; 90 } 91 92 public String getRawText() { 93 StringBuffer buf = new StringBuffer (); 94 List cases = getCases(); 95 TransientElement expr = (TransientElement) getExpression(); 96 formatElementPart(SWITCH_KEYWORD, buf, isNew()); 97 formatElementPart(STMT_OPEN_BRACKET, buf); 98 buf.append(expr.getSourceText()); 99 formatElementPart(STMT_CLOSE_BRACKET, buf); 100 formatElementPart(BLOCK_OPEN_CURLY, buf); 101 for (Iterator iter = cases.iterator(); iter.hasNext();) { 102 CaseImpl casik = (CaseImpl) iter.next(); 103 buf.append(casik.getSourceText()); 104 } 105 formatElementPart(BLOCK_CLOSE_CURLY, buf); 106 return buf.toString(); 107 } 108 109 public void getDiff(List diff) { 110 if (isChanged()) { 111 ASTProvider parser = getParser(); 112 ASTree tree = getASTree(); 113 114 if (isChanged(CHANGED_CASES)) { 115 diff.add(new DiffElement(parser.getToken(tree.getFirstToken()).getStartOffset(), parser.getToken(tree.getLastToken()).getEndOffset(), getSourceText())); 116 } else { 117 getChildDiff(diff, parser, tree.getSubTrees()[0], (MetadataElement) getExpression(), CHANGED_EXPRESSION); 118 if (isChanged(CHANGED_CHILDREN)) { 119 for (Iterator it = getCases().iterator(); it.hasNext();) { 120 CaseImpl c = (CaseImpl) it.next(); 121 if (c.isChanged()) { 122 c.getDiff(diff); 123 } 124 } 125 } 126 } 127 } 128 } 129 130 void setData(Expression expression, List cases) { 131 changeChild(null, expression); 133 this.expression = expression; 134 this.cases = createChildrenList("cases", cases, CHANGED_CASES); } 137 138 protected void _delete() { 139 if (childrenInited) { 141 deleteChildren(cases); 142 } 143 super._delete(); 147 } 148 149 public void replaceChild(Element oldElement,Element newElement) { 150 if (childrenInited) { 151 if (replaceObject(getCases(),oldElement,newElement)) 152 return; 153 super.replaceChild(oldElement,newElement); 154 } 155 } 156 157 public Element duplicate(JavaModelPackage targetExtent) { 158 return targetExtent.getSwitchStatement().createSwitchStatement( 159 (Expression) duplicateElement(getExpression(), targetExtent), 160 duplicateList(getCases(), targetExtent) 161 ); 162 } 163 } 164 | Popular Tags |