KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 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;
12
13 import org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenUpdate;
14 import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementContentProvider;
15 import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
16 import org.eclipse.jface.viewers.TreePath;
17
18 /**
19  * This class is public so the test suite has access - it should be default protection.
20  *
21  * @since 3.3
22  */

23 public class ChildrenUpdate extends ViewerUpdateMonitor implements IChildrenUpdate {
24     
25     private Object JavaDoc[] fElements;
26     private int fIndex;
27     private int fLength;
28
29     /**
30      * Constructs a request to update an element
31      *
32      * @param node node to update
33      * @param model model containing the node
34      */

35     public ChildrenUpdate(ModelContentProvider provider, TreePath elementPath, Object JavaDoc element, int index, IElementContentProvider elementContentProvider, IPresentationContext context) {
36         super(provider, elementPath, element, elementContentProvider, context);
37         fIndex = index;
38         fLength = 1;
39     }
40
41     /*
42      * (non-Javadoc)
43      *
44      * @see org.eclipse.debug.ui.viewers.AsynchronousRequestMonitor#performUpdate()
45      */

46     protected void performUpdate() {
47         TreeModelContentProvider provider = (TreeModelContentProvider) getContentProvider();
48         TreePath elementPath = getElementPath();
49         if (fElements != null) {
50             InternalTreeModelViewer viewer = (InternalTreeModelViewer) provider.getViewer();
51             for (int i = 0; i < fElements.length; i++) {
52                 int modelIndex = fIndex + i;
53                 Object JavaDoc element = fElements[i];
54                 if (element != null) {
55                     int viewIndex = provider.modelToViewIndex(elementPath, modelIndex);
56                     if (provider.shouldFilter(elementPath, element)) {
57                         if (provider.addFilteredIndex(elementPath, modelIndex, element)) {
58                             if (ModelContentProvider.DEBUG_CONTENT_PROVIDER) {
59                                 System.out.println("REMOVE(" + getElement() + ", modelIndex: " + modelIndex + " viewIndex: " + viewIndex + ", " + element + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
60
}
61                             viewer.remove(elementPath, viewIndex);
62                         }
63                     } else {
64                         if (provider.isFiltered(elementPath, modelIndex)) {
65                             provider.clearFilteredChild(elementPath, modelIndex);
66                             int insertIndex = provider.modelToViewIndex(elementPath, modelIndex);
67                             if (ModelContentProvider.DEBUG_CONTENT_PROVIDER) {
68                                 System.out.println("insert(" + getElement() + ", modelIndex: " + modelIndex + " insertIndex: " + insertIndex + ", " + element + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
69
}
70                             viewer.insert(elementPath, element, insertIndex);
71                         } else {
72                             if (ModelContentProvider.DEBUG_CONTENT_PROVIDER) {
73                                 System.out.println("replace(" + getElement() + ", modelIndex: " + modelIndex + " viewIndex: " + viewIndex + ", " + element + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
74
}
75                             viewer.replace(elementPath, viewIndex, element);
76                         }
77                         TreePath childPath = elementPath.createChildPath(element);
78                         provider.updateHasChildren(childPath);
79                     }
80                 }
81             }
82         } else {
83             provider.updateHasChildren(elementPath);
84         }
85     }
86     
87     /* (non-Javadoc)
88      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenUpdate#setChild(java.lang.Object, int)
89      */

90     public void setChild(Object JavaDoc child, int index) {
91         if (fElements == null) {
92             fElements = new Object JavaDoc[fLength];
93         }
94         fElements[index - fIndex] = child;
95     }
96
97     /* (non-Javadoc)
98      *
99      * This method is public so the test suite has access - it should be default protection.
100      *
101      * @see org.eclipse.debug.internal.ui.viewers.model.ViewerUpdateMonitor#coalesce(org.eclipse.debug.internal.ui.viewers.model.ViewerUpdateMonitor)
102      */

103     public synchronized boolean coalesce(ViewerUpdateMonitor request) {
104         if (request instanceof ChildrenUpdate) {
105             ChildrenUpdate cu = (ChildrenUpdate) request;
106             if (getElement().equals(cu.getElement()) && getElementPath().equals(cu.getElementPath())) {
107                 int end = fIndex + fLength;
108                 int otherStart = cu.getOffset();
109                 int otherEnd = otherStart + cu.getLength();
110                 if ((otherStart >= fIndex && otherStart <= end) || (otherEnd >= fIndex && otherEnd <= end)) {
111                     // overlap
112
fIndex = Math.min(fIndex, otherStart);
113                     end = Math.max(end, otherEnd);
114                     fLength = end - fIndex;
115                     if (ModelContentProvider.DEBUG_CONTENT_PROVIDER) {
116                         System.out.println("coalesced: " + this.toString()); //$NON-NLS-1$
117
}
118                     return true;
119                 }
120             }
121         }
122         return false;
123     }
124
125     /* (non-Javadoc)
126      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenUpdate#getLength()
127      */

128     public int getLength() {
129         return fLength;
130     }
131
132     /* (non-Javadoc)
133      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenUpdate#getOffset()
134      */

135     public int getOffset() {
136         return fIndex;
137     }
138     
139     /* (non-Javadoc)
140      * @see org.eclipse.debug.internal.ui.viewers.model.ViewerUpdateMonitor#startRequest()
141      */

142     void startRequest() {
143         getElementContentProvider().update(new IChildrenUpdate[]{this});
144     }
145
146     public String JavaDoc toString() {
147         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
148         buf.append("IChildrenUpdate: "); //$NON-NLS-1$
149
buf.append(getElement());
150         buf.append(" {"); //$NON-NLS-1$
151
buf.append(getOffset());
152         buf.append(',');
153         buf.append(getLength());
154         buf.append("}"); //$NON-NLS-1$
155
return buf.toString();
156     }
157
158     /* (non-Javadoc)
159      * @see org.eclipse.debug.internal.ui.viewers.model.ViewerUpdateMonitor#getPriority()
160      */

161     int getPriority() {
162         return 3;
163     }
164     
165     /* (non-Javadoc)
166      * @see org.eclipse.debug.internal.ui.viewers.model.ViewerUpdateMonitor#getSchedulingPath()
167      */

168     TreePath getSchedulingPath() {
169         return getElementPath();
170     }
171     
172     /**
173      * Sets this request's offset. Used when modifying a waiting request when
174      * the offset changes due to a removed element.
175      *
176      * @param offset new offset
177      */

178     void setOffset(int offset) {
179         fIndex = offset;
180     }
181 }
182
183
Popular Tags