1 11 package org.eclipse.debug.internal.ui.model.elements; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IProgressMonitor; 15 import org.eclipse.core.runtime.IStatus; 16 import org.eclipse.core.runtime.Status; 17 import org.eclipse.core.runtime.jobs.ISchedulingRule; 18 import org.eclipse.core.runtime.jobs.Job; 19 import org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenCountUpdate; 20 import org.eclipse.debug.internal.ui.viewers.model.provisional.IChildrenUpdate; 21 import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementContentProvider; 22 import org.eclipse.debug.internal.ui.viewers.model.provisional.IHasChildrenUpdate; 23 import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext; 24 import org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate; 25 26 29 public abstract class ElementContentProvider implements IElementContentProvider { 30 31 protected static final Object [] EMPTY = new Object [0]; 32 33 36 public void update(final IChildrenUpdate[] updates) { 37 Job job = new Job("children update") { protected IStatus run(IProgressMonitor monitor) { 39 for (int i = 0; i < updates.length; i++) { 40 IChildrenUpdate update = updates[i]; 41 if (!update.isCanceled()) { 42 retrieveChildren(update); 43 } 44 update.done(); 45 } 46 return Status.OK_STATUS; 47 } 48 }; 49 job.setSystem(true); 50 job.setRule(getRule(updates)); 51 job.schedule(); 52 } 53 54 57 public void update(final IChildrenCountUpdate[] updates) { 58 Job job = new Job("child count update") { protected IStatus run(IProgressMonitor monitor) { 60 for (int i = 0; i < updates.length; i++) { 61 IChildrenCountUpdate update = updates[i]; 62 if (!update.isCanceled()) { 63 retrieveChildCount(update); 64 } 65 update.done(); 66 } 67 return Status.OK_STATUS; 68 } 69 }; 70 job.setSystem(true); 71 job.setRule(getRule(updates)); 72 job.schedule(); 73 } 74 75 80 protected void retrieveChildren(IChildrenUpdate update) { 81 if (!update.isCanceled()) { 82 IStatus status = Status.OK_STATUS; 83 try { 84 IPresentationContext context = update.getPresentationContext(); 85 if (supportsContext(context)) { 86 int offset = update.getOffset(); 87 Object [] children = getChildren(update.getElement(), offset, update.getLength(), context, update); 88 if (!update.isCanceled() && children != null) { 89 for (int i = 0; i < children.length; i++) { 90 update.setChild(children[i], offset + i); 91 } 92 } 93 } 94 } catch (CoreException e) { 95 status = e.getStatus(); 96 } 97 update.setStatus(status); 98 } 99 } 100 101 108 protected void retrieveChildCount(IChildrenCountUpdate update) { 109 if (!update.isCanceled()) { 110 IStatus status = Status.OK_STATUS; 111 try { 112 IPresentationContext context = update.getPresentationContext(); 113 if (supportsContext(context)) { 114 int childCount = getChildCount( update.getElement(), context, update); 115 if (!update.isCanceled()) { 116 update.setChildCount(childCount); 117 } 118 } else { 119 update.setChildCount(0); 120 } 121 } catch (CoreException e) { 122 status = e.getStatus(); 123 } 124 update.setStatus(status); 125 } 126 } 127 128 139 protected abstract Object [] getChildren(Object parent, int index, int length, IPresentationContext context, IViewerUpdate monitor) throws CoreException; 140 141 149 protected abstract int getChildCount(Object element, IPresentationContext context, IViewerUpdate monitor) throws CoreException; 150 151 157 protected boolean supportsContext(IPresentationContext context) { 158 return supportsContextId(context.getId()); 159 } 160 161 167 protected abstract boolean supportsContextId(String id); 168 169 178 protected Object [] getElements(Object [] elements, int index, int length) { 179 int max = elements.length; 180 if (index < max && ((index + length) > max)) { 181 length = max - index; 182 } 183 if ((index + length) <= elements.length) { 184 Object [] sub = new Object [length]; 185 System.arraycopy(elements, index, sub, 0, length); 186 return sub; 187 } 188 return null; 189 } 190 191 194 public void update(final IHasChildrenUpdate[] updates) { 195 Job job = new Job("has children update") { protected IStatus run(IProgressMonitor monitor) { 197 for (int i = 0; i < updates.length; i++) { 198 IHasChildrenUpdate update = updates[i]; 199 if (!update.isCanceled()) { 200 updateHasChildren(update); 201 } 202 update.done(); 203 } 204 return Status.OK_STATUS; 205 } 206 }; 207 job.setSystem(true); 208 job.setRule(getRule(updates)); 209 job.schedule(); 210 } 211 212 217 protected void updateHasChildren(IHasChildrenUpdate update) { 218 if (!update.isCanceled()) { 219 IStatus status = Status.OK_STATUS; 220 try { 221 IPresentationContext context = update.getPresentationContext(); 222 if (supportsContext(context)) { 223 boolean hasChildren = hasChildren(update.getElement(), context, update); 224 if (!update.isCanceled()) { 225 update.setHasChilren(hasChildren); 226 } 227 } else { 228 update.setHasChilren(false); 229 } 230 } catch (CoreException e) { 231 status = e.getStatus(); 232 } 233 update.setStatus(status); 234 } 235 236 } 237 238 247 protected boolean hasChildren(Object element, IPresentationContext context, IViewerUpdate monitor) throws CoreException { 248 return getChildCount(element, context, monitor) > 0; 249 } 250 251 258 protected ISchedulingRule getRule(IChildrenCountUpdate[] updates) { 259 return null; 260 } 261 262 269 protected ISchedulingRule getRule(IChildrenUpdate[] updates) { 270 return null; 271 } 272 273 280 protected ISchedulingRule getRule(IHasChildrenUpdate[] updates) { 281 return null; 282 } 283 284 } 285 | Popular Tags |