KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > viewers > AsynchronousRequestMonitor


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;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.core.runtime.jobs.ISchedulingRule;
17 import org.eclipse.debug.internal.ui.commands.actions.AbstractRequestMonitor;
18 import org.eclipse.ui.progress.WorkbenchJob;
19
20 /**
21  * Base implementation of an asynchronous request monitor.
22  * <p>
23  * Not intended to be subclassed or instantiated by clients. For internal use
24  * with the <code>AsynchronousViewer</code> implementation.
25  * </p>
26  * @since 3.2
27  */

28 public abstract class AsynchronousRequestMonitor extends AbstractRequestMonitor {
29     
30     /**
31      * Model node the update is rooted at
32      */

33     private ModelNode fNode;
34     
35     /**
36      * Model the update is being performed for
37      */

38     private AsynchronousModel fModel;
39     
40     /**
41      * Whether this request's 'done' method has been called.
42      */

43     private boolean fDone = false;
44     
45     protected WorkbenchJob fViewerUpdateJob = new WorkbenchJob("Asynchronous viewer update") { //$NON-NLS-1$
46
public IStatus runInUIThread(IProgressMonitor monitor) {
47             // necessary to check if widget is disposed. The item may
48
// have been removed from the tree when another children update
49
// occurred.
50
getModel().viewerUpdateScheduled(AsynchronousRequestMonitor.this);
51             if (fDone) {
52                 getModel().requestComplete(AsynchronousRequestMonitor.this);
53             }
54             if (!isCanceled() && !getNode().isDisposed()) {
55                 IStatus status = getStatus();
56                 if (status != null && !status.isOK()) {
57                     getModel().getViewer().handlePresentationFailure(AsynchronousRequestMonitor.this, status);
58                 } else {
59                     performUpdate();
60                 }
61             }
62             getModel().viewerUpdateComplete(AsynchronousRequestMonitor.this);
63             return Status.OK_STATUS;
64         }
65     };
66     
67     /**
68      * Constructs an update rooted at the given item.
69      *
70      * @param node model node
71      * @param model model the node is in
72      */

73     public AsynchronousRequestMonitor(ModelNode node, AsynchronousModel model) {
74         fNode = node;
75         fModel = model;
76         // serialize updates per viewer
77
fViewerUpdateJob.setRule(getUpdateSchedulingRule());
78         fViewerUpdateJob.setSystem(true);
79     }
80     
81     /**
82      * Returns the scheduling rule for viewer update job.
83      *
84      * @return rule or <code>null</code>
85      */

86     protected ISchedulingRule getUpdateSchedulingRule() {
87         return AsynchronousSchedulingRuleFactory.getDefault().newSerialPerObjectRule(getModel().getViewer());
88     }
89     
90     /**
91      * Returns the model this update is being performed for
92      *
93      * @return the model this update is being performed for
94      */

95     protected AsynchronousModel getModel() {
96         return fModel;
97     }
98     
99     /**
100      * Returns the model node this update is rooted at
101      *
102      * @return the model node this update is rooted at
103      */

104     protected ModelNode getNode() {
105         return fNode;
106     }
107     
108     /**
109      * Returns whether this update contains the given node.
110      * That is, whether this update is for the same node or a child of
111      * the given node.
112      *
113      * @param ndoe node to test containment on
114      * @return whether this update contains the given node
115      */

116     protected boolean contains(ModelNode node) {
117         if (node == getNode()) {
118             return true;
119         }
120         ModelNode parentNode = getNode().getParentNode();
121         while (parentNode != null) {
122             if (parentNode.equals(getNode())) {
123                 return true;
124             }
125             parentNode = parentNode.getParentNode();
126         }
127         return false;
128     }
129     
130
131     /* (non-Javadoc)
132      * @see org.eclipse.core.runtime.IProgressMonitor#setCanceled(boolean)
133      */

134     public void setCanceled(boolean value) {
135         super.setCanceled(value);
136         if (value) {
137             getModel().requestCanceled(this);
138         }
139     }
140     
141     /* (non-Javadoc)
142      * @see org.eclipse.core.runtime.IProgressMonitor#done()
143      */

144     public final void done() {
145         synchronized (this) {
146             fDone = true;
147         }
148         scheduleViewerUpdate(0L);
149     }
150     
151     /**
152      * Returns whether this request is done yet.
153      *
154      * @return
155      */

156     protected synchronized boolean isDone() {
157         return fDone;
158     }
159
160     protected void scheduleViewerUpdate(long ms) {
161         if(!isCanceled())
162             fViewerUpdateJob.schedule(ms);
163     }
164     
165     /**
166      * Notification this update has been completed and should now be applied to
167      * this update's viewer. This method is called in the UI thread.
168      */

169     protected abstract void performUpdate();
170     
171     /**
172      * Returns whether this update effectively contains the given update.
173      * That is, whether this update will also perform the given update.
174      *
175      * @param update update to compare to
176      * @return whether this update will also perform the given update
177      */

178     protected abstract boolean contains(AsynchronousRequestMonitor update);
179     
180 }
181
Popular Tags