KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > codestructure > DefaultCodeExpression


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.form.codestructure;
21
22 import java.util.*;
23
24 /**
25  * Default implementation class of CodeExpression interface. No other class
26  * should implement CodeExpression or extend this class. All extensibility
27  * is done through the CodeExpressionOrigin implementations. CodeExpression
28  * is kept as an interface for compatibility and usability reasons.
29  *
30  * @author Tomas Pavek
31  */

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     // -------
54

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) { // changing one origin to another
77
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     // --------
147
// UsedCodeObject implementation - registering objects that use
148
// this expression
149

150     public void addUsingObject(UsingCodeObject usingObject,
151                                int useType,
152                                Object JavaDoc 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) // the elment is no longer used in the structure
174
codeStructure.removeExpressionFromVariable(this);
175         return stillUsed;
176     }
177
178     public Iterator getUsingObjectsIterator(int useType, Object JavaDoc 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     // ---------
189
// UsingCodeObject implementation - handling objects used by this expression
190

191     // notifying about registering this object in used object
192
public void usageRegistered(UsedCodeObject usedObject) {
193     }
194
195     // notifying about removing the used object from structure
196
public boolean usedObjectRemoved(UsedCodeObject usedObject) {
197 // if (!(usedObject instanceof CodeExpression))
198
// return true;
199
// an used expression was removed - this expression will be too ...
200
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     // --------
213

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     // --------
235

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 JavaDoc next() {
252             if (!hasNext())
253                 throw new java.util.NoSuchElementException JavaDoc();
254
255             Object JavaDoc obj = index > -1 ? parameters[index] :
256                                       getOrigin().getParentExpression();
257             index++;
258             return obj;
259         }
260
261         public void remove() {
262             throw new UnsupportedOperationException JavaDoc();
263         }
264     }
265 }
266
Popular Tags