comparison org.eclipse.jface/src/org/eclipse/jface/action/StatusLine.d @ 12:bc29606a740c

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