KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > progress > VirtualInfoItem


1 package org.eclipse.ui.internal.progress;
2
3 /*******************************************************************************
4  * Copyright (c) 2005, 2006 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM Corporation - initial API and implementation
12  *******************************************************************************/

13 import java.net.URL JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Date JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.jobs.Job;
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.dialogs.Dialog;
24 import org.eclipse.jface.dialogs.IDialogConstants;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.jface.resource.JFaceResources;
27 import org.eclipse.osgi.util.NLS;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.MouseAdapter;
30 import org.eclipse.swt.events.MouseEvent;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.graphics.Color;
34 import org.eclipse.swt.graphics.Image;
35 import org.eclipse.swt.layout.FormAttachment;
36 import org.eclipse.swt.layout.FormData;
37 import org.eclipse.swt.layout.FormLayout;
38 import org.eclipse.swt.layout.GridData;
39 import org.eclipse.swt.widgets.Canvas;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Control;
42 import org.eclipse.swt.widgets.Event;
43 import org.eclipse.swt.widgets.Label;
44 import org.eclipse.swt.widgets.Link;
45 import org.eclipse.swt.widgets.Listener;
46 import org.eclipse.swt.widgets.ProgressBar;
47 import org.eclipse.swt.widgets.ToolBar;
48 import org.eclipse.swt.widgets.ToolItem;
49 import org.eclipse.ui.PlatformUI;
50 import org.eclipse.ui.internal.WorkbenchImages;
51 import org.eclipse.ui.progress.IProgressConstants;
52
53 import com.ibm.icu.text.DateFormat;
54
55 /**
56  * VirtualInfoItem is the item used to show jobs which only displays when it is
57  * visible.
58  *
59  * @since 3.1
60  *
61  */

