KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > wizard > ProgressMonitorPart


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.jface.wizard;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.IProgressMonitorWithBlocking;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.jface.dialogs.ProgressIndicator;
17 import org.eclipse.jface.resource.JFaceResources;
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Font;
21 import org.eclipse.swt.graphics.FontMetrics;
22 import org.eclipse.swt.graphics.GC;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Event;
28 import org.eclipse.swt.widgets.Label;
29 import org.eclipse.swt.widgets.Layout;
30 import org.eclipse.swt.widgets.Listener;
31
32 /**
33  * A standard implementation of an IProgressMonitor. It consists
34  * of a label displaying the task and subtask name, and a
35  * progress indicator to show progress. In contrast to
36  * <code>ProgressMonitorDialog</code> this class only implements
37  * <code>IProgressMonitor</code>.
38  */

39 public class ProgressMonitorPart extends Composite implements
40         IProgressMonitorWithBlocking {
41
42     /** the label */
43     protected Label fLabel;
44
45     /** the current task name */
46     protected String JavaDoc fTaskName;
47
48     /** the current sub task name */
49     protected String JavaDoc fSubTaskName;
50
51     /** the progress indicator */
52     protected ProgressIndicator fProgressIndicator;
53
54     /** the cancel component */
55     protected Control fCancelComponent;
56
57     /** true if cancled */
58     protected boolean fIsCanceled;
59
60     /** current blocked status */
61     protected IStatus blockedStatus;
62
63     /** the cancle lister attached to the cancle component */
64     protected Listener fCancelListener = new Listener() {
65         public void handleEvent(Event e) {
66             setCanceled(true);
67             if (fCancelComponent != null) {
68                 fCancelComponent.setEnabled(false);
69             }
70         }
71     };
72
73     /**
74      * Creates a ProgressMonitorPart.
75      * @param parent The SWT parent of the part.
76      * @param layout The SWT grid bag layout used by the part. A client
77      * can supply the layout to control how the progress monitor part
78      * is layed out. If null is passed the part uses its default layout.
79      */

80     public ProgressMonitorPart(Composite parent, Layout layout) {
81         this(parent, layout, SWT.DEFAULT);
82     }
83
84     /**
85      * Creates a ProgressMonitorPart.
86      * @param parent The SWT parent of the part.
87      * @param layout The SWT grid bag layout used by the part. A client
88      * can supply the layout to control how the progress monitor part
89      * is layed out. If null is passed the part uses its default layout.
90      * @param progressIndicatorHeight The height of the progress indicator in pixel.
91      */

92     public ProgressMonitorPart(Composite parent, Layout layout,
93             int progressIndicatorHeight) {
94         super(parent, SWT.NONE);
95         initialize(layout, progressIndicatorHeight);
96     }
97
98     /**
99      * Attaches the progress monitor part to the given cancel
100      * component.
101      * @param cancelComponent the control whose selection will
102      * trigger a cancel
103      */

104     public void attachToCancelComponent(Control cancelComponent) {
105         Assert.isNotNull(cancelComponent);
106         fCancelComponent = cancelComponent;
107         fCancelComponent.addListener(SWT.Selection, fCancelListener);
108     }
109
110     /**
111      * Implements <code>IProgressMonitor.beginTask</code>.
112      * @see IProgressMonitor#beginTask(java.lang.String, int)
113      */

114     public void beginTask(String JavaDoc name, int totalWork) {
115         fTaskName = name;
116         updateLabel();
117         if (totalWork == IProgressMonitor.UNKNOWN || totalWork == 0) {
118             fProgressIndicator.beginAnimatedTask();
119         } else {
120             fProgressIndicator.beginTask(totalWork);
121         }
122     }
123
124     /**
125      * Implements <code>IProgressMonitor.done</code>.
126      * @see IProgressMonitor#done()
127      */

128     public void done() {
129         fLabel.setText("");//$NON-NLS-1$
130
fProgressIndicator.sendRemainingWork();
131         fProgressIndicator.done();
132     }
133
134     /**
135      * Escapes any occurrence of '&' in the given String so that
136      * it is not considered as a mnemonic
137      * character in SWT ToolItems, MenuItems, Button and Labels.
138      * @param in the original String
139      * @return The converted String
140      */

141     protected static String JavaDoc escapeMetaCharacters(String JavaDoc in) {
142         if (in == null || in.indexOf('&') < 0) {
143             return in;
144         }
145         int length = in.length();
146         StringBuffer JavaDoc out = new StringBuffer JavaDoc(length + 1);
147         for (int i = 0; i < length; i++) {
148             char c = in.charAt(i);
149             if (c == '&') {
150                 out.append("&&");//$NON-NLS-1$
151
} else {
152                 out.append(c);
153             }
154         }
155         return out.toString();
156     }
157
158     /**
159      * Creates the progress monitor's UI parts and layouts them
160      * according to the given layout. If the layou is <code>null</code>
161      * the part's default layout is used.
162      * @param layout The layoutfor the receiver.
163      * @param progressIndicatorHeight The suggested height of the indicator
164      */

165     protected void initialize(Layout layout, int progressIndicatorHeight) {
166         if (layout == null) {
167             GridLayout l = new GridLayout();
168             l.marginWidth = 0;
169             l.marginHeight = 0;
170             l.numColumns = 1;
171             layout = l;
172         }
173         setLayout(layout);
174
175         fLabel = new Label(this, SWT.LEFT);
176         fLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
177
178         if (progressIndicatorHeight == SWT.DEFAULT) {
179             GC gc = new GC(fLabel);
180             FontMetrics fm = gc.getFontMetrics();
181             gc.dispose();
182             progressIndicatorHeight = fm.getHeight();
183         }
184
185         fProgressIndicator = new ProgressIndicator(this);
186         GridData gd = new GridData();
187         gd.horizontalAlignment = GridData.FILL;
188         gd.grabExcessHorizontalSpace = true;
189         gd.verticalAlignment = GridData.CENTER;
190         gd.heightHint = progressIndicatorHeight;
191         fProgressIndicator.setLayoutData(gd);
192     }
193
194     /**
195      * Implements <code>IProgressMonitor.internalWorked</code>.
196      * @see IProgressMonitor#internalWorked(double)
197      */

198     public void internalWorked(double work) {
199         fProgressIndicator.worked(work);
200     }
201
202     /**
203      * Implements <code>IProgressMonitor.isCanceled</code>.
204      * @see IProgressMonitor#isCanceled()
205      */

206     public boolean isCanceled() {
207         return fIsCanceled;
208     }
209
210     /**
211      * Detach the progress monitor part from the given cancel
212      * component
213      * @param cc
214      */

215     public void removeFromCancelComponent(Control cc) {
216         Assert.isTrue(fCancelComponent == cc && fCancelComponent != null);
217         fCancelComponent.removeListener(SWT.Selection, fCancelListener);
218         fCancelComponent = null;
219     }
220
221     /**
222      * Implements <code>IProgressMonitor.setCanceled</code>.
223      * @see IProgressMonitor#setCanceled(boolean)
224      */

225     public void setCanceled(boolean b) {
226         fIsCanceled = b;
227     }
228
229     /**
230      * Sets the progress monitor part's font.
231      */

232     public void setFont(Font font) {
233         super.setFont(font);
234         fLabel.setFont(font);
235         fProgressIndicator.setFont(font);
236     }
237
238     /*
239      * (non-Javadoc)
240      * @see org.eclipse.core.runtime.IProgressMonitor#setTaskName(java.lang.String)
241      */

242     public void setTaskName(String JavaDoc name) {
243         fTaskName = name;
244         updateLabel();
245     }
246
247     /*
248      * (non-Javadoc)
249      * @see org.eclipse.core.runtime.IProgressMonitor#subTask(java.lang.String)
250      */

251     public void subTask(String JavaDoc name) {
252         fSubTaskName = name;
253         updateLabel();
254     }
255
256     /**
257      * Updates the label with the current task and subtask names.
258      */

259     protected void updateLabel() {
260         if (blockedStatus == null) {
261             String JavaDoc text = taskLabel();
262             fLabel.setText(text);
263         } else {
264             fLabel.setText(blockedStatus.getMessage());
265         }
266
267         //Force an update as we are in the UI Thread
268
fLabel.update();
269     }
270
271     /**
272      * Return the label for showing tasks
273      * @return String
274      */

275     private String JavaDoc taskLabel() {
276         String JavaDoc text = fSubTaskName == null ? "" : fSubTaskName; //$NON-NLS-1$
277
if (fTaskName != null && fTaskName.length() > 0) {
278             text = JFaceResources.format(
279                     "Set_SubTask", new Object JavaDoc[] { fTaskName, text });//$NON-NLS-1$
280
}
281         return escapeMetaCharacters(text);
282     }
283
284     /**
285      * Implements <code>IProgressMonitor.worked</code>.
286      * @see IProgressMonitor#worked(int)
287      */

288     public void worked(int work) {
289         internalWorked(work);
290     }
291
292     /* (non-Javadoc)
293      * @see org.eclipse.core.runtime.IProgressMonitorWithBlocking#clearBlocked()
294      */

295     public void clearBlocked() {
296         blockedStatus = null;
297         updateLabel();
298
299     }
300
301     /* (non-Javadoc)
302      * @see org.eclipse.core.runtime.IProgressMonitorWithBlocking#setBlocked(org.eclipse.core.runtime.IStatus)
303      */

304     public void setBlocked(IStatus reason) {
305         blockedStatus = reason;
306         updateLabel();
307
308     }
309 }
310
Popular Tags