35
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.wizard.ProgressMonitorPart;
|
|
14
|
|
15
|
|
16 import dwt.DWT;
|
|
17 import dwt.graphics.Font;
|
|
18 import dwt.graphics.FontMetrics;
|
|
19 import dwt.graphics.GC;
|
|
20 import dwt.layout.GridData;
|
|
21 import dwt.layout.GridLayout;
|
|
22 import dwt.widgets.Composite;
|
|
23 import dwt.widgets.Control;
|
|
24 import dwt.widgets.Event;
|
|
25 import dwt.widgets.Label;
|
|
26 import dwt.widgets.Layout;
|
|
27 import dwt.widgets.Listener;
|
|
28 import dwtx.core.runtime.Assert;
|
|
29 import dwtx.core.runtime.IProgressMonitor;
|
|
30 import dwtx.core.runtime.IProgressMonitorWithBlocking;
|
|
31 import dwtx.core.runtime.IStatus;
|
|
32 import dwtx.jface.dialogs.ProgressIndicator;
|
|
33 import dwtx.jface.resource.JFaceResources;
|
|
34
|
|
35 import dwt.dwthelper.utils;
|
|
36
|
|
37 /**
|
|
38 * A standard implementation of an IProgressMonitor. It consists
|
|
39 * of a label displaying the task and subtask name, and a
|
|
40 * progress indicator to show progress. In contrast to
|
|
41 * <code>ProgressMonitorDialog</code> this class only implements
|
|
42 * <code>IProgressMonitor</code>.
|
|
43 */
|
|
44 public class ProgressMonitorPart : Composite,
|
|
45 IProgressMonitorWithBlocking {
|
|
46
|
|
47 /** the label */
|
|
48 protected Label fLabel;
|
|
49
|
|
50 /** the current task name */
|
|
51 protected String fTaskName;
|
|
52
|
|
53 /** the current sub task name */
|
|
54 protected String fSubTaskName;
|
|
55
|
|
56 /** the progress indicator */
|
|
57 protected ProgressIndicator fProgressIndicator;
|
|
58
|
|
59 /** the cancel component */
|
|
60 protected Control fCancelComponent;
|
|
61
|
|
62 /** true if cancled */
|
|
63 protected bool fIsCanceled;
|
|
64
|
|
65 /** current blocked status */
|
|
66 protected IStatus blockedStatus;
|
|
67
|
|
68 /** the cancle lister attached to the cancle component */
|
|
69 protected Listener fCancelListener;
|
|
70 private void init_fCancelListener(){
|
|
71 fCancelListener = new class Listener {
|
|
72 public void handleEvent(Event e) {
|
|
73 setCanceled(true);
|
|
74 if (fCancelComponent !is null) {
|
|
75 fCancelComponent.setEnabled(false);
|
|
76 }
|
|
77 }
|
|
78 };
|
|
79 }
|
|
80 /**
|
|
81 * Creates a ProgressMonitorPart.
|
|
82 * @param parent The DWT parent of the part.
|
|
83 * @param layout The DWT grid bag layout used by the part. A client
|
|
84 * can supply the layout to control how the progress monitor part
|
|
85 * is layed out. If null is passed the part uses its default layout.
|
|
86 */
|
|
87 public this(Composite parent, Layout layout) {
|
|
88 this(parent, layout, DWT.DEFAULT);
|
|
89 }
|
|
90
|
|
91 /**
|
|
92 * Creates a ProgressMonitorPart.
|
|
93 * @param parent The DWT parent of the part.
|
|
94 * @param layout The DWT grid bag layout used by the part. A client
|
|
95 * can supply the layout to control how the progress monitor part
|
|
96 * is layed out. If null is passed the part uses its default layout.
|
|
97 * @param progressIndicatorHeight The height of the progress indicator in pixel.
|
|
98 */
|
|
99 public this(Composite parent, Layout layout,
|
|
100 int progressIndicatorHeight) {
|
|
101 init_fCancelListener();
|
|
102 super(parent, DWT.NONE);
|
|
103 initialize(layout, progressIndicatorHeight);
|
|
104 }
|
|
105
|
|
106 /**
|
|
107 * Attaches the progress monitor part to the given cancel
|
|
108 * component.
|
|
109 * @param cancelComponent the control whose selection will
|
|
110 * trigger a cancel
|
|
111 */
|
|
112 public void attachToCancelComponent(Control cancelComponent) {
|
|
113 Assert.isNotNull(cancelComponent);
|
|
114 fCancelComponent = cancelComponent;
|
|
115 fCancelComponent.addListener(DWT.Selection, fCancelListener);
|
|
116 }
|
|
117
|
|
118 /**
|
|
119 * Implements <code>IProgressMonitor.beginTask</code>.
|
|
120 * @see IProgressMonitor#beginTask(java.lang.String, int)
|
|
121 */
|
|
122 public void beginTask(String name, int totalWork) {
|
|
123 fTaskName = name;
|
|
124 updateLabel();
|
|
125 if (totalWork is IProgressMonitor.UNKNOWN || totalWork is 0) {
|
|
126 fProgressIndicator.beginAnimatedTask();
|
|
127 } else {
|
|
128 fProgressIndicator.beginTask(totalWork);
|
|
129 }
|
|
130 }
|
|
131
|
|
132 /**
|
|
133 * Implements <code>IProgressMonitor.done</code>.
|
|
134 * @see IProgressMonitor#done()
|
|
135 */
|
|
136 public void done() {
|
|
137 fLabel.setText("");//$NON-NLS-1$
|
|
138 fProgressIndicator.sendRemainingWork();
|
|
139 fProgressIndicator.done();
|
|
140 }
|
|
141
|
|
142 /**
|
|
143 * Escapes any occurrence of '&' in the given String so that
|
|
144 * it is not considered as a mnemonic
|
|
145 * character in DWT ToolItems, MenuItems, Button and Labels.
|
|
146 * @param in the original String
|
|
147 * @return The converted String
|
|
148 */
|
|
149 protected static String escapeMetaCharacters(String in_) {
|
|
150 if (in_ is null || in_.indexOf('&') < 0) {
|
|
151 return in_;
|
|
152 }
|
|
153 int length = in_.length;
|
|
154 StringBuffer out_ = new StringBuffer(length + 1);
|
|
155 for (int i = 0; i < length; i++) {
|
|
156 char c = in_.charAt(i);
|
|
157 if (c is '&') {
|
|
158 out_.append("&&");//$NON-NLS-1$
|
|
159 } else {
|
|
160 out_.append(c);
|
|
161 }
|
|
162 }
|
|
163 return out_.toString();
|
|
164 }
|
|
165
|
|
166 /**
|
|
167 * Creates the progress monitor's UI parts and layouts them
|
|
168 * according to the given layout. If the layou is <code>null</code>
|
|
169 * the part's default layout is used.
|
|
170 * @param layout The layoutfor the receiver.
|
|
171 * @param progressIndicatorHeight The suggested height of the indicator
|
|
172 */
|
|
173 protected void initialize(Layout layout, int progressIndicatorHeight) {
|
|
174 if (layout is null) {
|
|
175 GridLayout l = new GridLayout();
|
|
176 l.marginWidth = 0;
|
|
177 l.marginHeight = 0;
|
|
178 l.numColumns = 1;
|
|
179 layout = l;
|
|
180 }
|
|
181 setLayout(layout);
|
|
182
|
|
183 fLabel = new Label(this, DWT.LEFT);
|
|
184 fLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
|
|
185
|
|
186 if (progressIndicatorHeight is DWT.DEFAULT) {
|
|
187 GC gc = new GC(fLabel);
|
|
188 FontMetrics fm = gc.getFontMetrics();
|
|
189 gc.dispose();
|
|
190 progressIndicatorHeight = fm.getHeight();
|
|
191 }
|
|
192
|
|
193 fProgressIndicator = new ProgressIndicator(this);
|
|
194 GridData gd = new GridData();
|
|
195 gd.horizontalAlignment = GridData.FILL;
|
|
196 gd.grabExcessHorizontalSpace = true;
|
|
197 gd.verticalAlignment = GridData.CENTER;
|
|
198 gd.heightHint = progressIndicatorHeight;
|
|
199 fProgressIndicator.setLayoutData(gd);
|
|
200 }
|
|
201
|
|
202 /**
|
|
203 * Implements <code>IProgressMonitor.internalWorked</code>.
|
|
204 * @see IProgressMonitor#internalWorked(double)
|
|
205 */
|
|
206 public void internalWorked(double work) {
|
|
207 fProgressIndicator.worked(work);
|
|
208 }
|
|
209
|
|
210 /**
|
|
211 * Implements <code>IProgressMonitor.isCanceled</code>.
|
|
212 * @see IProgressMonitor#isCanceled()
|
|
213 */
|
|
214 public bool isCanceled() {
|
|
215 return fIsCanceled;
|
|
216 }
|
|
217
|
|
218 /**
|
|
219 * Detach the progress monitor part from the given cancel
|
|
220 * component
|
|
221 * @param cc
|
|
222 */
|
|
223 public void removeFromCancelComponent(Control cc) {
|
|
224 Assert.isTrue(fCancelComponent is cc && fCancelComponent !is null);
|
|
225 fCancelComponent.removeListener(DWT.Selection, fCancelListener);
|
|
226 fCancelComponent = null;
|
|
227 }
|
|
228
|
|
229 /**
|
|
230 * Implements <code>IProgressMonitor.setCanceled</code>.
|
|
231 * @see IProgressMonitor#setCanceled(bool)
|
|
232 */
|
|
233 public void setCanceled(bool b) {
|
|
234 fIsCanceled = b;
|
|
235 }
|
|
236
|
|
237 /**
|
|
238 * Sets the progress monitor part's font.
|
|
239 */
|
|
240 public void setFont(Font font) {
|
|
241 super.setFont(font);
|
|
242 fLabel.setFont(font);
|
|
243 fProgressIndicator.setFont(font);
|
|
244 }
|
|
245
|
|
246 /*
|
|
247 * (non-Javadoc)
|
|
248 * @see dwtx.core.runtime.IProgressMonitor#setTaskName(java.lang.String)
|
|
249 */
|
|
250 public void setTaskName(String name) {
|
|
251 fTaskName = name;
|
|
252 updateLabel();
|
|
253 }
|
|
254
|
|
255 /*
|
|
256 * (non-Javadoc)
|
|
257 * @see dwtx.core.runtime.IProgressMonitor#subTask(java.lang.String)
|
|
258 */
|
|
259 public void subTask(String name) {
|
|
260 fSubTaskName = name;
|
|
261 updateLabel();
|
|
262 }
|
|
263
|
|
264 /**
|
|
265 * Updates the label with the current task and subtask names.
|
|
266 */
|
|
267 protected void updateLabel() {
|
|
268 if (blockedStatus is null) {
|
|
269 String text = taskLabel();
|
|
270 fLabel.setText(text);
|
|
271 } else {
|
|
272 fLabel.setText(blockedStatus.getMessage());
|
|
273 }
|
|
274
|
|
275 //Force an update as we are in the UI Thread
|
|
276 fLabel.update();
|
|
277 }
|
|
278
|
|
279 /**
|
|
280 * Return the label for showing tasks
|
|
281 * @return String
|
|
282 */
|
|
283 private String taskLabel() {
|
|
284 String text = fSubTaskName is null ? "" : fSubTaskName; //$NON-NLS-1$
|
|
285 if (fTaskName !is null && fTaskName.length > 0) {
|
|
286 text = JFaceResources.format(
|
|
287 "Set_SubTask", [ fTaskName, text ]);//$NON-NLS-1$
|
|
288 }
|
|
289 return escapeMetaCharacters(text);
|
|
290 }
|
|
291
|
|
292 /**
|
|
293 * Implements <code>IProgressMonitor.worked</code>.
|
|
294 * @see IProgressMonitor#worked(int)
|
|
295 */
|
|
296 public void worked(int work) {
|
|
297 internalWorked(work);
|
|
298 }
|
|
299
|
|
300 /* (non-Javadoc)
|
|
301 * @see dwtx.core.runtime.IProgressMonitorWithBlocking#clearBlocked()
|
|
302 */
|
|
303 public void clearBlocked() {
|
|
304 blockedStatus = null;
|
|
305 updateLabel();
|
|
306
|
|
307 }
|
|
308
|
|
309 /* (non-Javadoc)
|
|
310 * @see dwtx.core.runtime.IProgressMonitorWithBlocking#setBlocked(dwtx.core.runtime.IStatus)
|
|
311 */
|
|
312 public void setBlocked(IStatus reason) {
|
|
313 blockedStatus = reason;
|
|
314 updateLabel();
|
|
315
|
|
316 }
|
|
317 }
|