62 class VirtualInfoItem extends Canvas {
63
64     static String JavaDoc STOP_IMAGE_KEY = "org.eclipse.ui.internal.progress.PROGRESS_STOP"; //$NON-NLS-1$
65

66     static String JavaDoc DISABLED_STOP_IMAGE_KEY = "org.eclipse.ui.internal.progress.DISABLED_PROGRESS_STOP"; //$NON-NLS-1$
67

68     static String JavaDoc CLEAR_FINISHED_JOB_KEY = "org.eclipse.ui.internal.progress.CLEAR_FINISHED_JOB"; //$NON-NLS-1$
69

70     static String JavaDoc DISABLED_CLEAR_FINISHED_JOB_KEY = "org.eclipse.ui.internal.progress.DISABLED_CLEAR_FINISHED_JOB"; //$NON-NLS-1$
71

72     static String JavaDoc DEFAULT_JOB_KEY = "org.eclipse.ui.internal.progress.PROGRESS_DEFAULT"; //$NON-NLS-1$
73

74     static String JavaDoc DARK_COLOR_KEY = "org.eclipse.ui.internal.progress.PROGRESS_DARK_COLOR"; //$NON-NLS-1$
75

76     JobTreeElement info;
77
78     Label progressLabel;
79
80     ToolBar actionBar;
81
82     ToolItem actionButton;
83
84     List JavaDoc taskEntries = new ArrayList JavaDoc(0);
85
86     private ProgressBar progressBar;
87
88     private Label jobImageLabel;
89
90     static final int MAX_PROGRESS_HEIGHT = 12;
91
92     static final int MIN_ICON_SIZE = 16;
93
94     private static final String JavaDoc EMPTY_STRING = "";//$NON-NLS-1$
95

96     private static final String JavaDoc TEXT_KEY = "Text"; //$NON-NLS-1$
97

98     private static final String JavaDoc ACTION_KEY = "Action";//$NON-NLS-1$
99

100     interface IndexListener {
101         /**
102          * Select the item previous to the receiver.
103          */

104         public void selectPrevious();
105
106         /**
107          * Select the next previous to the receiver.
108          */

109         public void selectNext();
110
111         /**
112          * Select the receiver.
113          */

114         public void select();
115     }
116
117     IndexListener indexListener;
118
119     private int currentIndex;
120
121     private boolean selected;
122
123     private MouseAdapter mouseListener;
124
125     private boolean isShowing = true;
126
127     static {
128         JFaceResources
129                 .getImageRegistry()
130                 .put(
131                         STOP_IMAGE_KEY,
132                         WorkbenchImages
133                                 .getWorkbenchImageDescriptor("elcl16/progress_stop.gif"));//$NON-NLS-1$
134

135         JFaceResources
136                 .getImageRegistry()
137                 .put(
138                         DISABLED_STOP_IMAGE_KEY,
139                         WorkbenchImages
140                                 .getWorkbenchImageDescriptor("dlcl16/progress_stop.gif"));//$NON-NLS-1$
141

142         JFaceResources
143                 .getImageRegistry()
144                 .put(
145                         DEFAULT_JOB_KEY,
146                         WorkbenchImages
147                                 .getWorkbenchImageDescriptor("progress/progress_task.gif")); //$NON-NLS-1$
148

149         JFaceResources
150                 .getImageRegistry()
151                 .put(
152                         CLEAR_FINISHED_JOB_KEY,
153                         WorkbenchImages
154                                 .getWorkbenchImageDescriptor("elcl16/progress_rem.gif")); //$NON-NLS-1$
155

156         JFaceResources
157                 .getImageRegistry()
158                 .put(
159                         DISABLED_CLEAR_FINISHED_JOB_KEY,
160                         WorkbenchImages
161                                 .getWorkbenchImageDescriptor("dlcl16/progress_rem.gif")); //$NON-NLS-1$
162

163         // Mac has different Gamma value
164
int shift = "carbon".equals(SWT.getPlatform()) ? -25 : -10;//$NON-NLS-1$
165

166         Color lightColor = PlatformUI.getWorkbench().getDisplay()
167                 .getSystemColor(SWT.COLOR_LIST_BACKGROUND);
168
169         // Determine a dark color by shifting the list color
170
Color darkColor = new Color(PlatformUI.getWorkbench().getDisplay(),
171                 Math.max(0, lightColor.getRed() + shift), Math.max(0,
172                         lightColor.getGreen() + shift), Math.max(0, lightColor
173                         .getBlue()
174                         + shift));
175         JFaceResources.getColorRegistry().put(DARK_COLOR_KEY,
176                 darkColor.getRGB());
177     }
178
179     /**
180      * Create a new instance of the receiver with the specified parent, style
181      * and info object/
182      *
183      * @param parent
184      * @param style
185      * @param progressInfo
186      */

187     public VirtualInfoItem(Composite parent, int style,
188             JobTreeElement progressInfo) {
189         super(parent, style);
190         info = progressInfo;
191         createChildren();
192         setData(info);
193         setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
194     }
195
196     /**
197      * Create the child widgets of the receiver.
198      */

199     /**
200      *
201      */

202     protected void createChildren() {
203
204         FormLayout layout = new FormLayout();
205         setLayout(layout);
206
207         jobImageLabel = new Label(this, SWT.NONE);
208         jobImageLabel.setImage(getInfoImage());
209         FormData imageData = new FormData();
210         imageData.top = new FormAttachment(IDialogConstants.VERTICAL_SPACING);
211         imageData.left = new FormAttachment(
212                 IDialogConstants.HORIZONTAL_SPACING / 2);
213         jobImageLabel.setLayoutData(imageData);
214
215         progressLabel = new Label(this, SWT.NONE);
216         progressLabel.setFont(JFaceResources.getFontRegistry().getBold(
217                 JFaceResources.DEFAULT_FONT));
218         progressLabel.setText(getMainTitle());
219
220         actionBar = new ToolBar(this, SWT.FLAT);
221         actionBar.setCursor(getDisplay().getSystemCursor(SWT.CURSOR_ARROW)); // set
222
// cursor
223
// to
224
// overwrite
225
// any
226
// busy
227

228         // cursor we might have
229
actionButton = new ToolItem(actionBar, SWT.NONE);
230         actionButton
231                 .setToolTipText(ProgressMessages.NewProgressView_CancelJobToolTip);
232         actionButton.addSelectionListener(new SelectionAdapter() {
233             public void widgetSelected(SelectionEvent e) {
234                 actionButton.setEnabled(false);
235                 cancelOrRemove();
236             }
237
238         });
239         actionBar.addListener(SWT.Traverse, new Listener() {
240             /*
241              * (non-Javadoc)
242              *
243              * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
244              */

245             public void handleEvent(Event event) {
246                 if (indexListener == null) {
247                     return;
248                 }
249                 int detail = event.detail;
250                 if (detail == SWT.TRAVERSE_ARROW_NEXT) {
251                     indexListener.selectNext();
252                 }
253                 if (detail == SWT.TRAVERSE_ARROW_PREVIOUS) {
254                     indexListener.selectPrevious();
255                 }
256
257             }
258         });
259         updateToolBarValues();
260
261         FormData progressData = new FormData();
262         progressData.top = new FormAttachment(IDialogConstants.VERTICAL_SPACING);
263         progressData.left = new FormAttachment(jobImageLabel,
264                 IDialogConstants.HORIZONTAL_SPACING / 2);
265         progressData.right = new FormAttachment(actionBar,
266                 IDialogConstants.HORIZONTAL_SPACING);
267         progressLabel.setLayoutData(progressData);
268
269         mouseListener = new MouseAdapter() {
270             /*
271              * (non-Javadoc)
272              *
273              * @see org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
274              */

275             public void mouseDown(MouseEvent e) {
276                 if (indexListener != null) {
277                     indexListener.select();
278                 }
279             }
280         };
281         addMouseListener(mouseListener);
282         jobImageLabel.addMouseListener(mouseListener);
283         progressLabel.addMouseListener(mouseListener);
284
285         setLayoutsForNoProgress();
286
287         refresh();
288     }
289
290     /**
291      * Set the layout of the widgets for the no progress case.
292      *
293      */

294     private void setLayoutsForNoProgress() {
295
296         FormData buttonData = new FormData();
297         buttonData.top = new FormAttachment(progressLabel, 0, SWT.TOP);
298         buttonData.right = new FormAttachment(100,
299                 IDialogConstants.HORIZONTAL_SPACING * -1);
300
301         actionBar.setLayoutData(buttonData);
302         if (taskEntries.size() > 0) {
303             FormData linkData = new FormData();
304             linkData.top = new FormAttachment(progressLabel,
305                     IDialogConstants.VERTICAL_SPACING);
306             linkData.left = new FormAttachment(progressLabel, 0, SWT.LEFT);
307             linkData.right = new FormAttachment(actionBar, 0, SWT.LEFT);
308             ((Link) taskEntries.get(0)).setLayoutData(linkData);
309
310         }
311     }
312
313     /**
314      * Cancel or remove the reciever.
315      *
316      */

317     protected void cancelOrRemove() {
318
319         if (FinishedJobs.getInstance().isFinished(info)) {
320             FinishedJobs.getInstance().remove(info);
321         } else {
322             info.cancel();
323         }
324
325     }
326
327     /**
328      * Get the image for the info.
329      *
330      * @return Image
331      */

332     private Image getInfoImage() {
333
334         if (!info.isJobInfo()) {
335             return JFaceResources.getImage(DEFAULT_JOB_KEY);
336         }
337
338         JobInfo jobInfo = (JobInfo) info;
339
340         ImageDescriptor descriptor = null;
341         Object JavaDoc property = jobInfo.getJob().getProperty(
342                 IProgressConstants.ICON_PROPERTY);
343
344         if (property instanceof ImageDescriptor) {
345             descriptor = (ImageDescriptor) property;
346         } else if (property instanceof URL JavaDoc) {
347             descriptor = ImageDescriptor.createFromURL((URL JavaDoc) property);
348         }
349
350         Image image = null;
351         if (descriptor == null) {
352             image = ProgressManager.getInstance().getIconFor(jobInfo.getJob());
353         } else {
354             image = JFaceResources.getResources().createImageWithDefault(
355                     descriptor);
356         }
357
358         if (image == null)
359             image = jobInfo.getDisplayImage();
360
361         return image;
362     }
363
364     /**
365      * Get the main title for the receiver.
366      *
367      * @return String
368      */

369     private String JavaDoc getMainTitle() {
370         if (info.isJobInfo()) {
371             return getJobNameAndStatus();
372         }
373         if (info.hasChildren()) {
374             return ((GroupInfo) info).getTaskName();
375         }
376         return info.getDisplayString();
377
378     }
379
380     /**
381      * Get the name and status for the main label.
382      *
383      * @return String
384      */

385     protected String JavaDoc getJobNameAndStatus() {
386
387         JobInfo jobInfo = (JobInfo) info;
388         Job job = jobInfo.getJob();
389
390         String JavaDoc name = job.getName();
391
392         if (job.isSystem()) {
393             name = NLS.bind(ProgressMessages.JobInfo_System, name);
394         }
395
396         if (jobInfo.isCanceled()) {
397             return NLS.bind(ProgressMessages.JobInfo_Cancelled, name);
398         }
399
400         if (jobInfo.isBlocked()) {
401             IStatus blockedStatus = jobInfo.getBlockedStatus();
402             return NLS.bind(ProgressMessages.JobInfo_Blocked, name,
403                     blockedStatus.getMessage());
404         }
405
406         switch (job.getState()) {
407         case Job.RUNNING:
408             return name;
409         case Job.SLEEPING: {
410             return NLS.bind(ProgressMessages.JobInfo_Sleeping, name);
411
412         }
413         case Job.NONE: // Only happens for kept jobs
414
return getJobInfoFinishedString(job, true);
415         default:
416             return NLS.bind(ProgressMessages.JobInfo_Waiting, name);
417         }
418     }
419
420     /**
421      * Return the finished String for a job.
422      *
423      * @param job
424      * the completed Job
425      * @param withTime
426      * @return String
427      */

428     String JavaDoc getJobInfoFinishedString(Job job, boolean withTime) {
429         String JavaDoc time = null;
430         if (withTime) {
431             time = getTimeString();
432         }
433         if (time != null) {
434             return NLS.bind(ProgressMessages.JobInfo_FinishedAt, job.getName(),
435                     time);
436         }
437         return NLS.bind(ProgressMessages.JobInfo_Finished, job.getName());
438     }
439
440     /**
441      * Get the time string the finished job
442      *
443      * @return String or <code>null</code> if this is not one of the finished
444      * jobs.
445      */

446     private String JavaDoc getTimeString() {
447         Date JavaDoc date = FinishedJobs.getInstance().getFinishDate(info);
448         if (date != null) {
449             return DateFormat.getTimeInstance(DateFormat.SHORT).format(date);
450         }
451         return null;
452     }
453
454     /**
455      * Refresh the contents of the receiver.
456      *
457      */

458     void refresh() {
459
460         // Don't refresh if not visible
461
if (isDisposed() || !isShowing)
462             return;
463
464         progressLabel.setText(getMainTitle());
465         int percentDone = getPercentDone();
466
467         JobInfo[] infos = getJobInfos();
468         if (isRunning()) {
469             if (progressBar == null) {
470                 if (percentDone == IProgressMonitor.UNKNOWN) {
471                     // Only do it if there is an indeterminate task
472
// There may be no task so we don't want to create it
473
// until we know for sure
474
for (int i = 0; i < infos.length; i++) {
475                         if (infos[i].hasTaskInfo()
476                                 && infos[i].getTaskInfo().totalWork == IProgressMonitor.UNKNOWN) {
477                             createProgressBar(SWT.INDETERMINATE);
478                             break;
479                         }
480                     }
481                 } else {
482                     createProgressBar(SWT.NONE);
483                     progressBar.setMinimum(0);
484                     progressBar.setMaximum(100);
485                 }
486             }
487
488             // Protect against bad counters
489
if (percentDone >= 0 && percentDone <= 100
490                     && percentDone != progressBar.getSelection()) {
491                 progressBar.setSelection(percentDone);
492             }
493         }
494
495         else if (isCompleted()) {
496
497             if (progressBar != null) {
498                 progressBar.dispose();
499                 progressBar = null;
500             }
501             setLayoutsForNoProgress();
502
503         }
504
505         for (int i = 0; i < infos.length; i++) {
506             JobInfo jobInfo = infos[i];
507             if (jobInfo.hasTaskInfo()) {
508
509                 String JavaDoc taskString = jobInfo.getTaskInfo().getTaskName();
510                 String JavaDoc subTaskString = null;
511                 Object JavaDoc[] jobChildren = jobInfo.getChildren();
512                 if (jobChildren.length > 0) {
513                     subTaskString = ((JobTreeElement) jobChildren[0])
514                             .getDisplayString();
515                 }
516
517                 if (subTaskString != null) {
518                     if (taskString == null) {
519                         taskString = subTaskString;
520                     } else {
521                         taskString = NLS.bind(
522                                 ProgressMessages.JobInfo_DoneNoProgressMessage,
523                                 taskString, subTaskString);
524                     }
525                 }
526                 if (taskString != null) {
527                     setLinkText(infos[i].getJob(), taskString, i);
528                 }
529             } else {// Check for the finished job state
530
Job job = jobInfo.getJob();
531                 if (job.getResult() != null) {
532                     IStatus result = job.getResult();
533                     String JavaDoc message = EMPTY_STRING;
534                     if (result != null) {
535                         message = result.getMessage();
536                     }
537                     setLinkText(job, message, i);
538                 }
539             }
540             setColor(currentIndex);
541         }
542
543         // Remove completed tasks
544
if (infos.length < taskEntries.size()) {
545             for (int i = infos.length; i < taskEntries.size(); i++) {
546                 ((Link) taskEntries.get(i)).dispose();
547
548             }
549             if (infos.length > 1)
550                 taskEntries = taskEntries.subList(0, infos.length - 1);
551             else
552                 taskEntries.clear();
553         }
554
555         updateToolBarValues();
556     }
557
558     /**
559      * Return whether or not the receiver is a completed job.
560      *
561      * @return boolean <code>true</code> if the state is Job#NONE.
562      */

563     private boolean isCompleted() {
564
565         JobInfo[] infos = getJobInfos();
566         for (int i = 0; i < infos.length; i++) {
567             if (infos[i].getJob().getState() != Job.NONE) {
568                 return false;
569             }
570         }
571         // Only completed if there are any jobs
572
return infos.length > 0;
573     }
574
575     /**
576      * Return the job infos in the receiver.
577      *
578      * @return JobInfo[]
579      */

580     private JobInfo[] getJobInfos() {
581         if (info.isJobInfo()) {
582             return new JobInfo[] { (JobInfo) info };
583         }
584         Object JavaDoc[] children = info.getChildren();
585         JobInfo[] infos = new JobInfo[children.length];
586         System.arraycopy(children, 0, infos, 0, children.length);
587         return infos;
588     }
589
590     /**
591      * Return whether or not the receiver is being displayed as running.
592      *
593      * @return boolean
594      */

595     private boolean isRunning() {
596         JobInfo[] infos = getJobInfos();
597         for (int i = 0; i < infos.length; i++) {
598             int state = infos[i].getJob().getState();
599             if (state == Job.RUNNING) {
600                 continue;
601             }
602             return false;
603         }
604         // Only completed if there are any jobs
605
return infos.length > 0;
606     }
607
608     /**
609      * Get the current percent done.
610      *
611      * @return int
612      */

613     private int getPercentDone() {
614         if (info.isJobInfo()) {
615             return ((JobInfo) info).getPercentDone();
616         }
617
618         if (info.hasChildren()) {
619             Object JavaDoc[] roots = ((GroupInfo) info).getChildren();
620             if (roots.length == 1 && roots[0] instanceof JobTreeElement) {
621                 TaskInfo ti = ((JobInfo) roots[0]).getTaskInfo();
622                 if (ti != null) {
623                     return ti.getPercentDone();
624                 }
625             }
626             return ((GroupInfo) info).getPercentDone();
627         }
628         return 0;
629     }
630
631     /**
632      * Set the images in the toolbar based on whether the receiver is finished
633      * or not. Also update tooltips if required.
634      *
635      */

636     private void updateToolBarValues() {
637         if (isCompleted()) {
638             actionButton.setImage(JFaceResources
639                     .getImage(CLEAR_FINISHED_JOB_KEY));
640             actionButton.setDisabledImage(JFaceResources
641                     .getImage(DISABLED_CLEAR_FINISHED_JOB_KEY));
642             actionButton
643                     .setToolTipText(ProgressMessages.NewProgressView_ClearJobToolTip);
644         } else {
645             actionButton.setImage(JFaceResources.getImage(STOP_IMAGE_KEY));
646             actionButton.setDisabledImage(JFaceResources
647                     .getImage(DISABLED_STOP_IMAGE_KEY));
648
649         }
650         JobInfo[] infos = getJobInfos();
651
652         for (int i = 0; i < infos.length; i++) {
653             // Only disable if there is an unresponsive operation
654
if (infos[i].isCanceled() && !isCompleted()) {
655                 actionButton.setEnabled(false);
656                 return;
657             }
658         }
659         actionButton.setEnabled(true);
660     }
661
662     /**
663      * Create the progress bar and apply any style bits from style.
664      *
665      * @param style
666      */

667     void createProgressBar(int style) {
668
669         FormData buttonData = new FormData();
670         buttonData.top = new FormAttachment(progressLabel, 0);
671         buttonData.right = new FormAttachment(100,
672                 IDialogConstants.HORIZONTAL_SPACING * -1);
673
674         actionBar.setLayoutData(buttonData);
675
676         progressBar = new ProgressBar(this, SWT.HORIZONTAL | style);
677         FormData barData = new FormData();
678         barData.top = new FormAttachment(actionBar,
679                 IDialogConstants.VERTICAL_SPACING, SWT.TOP);
680         barData.left = new FormAttachment(progressLabel, 0, SWT.LEFT);
681         barData.right = new FormAttachment(actionBar,
682                 IDialogConstants.HORIZONTAL_SPACING * -1);
683         barData.height = MAX_PROGRESS_HEIGHT;
684         barData.width = 0;// default is too large
685
progressBar.setLayoutData(barData);
686
687         if (taskEntries.size() > 0) {
688             // Reattach the link label if there is one
689
FormData linkData = new FormData();
690             linkData.top = new FormAttachment(progressBar,
691                     IDialogConstants.VERTICAL_SPACING);
692             linkData.left = new FormAttachment(
693                     IDialogConstants.HORIZONTAL_SPACING);
694             linkData.right = new FormAttachment(100);
695
696             ((Link) taskEntries.get(0)).setLayoutData(linkData);
697         }
698     }
699
700     /**
701      * Set the text of the link to the taskString.
702      *
703      * @param taskString
704      */

705     void setLinkText(Job linkJob, String JavaDoc taskString, int index) {
706
707         Link link;
708         if (index >= taskEntries.size()) {// Is it new?
709
link = new Link(this, SWT.NONE);
710
711             FormData linkData = new FormData();
712             if (index == 0 || taskEntries.size() == 0) {
713                 Control top = progressBar;
714                 if (top == null) {
715                     top = progressLabel;
716                 }
717                 linkData.top = new FormAttachment(top,
718                         IDialogConstants.VERTICAL_SPACING);
719                 linkData.left = new FormAttachment(top, 0, SWT.LEFT);
720             } else {
721                 Link previous = (Link) taskEntries.get(index - 1);
722                 linkData.top = new FormAttachment(previous,
723                         IDialogConstants.VERTICAL_SPACING);
724                 linkData.left = new FormAttachment(previous, 0, SWT.LEFT);
725             }
726
727             linkData.right = new FormAttachment(progressBar, 0, SWT.RIGHT);
728             link.setLayoutData(linkData);
729
730             final Link finalLink = link;
731
732             link.addSelectionListener(new SelectionAdapter() {
733                 /*
734                  * (non-Javadoc)
735                  *
736                  * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
737                  */

738                 public void widgetSelected(SelectionEvent e) {
739                     ((IAction) finalLink.getData(ACTION_KEY)).run();
740                 }
741             });
742
743             link.addListener(SWT.Resize, new Listener() {
744                 /*
745                  * (non-Javadoc)
746                  *
747                  * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
748                  */

749                 public void handleEvent(Event event) {
750                     updateText((String JavaDoc) finalLink.getData(TEXT_KEY), finalLink);
751
752                 }
753             });
754             taskEntries.add(link);
755         } else {
756             link = (Link) taskEntries.get(index);
757         }
758
759         link.setToolTipText(taskString);
760         link.setData(TEXT_KEY, taskString);
761
762         // check for action property
763
Object JavaDoc property = linkJob
764                 .getProperty(IProgressConstants.ACTION_PROPERTY);
765         if (property instanceof IAction) {
766             link.setData(ACTION_KEY, property);
767         }
768
769         updateText(taskString, link);
770
771     }
772
773     /**
774      * Update the text in the link
775      *
776      * @param taskString
777      * @param link
778      */

779     private void updateText(String JavaDoc taskString, Link link) {
780         taskString = Dialog.shortenText(taskString, link);
781
782         // Put in a hyperlink if there is an action
783
link.setText(link.getData(ACTION_KEY) == null ? taskString : NLS.bind(
784                 "<a>{0}</a>", taskString));//$NON-NLS-1$
785
}
786
787     /**
788      * Set the color base on the index
789      *
790      * @param i
791      */

792     public void setColor(int i) {
793         currentIndex = i;
794
795         if (selected) {
796             setAllBackgrounds(getDisplay().getSystemColor(
797                     SWT.COLOR_LIST_SELECTION));
798             setAllForegrounds(getDisplay().getSystemColor(
799                     SWT.COLOR_LIST_SELECTION_TEXT));
800             return;
801         }
802
803         if (i % 2 == 0) {
804             setAllBackgrounds(JFaceResources.getColorRegistry().get(
805                     DARK_COLOR_KEY));
806         } else {
807             setAllBackgrounds(getDisplay().getSystemColor(
808                     SWT.COLOR_LIST_BACKGROUND));
809         }
810         setAllForegrounds(getDisplay()
811                 .getSystemColor(SWT.COLOR_LIST_FOREGROUND));
812     }
813
814     /**
815      * Set the foreground of all widgets to the supplied color.
816      *
817      * @param color
818      */

819     private void setAllForegrounds(Color color) {
820         setForeground(color);
821         progressLabel.setForeground(color);
822
823         Iterator JavaDoc taskEntryIterator = taskEntries.iterator();
824         while (taskEntryIterator.hasNext()) {
825             ((Link) taskEntryIterator.next()).setForeground(color);
826         }
827
828     }
829
830     /**
831      * Set the background of all widgets to the supplied color.
832      *
833      * @param color
834      */

835     private void setAllBackgrounds(Color color) {
836         setBackground(color);
837         progressLabel.setBackground(color);
838         actionBar.setBackground(color);
839         jobImageLabel.setBackground(color);
840
841         Iterator JavaDoc taskEntryIterator = taskEntries.iterator();
842         while (taskEntryIterator.hasNext()) {
843             ((Link) taskEntryIterator.next()).setBackground(color);
844         }
845
846     }
847
848     /**
849      * Set the focus to the button.
850      *
851      */

852     void setButtonFocus() {
853         actionBar.setFocus();
854     }
855
856     /**
857      * Set the selection colors.
858      *
859      * @param select
860      * boolean that indicates whether or not to show selection.
861      */

862     void selectWidgets(boolean select) {
863         if (select) {
864             setButtonFocus();
865         }
866         selected = select;
867         setColor(currentIndex);
868     }
869
870     /**
871      * Set the listener for index changes.
872      *
873      * @param indexListener
874      */

875     void setIndexListener(IndexListener indexListener) {
876         this.indexListener = indexListener;
877     }
878
879     /**
880      * Return whether or not the receiver is selected.
881      *
882      * @return boolean
883      */

884     boolean isSelected() {
885         return selected;
886     }
887
888     /**
889      * Set whether or not the receiver is being displayed based on the top and
890      * bottom of the currently visible area.
891      *
892      * @param top
893      * @param bottom
894      */

895     void setDisplayed(int top, int bottom) {
896         int itemTop = getLocation().y;
897         int itemBottom = itemTop + getBounds().height;
898         setDisplayed(itemTop <= bottom && itemBottom > top);
899
900     }
901
902     /**
903      * Set whether or not the receiver is being displayed
904      *
905      * @param displayed
906      */

907     private void setDisplayed(boolean displayed) {
908         // See if this element has been turned off
909
boolean refresh = !isShowing && displayed;
910         isShowing = displayed;
911         if (refresh)
912             refresh();
913     }
914 }
915
Popular Tags