KickJava   Java API By Example, From Geeks To Geeks.

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


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

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 VirtualProgressViewer is a viewer that shows the details of all in
37  * progress job or jobs that are finished awaiting user input. It only updates
38  * those items that are currently visible.
39  *
40  * @since 3.3
41  *
42  */

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

58     public VirtualProgressViewer(Composite parent, int style) {
59         scrolled = new ScrolledComposite(parent, SWT.V_SCROLL | style);
60         int height = JFaceResources.getDefaultFont().getFontData()[0]
61                 .getHeight();
62         scrolled.getVerticalBar().setIncrement(height * 2);
63         scrolled.setExpandHorizontal(true);
64         scrolled.setExpandVertical(true);
65
66         control = new Composite(scrolled, SWT.NONE);
67
68         control.addControlListener(new ControlListener() {
69             /*
70              * (non-Javadoc)
71              *
72              * @see org.eclipse.swt.events.ControlListener#controlMoved(org.eclipse.swt.events.ControlEvent)
73              */

74             public void controlMoved(ControlEvent e) {
75                 updateVisibleItems();
76
77             }
78
79             /*
80              * (non-Javadoc)
81              *
82              * @see org.eclipse.swt.events.ControlListener#controlResized(org.eclipse.swt.events.ControlEvent)
83              */

84             public void controlResized(ControlEvent e) {
85                 updateVisibleItems();
86
87             }
88         });
89         GridLayout layout = new GridLayout();
90         layout.marginHeight = 0;
91         layout.marginWidth = 0;
92         control.setLayout(layout);
93         control.setBackground(parent.getDisplay().getSystemColor(
94                 SWT.COLOR_LIST_BACKGROUND));
95
96         control.addFocusListener(new FocusAdapter() {
97             /*
98              * (non-Javadoc)
99              *
100              * @see org.eclipse.swt.events.FocusAdapter#focusGained(org.eclipse.swt.events.FocusEvent)
101              */

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

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

172     private void updateForShowingProgress() {
173         if (control.getChildren().length > 0) {
174             scrolled.setContent(control);
175         } else {
176             scrolled.setContent(noEntryArea);
177         }
178     }
179
180     /**
181      * Create a new item for info.
182      *
183      * @param info
184      * @return VirtualInfoItem
185      */

186     private VirtualInfoItem createNewItem(JobTreeElement info) {
187         final VirtualInfoItem item = new VirtualInfoItem(control, SWT.NONE,
188                 info);
189
190         item.setIndexListener(new VirtualInfoItem.IndexListener() {
191             /*
192              * (non-Javadoc)
193              *
194              * @see org.eclipse.ui.internal.progress.VirtualInfoItem.IndexListener#selectNext()
195              */

196             public void selectNext() {
197                 VirtualProgressViewer.this.selectNext(item);
198
199             }
200
201             /*
202              * (non-Javadoc)
203              *
204              * @see org.eclipse.ui.internal.progress.VirtualInfoItem.IndexListener#selectPrevious()
205              */

206             public void selectPrevious() {
207                 VirtualProgressViewer.this.selectPrevious(item);
208
209             }
210
211             /*
212              * (non-Javadoc)
213              *
214              * @see org.eclipse.ui.internal.progress.VirtualInfoItem.IndexListener#select()
215              */

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

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

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

289     protected Widget doFindInputItem(Object JavaDoc element) {
290         return null;
291     }
292
293     /*
294      * (non-Javadoc)
295      *
296      * @see org.eclipse.jface.viewers.StructuredViewer#doFindItem(java.lang.Object)
297      */

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

318     protected void doUpdateItem(Widget item, Object JavaDoc element, boolean fullMap) {
319         if (usingElementMap()) {
320             unmapElement(item);
321         }
322         item.dispose();
323         add(new Object JavaDoc[] { element });
324     }
325
326     /*
327      * (non-Javadoc)
328      *
329      * @see org.eclipse.jface.viewers.Viewer#getControl()
330      */

331     public Control getControl() {
332         return scrolled;
333     }
334
335     /*
336      * (non-Javadoc)
337      *
338      * @see org.eclipse.jface.viewers.StructuredViewer#getSelectionFromWidget()
339      */

340     protected List JavaDoc getSelectionFromWidget() {
341         return new ArrayList JavaDoc(0);
342     }
343
344     /*
345      * (non-Javadoc)
346      *
347      * @see org.eclipse.jface.viewers.Viewer#inputChanged(java.lang.Object,
348      * java.lang.Object)
349      */

350     protected void inputChanged(Object JavaDoc input, Object JavaDoc oldInput) {
351         super.inputChanged(input, oldInput);
352         refreshAll();
353         updateForShowingProgress();
354     }
355
356     /*
357      * (non-Javadoc)
358      *
359      * @see org.eclipse.jface.viewers.StructuredViewer#internalRefresh(java.lang.Object)
360      */

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

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

426     public void reveal(Object JavaDoc element) {
427
428     }
429
430     /*
431      * (non-Javadoc)
432      *
433      * @see org.eclipse.jface.viewers.StructuredViewer#setSelectionToWidget(java.util.List,
434      * boolean)
435      */

436     protected void setSelectionToWidget(List JavaDoc l, boolean reveal) {
437
438     }
439
440     /**
441      * Cancel the current selection
442      *
443      */

444     public void cancelSelection() {
445
446     }
447
448     /**
449      * Set focus on the current selection.
450      *
451      */

452     public void setFocus() {
453         Control[] children = control.getChildren();
454         if (children.length > 0) {
455             for (int i = 0; i < children.length; i++) {
456                 VirtualInfoItem item = (VirtualInfoItem) children[i];
457                 item.setButtonFocus();
458                 return;
459             }
460         } else
461             noEntryArea.setFocus();
462     }
463
464     /**
465      * Refresh everything as the root is being refreshed.
466      */

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

491     private void updateVisibleItems() {
492         Control[] children = control.getChildren();
493         int top = scrolled.getOrigin().y;
494         int bottom = top + scrolled.getParent().getBounds().height;
495         for (int i = 0; i < children.length; i++) {
496             VirtualInfoItem item = (VirtualInfoItem) children[i];
497             item.setDisplayed(top, bottom);
498
499         }
500     }
501
502 }
503
Popular Tags