KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > viewers > AsynchronousTableViewer


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.debug.internal.ui.viewers;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.core.runtime.jobs.Job;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.jface.viewers.CellEditor;
23 import org.eclipse.jface.viewers.DoubleClickEvent;
24 import org.eclipse.jface.viewers.ICellModifier;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.viewers.OpenEvent;
28 import org.eclipse.jface.viewers.StructuredSelection;
29 import org.eclipse.jface.viewers.Viewer;
30 import org.eclipse.jface.viewers.ViewerSorter;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.custom.TableEditor;
33 import org.eclipse.swt.events.MouseAdapter;
34 import org.eclipse.swt.events.MouseEvent;
35 import org.eclipse.swt.graphics.Color;
36 import org.eclipse.swt.graphics.Font;
37 import org.eclipse.swt.graphics.FontData;
38 import org.eclipse.swt.graphics.Image;
39 import org.eclipse.swt.graphics.RGB;
40 import org.eclipse.swt.graphics.Rectangle;
41 import org.eclipse.swt.widgets.Composite;
42 import org.eclipse.swt.widgets.Control;
43 import org.eclipse.swt.widgets.Item;
44 import org.eclipse.swt.widgets.Listener;
45 import org.eclipse.swt.widgets.Table;
46 import org.eclipse.swt.widgets.TableItem;
47 import org.eclipse.swt.widgets.Widget;
48 import org.eclipse.ui.progress.WorkbenchJob;
49
50 /**
51  * @since 3.2
52  */

