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.StatementBlock; 25 import org.netbeans.lib.java.parser.ASTree; 26 import org.netbeans.lib.java.parser.ParserTokens; 27 import org.netbeans.lib.java.parser.Token; 28 import org.netbeans.mdr.storagemodel.StorableObject; 29 import org.netbeans.modules.javacore.parser.ASTProvider; 30 31 35 public abstract class StatementBlockImpl extends StatementImpl implements StatementBlock { 36 private LightAttrList statements = null; 37 38 39 public StatementBlockImpl(StorableObject o) { 40 super(o); 41 } 42 43 47 public List getStatements() { 48 if (!childrenInited) { 49 initChildren(); 50 } 51 return statements; 52 } 53 54 void setStatements(List statements) { 55 this.statements = createChildrenList("statements", statements, CHANGED_STATEMENTS); } 57 58 protected void _delete() { 59 if (childrenInited) { 61 deleteChildren(statements); 62 } 63 super._delete(); 67 } 68 69 public void replaceChild(Element oldElement,Element newElement) { 70 if (childrenInited) { 71 replaceObject(getStatements(),oldElement,newElement); 72 } 73 } 74 75 public List getChildren() { 76 return new ArrayList(getStatements()); 77 } 78 79 protected void initChildren() { 80 childrenInited = false; 81 statements = createChildrenList(statements, "statements", getASTree().getSubTrees(), CHANGED_STATEMENTS, false); childrenInited = true; 83 } 84 85 String getRawText() { 86 StringBuffer buf = new StringBuffer (); 87 List statements = getStatements(); 88 if (isNew()) { 89 buf.append('{'); 90 } else { 91 buf.append(IndentUtil.reformatTokenWithPads(this, getASTree().getFirstToken())); 92 } 93 for (Iterator iter = statements.iterator(); iter.hasNext();) { 94 MetadataElement stat = (MetadataElement) iter.next(); 95 buf.append(stat.getSourceText()); 96 } 97 if (isNew()) { 98 formatElementPart(BLOCK_CLOSE_CURLY, buf); 99 } else { 100 buf.append(IndentUtil.reformatTokenWithPads(this, getASTree().getLastToken())); 101 } 102 return buf.toString(); 103 } 104 105 protected String getIndentation() { 106 Object comp = refImmediateComposite(); 107 if (comp instanceof SemiPersistentElement) { 110 return ((SemiPersistentElement) comp).getIndentation(); 111 } else if (!(comp instanceof StatementBlock)) { 112 return ((TransientElement) comp).getIndentation(); 113 } else { 114 return ((TransientElement) comp).getIndentation().concat(INDENTATION); 115 } 116 } 117 118 public void getDiff(List diff) { 119 ASTProvider parser = getParser(); 120 ASTree tree = getASTree(); 121 ASTree[] children = tree.getSubTrees(); 122 if (children == null) 123 children = new ASTree[0]; 124 125 Token startToken = parser.getToken(tree.getLastToken()); 126 if (isChanged(CHANGED_STATEMENTS)) { 127 getStatementsDiff(diff, parser, children, getStatements(), getTokenStart(parser, startToken), "", null, 0, true); 128 } else if (isChanged(CHANGED_CHILDREN)) { 129 getElementsDiff(diff, getStatements()); 130 } 131 } 132 133 private static int getTokenStart(ASTProvider parser, Token startToken) { 134 int startOffset = parser.getToken(startToken.getFirstToken()-1).getEndOffset(); 135 Token[] pad = startToken.getPadding(); 136 if (pad.length > 0) { 137 for (int i = 0; i < pad.length; i++) { 138 if (pad[i].getType() == ParserTokens.EOL) { 139 break; 140 } 141 } 142 } 143 return startOffset; 144 156 } 157 158 protected int getStartOffset(ASTProvider parser, ASTree tree, boolean includePaddings) { 159 Token startToken = parser.getToken(tree.getFirstToken()); 160 if (includePaddings) { 161 return getTokenStart(parser, startToken); 162 } 163 return startToken.getStartOffset(); 164 } 165 166 public Element duplicate(JavaModelPackage targetExtent) { 167 return targetExtent.getStatementBlock().createStatementBlock(duplicateList(getStatements(), targetExtent)); 168 } 169 } 170 | Popular Tags |