KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > viewers > model > provisional > ModelDelta


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.debug.internal.ui.viewers.model.provisional;
12
13
14
15
16 /**
17  * A model delta. Used to create model deltas.
18  * <p>
19  * Clients may instantiate this class; not intended to be subclassed.
20  * </p>
21  * @see org.eclipse.debug.internal.ui.viewers.IModelDelta
22  * @since 3.2
23  */

24 public class ModelDelta implements IModelDelta {
25
26     private IModelDelta fParent;
27     private Object JavaDoc fElement;
28     private int fFlags;
29     private ModelDelta[] fNodes = EMPTY_NODES;
30     private Object JavaDoc fReplacement;
31     private int fIndex = -1;
32     private int fChildCount = -1;
33     private static final ModelDelta[] EMPTY_NODES = new ModelDelta[0];
34
35     /**
36      * Constructs a new delta for the given element.
37      *
38      * @param element model element
39      * @param flags change flags
40      */

41     public ModelDelta(Object JavaDoc element, int flags) {
42         fElement = element;
43         fFlags = flags;
44     }
45
46     /**
47      * Constructs a new delta for the given element to be replaced
48      * with the specified replacement element.
49      *
50      * @param element model element
51      * @param replacement replacement element
52      * @param flags change flags
53      */

54     public ModelDelta(Object JavaDoc element, Object JavaDoc replacement, int flags) {
55         fElement = element;
56         fReplacement = replacement;
57         fFlags = flags;
58     }
59
60     /**
61      * Constructs a new delta for the given element to be inserted at
62      * the specified index.
63      *
64      * @param element model element
65      * @param index insertion position
66      * @param flags change flags
67      */

68     public ModelDelta(Object JavaDoc element, int index, int flags) {
69         fElement = element;
70         fIndex = index;
71         fFlags = flags;
72     }
73     
74     /**
75      * Constructs a new delta for the given element at the specified index
76      * relative to its parent with the given number of children.
77      *
78      * @param element model element
79      * @param index insertion position
80      * @param flags change flags
81      * @param childCount number of children this node has
82      */

83     public ModelDelta(Object JavaDoc element, int index, int flags, int childCount) {
84         fElement = element;
85         fIndex = index;
86         fFlags = flags;
87         fChildCount = childCount;
88     }
89
90     /* (non-Javadoc)
91      * @see org.eclipse.debug.internal.ui.viewers.IModelDelta#getElement()
92      */

93     public Object JavaDoc getElement() {
94         return fElement;
95     }
96
97     /* (non-Javadoc)
98      * @see org.eclipse.debug.internal.ui.viewers.IModelDelta#getFlags()
99      */

100     public int getFlags() {
101         return fFlags;
102     }
103
104     /**
105      * Adds a child node to this delta with the given element and change flags,
106      * and returns the child delta.
107      *
108      * @param element child element to add
109      * @param flags change flags for child
110      * @return newly created child delta
111      */

112     public ModelDelta addNode(Object JavaDoc element, int flags) {
113         ModelDelta node = new ModelDelta(element, flags);
114         node.setParent(this);
115         addDelta(node);
116         return node;
117     }
118     
119     /**
120      * Returns the child delta for the given element, or <code>null</code> if none.
121      *
122      * @param element child element
123      * @return corresponding delta node, or <code>null</code>
124      */

125     public ModelDelta getChildDelta(Object JavaDoc element) {
126         if (fNodes != null) {
127             for (int i = 0; i < fNodes.length; i++) {
128                 ModelDelta delta = fNodes[i];
129                 if (element.equals(delta.getElement())) {
130                     return delta;
131                 }
132             }
133         }
134         return null;
135     }
136
137     /**
138      * Adds a child node to this delta to replace the given element with the
139      * specified replacement element and change flags, and returns the
140      * newly created child delta.
141      *
142      * @param element child element to add to this delta
143      * @param replacement replacement element for the child element
144      * @param flags change flags
145      * @return newly created child delta
146      */

147     public ModelDelta addNode(Object JavaDoc element, Object JavaDoc replacement, int flags) {
148         ModelDelta node = new ModelDelta(element, replacement, flags);
149         node.setParent(this);
150         addDelta(node);
151         return node;
152     }
153
154     /**
155      * Adds a child delta to this delta to insert the specified element at
156      * the given index, and returns the newly created child delta.
157      *
158      * @param element child element in insert
159      * @param index index of insertion
160      * @param flags change flags
161      * @return newly created child delta
162      */

163     public ModelDelta addNode(Object JavaDoc element, int index, int flags) {
164         ModelDelta node = new ModelDelta(element, index, flags);
165         node.setParent(this);
166         addDelta(node);
167         return node;
168     }
169     
170     /**
171      * Adds a child delta to this delta at the specified index with the
172      * given number of children, and returns the newly created child delta.
173      *
174      * @param element child element in insert
175      * @param index index of the element relative to parent
176      * @param flags change flags
177      * @param numChildren the number of children the element has
178      * @return newly created child delta
179      */

180     public ModelDelta addNode(Object JavaDoc element, int index, int flags, int numChildren) {
181         ModelDelta node = new ModelDelta(element, index, flags, numChildren);
182         node.setParent(this);
183         addDelta(node);
184         return node;
185     }
186     
187     /**
188      * Sets the parent delta of this delta
189      *
190      * @param node parent delta
191      */

192     void setParent(ModelDelta node) {
193         fParent = node;
194     }
195     
196     /* (non-Javadoc)
197      * @see org.eclipse.debug.internal.ui.viewers.IModelDelta#getParent()
198      */

199     public IModelDelta getParentDelta() {
200         return fParent;
201     }
202
203     /* (non-Javadoc)
204      * @see org.eclipse.debug.internal.ui.viewers.IModelDelta#getReplacementElement()
205      */

206     public Object JavaDoc getReplacementElement() {
207         return fReplacement;
208     }
209
210     /* (non-Javadoc)
211      * @see org.eclipse.debug.internal.ui.viewers.IModelDelta#getIndex()
212      */

213     public int getIndex() {
214         return fIndex;
215     }
216     
217     /* (non-Javadoc)
218      * @see org.eclipse.debug.internal.ui.viewers.IModelDelta#getNodes()
219      */

220     public IModelDelta[] getChildDeltas() {
221         return fNodes;
222     }
223     
224     private void addDelta(ModelDelta delta) {
225         if (fNodes.length == 0) {
226             fNodes = new ModelDelta[]{delta};
227         } else {
228             ModelDelta[] nodes = new ModelDelta[fNodes.length + 1];
229             System.arraycopy(fNodes, 0, nodes, 0, fNodes.length);
230             nodes[fNodes.length] = delta;
231             fNodes = nodes;
232         }
233     }
234     
235     public String JavaDoc toString() {
236         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
237         buf.append("Model Delta Start\n"); //$NON-NLS-1$
238
appendDetail(buf, this);
239         buf.append("Model Delta End\n"); //$NON-NLS-1$
240
return buf.toString();
241     }
242     
243     private void appendDetail(StringBuffer JavaDoc buf, IModelDelta delta) {
244         buf.append("\tElement: "); //$NON-NLS-1$
245
buf.append(delta.getElement());
246         buf.append('\n');
247         buf.append("\t\tFlags: "); //$NON-NLS-1$
248
int flags = delta.getFlags();
249         if (flags == 0) {
250             buf.append("NO_CHANGE"); //$NON-NLS-1$
251
} else {
252             if ((flags & IModelDelta.ADDED) > 0) {
253                 buf.append("ADDED | "); //$NON-NLS-1$
254
}
255             if ((flags & IModelDelta.CONTENT) > 0) {
256                 buf.append("CONTENT | "); //$NON-NLS-1$
257
}
258             if ((flags & IModelDelta.EXPAND) > 0) {
259                 buf.append("EXPAND | "); //$NON-NLS-1$
260
}
261             if ((flags & IModelDelta.INSERTED) > 0) {
262                 buf.append("INSERTED | "); //$NON-NLS-1$
263
}
264             if ((flags & IModelDelta.REMOVED) > 0) {
265                 buf.append("REMOVED | "); //$NON-NLS-1$
266
}
267             if ((flags & IModelDelta.REPLACED) > 0) {
268                 buf.append("REPLACED | "); //$NON-NLS-1$
269
}
270             if ((flags & IModelDelta.SELECT) > 0) {
271                 buf.append("SELECT | "); //$NON-NLS-1$
272
}
273             if ((flags & IModelDelta.STATE) > 0) {
274                 buf.append("STATE | "); //$NON-NLS-1$
275
}
276             if ((flags & IModelDelta.INSTALL) > 0) {
277                 buf.append("INSTALL | "); //$NON-NLS-1$
278
}
279             if ((flags & IModelDelta.UNINSTALL) > 0) {
280                 buf.append("UNINSTALL | "); //$NON-NLS-1$
281
}
282         }
283         buf.append('\n');
284         buf.append("\t\tIndex: "); //$NON-NLS-1$
285
buf.append(delta.getIndex());
286         buf.append(" Child Count: "); //$NON-NLS-1$
287
buf.append(delta.getChildCount());
288         buf.append('\n');
289         IModelDelta[] nodes = delta.getChildDeltas();
290         for (int i = 0; i < nodes.length; i++) {
291             appendDetail(buf, nodes[i]);
292         }
293     }
294
295     /* (non-Javadoc)
296      * @see org.eclipse.debug.internal.ui.viewers.provisional.IModelDelta#getChildCount()
297      */

298     public int getChildCount() {
299         return fChildCount;
300     }
301
302     /* (non-Javadoc)
303      * @see org.eclipse.debug.internal.ui.viewers.provisional.IModelDelta#accept(org.eclipse.debug.internal.ui.viewers.provisional.IModelDeltaVisitor)
304      */

305     public void accept(IModelDeltaVisitor visitor) {
306         doAccept(visitor, 0);
307     }
308     
309     protected void doAccept(IModelDeltaVisitor visitor, int depth) {
310         if (visitor.visit(this, depth)) {
311             IModelDelta[] childDeltas = getChildDeltas();
312             for (int i = 0; i < childDeltas.length; i++) {
313                 ((ModelDelta)childDeltas[i]).doAccept(visitor, depth+1);
314             }
315         }
316     }
317     
318     /**
319      * Sets this delta's element
320      *
321      * @param element
322      */

323     public void setElement(Object JavaDoc element) {
324         fElement = element;
325     }
326     
327     /**
328      * Sets this delta's flags.
329      *
330      * @param flags
331      */

332     public void setFlags(int flags) {
333         fFlags = flags;
334     }
335 }
336
Popular Tags