comparison dwtx/jface/action/StatusLine.d @ 26:87d8cf0a3074

StatusLine
author Frank Benoit <benoit@tionex.de>
date Thu, 03 Apr 2008 05:03:09 +0200
parents
children c884a1ab6db3
comparison
equal deleted inserted replaced
25:ca63e2bea4bf 26:87d8cf0a3074
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
14 module dwtx.jface.action.StatusLine;
15
16 import dwtx.jface.action.StatusLineLayoutData;
17
18 import dwt.DWT;
19 import dwt.custom.CLabel;
20 import dwt.events.DisposeEvent;
21 import dwt.events.DisposeListener;
22 import dwt.events.SelectionAdapter;
23 import dwt.events.SelectionEvent;
24 import dwt.graphics.Cursor;
25 import dwt.graphics.Font;
26 import dwt.graphics.Image;
27 import dwt.graphics.Point;
28 import dwt.graphics.Rectangle;
29 import dwt.layout.GridData;
30 import dwt.layout.GridLayout;
31 import dwt.widgets.Composite;
32 import dwt.widgets.Control;
33 import dwt.widgets.Display;
34 import dwt.widgets.Layout;
35 import dwt.widgets.ToolBar;
36 import dwt.widgets.ToolItem;
37 import dwtx.core.runtime.IProgressMonitor;
38 import dwtx.jface.dialogs.ProgressIndicator;
39 import dwtx.jface.resource.ImageDescriptor;
40 import dwtx.jface.resource.JFaceColors;
41 import dwtx.jface.resource.JFaceResources;
42
43 import dwt.dwthelper.utils;
44 import dwt.dwthelper.Runnable;
45
46 /**
47 * A StatusLine control is a DWT Composite with a horizontal layout which hosts
48 * a number of status indication controls.
49 * Typically it is situated below the content area of the window.
50 * <p>
51 * By default a StatusLine has two predefined status controls: a MessageLine and a
52 * ProgressIndicator and it provides API for easy access.
53 * </p>
54 * <p>
55 * This is an internal class, not intended to be used outside the JFace framework.
56 * </p>
57 */
58 /* package */class StatusLine : Composite, IProgressMonitor {
59
60 /** Horizontal gaps between items. */
61 public static const int GAP = 3;
62
63 /** Progress bar creation is delayed by this ms */
64 public static const int DELAY_PROGRESS = 500;
65
66 /** visibility state of the progressbar */
67 protected bool fProgressIsVisible = false;
68
69 /** visibility state of the cancle button */
70 protected bool fCancelButtonIsVisible = false;
71
72 /** enablement state of the cancle button */
73 protected bool fCancelEnabled = false;
74
75 /** name of the task */
76 protected String fTaskName;
77
78 /** is the task is cancled */
79 protected bool fIsCanceled;
80
81 /** the start time of the task */
82 protected long fStartTime;
83
84 private Cursor fStopButtonCursor;
85
86 /** the message text */
87 protected String fMessageText;
88
89 /** the message image */
90 protected Image fMessageImage;
91
92 /** the error text */
93 protected String fErrorText;
94
95 /** the error image */
96 protected Image fErrorImage;
97
98 /** the message label */
99 protected CLabel fMessageLabel;
100
101 /** the composite parent of the progress bar */
102 protected Composite fProgressBarComposite;
103
104 /** the progress bar */
105 protected ProgressIndicator fProgressBar;
106
107 /** the toolbar */
108 protected ToolBar fToolBar;
109
110 /** the cancle button */
111 protected ToolItem fCancelButton;
112
113 /** stop image descriptor */
114 protected static ImageDescriptor fgStopImage;
115
116 static this() {
117 JFaceResources.getImageRegistry().put(
118 "dwtx.jface.parts.StatusLine.stopImage", fgStopImage);//$NON-NLS-1$
119 fgStopImage = ImageDescriptor
120 .createFromFile(StatusLine.classinfo, "images/stop.gif");//$NON-NLS-1$
121 }
122
123 /**
124 * Layout the contribution item controls on the status line.
125 */
126 public class StatusLineLayout : Layout {
127 private const StatusLineLayoutData DEFAULT_DATA;
128
129 this(){
130 DEFAULT_DATA = new StatusLineLayoutData();
131 }
132
133 public Point computeSize(Composite composite, int wHint, int hHint,
134 bool changed) {
135
136 if (wHint !is DWT.DEFAULT && hHint !is DWT.DEFAULT) {
137 return new Point(wHint, hHint);
138 }
139
140 Control[] children = composite.getChildren();
141 int totalWidth = 0;
142 int maxHeight = 0;
143 int totalCnt = 0;
144 for (int i = 0; i < children.length; i++) {
145 bool useWidth = true;
146 Control w = children[i];
147 if (w is fProgressBarComposite && !fProgressIsVisible) {
148 useWidth = false;
149 } else if (w is fToolBar && !fCancelButtonIsVisible) {
150 useWidth = false;
151 }
152 StatusLineLayoutData data = cast(StatusLineLayoutData) w
153 .getLayoutData();
154 if (data is null) {
155 data = DEFAULT_DATA;
156 }
157 Point e = w.computeSize(data.widthHint, data.heightHint,
158 changed);
159 if (useWidth) {
160 totalWidth += e.x;
161 totalCnt++;
162 }
163 maxHeight = Math.max(maxHeight, e.y);
164 }
165 if (totalCnt > 0) {
166 totalWidth += (totalCnt - 1) * GAP;
167 }
168 if (totalWidth <= 0) {
169 totalWidth = maxHeight * 4;
170 }
171 return new Point(totalWidth, maxHeight);
172 }
173
174 public void layout(Composite composite, bool flushCache) {
175
176 if (composite is null) {
177 return;
178 }
179
180 // StatusLineManager skips over the standard status line widgets
181 // in its update method. There is thus a dependency
182 // between the layout of the standard widgets and the update method.
183
184 // Make sure cancel button and progress bar are before contributions.
185 fMessageLabel.moveAbove(null);
186 fToolBar.moveBelow(fMessageLabel);
187 fProgressBarComposite.moveBelow(fToolBar);
188
189 Rectangle rect = composite.getClientArea();
190 Control[] children = composite.getChildren();
191 int count = children.length;
192
193 int ws[] = new int[count];
194
195 int h = rect.height;
196 int totalWidth = -GAP;
197 for (int i = 0; i < count; i++) {
198 Control w = children[i];
199 if (w is fProgressBarComposite && !fProgressIsVisible) {
200 continue;
201 }
202 if (w is fToolBar && !fCancelButtonIsVisible) {
203 continue;
204 }
205 StatusLineLayoutData data = cast(StatusLineLayoutData) w
206 .getLayoutData();
207 if (data is null) {
208 data = DEFAULT_DATA;
209 }
210 int width = w.computeSize(data.widthHint, h, flushCache).x;
211 ws[i] = width;
212 totalWidth += width + GAP;
213 }
214
215 int diff = rect.width - totalWidth;
216 ws[0] += diff; // make the first StatusLabel wider
217
218 // Check against minimum recommended width
219 final int msgMinWidth = rect.width / 3;
220 if (ws[0] < msgMinWidth) {
221 diff = ws[0] - msgMinWidth;
222 ws[0] = msgMinWidth;
223 } else {
224 diff = 0;
225 }
226
227 // Take space away from the contributions first.
228 for (int i = count - 1; i >= 0 && diff < 0; --i) {
229 int min = Math.min(ws[i], -diff);
230 ws[i] -= min;
231 diff += min + GAP;
232 }
233
234 int x = rect.x;
235 int y = rect.y;
236 for (int i = 0; i < count; i++) {
237 Control w = children[i];
238 /*
239 * Workaround for Linux Motif:
240 * Even if the progress bar and cancel button are
241 * not set to be visible ad of width 0, they still
242 * draw over the first pixel of the editor
243 * contributions.
244 *
245 * The fix here is to draw the progress bar and
246 * cancel button off screen if they are not visible.
247 */
248 if (w is fProgressBarComposite && !fProgressIsVisible
249 || w is fToolBar && !fCancelButtonIsVisible) {
250 w.setBounds(x + rect.width, y, ws[i], h);
251 continue;
252 }
253 w.setBounds(x, y, ws[i], h);
254 if (ws[i] > 0) {
255 x += ws[i] + GAP;
256 }
257 }
258 }
259 }
260
261 /**
262 * Create a new StatusLine as a child of the given parent.
263 *
264 * @param parent the parent for this Composite
265 * @param style the style used to create this widget
266 */
267 public this(Composite parent, int style) {
268 super(parent, style);
269
270 addDisposeListener(new class DisposeListener {
271 public void widgetDisposed(DisposeEvent e) {
272 handleDispose();
273 }
274 });
275
276 // StatusLineManager skips over the standard status line widgets
277 // in its update method. There is thus a dependency
278 // between this code defining the creation and layout of the standard
279 // widgets and the update method.
280
281 setLayout(new StatusLineLayout());
282
283 fMessageLabel = new CLabel(this, DWT.NONE);//DWT.SHADOW_IN);
284 // Color[] colors = new Color[2];
285 // colors[0] = parent.getDisplay().getSystemColor(DWT.COLOR_WIDGET_LIGHT_SHADOW);
286 // colors[1] = fMessageLabel.getBackground();
287 // int[] gradient = new int[] {JFaceColors.STATUS_PERCENT};
288 // fMessageLabel.setBackground(colors, gradient);
289
290 fProgressIsVisible = false;
291 fCancelEnabled = false;
292
293 fToolBar = new ToolBar(this, DWT.FLAT);
294 fCancelButton = new ToolItem(fToolBar, DWT.PUSH);
295 fCancelButton.setImage(fgStopImage.createImage());
296 fCancelButton.setToolTipText(JFaceResources
297 .getString("Cancel_Current_Operation")); //$NON-NLS-1$
298 fCancelButton.addSelectionListener(new class SelectionAdapter {
299 public void widgetSelected(SelectionEvent e) {
300 setCanceled(true);
301 }
302 });
303 fCancelButton.addDisposeListener(new class DisposeListener {
304 public void widgetDisposed(DisposeEvent e) {
305 Image i = fCancelButton.getImage();
306 if ((i !is null) && (!i.isDisposed())) {
307 i.dispose();
308 }
309 }
310 });
311
312 // We create a composite to create the progress bar in
313 // so that it can be centered. See bug #32331
314 fProgressBarComposite = new Composite(this, DWT.NONE);
315 GridLayout layout = new GridLayout();
316 layout.horizontalSpacing = 0;
317 layout.verticalSpacing = 0;
318 layout.marginHeight = 0;
319 layout.marginWidth = 0;
320 fProgressBarComposite.setLayout(layout);
321 fProgressBar = new ProgressIndicator(fProgressBarComposite);
322 fProgressBar.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
323 | GridData.GRAB_VERTICAL));
324
325 fStopButtonCursor = new Cursor(getDisplay(), DWT.CURSOR_ARROW);
326 }
327
328 /**
329 * Notifies that the main task is beginning.
330 *
331 * @param name the name (or description) of the main task
332 * @param totalWork the total number of work units into which
333 * the main task is been subdivided. If the value is 0 or UNKNOWN the
334 * implemenation is free to indicate progress in a way which doesn't
335 * require the total number of work units in advance. In general users
336 * should use the UNKNOWN value if they don't know the total amount of
337 * work units.
338 */
339 public void beginTask(String name, int totalWork) {
340 long timestamp = System.currentTimeMillis();
341 fStartTime = timestamp;
342 bool animated = (totalWork is UNKNOWN || totalWork is 0);
343 // make sure the progress bar is made visible while
344 // the task is running. Fixes bug 32198 for the non-animated case.
345 Runnable timer = new class Runnable {
346 bool animated_;
347 long timestamp_;
348 this(){
349 animated_=animated;
350 timestamp_=timestamp;
351 }
352 public void run() {
353 this.outer.startTask(timestamp_, animated_);
354 }
355 };
356 if (fProgressBar is null) {
357 return;
358 }
359
360 fProgressBar.getDisplay().timerExec(DELAY_PROGRESS, timer);
361 if (!animated) {
362 fProgressBar.beginTask(totalWork);
363 }
364 if (name is null) {
365 fTaskName = "";//$NON-NLS-1$
366 } else {
367 fTaskName = name;
368 }
369 setMessage(fTaskName);
370 }
371
372 /**
373 * Notifies that the work is done; that is, either the main task is completed or the
374 * user cancelled it.
375 * Done() can be called more than once; an implementation should be prepared to handle
376 * this case.
377 */
378 public void done() {
379
380 fStartTime = 0;
381
382 if (fProgressBar !is null) {
383 fProgressBar.sendRemainingWork();
384 fProgressBar.done();
385 }
386 setMessage(null);
387
388 hideProgress();
389 }
390
391 /**
392 * Returns the status line's progress monitor
393 * @return {@link IProgressMonitor} the progress monitor
394 */
395 public IProgressMonitor getProgressMonitor() {
396 return this;
397 }
398
399 /**
400 * @private
401 */
402 protected void handleDispose() {
403 if (fStopButtonCursor !is null) {
404 fStopButtonCursor.dispose();
405 fStopButtonCursor = null;
406 }
407 if (fProgressBar !is null) {
408 fProgressBar.dispose();
409 fProgressBar = null;
410 }
411 }
412
413 /**
414 * Hides the Cancel button and ProgressIndicator.
415 * @private
416 */
417 protected void hideProgress() {
418
419 if (fProgressIsVisible && !isDisposed()) {
420 fProgressIsVisible = false;
421 fCancelEnabled = false;
422 fCancelButtonIsVisible = false;
423 if (fToolBar !is null && !fToolBar.isDisposed()) {
424 fToolBar.setVisible(false);
425 }
426 if (fProgressBarComposite !is null
427 && !fProgressBarComposite.isDisposed()) {
428 fProgressBarComposite.setVisible(false);
429 }
430 layout();
431 }
432 }
433
434 /**
435 * @see IProgressMonitor#internalWorked(double)
436 */
437 public void internalWorked(double work) {
438 if (!fProgressIsVisible) {
439 if (System.currentTimeMillis() - fStartTime > DELAY_PROGRESS) {
440 showProgress();
441 }
442 }
443
444 if (fProgressBar !is null) {
445 fProgressBar.worked(work);
446 }
447 }
448
449 /**
450 * Returns true if the user does some UI action to cancel this operation.
451 * (like hitting the Cancel button on the progress dialog).
452 * The long running operation typically polls isCanceled().
453 */
454 public bool isCanceled() {
455 return fIsCanceled;
456 }
457
458 /**
459 * Returns <code>true</true> if the ProgressIndication provides UI for canceling
460 * a long running operation.
461 * @return <code>true</true> if the ProgressIndication provides UI for canceling
462 */
463 public bool isCancelEnabled() {
464 return fCancelEnabled;
465 }
466
467 /**
468 * Sets the cancel status. This method is usually called with the
469 * argument false if a client wants to abort a cancel action.
470 */
471 public void setCanceled(bool b) {
472 fIsCanceled = b;
473 if (fCancelButton !is null) {
474 fCancelButton.setEnabled(!b);
475 }
476 }
477
478 /**
479 * Controls whether the ProgressIndication provides UI for canceling
480 * a long running operation.
481 * If the ProgressIndication is currently visible calling this method may have
482 * a direct effect on the layout because it will make a cancel button visible.
483 *
484 * @param enabled <code>true</true> if cancel should be enabled
485 */
486 public void setCancelEnabled(bool enabled) {
487 fCancelEnabled = enabled;
488 if (fProgressIsVisible && !fCancelButtonIsVisible && enabled) {
489 showButton();
490 layout();
491 }
492 if (fCancelButton !is null && !fCancelButton.isDisposed()) {
493 fCancelButton.setEnabled(enabled);
494 }
495 }
496
497 /**
498 * Sets the error message text to be displayed on the status line.
499 * The image on the status line is cleared.
500 *
501 * @param message the error message, or <code>null</code> for no error message
502 */
503 public void setErrorMessage(String message) {
504 setErrorMessage(null, message);
505 }
506
507 /**
508 * Sets an image and error message text to be displayed on the status line.
509 *
510 * @param image the image to use, or <code>null</code> for no image
511 * @param message the error message, or <code>null</code> for no error message
512 */
513 public void setErrorMessage(Image image, String message) {
514 fErrorText = trim(message);
515 fErrorImage = image;
516 updateMessageLabel();
517 }
518
519 /**
520 * Applies the given font to this status line.
521 */
522 public void setFont(Font font) {
523 super.setFont(font);
524 Control[] children = getChildren();
525 for (int i = 0; i < children.length; i++) {
526 children[i].setFont(font);
527 }
528 }
529
530 /**
531 * Sets the message text to be displayed on the status line.
532 * The image on the status line is cleared.
533 *
534 * @param message the error message, or <code>null</code> for no error message
535 */
536 public void setMessage(String message) {
537 setMessage(null, message);
538 }
539
540 /**
541 * Sets an image and a message text to be displayed on the status line.
542 *
543 * @param image the image to use, or <code>null</code> for no image
544 * @param message the message, or <code>null</code> for no message
545 */
546 public void setMessage(Image image, String message) {
547 fMessageText = trim(message);
548 fMessageImage = image;
549 updateMessageLabel();
550 }
551
552 /**
553 * @see IProgressMonitor#setTaskName(java.lang.String)
554 */
555 public void setTaskName(String name) {
556 fTaskName = name;
557 }
558
559 /**
560 * Makes the Cancel button visible.
561 * @private
562 */
563 protected void showButton() {
564 if (fToolBar !is null && !fToolBar.isDisposed()) {
565 fToolBar.setVisible(true);
566 fToolBar.setEnabled(true);
567 fToolBar.setCursor(fStopButtonCursor);
568 fCancelButtonIsVisible = true;
569 }
570 }
571
572 /**
573 * Shows the Cancel button and ProgressIndicator.
574 * @private
575 */
576 protected void showProgress() {
577 if (!fProgressIsVisible && !isDisposed()) {
578 fProgressIsVisible = true;
579 if (fCancelEnabled) {
580 showButton();
581 }
582 if (fProgressBarComposite !is null
583 && !fProgressBarComposite.isDisposed()) {
584 fProgressBarComposite.setVisible(true);
585 }
586 layout();
587 }
588 }
589
590 /**
591 * @private
592 */
593 void startTask(long timestamp, bool animated) {
594 if (!fProgressIsVisible && fStartTime is timestamp) {
595 showProgress();
596 if (animated) {
597 if (fProgressBar !is null && !fProgressBar.isDisposed()) {
598 fProgressBar.beginAnimatedTask();
599 }
600 }
601 }
602 }
603
604 /**
605 * Notifies that a subtask of the main task is beginning.
606 * Subtasks are optional; the main task might not have subtasks.
607 * @param name the name (or description) of the subtask
608 * @see IProgressMonitor#subTask(String)
609 */
610 public void subTask(String name) {
611 String text;
612 if (fTaskName.length is 0) {
613 text = name;
614 } else {
615 text = JFaceResources.format(
616 "Set_SubTask", [ fTaskName, name ]);//$NON-NLS-1$
617 }
618 setMessage(text);
619 }
620
621 /**
622 * Trims the message to be displayable in the status line.
623 * This just pulls out the first line of the message.
624 * Allows null.
625 */
626 String trim(String message) {
627 if (message is null) {
628 return null;
629 }
630 int cr = message.indexOf('\r');
631 int lf = message.indexOf('\n');
632 if (cr is -1 && lf is -1) {
633 return message;
634 }
635 int len;
636 if (cr is -1) {
637 len = lf;
638 } else if (lf is -1) {
639 len = cr;
640 } else {
641 len = Math.min(cr, lf);
642 }
643 return message.substring(0, len);
644 }
645
646 /**
647 * Updates the message label widget.
648 */
649 protected void updateMessageLabel() {
650 if (fMessageLabel !is null && !fMessageLabel.isDisposed()) {
651 Display display = fMessageLabel.getDisplay();
652 if ((fErrorText !is null && fErrorText.length > 0)
653 || fErrorImage !is null) {
654 fMessageLabel.setForeground(JFaceColors.getErrorText(display));
655 fMessageLabel.setText(fErrorText);
656 fMessageLabel.setImage(fErrorImage);
657 } else {
658 fMessageLabel.setForeground(display
659 .getSystemColor(DWT.COLOR_WIDGET_FOREGROUND));
660 fMessageLabel.setText(fMessageText is null ? "" : fMessageText); //$NON-NLS-1$
661 fMessageLabel.setImage(fMessageImage);
662 }
663 }
664 }
665
666 /**
667 * @see IProgressMonitor#worked(int)
668 */
669 public void worked(int work) {
670 internalWorked(work);
671 }
672 }