KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.provisional;
12
13
14
15 /**
16  * A model delta. Used to create model deltas.
17  * <p>
18  * Clients may instantiate this class; not intended to be subclassed.
19  * </p>
20  * @see org.eclipse.debug.internal.ui.viewers.IModelDelta
21  * @since 3.2
22  */

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

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

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

66     public ModelDelta(Object JavaDoc element, int index, int flags) {
67         fElement = element;
68         fIndex = index;
69         fFlags = flags;
70     }
71
72     /* (non-Javadoc)
73      * @see org.eclipse.debug.internal.ui.viewers.IModelDelta#getElement()
74      */

75     public Object JavaDoc getElement() {
76         return fElement;
77     }
78
79     /* (non-Javadoc)
80      * @see org.eclipse.debug.internal.ui.viewers.IModelDelta#getFlags()
81      */

82     public int getFlags() {
83         return fFlags;
84     }
85
86     /**
87      * Adds a child node to this delta with the given element and change flags,
88      * and returns the child delta.
89      *
90      * @param element child element to add
91      * @param flags change flags for child
92      * @return newly created child delta
93      */

94     public ModelDelta addNode(Object JavaDoc element, int flags) {
95         ModelDelta node = new ModelDelta(element, flags);
96         node.setParent(this);
97         addDelta(node);
98         return node;
99     }
100
101     /**
102      * Adds a child node to this delta to replace the given element with the
103      * specified replacement element and change flags, and returns the
104      * newly created child delta.
105      *
106      * @param element child element to add to this delta
107      * @param replacement replacement element for the child element
108      * @param flags change flags
109      * @return newly created child delta
110      */

111     public ModelDelta addNode(Object JavaDoc element, Object JavaDoc replacement, int flags) {
112         ModelDelta node = new ModelDelta(element, replacement, flags);
113         node.setParent(this);
114         addDelta(node);
115         return node;
116     }
117
118     /**
119      * Adds a child delta to this delta to insert the specified element at
120      * the given index, and returns the newly created child delta.
121      *
122      * @param element child element in insert
123      * @param index index of insertion
124      * @param flags change flags
125      * @return newly created child delta
126      */

127     public ModelDelta addNode(Object JavaDoc element, int index, int flags) {
128         ModelDelta node = new ModelDelta(element, index, flags);
129         node.setParent(this);
130         addDelta(node);
131         return node;
132     }
133     
134     /**
135      * Sets the parent delta of this delta
136      *
137      * @param node parent delta
138      */

139     void setParent(ModelDelta node) {
140         fParent = node;
141     }
142     
143     /* (non-Javadoc)
144      * @see org.eclipse.debug.internal.ui.viewers.IModelDelta#getParent()
145      */

146     public IModelDelta getParent() {
147         return fParent;
148     }
149
150     /* (non-Javadoc)
151      * @see org.eclipse.debug.internal.ui.viewers.IModelDelta#getReplacementElement()
152      */

153     public Object JavaDoc getReplacementElement() {
154         return fReplacement;
155     }
156
157     /* (non-Javadoc)
158      * @see org.eclipse.debug.internal.ui.viewers.IModelDelta#getIndex()
159      */

160     public int getIndex() {
161         return fIndex;
162     }
163     
164     /* (non-Javadoc)
165      * @see org.eclipse.debug.internal.ui.viewers.IModelDelta#getNodes()
166      */

167     public ModelDelta[] getNodes() {
168         return fNodes;
169     }
170     
171     private void addDelta(ModelDelta delta) {
172         if (fNodes.length == 0) {
173             fNodes = new ModelDelta[]{delta};
174         } else {
175             ModelDelta[] nodes = new ModelDelta[fNodes.length + 1];
176             System.arraycopy(fNodes, 0, nodes, 0, fNodes.length);
177             nodes[fNodes.length] = delta;
178             fNodes = nodes;
179         }
180     }
181     
182     public String JavaDoc toString() {
183         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
184         buf.append("Model Delta Start\n"); //$NON-NLS-1$
185
appendDetail(buf, this);
186         buf.append("Model Delta End\n"); //$NON-NLS-1$
187
return buf.toString();
188     }
189     
190     private void appendDetail(StringBuffer JavaDoc buf, ModelDelta delta) {
191         buf.append("\tElement: "); //$NON-NLS-1$
192
buf.append(delta.fElement);
193         buf.append('\n');
194         buf.append("\t\tFlags: "); //$NON-NLS-1$
195
int flags = delta.getFlags();
196         if (flags == 0) {
197             buf.append("NO_CHANGE"); //$NON-NLS-1$
198
} else {
199             if ((flags & IModelDelta.ADDED) > 0) {
200                 buf.append("ADDED | "); //$NON-NLS-1$
201
}
202             if ((flags & IModelDelta.CONTENT) > 0) {
203                 buf.append("CONTENT | "); //$NON-NLS-1$
204
}
205             if ((flags & IModelDelta.EXPAND) > 0) {
206                 buf.append("EXPAND | "); //$NON-NLS-1$
207
}
208             if ((flags & IModelDelta.INSERTED) > 0) {
209                 buf.append("INSERTED | "); //$NON-NLS-1$
210
}
211             if ((flags & IModelDelta.REMOVED) > 0) {
212                 buf.append("REMOVED | "); //$NON-NLS-1$
213
}
214             if ((flags & IModelDelta.REPLACED) > 0) {
215                 buf.append("REPLACED | "); //$NON-NLS-1$
216
}
217             if ((flags & IModelDelta.SELECT) > 0) {
218                 buf.append("SELECT | "); //$NON-NLS-1$
219
}
220             if ((flags & IModelDelta.STATE) > 0) {
221                 buf.append("STATE | "); //$NON-NLS-1$
222
}
223         }
224         buf.append('\n');
225         ModelDelta[] nodes = delta.getNodes();
226         for (int i = 0; i < nodes.length; i++) {
227             appendDetail(buf, nodes[i]);
228         }
229     }
230 }
231
Popular Tags