|
1 /* |
|
2 * Copyright (c) 2009,2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 package javax.microedition.lcdui; |
|
18 |
|
19 import java.util.Timer; |
|
20 import java.util.TimerTask; |
|
21 import org.eclipse.swt.SWT; |
|
22 import org.eclipse.swt.graphics.Point; |
|
23 import org.eclipse.swt.internal.extension.Style; |
|
24 import org.eclipse.swt.internal.extension.LabelExtension; |
|
25 import org.eclipse.swt.layout.*; |
|
26 import org.eclipse.swt.widgets.*; |
|
27 |
|
28 /** |
|
29 * The <code>Alert</code> class is basically a message dialog which shows |
|
30 * information (in form of text and image) to the user. It's highly customizable |
|
31 * and it can be: |
|
32 * <li>timed - when it waits for a predefined period of time and dismisses |
|
33 * itself automatically </li> |
|
34 * <li>modal - user has to choose a command and dismiss the dialog |
|
35 * explicitly</li> |
|
36 */ |
|
37 public class Alert extends Screen |
|
38 { |
|
39 |
|
40 /** |
|
41 * Timeout constant used for modal Alerts. |
|
42 * |
|
43 * @value for FOREVER is -2. |
|
44 */ |
|
45 public static final int FOREVER = -2; |
|
46 |
|
47 /** |
|
48 * The default command triggered when dismissing an Alert. |
|
49 */ |
|
50 public static final Command DISMISS_COMMAND = |
|
51 new Command("", Command.OK, 0); |
|
52 |
|
53 /** |
|
54 * Default command listener. |
|
55 */ |
|
56 private AlertCommandListener implicitListener = new AlertCommandListener(); |
|
57 |
|
58 /** |
|
59 * Alert Timer. |
|
60 */ |
|
61 private Timer timer = new Timer(); |
|
62 |
|
63 /** |
|
64 * Alert Timer Task. |
|
65 */ |
|
66 private AlertTimerTask timerTask; |
|
67 |
|
68 /** |
|
69 * Stores next Displayable to switch to. |
|
70 */ |
|
71 private Displayable nextDisplayable; |
|
72 |
|
73 private int timeout; |
|
74 private String text; |
|
75 private Image image; |
|
76 private AlertType type; |
|
77 private Gauge indicator; |
|
78 private boolean textScrolling; |
|
79 |
|
80 // eSWT widgets |
|
81 private ScrolledTextComposite eswtScrolledText; |
|
82 private LabelExtension eswtImgLabel; |
|
83 private ProgressBar eswtProgressBar; |
|
84 private FormData eswtProgbarLD; |
|
85 |
|
86 private Shell topShell; |
|
87 private ResizeListener resizeListener = new ResizeListener(); |
|
88 private KeyListener keyListener = new KeyListener(); |
|
89 |
|
90 /** |
|
91 * Create a new empty Alert with the specified title. |
|
92 * |
|
93 * @param aTitle the title string |
|
94 */ |
|
95 public Alert(String aTitle) |
|
96 { |
|
97 this(aTitle, null, null, null); |
|
98 } |
|
99 |
|
100 /** |
|
101 * Create a new Alert with the specified title, text, image, and alert type. |
|
102 * |
|
103 * @param title the title string |
|
104 * @param text the text string |
|
105 * @param image the image |
|
106 * @param type the alert type |
|
107 */ |
|
108 public Alert(String title, String text, Image image, AlertType type) |
|
109 { |
|
110 super(title); |
|
111 construct(); |
|
112 this.type = type; |
|
113 setImage(image); |
|
114 setString(text); |
|
115 setTimeout(getDefaultTimeout()); |
|
116 super.addCommand(DISMISS_COMMAND); |
|
117 super.setCommandListener(implicitListener); |
|
118 } |
|
119 |
|
120 /** |
|
121 * Constructs custom eSWT shell for alert dialog. |
|
122 * |
|
123 * @return custom eSWT dialog shell |
|
124 */ |
|
125 Shell eswtConstructShell(int style) |
|
126 { |
|
127 topShell = super.eswtConstructShell(style); |
|
128 Shell dialogShell = new Shell(topShell, style | SWT.DIALOG_TRIM | SWT.RESIZE); |
|
129 return dialogShell; |
|
130 } |
|
131 |
|
132 /** |
|
133 * Creates content Composite. |
|
134 */ |
|
135 Composite eswtConstructContent(int style) |
|
136 { |
|
137 Composite comp = super.eswtConstructContent(SWT.VERTICAL); |
|
138 |
|
139 FormLayout layout = new FormLayout(); |
|
140 layout.marginBottom = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTBOTTOMMARGIN); |
|
141 layout.marginTop = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTTOPMARGIN); |
|
142 layout.marginLeft = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTLEFTMARGIN); |
|
143 layout.marginRight = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTRIGHTMARGIN); |
|
144 layout.spacing = Style.pixelMetric(Style.QSTYLE_PM_LAYOUTVERTICALSPACING); |
|
145 comp.setLayout(layout); |
|
146 |
|
147 eswtScrolledText = new ScrolledTextComposite(comp, comp |
|
148 .getVerticalBar()); |
|
149 eswtImgLabel = new LabelExtension(comp, SWT.NONE); |
|
150 |
|
151 FormData imageLD = new FormData(); |
|
152 imageLD.right = new FormAttachment(100); |
|
153 imageLD.top = new FormAttachment(0); |
|
154 eswtImgLabel.setLayoutData(imageLD); |
|
155 |
|
156 FormData scrolledTextLD = new FormData(); |
|
157 scrolledTextLD.left = new FormAttachment(0); |
|
158 scrolledTextLD.top = new FormAttachment(0); |
|
159 scrolledTextLD.right = new FormAttachment(eswtImgLabel); |
|
160 eswtScrolledText.setLayoutData(scrolledTextLD); |
|
161 |
|
162 eswtProgbarLD = new FormData(); |
|
163 eswtProgbarLD.left = new FormAttachment(0); |
|
164 eswtProgbarLD.right = new FormAttachment(100); |
|
165 eswtProgbarLD.bottom = new FormAttachment(100); |
|
166 |
|
167 eswtUpdateProgressbar(comp, false, false); |
|
168 |
|
169 return comp; |
|
170 } |
|
171 |
|
172 int eswtGetPreferredContentHeight() |
|
173 { |
|
174 int ret = getContentComp().computeSize(SWT.DEFAULT, SWT.DEFAULT).y; |
|
175 |
|
176 // Point imgSize = (eswtImgLabel != null |
|
177 // ? eswtImgLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT) : new Point(0, 0)); |
|
178 // int ret = Math.max( |
|
179 // Math.min( |
|
180 // eswtScrolledText.computeSize(topShell.getClientArea().width - imgSize.x, SWT.DEFAULT).y, |
|
181 // topShell.getClientArea().height / 2), |
|
182 // imgSize.y); |
|
183 |
|
184 if(eswtProgressBar != null && eswtProgressBar.isVisible()) |
|
185 { |
|
186 ret += eswtProgressBar.computeSize(SWT.DEFAULT, SWT.DEFAULT).y; |
|
187 } |
|
188 return ret; |
|
189 } |
|
190 |
|
191 /** |
|
192 * Update ProgressBar widget.<br> |
|
193 * Try to reuse the old ProgressBar if possible, otherwise dispose it and |
|
194 * create a new one. |
|
195 * |
|
196 * @param parent |
|
197 * @param indeterminate |
|
198 * @param visible |
|
199 */ |
|
200 void eswtUpdateProgressbar(Composite parent, |
|
201 boolean indeterminate, |
|
202 boolean visible) |
|
203 { |
|
204 // Only dispose old ProgressBar if it has wrong style |
|
205 if(eswtProgressBar != null) |
|
206 { |
|
207 boolean isIndeterminate = |
|
208 (eswtProgressBar.getStyle() & SWT.INDETERMINATE) != 0; |
|
209 if(indeterminate != isIndeterminate) |
|
210 { |
|
211 eswtProgressBar.setLayoutData(null); |
|
212 eswtProgressBar.dispose(); |
|
213 eswtProgressBar = null; |
|
214 } |
|
215 } |
|
216 // create new ProgressBar |
|
217 if(eswtProgressBar == null) |
|
218 { |
|
219 int newStyle = indeterminate ? SWT.INDETERMINATE : SWT.NONE; |
|
220 eswtProgressBar = new ProgressBar(parent, newStyle); |
|
221 eswtProgressBar.setLayoutData(eswtProgbarLD); |
|
222 // update ScrolledText's layoutdata |
|
223 FormData imageLD = (FormData) eswtImgLabel.getLayoutData(); |
|
224 imageLD.bottom = new FormAttachment(eswtProgressBar); |
|
225 } |
|
226 // set Progressbar visibility |
|
227 if(eswtProgressBar != null) |
|
228 { |
|
229 eswtProgbarLD.top = (visible ? null : new FormAttachment(100)); |
|
230 eswtProgressBar.setVisible(visible); |
|
231 } |
|
232 } |
|
233 |
|
234 /** |
|
235 * Update ProgressBar values. |
|
236 * |
|
237 * @param minValue the minimum value |
|
238 * @param maxValue the maximum value |
|
239 * @param selValue the value |
|
240 */ |
|
241 void eswtSetProgressbarValues(int minValue, int maxValue, int selValue) |
|
242 { |
|
243 if(eswtProgressBar != null) |
|
244 { |
|
245 eswtProgressBar.setMinimum(minValue); |
|
246 eswtProgressBar.setMaximum(maxValue); |
|
247 eswtProgressBar.setSelection(selValue); |
|
248 } |
|
249 } |
|
250 |
|
251 /* (non-Javadoc) |
|
252 * @see javax.microedition.lcdui.Displayable#handleShowEvent() |
|
253 */ |
|
254 void eswtHandleShowCurrentEvent() |
|
255 { |
|
256 super.eswtHandleShowCurrentEvent(); |
|
257 topShell.addListener(SWT.Resize, resizeListener); |
|
258 |
|
259 // add key filter for scrollable text composite |
|
260 org.eclipse.swt.widgets.Display.getCurrent().addFilter( |
|
261 SWT.KeyDown, keyListener); |
|
262 org.eclipse.swt.widgets.Display.getCurrent().addFilter( |
|
263 SWT.Traverse, keyListener); |
|
264 org.eclipse.swt.widgets.Display.getCurrent().addFilter( |
|
265 SWT.MouseUp, keyListener); |
|
266 resetTimerTask(true); |
|
267 } |
|
268 |
|
269 /* (non-Javadoc) |
|
270 * @see javax.microedition.lcdui.Displayable#handleHideEvent() |
|
271 */ |
|
272 void eswtHandleHideCurrentEvent() |
|
273 { |
|
274 super.eswtHandleHideCurrentEvent(); |
|
275 topShell.removeListener(SWT.Resize, resizeListener); |
|
276 |
|
277 nextDisplayable = null; |
|
278 resetTimerTask(false); |
|
279 |
|
280 // remove key filter for scrollable text composite |
|
281 org.eclipse.swt.widgets.Display.getCurrent().removeFilter( |
|
282 SWT.KeyDown, keyListener); |
|
283 org.eclipse.swt.widgets.Display.getCurrent().removeFilter( |
|
284 SWT.Traverse, keyListener); |
|
285 org.eclipse.swt.widgets.Display.getCurrent().removeFilter( |
|
286 SWT.MouseUp, keyListener); |
|
287 } |
|
288 |
|
289 |
|
290 /** |
|
291 * Set the next displayable to be shown. |
|
292 * |
|
293 * @param next next displayable |
|
294 */ |
|
295 void setNextDisplayable(Displayable next) |
|
296 { |
|
297 nextDisplayable = next; |
|
298 } |
|
299 |
|
300 /** |
|
301 * Gets the default Alert image based on the type. |
|
302 * |
|
303 * @param type the alert type |
|
304 * @return the default image based on the type or null if the type is null |
|
305 */ |
|
306 private static int getDefaultImageType(final AlertType type) |
|
307 { |
|
308 if(type == AlertType.ERROR) |
|
309 { |
|
310 return LabelExtension.STANDARDICON_ERROR; |
|
311 } |
|
312 else if(type == AlertType.WARNING) |
|
313 { |
|
314 return LabelExtension.STANDARDICON_WARNING; |
|
315 } |
|
316 else if(type == AlertType.INFO) |
|
317 { |
|
318 return LabelExtension.STANDARDICON_INFO; |
|
319 } |
|
320 else if(type == AlertType.CONFIRMATION) |
|
321 { |
|
322 return LabelExtension.STANDARDICON_CONFIRMATION; |
|
323 } |
|
324 else if(type == AlertType.ALARM) |
|
325 { |
|
326 return LabelExtension.STANDARDICON_ALARM; |
|
327 } |
|
328 else |
|
329 { |
|
330 return LabelExtension.STANDARDICON_ALARM; |
|
331 } |
|
332 |
|
333 } |
|
334 |
|
335 /** |
|
336 * Gets the default Alert text based on the type. |
|
337 * |
|
338 * @param type the alert type |
|
339 * @return the default text based on the type |
|
340 */ |
|
341 private static String getDefaultText(final AlertType type) |
|
342 { |
|
343 if(type == AlertType.ERROR) |
|
344 { |
|
345 return MsgRepository.ALERT_DEFAULT_TEXT_ERROR; |
|
346 } |
|
347 else if(type == AlertType.WARNING) |
|
348 { |
|
349 return MsgRepository.ALERT_DEFAULT_TEXT_WARNING; |
|
350 } |
|
351 else if(type == AlertType.INFO) |
|
352 { |
|
353 return MsgRepository.ALERT_DEFAULT_TEXT_INFO; |
|
354 } |
|
355 else if(type == AlertType.CONFIRMATION) |
|
356 { |
|
357 return MsgRepository.ALERT_DEFAULT_TEXT_CONFIRMATION; |
|
358 } |
|
359 else if(type == AlertType.ALARM) |
|
360 { |
|
361 return MsgRepository.ALERT_DEFAULT_TEXT_ALARM; |
|
362 } |
|
363 else |
|
364 { |
|
365 return MsgRepository.ALERT_DEFAULT_TEXT_ALERT; |
|
366 } |
|
367 } |
|
368 |
|
369 /** |
|
370 * Returns if the Alert is modal. |
|
371 */ |
|
372 private boolean isModal() |
|
373 { |
|
374 return ((timeout == FOREVER) |
|
375 || (getNumCommands() > 1) |
|
376 || isTextScrolling()); |
|
377 } |
|
378 |
|
379 /** |
|
380 * Set the time for the Alert to be shown. |
|
381 * |
|
382 * @param timeout the timeout value in milliseconds or FOREVER |
|
383 * @throws IllegalArgumentException if time is not positive nor FOREVER. |
|
384 */ |
|
385 public void setTimeout(int timeout) |
|
386 { |
|
387 if(timeout > 0 || timeout == FOREVER) |
|
388 { |
|
389 this.timeout = timeout; |
|
390 setCommandsVisibility(isModal()); |
|
391 } |
|
392 else |
|
393 { |
|
394 throw new IllegalArgumentException( |
|
395 MsgRepository.ALERT_EXCEPTION_INVALID_TIMEOUT); |
|
396 } |
|
397 } |
|
398 |
|
399 /** |
|
400 * Get the time the Alert is shown. |
|
401 * |
|
402 * @return timeout in milliseconds, or FOREVER |
|
403 */ |
|
404 public int getTimeout() |
|
405 { |
|
406 if(isModal()) |
|
407 { |
|
408 return FOREVER; |
|
409 } |
|
410 else |
|
411 { |
|
412 return timeout; |
|
413 } |
|
414 } |
|
415 |
|
416 /** |
|
417 * Get the default time for showing an Alert. |
|
418 * |
|
419 * @return default timeout in milliseconds |
|
420 */ |
|
421 public int getDefaultTimeout() |
|
422 { |
|
423 return Config.ALERT_DEFAULT_TIMEOUT; |
|
424 } |
|
425 |
|
426 /** |
|
427 * Sets the type of the Alert. |
|
428 * |
|
429 * @param type an AlertType or null if it doesn't have a specific type |
|
430 */ |
|
431 public void setType(AlertType type) |
|
432 { |
|
433 this.type = type; |
|
434 if(text == null) |
|
435 { |
|
436 // show default text |
|
437 setString(text); |
|
438 } |
|
439 if(image == null) |
|
440 { |
|
441 // show default image |
|
442 setImage(image); |
|
443 } |
|
444 } |
|
445 |
|
446 /** |
|
447 * Gets the type of the Alert. |
|
448 * |
|
449 * @return an AlertType or null if it doesn't have a specific type |
|
450 */ |
|
451 public AlertType getType() |
|
452 { |
|
453 return type; |
|
454 } |
|
455 |
|
456 /** |
|
457 * Sets the image of this Alert. |
|
458 * |
|
459 * @param newImage an Image, or null if there is no image |
|
460 */ |
|
461 public void setImage(Image newImage) |
|
462 { |
|
463 image = newImage; |
|
464 ESWTUIThreadRunner.syncExec(new Runnable() |
|
465 { |
|
466 public void run() |
|
467 { |
|
468 Image temp = (image != null) ? image : null; |
|
469 //Get the image size from QT Style for scaling |
|
470 int scaleToSize = Style.pixelMetric(Style.QSTYLE_PM_MESSAGEBOXICONSIZE); |
|
471 |
|
472 if(temp != null) |
|
473 { |
|
474 |
|
475 //calculate the aspect ratio |
|
476 float aspectRatio = (float)temp.getHeight()/temp.getWidth(); |
|
477 |
|
478 //check the image size |
|
479 if((temp.getWidth() > scaleToSize) || |
|
480 (temp.getHeight() > scaleToSize)) |
|
481 { |
|
482 // we need to scale down the image |
|
483 if(temp.getWidth() > scaleToSize) |
|
484 { |
|
485 //Width is greater |
|
486 Image wScaled = Image.createImage(temp, |
|
487 scaleToSize, |
|
488 (int)(scaleToSize*aspectRatio)); |
|
489 |
|
490 //now check the new dimension against height |
|
491 if(wScaled.getHeight() > scaleToSize) |
|
492 { |
|
493 //scale the image again |
|
494 Image whScaled = Image.createImage(temp, |
|
495 scaleToSize, |
|
496 scaleToSize); |
|
497 eswtImgLabel.setImage(Image.getESWTImage(whScaled)); |
|
498 } |
|
499 else |
|
500 { |
|
501 //height was ok after scaling on width |
|
502 eswtImgLabel.setImage(Image.getESWTImage(wScaled)); |
|
503 } |
|
504 } |
|
505 else if(temp.getHeight() > scaleToSize) |
|
506 { |
|
507 //Height is greater |
|
508 Image hScaled = Image.createImage(temp, |
|
509 (int)(scaleToSize/aspectRatio), |
|
510 scaleToSize); |
|
511 |
|
512 //now check the new dimension against width |
|
513 if(hScaled.getWidth()> scaleToSize) |
|
514 { |
|
515 //scale the image again |
|
516 Image hwScaled = Image.createImage(temp, |
|
517 scaleToSize, |
|
518 scaleToSize); |
|
519 eswtImgLabel.setImage(Image.getESWTImage(hwScaled)); |
|
520 } |
|
521 else |
|
522 { |
|
523 //widh was ok after scaling using height |
|
524 eswtImgLabel.setImage(Image.getESWTImage(hScaled)); |
|
525 } |
|
526 } |
|
527 |
|
528 } |
|
529 else |
|
530 { |
|
531 // image is right size |
|
532 eswtImgLabel.setImage(Image.getESWTImage(temp)); |
|
533 } |
|
534 } |
|
535 else |
|
536 { |
|
537 // no image |
|
538 if(type != null) |
|
539 { |
|
540 //display the default image |
|
541 eswtImgLabel.setStandardIcon(getDefaultImageType(type),scaleToSize,scaleToSize); |
|
542 eswtImgLabel.pack(); |
|
543 } |
|
544 } |
|
545 eswtSetPreferredContentSize(-1, eswtGetPreferredContentHeight()); |
|
546 getContentComp().layout(); |
|
547 } |
|
548 }); |
|
549 } |
|
550 |
|
551 /** |
|
552 * Gets the image of this Alert. |
|
553 * |
|
554 * @return an Image, or null if there is no image |
|
555 */ |
|
556 public Image getImage() |
|
557 { |
|
558 return image; |
|
559 } |
|
560 |
|
561 /** |
|
562 * Checks if the text label's scrollbar is visible. |
|
563 * |
|
564 * @return true if the scrollbar is visible. |
|
565 */ |
|
566 private boolean isTextScrolling() |
|
567 { |
|
568 ESWTUIThreadRunner.syncExec(new Runnable() |
|
569 { |
|
570 public void run() |
|
571 { |
|
572 textScrolling = eswtScrolledText.isTextScrolling(); |
|
573 } |
|
574 }); |
|
575 return textScrolling; |
|
576 } |
|
577 |
|
578 /** |
|
579 * Sets the text used in the Alert. |
|
580 * |
|
581 * @param newText the Alert's text string, or null if there is no text |
|
582 */ |
|
583 public void setString(String newText) |
|
584 { |
|
585 text = newText; |
|
586 ESWTUIThreadRunner.syncExec(new Runnable() |
|
587 { |
|
588 public void run() |
|
589 { |
|
590 String temp = (text != null) ? text : getDefaultText(type); |
|
591 eswtScrolledText.setText(temp); |
|
592 eswtSetPreferredContentSize(-1, eswtGetPreferredContentHeight()); |
|
593 getContentComp().layout(); |
|
594 } |
|
595 }); |
|
596 setCommandsVisibility(isModal()); |
|
597 } |
|
598 |
|
599 /** |
|
600 * Gets the text used in the Alert. |
|
601 * |
|
602 * @return the Alert's text string, or null if there is no text |
|
603 */ |
|
604 public String getString() |
|
605 { |
|
606 return text; |
|
607 } |
|
608 |
|
609 /** |
|
610 * Sets an activity indicator on this Alert. |
|
611 * |
|
612 * @param newIndicator the activity indicator for this Alert, or null if |
|
613 * there is none |
|
614 * @throws IllegalArgumentException if indicator does not meet the |
|
615 * restrictions for its use in an Alert |
|
616 */ |
|
617 public void setIndicator(Gauge newIndicator) |
|
618 { |
|
619 if(newIndicator != null && !newIndicator.isSuitableForAlert()) |
|
620 { |
|
621 throw new IllegalArgumentException( |
|
622 MsgRepository.ALERT_EXCEPTION_INVALID_INDICATOR); |
|
623 } |
|
624 // remove old Gauge parent |
|
625 if(indicator != null) |
|
626 { |
|
627 indicator.setParent(null); |
|
628 } |
|
629 // store the indicator |
|
630 indicator = newIndicator; |
|
631 // set new Gauge parent |
|
632 if(indicator != null) |
|
633 { |
|
634 indicator.setParent(this); |
|
635 } |
|
636 updateIndicator(); |
|
637 } |
|
638 |
|
639 /** |
|
640 * Gets the activity indicator of this Alert. |
|
641 * |
|
642 * @return the activity indicator of this Alert, or null if there is none |
|
643 */ |
|
644 public Gauge getIndicator() |
|
645 { |
|
646 return indicator; |
|
647 } |
|
648 |
|
649 /** |
|
650 * Update indicator if it changed. |
|
651 */ |
|
652 void updateIndicator() |
|
653 { |
|
654 ESWTUIThreadRunner.syncExec(new Runnable() |
|
655 { |
|
656 public void run() |
|
657 { |
|
658 if(indicator != null) |
|
659 { |
|
660 // show ProgressBar |
|
661 if(indicator.isIndefinite()) |
|
662 { |
|
663 // indefinite ProgressBar |
|
664 switch(indicator.getValue()) |
|
665 { |
|
666 case Gauge.CONTINUOUS_IDLE: |
|
667 case Gauge.INCREMENTAL_IDLE: |
|
668 // currently these are mapped to full progress bar |
|
669 // TODO: eSWT support required |
|
670 eswtUpdateProgressbar(getContentComp(), false, true); |
|
671 eswtSetProgressbarValues(0, 1, 1); |
|
672 break; |
|
673 case Gauge.CONTINUOUS_RUNNING: |
|
674 eswtUpdateProgressbar(getContentComp(), true, true); |
|
675 break; |
|
676 case Gauge.INCREMENTAL_UPDATING: |
|
677 // currently this are mapped to blinking |
|
678 // empty and full progress bar |
|
679 // TODO: eSWT support required |
|
680 eswtUpdateProgressbar(getContentComp(), false, true); |
|
681 if(eswtProgressBar.getSelection() > 0) |
|
682 { |
|
683 eswtSetProgressbarValues(0, 1, 0); |
|
684 } |
|
685 else |
|
686 { |
|
687 eswtSetProgressbarValues(0, 1, 1); |
|
688 } |
|
689 break; |
|
690 default: |
|
691 break; |
|
692 } |
|
693 } |
|
694 else |
|
695 { |
|
696 // definite ProgressBar |
|
697 eswtUpdateProgressbar(getContentComp(), false, true); |
|
698 eswtSetProgressbarValues(0, indicator.getMaxValue(), |
|
699 indicator.getValue()); |
|
700 } |
|
701 } |
|
702 else |
|
703 { |
|
704 // hide ProgressBar |
|
705 eswtUpdateProgressbar(getContentComp(), false, false); |
|
706 } |
|
707 eswtSetPreferredContentSize(-1, eswtGetPreferredContentHeight()); |
|
708 getContentComp().layout(); |
|
709 } |
|
710 }); |
|
711 } |
|
712 |
|
713 /* (non-Javadoc) |
|
714 * @see Displayable#addCommand(Command) |
|
715 */ |
|
716 public void addCommand(Command command) |
|
717 { |
|
718 if(command != DISMISS_COMMAND) |
|
719 { |
|
720 super.addCommand(command); |
|
721 super.removeCommand(DISMISS_COMMAND); |
|
722 setCommandsVisibility(isModal()); |
|
723 } |
|
724 } |
|
725 |
|
726 /* (non-Javadoc) |
|
727 * @see Displayable#removeCommand(Command) |
|
728 */ |
|
729 public void removeCommand(Command command) |
|
730 { |
|
731 if(command != DISMISS_COMMAND) |
|
732 { |
|
733 super.removeCommand(command); |
|
734 if(getNumCommands() == 0) |
|
735 { |
|
736 super.addCommand(DISMISS_COMMAND); |
|
737 } |
|
738 setCommandsVisibility(isModal()); |
|
739 } |
|
740 } |
|
741 |
|
742 /* (non-Javadoc) |
|
743 * @see Displayable#setCommandListener(CommandListener) |
|
744 */ |
|
745 public void setCommandListener(CommandListener listener) |
|
746 { |
|
747 if(listener == null) |
|
748 { |
|
749 listener = implicitListener; |
|
750 } |
|
751 super.setCommandListener(listener); |
|
752 } |
|
753 |
|
754 void resetTimerTask(boolean reset) |
|
755 { |
|
756 if(timerTask != null) |
|
757 { |
|
758 timerTask.cancel(); |
|
759 timerTask = null; |
|
760 } |
|
761 if(reset && !isModal()) |
|
762 { |
|
763 // if not modal schedule new timer |
|
764 timerTask = new AlertTimerTask(); |
|
765 timer.schedule(timerTask, timeout); |
|
766 } |
|
767 } |
|
768 |
|
769 /** |
|
770 * Alert Timer task. Triggers the first command on the Alert. |
|
771 */ |
|
772 class AlertTimerTask extends TimerTask |
|
773 { |
|
774 |
|
775 public void run() |
|
776 { |
|
777 // trigger the first available command on the listener |
|
778 if(!isModal()) |
|
779 { |
|
780 callCommandAction(getCommand(0)); |
|
781 } |
|
782 } |
|
783 |
|
784 } |
|
785 |
|
786 /** |
|
787 * Default (implicit) command listener. Any Commands close the Alert. |
|
788 */ |
|
789 class AlertCommandListener implements CommandListener |
|
790 { |
|
791 |
|
792 public void commandAction(Command aCommand, Displayable aSource) |
|
793 { |
|
794 final Alert alert = (Alert) aSource; |
|
795 Display.getDisplay().setCurrent(alert.nextDisplayable); |
|
796 } |
|
797 |
|
798 } |
|
799 |
|
800 /** |
|
801 * Key listener. Also handles scrolling of text composite. |
|
802 */ |
|
803 class KeyListener implements Listener |
|
804 { |
|
805 |
|
806 public void handleEvent(Event e) |
|
807 { |
|
808 if(e.type == SWT.Traverse) |
|
809 { |
|
810 e.doit = false; |
|
811 } |
|
812 else if(e.type == SWT.KeyDown) |
|
813 { |
|
814 if(!isModal()) |
|
815 { |
|
816 resetTimerTask(false); |
|
817 callCommandAction(getCommand(0)); |
|
818 } |
|
819 else if(e.keyCode == SWT.ARROW_DOWN) |
|
820 { |
|
821 Point p = eswtScrolledText.getOrigin(); |
|
822 eswtScrolledText.setOrigin(p.x, p.y |
|
823 + Config.ALERT_TEXT_SCROLLING_DELTA); |
|
824 } |
|
825 else if(e.keyCode == SWT.ARROW_UP) |
|
826 { |
|
827 Point p = eswtScrolledText.getOrigin(); |
|
828 eswtScrolledText.setOrigin(p.x, p.y |
|
829 - Config.ALERT_TEXT_SCROLLING_DELTA); |
|
830 } |
|
831 } |
|
832 else if(e.type == SWT.MouseUp) |
|
833 { |
|
834 if(!isModal()) |
|
835 { |
|
836 resetTimerTask(false); |
|
837 callCommandAction(getCommand(0)); |
|
838 } |
|
839 } |
|
840 } |
|
841 |
|
842 } |
|
843 |
|
844 /** |
|
845 * Resize listener which listens to bottom shell's resize events and |
|
846 * forwards them to top shell. |
|
847 */ |
|
848 class ResizeListener implements Listener |
|
849 { |
|
850 |
|
851 public void handleEvent(Event event) |
|
852 { |
|
853 // explicitly forward topShell resize events to dialogShell |
|
854 getShell().notifyListeners(SWT.Resize, event); |
|
855 } |
|
856 |
|
857 } |
|
858 |
|
859 /** |
|
860 * Dispose Alert. |
|
861 */ |
|
862 void dispose() |
|
863 { |
|
864 super.dispose(); |
|
865 ESWTUIThreadRunner.syncExec(new Runnable() |
|
866 { |
|
867 public void run() |
|
868 { |
|
869 topShell.dispose(); |
|
870 } |
|
871 }); |
|
872 } |
|
873 |
|
874 } |