Mercurial > projects > dwt-addons
annotate dwtx/jface/wizard/ProgressMonitorPart.d @ 192:c3583c6ec027
Added missing default cases for switch statements
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 03 Nov 2008 22:52:26 +0100 |
parents | 5df4896124c7 |
children |
rev | line source |
---|---|
35 | 1 /******************************************************************************* |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
2 * Copyright (c) 2000, 2007 IBM Corporation and others. |
35 | 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 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
62 /** true if canceled */ |
35 | 63 protected bool fIsCanceled; |
64 | |
65 /** current blocked status */ | |
66 protected IStatus blockedStatus; | |
67 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
68 /** the cancel lister attached to the cancel component */ |
35 | 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 | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
168 * according to the given layout. If the layout is <code>null</code> |
35 | 169 * the part's default layout is used. |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
170 * @param layout The layout for the receiver. |
35 | 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 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
35
diff
changeset
|
240 public override void setFont(Font font) { |
35 | 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() { | |
72
5df4896124c7
JFace and its examples do compile
Frank Benoit <benoit@tionex.de>
parents:
71
diff
changeset
|
284 bool hasTask= fTaskName !is null && fTaskName.length > 0; |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
285 bool hasSubtask= fSubTaskName !is null && fSubTaskName.length > 0; |
71 | 286 |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
287 if (hasTask) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
288 if (hasSubtask) |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
289 return escapeMetaCharacters(JFaceResources.format( |
71 | 290 "Set_SubTask", [ fTaskName, fSubTaskName ] ));//$NON-NLS-1$ |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
291 return escapeMetaCharacters(fTaskName); |
71 | 292 |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
293 } else if (hasSubtask) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
294 return escapeMetaCharacters(fSubTaskName); |
71 | 295 |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
296 } else { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
43
diff
changeset
|
297 return ""; //$NON-NLS-1$ |
35 | 298 } |
299 } | |
300 | |
301 /** | |
302 * Implements <code>IProgressMonitor.worked</code>. | |
303 * @see IProgressMonitor#worked(int) | |
304 */ | |
305 public void worked(int work) { | |
306 internalWorked(work); | |
307 } | |
308 | |
309 /* (non-Javadoc) | |
310 * @see dwtx.core.runtime.IProgressMonitorWithBlocking#clearBlocked() | |
311 */ | |
312 public void clearBlocked() { | |
313 blockedStatus = null; | |
314 updateLabel(); | |
315 | |
316 } | |
317 | |
318 /* (non-Javadoc) | |
319 * @see dwtx.core.runtime.IProgressMonitorWithBlocking#setBlocked(dwtx.core.runtime.IStatus) | |
320 */ | |
321 public void setBlocked(IStatus reason) { | |
322 blockedStatus = reason; | |
323 updateLabel(); | |
324 | |
325 } | |
326 } |