1 19 20 package org.netbeans.modules.form.codestructure; 21 22 import java.util.*; 23 24 32 33 final class DefaultCodeExpression implements CodeExpression { 34 35 private CodeStructure codeStructure; 36 37 private CodeExpressionOrigin expressionOrigin; 38 39 private CodeObjectUsage expressionUsage; 40 41 42 public DefaultCodeExpression(CodeStructure codeStructure, 43 CodeExpressionOrigin origin) 44 { 45 this.codeStructure = codeStructure; 46 setOrigin(origin); 47 } 48 49 DefaultCodeExpression(CodeStructure codeStructure) { 50 this.codeStructure = codeStructure; 51 } 52 53 55 public CodeStructure getCodeStructure() { 56 return codeStructure; 57 } 58 59 public CodeVariable getVariable() { 60 return codeStructure.getVariable(this); 61 } 62 63 public CodeExpressionOrigin getOrigin() { 64 return expressionOrigin; 65 } 66 67 public void setOrigin(CodeExpressionOrigin newOrigin) { 68 CodeExpressionOrigin oldOrigin = expressionOrigin; 69 if (oldOrigin == newOrigin) 70 return; 71 72 CodeExpression registerParent = null; 73 List registerParams = null; 74 75 if (oldOrigin != null) { 76 if (newOrigin != null) { CodeExpression oldParent = oldOrigin.getParentExpression(); 78 CodeExpression newParent = newOrigin.getParentExpression(); 79 if (oldParent != null && oldParent != newParent) 80 CodeStructure.unregisterObjectUsage(this, oldParent); 81 if (newParent != null && newParent != oldParent) 82 registerParent = newParent; 83 84 CodeExpression[] oldParams = oldOrigin.getCreationParameters(); 85 CodeExpression[] newParams = newOrigin.getCreationParameters(); 86 87 for (int i=0; i < oldParams.length; i++) { 88 CodeExpression oldPar = oldParams[i]; 89 if (i < newParams.length && oldPar == newParams[i]) 90 continue; 91 int j = 0; 92 while (j < newParams.length) { 93 if (oldPar == newParams[j]) 94 break; 95 j++; 96 } 97 if (j == newParams.length) 98 CodeStructure.unregisterObjectUsage(this, oldPar); 99 } 100 101 for (int i=0; i < newParams.length; i++) { 102 CodeExpression newPar = newParams[i]; 103 if (i < oldParams.length && newPar == oldParams[i]) 104 continue; 105 int j = 0; 106 while (j < oldParams.length) { 107 if (newPar == oldParams[j]) 108 break; 109 j++; 110 } 111 if (j == oldParams.length) { 112 if (registerParams == null) 113 registerParams = new ArrayList(); 114 registerParams.add(newPar); 115 } 116 } 117 } 118 else CodeStructure.unregisterUsingCodeObject(this); 119 } 120 121 expressionOrigin = newOrigin; 122 123 if (codeStructure.isUndoRedoRecording()) 124 codeStructure.logUndoableChange( 125 new OriginChange(oldOrigin, newOrigin)); 126 127 if (newOrigin != null) { 128 if (oldOrigin != null) { 129 if (registerParent != null) 130 registerParent.addUsingObject( 131 this, UsedCodeObject.DEFINED, CodeExpression.class); 132 133 if (registerParams != null) 134 for (int i=0, n=registerParams.size(); i < n; i++) { 135 CodeExpression param = (CodeExpression) 136 registerParams.get(i); 137 param.addUsingObject(this, 138 UsedCodeObject.USING, 139 CodeExpression.class); 140 } 141 } 142 else CodeStructure.registerUsingCodeObject(this); 143 } 144 } 145 146 150 public void addUsingObject(UsingCodeObject usingObject, 151 int useType, 152 Object useCategory) 153 { 154 CodeStructureChange undoableChange = 155 getExpressionUsage().addUsingObject( 156 usingObject, 157 useType, 158 useCategory, 159 codeStructure.isUndoRedoRecording()); 160 if (undoableChange != null) 161 codeStructure.logUndoableChange(undoableChange); 162 } 163 164 public boolean removeUsingObject(UsingCodeObject usingObject) { 165 CodeStructureChange undoableChange = 166 getExpressionUsage().removeUsingObject( 167 usingObject, 168 codeStructure.isUndoRedoRecording()); 169 if (undoableChange != null) 170 codeStructure.logUndoableChange(undoableChange); 171 172 boolean stillUsed = !getExpressionUsage().isEmpty(); 173 if (!stillUsed) codeStructure.removeExpressionFromVariable(this); 175 return stillUsed; 176 } 177 178 public Iterator getUsingObjectsIterator(int useType, Object useCategory) { 179 return getExpressionUsage().getUsingObjectsIterator(useType, useCategory); 180 } 181 182 private CodeObjectUsage getExpressionUsage() { 183 if (expressionUsage == null) 184 expressionUsage = new CodeObjectUsage(this); 185 return expressionUsage; 186 } 187 188 191 public void usageRegistered(UsedCodeObject usedObject) { 193 } 194 195 public boolean usedObjectRemoved(UsedCodeObject usedObject) { 197 codeStructure.removeExpressionFromVariable(this); 201 return false; 202 } 203 204 public UsedCodeObject getDefiningObject() { 205 return getOrigin().getParentExpression(); 206 } 207 208 public Iterator getUsedObjectsIterator() { 209 return new UsedObjectsIterator(); 210 } 211 212 214 private class OriginChange implements CodeStructureChange { 215 private CodeExpressionOrigin oldOrigin; 216 private CodeExpressionOrigin newOrigin; 217 218 OriginChange(CodeExpressionOrigin oldOrigin, 219 CodeExpressionOrigin newOrigin) 220 { 221 this.oldOrigin = oldOrigin; 222 this.newOrigin = newOrigin; 223 } 224 225 public void undo() { 226 expressionOrigin = oldOrigin; 227 } 228 229 public void redo() { 230 expressionOrigin = newOrigin; 231 } 232 } 233 234 236 private class UsedObjectsIterator implements Iterator { 237 int index; 238 CodeExpression[] parameters; 239 240 UsedObjectsIterator() { 241 index = getOrigin().getParentExpression() != null ? -1 : 0; 242 parameters = getOrigin().getCreationParameters(); 243 if (parameters == null) 244 parameters = CodeStructure.EMPTY_PARAMS; 245 } 246 247 public boolean hasNext() { 248 return index < parameters.length; 249 } 250 251 public Object next() { 252 if (!hasNext()) 253 throw new java.util.NoSuchElementException (); 254 255 Object obj = index > -1 ? parameters[index] : 256 getOrigin().getParentExpression(); 257 index++; 258 return obj; 259 } 260 261 public void remove() { 262 throw new UnsupportedOperationException (); 263 } 264 } 265 } 266 | Popular Tags |