KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > splash > BasicSplashHandler


1 /*******************************************************************************
2  * Copyright (c) 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.ui.splash;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.jface.dialogs.ProgressIndicator;
15 import org.eclipse.jface.window.Window;
16 import org.eclipse.jface.wizard.ProgressMonitorPart;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.graphics.Color;
19 import org.eclipse.swt.graphics.Font;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.graphics.RGB;
22 import org.eclipse.swt.graphics.Rectangle;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.ui.internal.StartupThreading.StartupRunnable;
28
29 /**
30  * Basic splash implementation that provides an absolute positioned progress bar
31  * and message string that is hooked up to a progress monitor.
32  *
33  * @since 3.3
34  */

35 public abstract class BasicSplashHandler extends AbstractSplashHandler {
36
37     /**
38      * Hacks the progress monitor to have absolute positioning for its controls.
39      * In addition, all methods that access the controls will be wrapped in an
40      * asynchExec().
41      */

42     class AbsolutePositionProgressMonitorPart extends ProgressMonitorPart {
43         public AbsolutePositionProgressMonitorPart(Composite parent) {
44             super(parent, null);
45             setLayout(null);
46         }
47
48         public ProgressIndicator getProgressIndicator() {
49             return fProgressIndicator;
50         }
51
52         public Label getProgressText() {
53             return fLabel;
54         }
55
56         /* (non-Javadoc)
57          * @see org.eclipse.jface.wizard.ProgressMonitorPart#beginTask(java.lang.String, int)
58          */

59         public void beginTask(final String JavaDoc name, final int totalWork) {
60
61             updateUI(new Runnable JavaDoc() {
62
63                 public void run() {
64                     if (isDisposed())
65                         return;
66                     AbsolutePositionProgressMonitorPart.super.beginTask(name,
67                             totalWork);
68                 }
69             });
70
71         }
72
73         /*
74          * (non-Javadoc)
75          *
76          * @see org.eclipse.jface.wizard.ProgressMonitorPart#done()
77          */

78         public void done() {
79
80             updateUI(new Runnable JavaDoc() {
81
82                 public void run() {
83                     if (isDisposed())
84                         return;
85                     AbsolutePositionProgressMonitorPart.super.done();
86                 }
87             });
88
89         }
90
91         /*
92          * (non-Javadoc)
93          *
94          * @see org.eclipse.jface.wizard.ProgressMonitorPart#internalWorked(double)
95          */

96         public void internalWorked(final double work) {
97
98             updateUI(new Runnable JavaDoc() {
99
100                 public void run() {
101                     if (isDisposed())
102                         return;
103                     AbsolutePositionProgressMonitorPart.super
104                             .internalWorked(work);
105                 }
106             });
107
108         }
109         
110         /*
111          * (non-Javadoc)
112          *
113          * @see org.eclipse.jface.wizard.ProgressMonitorPart#setFont(org.eclipse.swt.graphics.Font)
114          */

115         public void setFont(final Font font) {
116
117             updateUI(new Runnable JavaDoc() {
118
119                 public void run() {
120                     if (isDisposed())
121                         return;
122                     AbsolutePositionProgressMonitorPart.super.setFont(font);
123                 }
124             });
125
126         }
127
128         /*
129          * (non-Javadoc)
130          *
131          * @see org.eclipse.jface.wizard.ProgressMonitorPart#updateLabel()
132          */

133         protected void updateLabel() {
134
135             updateUI(new Runnable JavaDoc() {
136
137                 public void run() {
138                     if (isDisposed())
139                         return;
140                     AbsolutePositionProgressMonitorPart.super.updateLabel();
141                 }
142             });
143
144         }
145     }
146
147     private Color foreground = null;
148     private AbsolutePositionProgressMonitorPart monitor;
149     private Rectangle messageRect;
150     private Rectangle progressRect;
151
152     /*
153      * (non-Javadoc)
154      *
155      * @see org.eclipse.ui.splash.AbstractSplashHandler#getBundleProgressMonitor()
156      */

157     public IProgressMonitor getBundleProgressMonitor() {
158         if (monitor == null) {
159             Composite parent = new Composite(getSplash(), Window.getDefaultOrientation());
160             Point size = getSplash().getSize();
161             parent.setBounds(new Rectangle(0,0,size.x,size.y));
162             monitor = new AbsolutePositionProgressMonitorPart(parent);
163             monitor.setSize(size);
164             if (progressRect != null)
165                 monitor.getProgressIndicator().setBounds(progressRect);
166             else
167                 monitor.getProgressIndicator().setVisible(false);
168
169             if (messageRect != null)
170                 monitor.getProgressText().setBounds(messageRect);
171             else
172                 monitor.getProgressText().setVisible(false);
173
174             if (foreground != null)
175                 monitor.getProgressText().setForeground(foreground);
176             monitor.setBackgroundMode(SWT.INHERIT_FORCE);
177             monitor.setBackgroundImage(getSplash().getShell()
178                     .getBackgroundImage());
179         }
180         return monitor;
181     }
182
183     /*
184      * (non-Javadoc)
185      *
186      * @see org.eclipse.ui.splash.AbstractSplashHandler#dispose()
187      */

188     public void dispose() {
189         super.dispose();
190         if (foreground != null)
191             foreground.dispose();
192     }
193
194     /**
195      * Set the foreground text color. This method has no effect after
196      * {@link #getBundleProgressMonitor()} has been invoked.
197      *
198      * @param foregroundRGB
199      * the color
200      */

201     protected void setForeground(RGB foregroundRGB) {
202         if (monitor != null)
203             return;
204         if (this.foreground != null)
205             this.foreground.dispose();
206         this.foreground = new Color(getSplash().getShell().getDisplay(),
207                 foregroundRGB);
208     }
209     
210     /**
211      * Get the foreground text color. This color should not be disposed by
212      * callers.
213      *
214      * @return the foreground color
215      */

216     protected Color getForeground() {
217         return foreground;
218     }
219
220     /**
221      * Set the location of the message text in the splash. This method has no
222      * effect after {@link #getBundleProgressMonitor()} has been invoked.
223      *
224      * @param messageRect
225      * the location of the message text
226      */

227     protected void setMessageRect(Rectangle messageRect) {
228         this.messageRect = messageRect;
229     }
230
231     /**
232      * Set the location of the progress bar in the splash. This method has no
233      * effect after {@link #getBundleProgressMonitor()} has been invoked.
234      *
235      * @param progressRect
236      * the location of the progress bar
237      */

238     protected void setProgressRect(Rectangle progressRect) {
239         this.progressRect = progressRect;
240     }
241     
242     /**
243      * Get the composite on which any supplemental controls should be drawn.
244      * This will not have a layout set and clients are responsible for setting
245      * the location of child controls manually.
246      *
247      * <p>
248      * This method must be called in the
249      * {@link #init(org.eclipse.swt.widgets.Shell)} method of a subclasses to
250      * ensure proper creation of controls
251      * </p>
252      *
253      * <p>
254      * Please note that the default implementation of this method assumes that
255      * the {@link IProgressMonitor} returned from
256      * {@link #getBundleProgressMonitor()} can be safely casted to a
257      * {@link Composite}. If this is not the case this method must be
258      * reimplemented to reflect the new progress controls.
259      * </p>
260      *
261      * @see #init(org.eclipse.swt.widgets.Shell)
262      * @return the composite
263      */

264     protected Composite getContent() {
265         return (Composite) getBundleProgressMonitor();
266     }
267     
268     /**
269      * Perform some update on the splash. If called from a non-UI thread it will
270      * be wrapped by a runnable that may be run before the workbench has been
271      * fully realized.
272      *
273      * @param r
274      * the update runnable
275      * @throws Throwable
276      */

277     private void updateUI(final Runnable JavaDoc r) {
278         Shell splashShell = getSplash();
279         if (splashShell == null || splashShell.isDisposed())
280             return;
281         
282         Display display = splashShell.getDisplay();
283         
284         if (Thread.currentThread() == display.getThread())
285             r.run(); // run immediatley if we're on the UI thread
286
else {
287             // wrapper with a StartupRunnable to ensure that it will run before
288
// the UI is fully initialized
289
StartupRunnable startupRunnable = new StartupRunnable() {
290
291                 public void runWithException() throws Throwable JavaDoc {
292                     r.run();
293                 }
294             };
295             display.asyncExec(startupRunnable);
296         }
297     }
298 }
299
Popular Tags