001: /*******************************************************************************
002: * Copyright (c) 2004, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.jobs.views;
011:
012: import org.eclipse.core.runtime.IProgressMonitor;
013: import org.eclipse.core.runtime.jobs.ISchedulingRule;
014: import org.eclipse.jface.resource.ImageDescriptor;
015: import org.eclipse.ui.progress.IDeferredWorkbenchAdapter;
016: import org.eclipse.ui.progress.IElementCollector;
017:
018: public class SlowElementAdapter implements IDeferredWorkbenchAdapter {
019:
020: private static boolean serializeFetching = false;
021: private static boolean batchFetchedChildren = false;
022:
023: final ISchedulingRule mutexRule = new ISchedulingRule() {
024: public boolean isConflicting(ISchedulingRule rule) {
025: return rule == mutexRule;
026: }
027:
028: public boolean contains(ISchedulingRule rule) {
029: return rule == mutexRule;
030: }
031: };
032:
033: /*
034: * (non-Javadoc)
035: *
036: * @see org.eclipse.ui.progress.IDeferredWorkbenchAdapter#fetchDeferredChildren(java.lang.Object,
037: * org.eclipse.jface.progress.IElementCollector,
038: * org.eclipse.core.runtime.IProgressMonitor)
039: */
040: public void fetchDeferredChildren(Object object,
041: IElementCollector collector, IProgressMonitor monitor) {
042: if (object instanceof SlowElement) {
043: Object[] children = ((SlowElement) object).getChildren();
044: if (isBatchFetchedChildren()) {
045: sleep(4000);
046: collector.add(children, monitor);
047: } else {
048: for (int i = 0; i < children.length; i++) {
049: if (monitor.isCanceled()) {
050: return;
051: }
052: collector.add(children[i], monitor);
053: sleep(4000);
054: }
055: }
056: }
057: }
058:
059: private void sleep(long mills) {
060: try {
061: Thread.sleep(1000);
062: } catch (InterruptedException e) {
063: //ignore
064: }
065: }
066:
067: /*
068: * (non-Javadoc)
069: *
070: * @see org.eclipse.ui.progress.IDeferredWorkbenchAdapter#isContainer()
071: */
072: public boolean isContainer() {
073: return true;
074: }
075:
076: /*
077: * (non-Javadoc)
078: *
079: * @see org.eclipse.ui.progress.IDeferredWorkbenchAdapter#getRule(java.lang.Object)
080: */
081: public ISchedulingRule getRule(final Object object) {
082: if (isSerializeFetching())
083: return mutexRule;
084: // Allow several SlowElement parents to fetch children concurrently
085: return null;
086: }
087:
088: /*
089: * (non-Javadoc)
090: *
091: * @see org.eclipse.ui.model.IWorkbenchAdapter#getChildren(java.lang.Object)
092: */
093: public Object[] getChildren(Object object) {
094: if (object instanceof SlowElement) {
095: return ((SlowElement) object).getChildren();
096: }
097: return new Object[0];
098: }
099:
100: /*
101: * (non-Javadoc)
102: *
103: * @see org.eclipse.ui.model.IWorkbenchAdapter#getImageDescriptor(java.lang.Object)
104: */
105: public ImageDescriptor getImageDescriptor(Object object) {
106: // TODO Auto-generated method stub
107: return null;
108: }
109:
110: /*
111: * (non-Javadoc)
112: *
113: * @see org.eclipse.ui.model.IWorkbenchAdapter#getLabel(java.lang.Object)
114: */
115: public String getLabel(Object o) {
116: if (o instanceof SlowElement) {
117: return ((SlowElement) o).getName();
118: }
119: return "unknown"; //$NON-NLS-1$
120: }
121:
122: /*
123: * (non-Javadoc)
124: *
125: * @see org.eclipse.ui.model.IWorkbenchAdapter#getParent(java.lang.Object)
126: */
127: public Object getParent(Object o) {
128: if (o instanceof SlowElement) {
129: return ((SlowElement) o).getParent();
130: }
131: return null;
132: }
133:
134: /**
135: * @return Returns the batchFetchedChildren.
136: */
137: public static boolean isBatchFetchedChildren() {
138: return batchFetchedChildren;
139: }
140:
141: /**
142: * @param batchFetchedChildren
143: * The batchFetchedChildren to set.
144: */
145: public static void setBatchFetchedChildren(
146: boolean batchFetchedChildren) {
147: SlowElementAdapter.batchFetchedChildren = batchFetchedChildren;
148: }
149:
150: /**
151: * @return Returns the serializeFetching.
152: */
153: public static boolean isSerializeFetching() {
154: return serializeFetching;
155: }
156:
157: /**
158: * @param serializeFetching
159: * The serializeFetching to set.
160: */
161: public static void setSerializeFetching(boolean serializeFetching) {
162: SlowElementAdapter.serializeFetching = serializeFetching;
163: }
164:
165: }
|