1 19 package org.netbeans.modules.javacore.jmiimpl.javamodel; 20 21 import java.lang.ref.Reference ; 22 import java.util.*; 23 import org.netbeans.lib.java.parser.ASTree; 24 import org.netbeans.lib.java.parser.Token; 25 import org.netbeans.mdr.storagemodel.StorableObject; 26 import org.netbeans.modules.javacore.JMManager; 27 import org.netbeans.modules.javacore.parser.ASTProvider; 28 import org.netbeans.modules.javacore.parser.ElementInfo; 29 import org.netbeans.modules.javacore.parser.MDRParser; 30 import org.openide.ErrorManager; 31 32 36 public abstract class TransientElement extends MetadataElement { 37 private Reference tree; 38 private int firstToken, lastToken; 39 private int astType; 40 private ResourceImpl resource; 41 42 private boolean isNew = false; 43 44 45 public TransientElement(StorableObject o) { 46 super(o); 47 } 48 49 protected final void init(ASTree tree) { 50 MDRParser parser = (MDRParser) tree.getASTContext(); 51 if (parser == null) { 52 JMManager.getLog().notify(ErrorManager.INFORMATIONAL, new Exception ("getASTContext() returned null for: " + tree)); JMManager.getLog().log("Using Element.getResource() instead of parser.getResource() to retrieve the parent resource..."); 55 this.resource = (ResourceImpl) getResource(); 56 } else { 57 this.resource = (ResourceImpl) parser.getResource(); 58 } 59 this.tree = ElementInfo.createASTReference(tree); 60 this.firstToken = tree.getFirstToken(); 61 this.lastToken = tree.getLastToken(); 62 this.astType = tree.getType(); 63 if (childrenInited) { 65 initChildren(); 66 } 67 resetChange(); 68 } 69 70 public final ASTree getASTree() { 71 initCheck(); 72 if (tree == null) return null; 73 ASTree result = (ASTree) tree.get(); 74 if (result == null) { 75 ASTree parentTree = resource.getASTree(); 76 MDRParser parser = (MDRParser) parentTree.getASTContext(); 77 result = parser.findTree(parentTree, firstToken, lastToken, astType); 78 this.tree = ElementInfo.createASTReference(result); 79 } 80 return result; 81 82 } 83 84 public final MDRParser getParser() { 85 ASTree tree = getASTree(); if (tree == null) return null; 87 return (MDRParser) tree.getASTContext(); 88 } 89 90 protected final Object getInternalForm() { 91 return getASTree(); 92 } 93 94 protected final boolean isInitialized() { 95 return (tree != null) || isNew; 96 } 97 98 protected final void uninitialize() { 99 tree = null; 100 resource = null; 101 isNew = false; 102 } 103 104 105 protected void setNew() { 106 setChanged(); 107 isNew = true; 108 childrenInited = true; 109 } 110 111 protected final boolean isNew() { 112 return isNew; 113 } 114 115 public void getCollectionDiff(List diff, ASTProvider parser, int changeMask, ASTree parentNode, int groupNodeType, Collection newElements, int endOffset, String separator) { 116 ASTree[] oldElements = getChildNodes(parentNode, groupNodeType); 117 getCollectionDiff(diff, parser, changeMask, oldElements, newElements, endOffset, separator); 118 } 119 120 protected String getIndentation() { 121 super.getIndentation(); 122 Object comp = refImmediateComposite(); 123 if (comp instanceof SemiPersistentElement) { 124 return ((SemiPersistentElement) refImmediateComposite()).getIndentation().concat(INDENTATION); 125 } else { 126 return ((TransientElement) refImmediateComposite()).getIndentation().concat(INDENTATION); 127 } 128 } 129 130 protected void printListOfItems (String origSrc, StringBuffer buf, ASTree[] children, Map oldItems, List items) { 131 ASTProvider parser = getParser(); 132 if (oldItems == null) { 133 oldItems = new HashMap(); 134 for (int x = 0; x < children.length; x++) { 135 Token tok = (x < children.length - 1) ? 136 parser.getToken(children[x].getLastToken() + 1) : null; 137 oldItems.put(children[x], tok); 138 } 139 } 140 Iterator iter = items.iterator(); 141 TransientElement item = null; 142 int x; 143 for (x = 0; iter.hasNext(); x++) { 144 item = (TransientElement) iter.next(); 145 ASTree itemTree = item.getASTree(); 146 if (!((x < children.length) && (itemTree == children[x]) && !item.isChanged())) { 147 x--; 148 break; 149 } 150 } 151 if ((x > -1) && (item != null)) { 152 Token token = null; 153 if (x == (children.length - 1)) { 154 item = null; 155 token = parser.getToken(children[x].getLastToken()); 156 } else { 157 token = parser.getToken(children[x].getLastToken() + 1); 158 } 159 int start = parser.getToken(children[0].getFirstToken()).getStartOffset(); 160 buf.append(origSrc.substring(start, token.getEndOffset())); 161 } 162 163 while (item != null) { 164 ASTree itemTree = item.getASTree(); 165 Token tok = (Token) oldItems.get (itemTree); 166 buf.append(item.getSourceText()); 167 if (iter.hasNext()) { 168 if (tok != null) { 169 buf.append(origSrc.substring(tok.getStartOffset(), tok.getEndOffset())); 170 } else { 171 buf.append(", "); } 173 } 174 item = iter.hasNext() ? (StatementImpl) iter.next() : null; 175 } } 177 178 protected void invalidate() { 179 super._delete(); 180 } 181 182 191 protected final void rollback() { 192 if (isValid()) { 194 _delete(); 195 } 196 } 197 } 198 | Popular Tags |