KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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  *******************************************************************************/

11 package org.eclipse.ui.internal.progress;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.jface.dialogs.IDialogConstants;
17 import org.eclipse.jface.resource.JFaceResources;
18 import org.eclipse.jface.viewers.ViewerComparator;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.custom.ScrolledComposite;
21 import org.eclipse.swt.events.ControlEvent;
22 import org.eclipse.swt.events.ControlListener;
23 import org.eclipse.swt.events.FocusAdapter;
24 import org.eclipse.swt.events.FocusEvent;
25 import org.eclipse.swt.graphics.Point;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Text;
31 import org.eclipse.swt.widgets.Widget;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
34
35 /**
36  * The DetailedProgressViewer is a viewer that shows the details of all in
37  * progress job or jobs that are finished awaiting user input.
38  *
39  * @since 3.2
40  *
41  */

42 public class DetailedProgressViewer extends AbstractProgressViewer {
43
44     Composite control;
45
46     private ScrolledComposite scrolled;
47
48     private Composite noEntryArea;
49
50     /**
51      * Create a new instance of the receiver with a control that is a child of
52      * parent with style style.
53      *
54      * @param parent
55      * @param style
56      */

57     public DetailedProgressViewer(Composite parent, int style) {
58         scrolled = new ScrolledComposite(parent, SWT.V_SCROLL | style);
59         int height = JFaceResources.getDefaultFont().getFontData()[0]
60                 .getHeight();
61         scrolled.getVerticalBar().setIncrement(height * 2);
62         scrolled.setExpandHorizontal(true);
63         scrolled.setExpandVertical(true);
64
65         control = new Composite(scrolled, SWT.NONE);
66         GridLayout layout = new GridLayout();
67         layout.marginHeight = 0;
68         layout.marginWidth = 0;
69         control.setLayout(layout);
70         control.setBackground(parent.getDisplay().getSystemColor(
71                 SWT.COLOR_LIST_BACKGROUND));
72
73         control.addFocusListener(new FocusAdapter() {
74
75             private boolean settingFocus = false;
76
77             /*
78              * (non-Javadoc)
79              *
80              * @see org.eclipse.swt.events.FocusAdapter#focusGained(org.eclipse.swt.events.FocusEvent)
81              */

82             public void focusGained(FocusEvent e) {
83                 if (!settingFocus) {
84                     //Prevent new focus events as a result this update occurring
85
settingFocus = true;
86                     setFocus();
87                     settingFocus = false;
88                 }
89             }
90         });
91
92         control.addControlListener(new ControlListener() {
93             /*
94              * (non-Javadoc)
95              *
96              * @see org.eclipse.swt.events.ControlListener#controlMoved(org.eclipse.swt.events.ControlEvent)
97              */

98             public void controlMoved(ControlEvent e) {
99                 updateVisibleItems();
100
101             }
102
103             /*
104              * (non-Javadoc)
105              *
106              * @see org.eclipse.swt.events.ControlListener#controlResized(org.eclipse.swt.events.ControlEvent)
107              */

108             public void controlResized(ControlEvent e) {
109                 updateVisibleItems();
110             }
111         });
112
113         PlatformUI.getWorkbench().getHelpSystem().setHelp(control,
114                 IWorkbenchHelpContextIds.RESPONSIVE_UI);
115
116         scrolled.setContent(control);
117         hookControl(control);
118
119         noEntryArea = new Composite(scrolled, SWT.NONE);
120         noEntryArea.setLayout(new GridLayout());
121
122         Text noEntryLabel = new Text(noEntryArea, SWT.SINGLE);
123         noEntryLabel.setText(ProgressMessages.ProgressView_NoOperations);
124         noEntryLabel.setBackground(noEntryArea.getDisplay().getSystemColor(
125                 SWT.COLOR_WIDGET_BACKGROUND));
126         GridData textData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
127         noEntryLabel.setLayoutData(textData);
128         noEntryLabel.setEditable(false);
129
130         PlatformUI.getWorkbench().getHelpSystem().setHelp(noEntryLabel,
131                 IWorkbenchHelpContextIds.RESPONSIVE_UI);
132
133     }
134
135     /*
136      * (non-Javadoc)
137      *
138      * @see org.eclipse.ui.internal.progress.AbstractProgressViewer#add(java.lang.Object[])
139      */

140     public void add(Object JavaDoc[] elements) {
141         ViewerComparator sorter = getComparator();
142         ArrayList JavaDoc newItems = new ArrayList JavaDoc(control.getChildren().length
143                 + elements.length);
144
145         Control[] existingChildren = control.getChildren();
146         for (int i = 0; i < existingChildren.length; i++) {
147             newItems.add(existingChildren[i].getData());
148         }
149
150         for (int i = 0; i < elements.length; i++) {
151             newItems.add(elements[i]);
152         }
153
154         JobTreeElement[] infos = new JobTreeElement[newItems.size()];
155         newItems.toArray(infos);
156
157         if (sorter != null) {
158             sorter.sort(this, infos);
159         }
160
161         // Update with the new elements to prevent flash
162
for (int i = 0; i < existingChildren.length; i++) {
163             ((ProgressInfoItem) existingChildren[i]).dispose();
164         }
165
166         for (int i = 0; i < newItems.size(); i++) {
167             ProgressInfoItem item = createNewItem(infos[i]);
168             item.setColor(i);
169         }
170
171         control.layout(true);
172         updateForShowingProgress();
173     }
174
175     /**
176      * Update for the progress being displayed.
177      */

178     private void updateForShowingProgress() {
179         if (control.getChildren().length > 0) {
180             scrolled.setContent(control);
181         } else {
182             scrolled.setContent(noEntryArea);
183         }
184     }
185
186     /**
187      * Create a new item for info.
188      *
189      * @param info
190      * @return ProgressInfoItem
191      */

192     private ProgressInfoItem createNewItem(JobTreeElement info) {
193         final ProgressInfoItem item = new ProgressInfoItem(control, SWT.NONE,
194                 info);
195
196         item.setIndexListener(new ProgressInfoItem.IndexListener() {
197             /*
198              * (non-Javadoc)
199              *
200              * @see org.eclipse.ui.internal.progress.ProgressInfoItem.IndexListener#selectNext()
201              */

202             public void selectNext() {
203                 DetailedProgressViewer.this.selectNext(item);
204
205             }
206
207             /*
208              * (non-Javadoc)
209              *
210              * @see org.eclipse.ui.internal.progress.ProgressInfoItem.IndexListener#selectPrevious()
211              */

212             public void selectPrevious() {
213                 DetailedProgressViewer.this.selectPrevious(item);
214
215             }
216
217             /*
218              * (non-Javadoc)
219              *
220              * @see org.eclipse.ui.internal.progress.ProgressInfoItem.IndexListener#select()
221              */

222             public void select() {
223
224                 Control[] children = control.getChildren();
225                 for (int i = 0; i < children.length; i++) {
226                     ProgressInfoItem child = (ProgressInfoItem) children[i];
227                     if (!item.equals(child)) {
228                         child.selectWidgets(false);
229                     }
230                 }
231                 item.selectWidgets(true);
232
233             }
234         });
235
236         // Refresh to populate with the current tasks
237
item.refresh();
238         return item;
239     }
240
241     /**
242      * Select the previous item in the receiver.
243      *
244      * @param item
245      */

246     protected void selectPrevious(ProgressInfoItem item) {
247         Control[] children = control.getChildren();
248         for (int i = 0; i < children.length; i++) {
249             ProgressInfoItem child = (ProgressInfoItem) children[i];
250             if (item.equals(child)) {
251                 ProgressInfoItem previous;
252                 if (i == 0) {
253                     previous = (ProgressInfoItem) children[children.length - 1];
254                 } else {
255                     previous = (ProgressInfoItem) children[i - 1];
256                 }
257
258                 item.selectWidgets(false);
259                 previous.selectWidgets(true);
260                 return;
261             }
262         }
263     }
264
265     /**
266      * Select the next item in the receiver.
267      *
268      * @param item
269      */

270     protected void selectNext(ProgressInfoItem item) {
271         Control[] children = control.getChildren();
272         for (int i = 0; i < children.length; i++) {
273             ProgressInfoItem child = (ProgressInfoItem) children[i];
274             if (item.equals(child)) {
275                 ProgressInfoItem next;
276                 if (i == children.length - 1) {
277                     next = (ProgressInfoItem) children[0];
278                 } else {
279                     next = (ProgressInfoItem) children[i + 1];
280                 }
281                 item.selectWidgets(false);
282                 next.selectWidgets(true);
283
284                 return;
285             }
286         }
287
288     }
289
290     /*
291      * (non-Javadoc)
292      *
293      * @see org.eclipse.jface.viewers.StructuredViewer#doFindInputItem(java.lang.Object)
294      */

295     protected Widget doFindInputItem(Object JavaDoc element) {
296         return null;
297     }
298
299     /*
300      * (non-Javadoc)
301      *
302      * @see org.eclipse.jface.viewers.StructuredViewer#doFindItem(java.lang.Object)
303      */

304     protected Widget doFindItem(Object JavaDoc element) {
305         Control[] existingChildren = control.getChildren();
306         for (int i = 0; i < existingChildren.length; i++) {
307             if (existingChildren[i].isDisposed()
308                     || existingChildren[i].getData() == null) {
309                 continue;
310             }
311             if (existingChildren[i].getData().equals(element)) {
312                 return existingChildren[i];
313             }
314         }
315         return null;
316     }
317
318     /*
319      * (non-Javadoc)
320      *
321      * @see org.eclipse.jface.viewers.StructuredViewer#doUpdateItem(org.eclipse.swt.widgets.Widget,
322      * java.lang.Object, boolean)
323      */

324     protected void doUpdateItem(Widget item, Object JavaDoc element, boolean fullMap) {
325         if (usingElementMap()) {
326             unmapElement(item);
327         }
328         item.dispose();
329         add(new Object JavaDoc[] { element });
330     }
331
332     /*
333      * (non-Javadoc)
334      *
335      * @see org.eclipse.jface.viewers.Viewer#getControl()
336      */

337     public Control getControl() {
338         return scrolled;
339     }
340
341     /*
342      * (non-Javadoc)
343      *
344      * @see org.eclipse.jface.viewers.StructuredViewer#getSelectionFromWidget()
345      */

346     protected List JavaDoc getSelectionFromWidget() {
347         return new ArrayList JavaDoc(0);
348     }
349
350     /*
351      * (non-Javadoc)
352      *
353      * @see org.eclipse.jface.viewers.Viewer#inputChanged(java.lang.Object,
354      * java.lang.Object)
355      */

356     protected void inputChanged(Object JavaDoc input, Object JavaDoc oldInput) {
357         super.inputChanged(input, oldInput);
358         refreshAll();
359         updateForShowingProgress();
360     }
361
362     /*
363      * (non-Javadoc)
364      *
365      * @see org.eclipse.jface.viewers.StructuredViewer#internalRefresh(java.lang.Object)
366      */

367     protected void internalRefresh(Object JavaDoc element) {
368         if (element == null) {
369             return;
370         }
371
372         if (element.equals(getRoot())) {
373             refreshAll();
374             return;
375         }
376         Widget widget = findItem(element);
377         if (widget == null) {
378             add(new Object JavaDoc[] { element });
379             return;
380         }
381         ((ProgressInfoItem) widget).refresh();
382
383         // Update the minimum size
384
Point size = control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
385         size.x += IDialogConstants.HORIZONTAL_SPACING;
386         size.y += IDialogConstants.VERTICAL_SPACING;
387
388         scrolled.setMinSize(size);
389     }
390
391     /*
392      * (non-Javadoc)
393      *
394      * @see org.eclipse.ui.internal.progress.AbstractProgressViewer#remove(java.lang.Object[])
395      */

396     public void remove(Object JavaDoc[] elements) {
397
398         for (int i = 0; i < elements.length; i++) {
399
400             // Make sure we are not keeping this one
401
if (((JobTreeElement) elements[i]).isJobInfo()
402                     && FinishedJobs.getInstance().isFinished(
403                             (JobInfo) elements[i])) {
404                 Widget item = doFindItem(elements[i]);
405                 if (item != null) {
406                     ((ProgressInfoItem) item).refresh();
407                 }
408
409             } else {
410                 Widget item = doFindItem(elements[i]);
411                 if (item != null) {
412                     unmapElement(elements[i]);
413                     item.dispose();
414                 }
415             }
416         }
417
418         Control[] existingChildren = control.getChildren();
419         for (int i = 0; i < existingChildren.length; i++) {
420             ProgressInfoItem item = (ProgressInfoItem) existingChildren[i];
421             item.setColor(i);
422         }
423         control.layout(true);
424         updateForShowingProgress();
425     }
426
427     /*
428      * (non-Javadoc)
429      *
430      * @see org.eclipse.jface.viewers.StructuredViewer#reveal(java.lang.Object)
431      */

432     public void reveal(Object JavaDoc element) {
433
434     }
435
436     /*
437      * (non-Javadoc)
438      *
439      * @see org.eclipse.jface.viewers.StructuredViewer#setSelectionToWidget(java.util.List,
440      * boolean)
441      */

442     protected void setSelectionToWidget(List JavaDoc l, boolean reveal) {
443
444     }
445
446     /**
447      * Cancel the current selection
448      *
449      */

450     public void cancelSelection() {
451
452     }
453
454     /**
455      * Set focus on the current selection.
456      *
457      */

458     public void setFocus() {
459         Control[] children = control.getChildren();
460         if (children.length > 0) {
461             for (int i = 0; i < children.length; i++) {
462                 ProgressInfoItem item = (ProgressInfoItem) children[i];
463                 item.setButtonFocus();
464                 return;
465             }
466         } else
467             noEntryArea.setFocus();
468     }
469
470     /**
471      * Refresh everything as the root is being refreshed.
472      */

473     private void refreshAll() {
474
475         Object JavaDoc[] infos = getSortedChildren(getRoot());
476         Control[] existingChildren = control.getChildren();
477
478         for (int i = 0; i < existingChildren.length; i++) {
479             existingChildren[i].dispose();
480
481         }
482         // Create new ones if required
483
for (int i = 0; i < infos.length; i++) {
484             ProgressInfoItem item = createNewItem((JobTreeElement) infos[i]);
485             item.setColor(i);
486         }
487
488         control.layout(true);
489         updateForShowingProgress();
490
491     }
492
493     /**
494      * Set the virtual items to be visible or not depending on the displayed
495      * area.
496      */

497     private void updateVisibleItems() {
498         Control[] children = control.getChildren();
499         int top = scrolled.getOrigin().y;
500         int bottom = top + scrolled.getParent().getBounds().height;
501         for (int i = 0; i < children.length; i++) {
502             ProgressInfoItem item = (ProgressInfoItem) children[i];
503             item.setDisplayed(top, bottom);
504
505         }
506     }
507
508 }
509
Popular Tags