53 public class AsynchronousTableViewer extends AsynchronousViewer implements Listener {
54
55     private Table fTable;
56
57     private TableEditor fTableEditor;
58
59     private TableEditorImpl fTableEditorImpl;
60
61     public AsynchronousTableViewer(Composite parent) {
62         this(parent, SWT.VIRTUAL);
63     }
64
65     public AsynchronousTableViewer(Composite parent, int style) {
66         this(new Table(parent, style));
67     }
68
69     /**
70      * Table must be SWT.VIRTUAL. This is intentional. Labels will never be
71      * retrieved for non-visible items.
72      *
73      * @see SWT.VIRTUAL
74      * @param table
75      */

76     public AsynchronousTableViewer(Table table) {
77         Assert.isTrue((table.getStyle() & SWT.VIRTUAL) != 0);
78         fTable = table;
79         hookControl(fTable);
80         fTableEditor = new TableEditor(fTable);
81         fTableEditorImpl = createTableEditorImpl();
82     }
83
84     protected void hookControl(Control control) {
85         super.hookControl(control);
86         control.addMouseListener(new MouseAdapter() {
87             public void mouseDown(MouseEvent e) {
88                 fTableEditorImpl.handleMouseDown(e);
89             }
90         });
91     }
92
93     public synchronized void dispose() {
94         fTableEditor.dispose();
95         fTable.dispose();
96         super.dispose();
97     }
98
99     protected ISelection doAttemptSelectionToWidget(ISelection selection, boolean reveal) {
100         if (acceptsSelection(selection)) {
101             List JavaDoc list = ((IStructuredSelection) selection).toList();
102             if (list == null) {
103                 fTable.deselectAll();
104                 return StructuredSelection.EMPTY;
105             }
106
107             int[] indices = new int[list.size()];
108             ModelNode[] nodes = getModel().getRootNode().getChildrenNodes();
109             if (nodes != null) {
110                 int index = 0;
111     
112                 // I'm not sure if it would be faster to check TableItems first...
113
for (int i = 0; i < nodes.length; i++) {
114                     Object JavaDoc element = nodes[i].getElement();
115                     if (list.contains(element)) {
116                         indices[index] = i;
117                         index++;
118                     }
119                 }
120     
121                 fTable.setSelection(indices);
122                 if (reveal && indices.length > 0) {
123                     TableItem item = fTable.getItem(indices[0]);
124                     fTable.showItem(item);
125                 }
126             }
127         }
128         return StructuredSelection.EMPTY;
129     }
130
131     protected boolean acceptsSelection(ISelection selection) {
132         return selection instanceof IStructuredSelection;
133     }
134
135     protected ISelection getEmptySelection() {
136         return StructuredSelection.EMPTY;
137     }
138
139     protected Widget getParent(Widget widget) {
140         if (widget instanceof TableItem) {
141             return fTable;
142         }
143         return null;
144     }
145
146     protected List JavaDoc getSelectionFromWidget() {
147         TableItem[] selection = fTable.getSelection();
148         List JavaDoc datas = new ArrayList JavaDoc(selection.length);
149         for (int i = 0; i < selection.length; i++) {
150             datas.add(selection[i].getData());
151         }
152         return datas;
153     }
154
155     public Control getControl() {
156         return fTable;
157     }
158
159     public Table getTable() {
160         return (Table) getControl();
161     }
162
163     /* (non-Javadoc)
164      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.viewers.AsynchronousViewer#internalRefresh(org.eclipse.debug.internal.ui.viewers.model.provisional.viewers.ModelNode)
165      */

166     protected void internalRefresh(ModelNode node) {
167         super.internalRefresh(node);
168         if (node.getElement().equals(getInput())) {
169             updateChildren(node);
170         }
171     }
172
173     protected void restoreLabels(Item item) {
174         TableItem tableItem = (TableItem) item;
175         String JavaDoc[] values = (String JavaDoc[]) tableItem.getData(OLD_LABEL);
176         Image[] images = (Image[]) tableItem.getData(OLD_IMAGE);
177         if (values != null) {
178             tableItem.setText(values);
179             tableItem.setImage(images);
180         }
181     }
182
183     public void setLabels(Widget widget, String JavaDoc[] labels, ImageDescriptor[] imageDescriptors) {
184         TableItem item = (TableItem) widget;
185         item.setText(labels);
186         item.setData(OLD_LABEL, labels);
187         Image[] images = new Image[labels.length];
188         item.setData(OLD_IMAGE, images);
189         if (imageDescriptors != null) {
190             for (int i = 0; i < images.length; i++) {
191                 if (i < imageDescriptors.length)
192                     images[i] = getImage(imageDescriptors[i]);
193             }
194         }
195         item.setImage(images);
196     }
197
198     public void setColors(Widget widget, RGB[] foregrounds, RGB[] backgrounds) {
199         TableItem item = (TableItem) widget;
200         if (foregrounds == null) {
201             foregrounds = new RGB[fTable.getColumnCount()];
202         }
203         if (backgrounds == null) {
204             backgrounds = new RGB[fTable.getColumnCount()];
205         }
206
207         for (int i = 0; i < foregrounds.length; i++) {
208             Color fg = getColor(foregrounds[i]);
209             item.setForeground(i, fg);
210         }
211         for (int i = 0; i < backgrounds.length; i++) {
212             Color bg = getColor(backgrounds[i]);
213             item.setBackground(i, bg);
214         }
215     }
216
217     public void setFonts(Widget widget, FontData[] fontDatas) {
218         TableItem item = (TableItem) widget;
219         if (fontDatas != null) {
220             for (int i = 0; i < fontDatas.length; i++) {
221                 Font font = getFont(fontDatas[i]);
222                 item.setFont(i, font);
223             }
224         }
225     }
226
227     public void setColumnHeaders(final String JavaDoc[] headers) {
228         fTableEditorImpl.setColumnProperties(headers);
229     }
230
231     public Object JavaDoc[] getColumnProperties() {
232         return fTableEditorImpl.getColumnProperties();
233     }
234
235     public void showColumnHeader(final boolean showHeaders) {
236         WorkbenchJob job = new WorkbenchJob("Set Header Visibility") { //$NON-NLS-1$
237
public IStatus runInUIThread(IProgressMonitor monitor) {
238                 if (!fTable.isDisposed())
239                 {
240                     fTable.setHeaderVisible(showHeaders);
241                 }
242                 return Status.OK_STATUS;
243             }
244         };
245         job.setPriority(Job.INTERACTIVE);
246         job.setSystem(true);
247         job.schedule();
248     }
249
250     /*
251      * (non-Javadoc)
252      *
253      * @see org.eclipse.jface.viewers.StructuredViewer#reveal(java.lang.Object)
254      */

255     public void reveal(Object JavaDoc element) {
256         Assert.isNotNull(element);
257         Widget w = findItem(element);
258         if (w instanceof TableItem)
259             getTable().showItem((TableItem) w);
260     }
261
262     /**
263      * Sets the cell editors of this table viewer.
264      *
265      * @param editors
266      * the list of cell editors
267      */

268     public void setCellEditors(CellEditor[] editors) {
269         fTableEditorImpl.setCellEditors(editors);
270     }
271
272     /**
273      * Sets the cell modifier of this table viewer.
274      *
275      * @param modifier
276      * the cell modifier
277      */

278     public void setCellModifier(ICellModifier modifier) {
279         fTableEditorImpl.setCellModifier(modifier);
280     }
281
282     protected TableEditorImpl createTableEditorImpl() {
283         return new TableEditorImpl(this) {
284             Rectangle getBounds(Item item, int columnNumber) {
285                 return ((TableItem) item).getBounds(columnNumber);
286             }
287
288             int getColumnCount() {
289                 return getTable().getColumnCount();
290             }
291
292             Item[] getSelection() {
293                 return getTable().getSelection();
294             }
295
296             void setEditor(Control w, Item item, int columnNumber) {
297                 fTableEditor.setEditor(w, (TableItem) item, columnNumber);
298             }
299
300             void setSelection(StructuredSelection selection, boolean b) {
301                 AsynchronousTableViewer.this.setSelection(selection, b);
302             }
303
304             void showSelection() {
305                 getTable().showSelection();
306             }
307
308             void setLayoutData(CellEditor.LayoutData layoutData) {
309                 fTableEditor.grabHorizontal = layoutData.grabHorizontal;
310                 fTableEditor.horizontalAlignment = layoutData.horizontalAlignment;
311                 fTableEditor.minimumWidth = layoutData.minimumWidth;
312             }
313
314             void handleDoubleClickEvent() {
315                 Viewer viewer = getViewer();
316                 fireDoubleClick(new DoubleClickEvent(viewer, viewer.getSelection()));
317                 fireOpen(new OpenEvent(viewer, viewer.getSelection()));
318             }
319         };
320     }
321
322     protected ISelection newSelectionFromWidget() {
323         Control control = getControl();
324         if (control == null || control.isDisposed()) {
325             return StructuredSelection.EMPTY;
326         }
327         List JavaDoc list = getSelectionFromWidget();
328         return new StructuredSelection(list);
329     }
330
331     public CellEditor[] getCellEditors() {
332         return fTableEditorImpl.getCellEditors();
333     }
334
335     public ICellModifier getCellModifier() {
336         return fTableEditorImpl.getCellModifier();
337     }
338
339     public boolean isCellEditorActive() {
340         return fTableEditorImpl.isCellEditorActive();
341     }
342
343     /**
344      * This is not asynchronous. This method must be called in the UI Thread.
345      */

346     public void cancelEditing() {
347         fTableEditorImpl.cancelEditing();
348     }
349
350     /**
351      * This is not asynchronous. This method must be called in the UI Thread.
352      *
353      * @param element
354      * The element to edit. Each element maps to a row in the Table.
355      * @param column
356      * The column to edit
357      */

358     public void editElement(Object JavaDoc element, int column) {
359         fTableEditorImpl.editElement(element, column);
360     }
361
362     protected int indexForElement(Object JavaDoc element) {
363         ViewerSorter sorter = getSorter();
364         if (sorter == null)
365             return fTable.getItemCount();
366         int count = fTable.getItemCount();
367         int min = 0, max = count - 1;
368         while (min <= max) {
369             int mid = (min + max) / 2;
370             Object JavaDoc data = fTable.getItem(mid).getData();
371             int compare = sorter.compare(this, data, element);
372             if (compare == 0) {
373                 // find first item > element
374
while (compare == 0) {
375                     ++mid;
376                     if (mid >= count) {
377                         break;
378                     }
379                     data = fTable.getItem(mid).getData();
380                     compare = sorter.compare(this, data, element);
381                 }
382                 return mid;
383             }
384             if (compare < 0)
385                 min = mid + 1;
386             else
387                 max = mid - 1;
388         }
389         return min;
390     }
391
392     public void add(Object JavaDoc element) {
393         if (element != null)
394             add(new Object JavaDoc[] { element });
395     }
396
397     public void add(Object JavaDoc[] elements) {
398         if (elements == null || elements.length == 0)
399             return; // done
400
((AsynchronousTableModel)getModel()).add(elements);
401     }
402
403     public void remove(Object JavaDoc element) {
404         if (element != null)
405             remove(new Object JavaDoc[] { element });
406     }
407
408     public void remove(final Object JavaDoc[] elements) {
409         if (elements == null || elements.length == 0)
410             return; // done
411
((AsynchronousTableModel)getModel()).remove(elements);
412     }
413
414     public void insert(Object JavaDoc element, int position) {
415         if (element != null)
416             insert(new Object JavaDoc[] { element }, position);
417     }
418
419     public void insert(Object JavaDoc[] elements, int position) {
420         if (elements == null || elements.length == 0)
421             return;
422         ((AsynchronousTableModel)getModel()).insert(elements, position);
423     }
424
425     public void replace(Object JavaDoc element, Object JavaDoc replacement) {
426         if (element == null || replacement == null)
427             throw new IllegalArgumentException JavaDoc("unexpected null parameter"); //$NON-NLS-1$
428
((AsynchronousTableModel)getModel()).replace(element, replacement);
429     }
430
431     /* (non-Javadoc)
432      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.viewers.AsynchronousViewer#createModel()
433      */

434     protected AsynchronousModel createModel() {
435         return new AsynchronousTableModel(this);
436     }
437
438     /* (non-Javadoc)
439      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.viewers.AsynchronousViewer#setItemCount(org.eclipse.swt.widgets.Widget, int)
440      */

441     protected void setItemCount(Widget parent, int itemCount) {
442         fTable.setItemCount(itemCount);
443     }
444
445     protected int getVisibleItemCount(int top) {
446         int itemCount = fTable.getItemCount();
447         return Math.min((fTable.getBounds().height / fTable.getItemHeight()) + 2, itemCount - top);
448     }
449
450     /* (non-Javadoc)
451      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.viewers.AsynchronousViewer#createUpdatePolicy()
452      */

453     public AbstractUpdatePolicy createUpdatePolicy() {
454         return new TableUpdatePolicy();
455     }
456
457     /* (non-Javadoc)
458      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.viewers.AsynchronousViewer#getParentWidget(org.eclipse.swt.widgets.Widget)
459      */

460     protected Widget getParentWidget(Widget widget) {
461         if (widget instanceof TableItem) {
462             return ((TableItem)widget).getParent();
463         }
464         return null;
465     }
466
467     /* (non-Javadoc)
468      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.viewers.AsynchronousViewer#getChildWidget(org.eclipse.swt.widgets.Widget, int)
469      */

470     protected Widget getChildWidget(Widget parent, int index) {
471         if (index < fTable.getItemCount()) {
472             return fTable.getItem(index);
473         }
474         return null;
475     }
476
477     /* (non-Javadoc)
478      * @see org.eclipse.debug.internal.ui.viewers.model.provisional.viewers.AsynchronousViewer#clear(org.eclipse.swt.widgets.Widget)
479      */

480     protected void clear(Widget item) {
481         if (item instanceof TableItem) {
482             int i = fTable.indexOf((TableItem)item);
483             if (i >= 0) {
484                 fTable.clear(i);
485             }
486         }
487     }
488     
489     /* (non-Javadoc)
490      * @see org.eclipse.debug.internal.ui.viewers.AsynchronousViewer#clearChild(org.eclipse.swt.widgets.Widget, int)
491      */

492     protected void clearChild(Widget parent, int childIndex) {
493         if (parent instanceof Table) {
494             fTable.clear(childIndex);
495         }
496     }
497
498     /* (non-Javadoc)
499      * @see org.eclipse.debug.internal.ui.viewers.AsynchronousViewer#clearChildren(org.eclipse.swt.widgets.Widget)
500      */

501     protected void clearChildren(Widget item) {
502         if (item instanceof Table) {
503             fTable.clearAll();
504         }
505     }
506
507     /* (non-Javadoc)
508      * @see org.eclipse.debug.internal.ui.viewers.AsynchronousViewer#indexOf(org.eclipse.swt.widgets.Widget, org.eclipse.swt.widgets.Widget)
509      */

510     protected int indexOf(Widget parent, Widget child) {
511         if (parent instanceof Table) {
512             return ((Table)parent).indexOf((TableItem)child);
513         }
514         return -1;
515     }
516     
517 }
518
Popular Tags