KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.core.commands.Request;
18 import org.eclipse.debug.internal.ui.viewers.AsynchronousSchedulingRuleFactory;
19 import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementContentProvider;
20 import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
21 import org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate;
22 import org.eclipse.jface.viewers.TreePath;
23 import org.eclipse.ui.progress.WorkbenchJob;
24
25 /**
26  * @since 3.3
27  */

28 abstract class ViewerUpdateMonitor extends Request implements IViewerUpdate {
29
30     private ModelContentProvider fContentProvider;
31     
32     /**
33      * Element's tree path
34      */

35     private TreePath fElementPath;
36     
37     /**
38      * Element
39      */

40     private Object JavaDoc fElement;
41     
42     /**
43      * Element content provider
44      */

45     private IElementContentProvider fElementContentProvider;
46     
47     /**
48      * Whether this request's 'done' method has been called.
49      */

50     private boolean fDone = false;
51     
52     /**
53      * Whether this request has been started
54      */

55     private boolean fStarted = false;
56     
57     /**
58      * Presentation context
59      */

60     private IPresentationContext fContext;
61     
62     protected WorkbenchJob fViewerUpdateJob = new WorkbenchJob("Asynchronous viewer update") { //$NON-NLS-1$
63
public IStatus runInUIThread(IProgressMonitor monitor) {
64             // necessary to check if viewer is disposed
65
try {
66                 if (!isCanceled() && !getContentProvider().isDisposed()) {
67                     IStatus status = getStatus();
68                     if (status == null || status.isOK()) {
69                         performUpdate();
70                     }
71                 }
72             } finally {
73                 getContentProvider().updateComplete(ViewerUpdateMonitor.this);
74             }
75             return Status.OK_STATUS;
76         }
77     };
78     
79     /**
80      * Constructs an update for the given content provider
81      *
82      * @param contentProvider content provider
83      * @param elementPath path to associated model element - empty for root element
84      * @param element associated model element
85      */

86     public ViewerUpdateMonitor(ModelContentProvider contentProvider, TreePath elementPath, Object JavaDoc element, IElementContentProvider elementContentProvider, IPresentationContext context) {
87         fContext = context;
88         fElementContentProvider = elementContentProvider;
89         fContentProvider = contentProvider;
90         fElement = element;
91         fElementPath = elementPath;
92         // serialize updates per viewer
93
fViewerUpdateJob.setRule(getUpdateSchedulingRule());
94         fViewerUpdateJob.setSystem(true);
95     }
96     
97     /**
98      * Returns the scheduling rule for viewer update job.
99      *
100      * @return rule or <code>null</code>
101      */

102     protected ISchedulingRule getUpdateSchedulingRule() {
103         return AsynchronousSchedulingRuleFactory.getDefault().newSerialPerObjectRule(getContentProvider());
104     }
105     
106     /**
107      * Returns the model content provider this update is being performed for.
108      *
109      * @return the model content provider this update is being performed for
110      */

111     protected ModelContentProvider getContentProvider() {
112         return fContentProvider;
113     }
114     
115     /**
116      * Returns the element content provider to use for this request
117      *
118      * @return element content provider
119      */

120     protected IElementContentProvider getElementContentProvider() {
121         return fElementContentProvider;
122     }
123     
124     /* (non-Javadoc)
125      * @see org.eclipse.core.runtime.IProgressMonitor#done()
126      */

127     public final void done() {
128         synchronized (this) {
129             if (isDone()) {
130                 return;
131             }
132             fDone = true;
133         }
134         scheduleViewerUpdate();
135     }
136     
137     /**
138      * Returns whether this request is done yet.
139      *
140      * @return
141      */

142     protected synchronized boolean isDone() {
143         return fDone;
144     }
145
146     protected void scheduleViewerUpdate() {
147         if(!isCanceled()) {
148             fViewerUpdateJob.schedule();
149         } else {
150             getContentProvider().updateComplete(this);
151         }
152     }
153     
154     /**
155      * Notification this update has been completed and should now be applied to
156      * this update's viewer. This method is called in the UI thread.
157      */

158     protected abstract void performUpdate();
159     
160     /* (non-Javadoc)
161      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate#getPresentationContext()
162      */

163     public IPresentationContext getPresentationContext() {
164         return fContext;
165     }
166
167     /* (non-Javadoc)
168      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate#getElement()
169      */

170     public Object JavaDoc getElement() {
171         return fElement;
172     }
173     
174     /* (non-Javadoc)
175      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdate#getElementPath()
176      */

177     public TreePath getElementPath() {
178         return fElementPath;
179     }
180     
181     /**
182      * Returns whether this request can coalesce the given request, and performs the
183      * coalesce if it can.
184      *
185      * @param update request to coalesce with this request
186      * @return whether it worked
187      */

188     abstract boolean coalesce(ViewerUpdateMonitor update);
189     
190     /**
191      * Starts this request. Subclasses must override startRequest().
192      */

193     final void start() {
194         synchronized (this) {
195             if (fStarted) {
196                 return;
197             }
198             fStarted = true;
199         }
200         getContentProvider().updateStarted(this);
201         if (!isCanceled()) {
202             startRequest();
203         } else {
204             done();
205         }
206     }
207     
208     /**
209      * Subclasses must override to initiate specific request types.
210      */

211     abstract void startRequest();
212     
213     /**
214      * Returns the priority of this request. Subclasses must override. The
215      * highest priority is 1. Priorities indicate the order that waiting
216      * requests should be started in (for example, 'hasChildren' before 'update child count').
217      *
218      * @return priority
219      */

220     abstract int getPriority();
221     
222     /**
223      * Returns a path used to schedule this request - i.e. based on this path, this
224      * request will be scheduled to run when no requests are running against the
225      * same element or a parent of the element denoted by the path.
226      *
227      * @return path used to schedule request
228      */

229     abstract TreePath getSchedulingPath();
230 }
231
Popular Tags