KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > TableViewer


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  * Tom Schindl <tom.schindl@bestsolution.at> - concept of ViewerRow,
11  * fix for 159597, refactoring (bug 153993),
12  * widget-independency (bug 154329), fix for 187826, 191468
13  *******************************************************************************/

14
15 package org.eclipse.jface.viewers;
16
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.Point;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Item;
24 import org.eclipse.swt.widgets.Table;
25 import org.eclipse.swt.widgets.TableItem;
26 import org.eclipse.swt.widgets.Widget;
27
28 /**
29  * A concrete viewer based on a SWT <code>Table</code> control.
30  * <p>
31  * This class is not intended to be subclassed outside the viewer framework. It
32  * is designed to be instantiated with a pre-existing SWT table control and
33  * configured with a domain-specific content provider, table label provider,
34  * element filter (optional), and element sorter (optional).
35  * </p>
36  * <p>
37  * Label providers for table viewers must implement either the
38  * <code>ITableLabelProvider</code> or the <code>ILabelProvider</code>
39  * interface (see <code>TableViewer.setLabelProvider</code> for more details).
40  * </p>
41  * <p>
42  * As of 3.1 the TableViewer now supports the SWT.VIRTUAL flag. If the
43  * underlying table is SWT.VIRTUAL, the content provider may implement
44  * {@link ILazyContentProvider} instead of {@link IStructuredContentProvider}.
45  * Note that in this case, the viewer does not support sorting or filtering.
46  * Also note that in this case, the Widget based APIs may return null if the
47  * element is not specified or not created yet.
48  * </p>
49  * <p>
50  * Users of SWT.VIRTUAL should also avoid using getItems() from the Table within
51  * the TreeViewer as this does not necessarily generate a callback for the
52  * TreeViewer to populate the items. It also has the side effect of creating all
53  * of the items thereby eliminating the performance improvements of SWT.VIRTUAL.
54  * </p>
55  *
56  * @see SWT#VIRTUAL
57  * @see #doFindItem(Object)
58  * @see #internalRefresh(Object, boolean)
59  */

60 public class TableViewer extends AbstractTableViewer {
61     /**
62      * This viewer's table control.
63      */

64     private Table table;
65
66     /**
67      * The cached row which is reused all over
68      */

69     private TableViewerRow cachedRow;
70
71     /**
72      * Creates a table viewer on a newly-created table control under the given
73      * parent. The table control is created using the SWT style bits
74      * <code>MULTI, H_SCROLL, V_SCROLL,</code> and <code>BORDER</code>. The
75      * viewer has no input, no content provider, a default label provider, no
76      * sorter, and no filters. The table has no columns.
77      *
78      * @param parent
79      * the parent control
80      */

81     public TableViewer(Composite parent) {
82         this(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
83     }
84
85     /**
86      * Creates a table viewer on a newly-created table control under the given
87      * parent. The table control is created using the given style bits. The
88      * viewer has no input, no content provider, a default label provider, no
89      * sorter, and no filters. The table has no columns.
90      *
91      * @param parent
92      * the parent control
93      * @param style
94      * SWT style bits
95      */

96     public TableViewer(Composite parent, int style) {
97         this(new Table(parent, style));
98     }
99
100     /**
101      * Creates a table viewer on the given table control. The viewer has no
102      * input, no content provider, a default label provider, no sorter, and no
103      * filters.
104      *
105      * @param table
106      * the table control
107      */

108     public TableViewer(Table table) {
109         this.table = table;
110         hookControl(table);
111     }
112
113     public Control getControl() {
114         return table;
115     }
116
117     /**
118      * Returns this table viewer's table control.
119      *
120      * @return the table control
121      */

122     public Table getTable() {
123         return table;
124     }
125
126     protected ColumnViewerEditor createViewerEditor() {
127         return new TableViewerEditor(this,null,new ColumnViewerEditorActivationStrategy(this),ColumnViewerEditor.DEFAULT);
128     }
129
130     /**
131      * <p>
132      * Sets a new selection for this viewer and optionally makes it visible. The
133      * TableViewer implementation of this method is inefficient for the
134      * ILazyContentProvider as lookup is done by indices rather than elements
135      * and may require population of the entire table in worse case.
136      * </p>
137      * <p>
138      * Use Table#setSelection(int[] indices) and Table#showSelection() if you
139      * wish to set selection more efficiently when using a ILazyContentProvider.
140      * </p>
141      *
142      * @param selection
143      * the new selection
144      * @param reveal
145      * <code>true</code> if the selection is to be made visible,
146      * and <code>false</code> otherwise
147      * @see Table#setSelection(int[])
148      * @see Table#showSelection()
149      */

150     public void setSelection(ISelection selection, boolean reveal) {
151         super.setSelection(selection, reveal);
152     }
153
154     protected ViewerRow getViewerRowFromItem(Widget item) {
155         if( cachedRow == null ) {
156             cachedRow = new TableViewerRow((TableItem) item);
157         } else {
158             cachedRow.setItem((TableItem) item);
159         }
160
161         return cachedRow;
162     }
163
164     /**
165      * Create a new row with style at index
166      *
167      * @param style
168      * @param rowIndex
169      * @return ViewerRow
170      * @since 3.3
171      */

172     protected ViewerRow internalCreateNewRowPart(int style, int rowIndex) {
173         TableItem item;
174
175         if (rowIndex >= 0) {
176             item = new TableItem(table, style, rowIndex);
177         } else {
178             item = new TableItem(table, style);
179         }
180
181         return getViewerRowFromItem(item);
182     }
183
184     protected Item getItemAt(Point p) {
185         TableItem[] selection = table.getSelection();
186
187         if( selection.length == 1 ) {
188             int columnCount = table.getColumnCount();
189             
190             for( int i = 0; i < columnCount; i++ ) {
191                 if( selection[0].getBounds(i).contains(p) ) {
192                     return selection[0];
193                 }
194             }
195         }
196
197         return table.getItem(p);
198     }
199
200     // Methods to provide widget independency
201

202     protected int doGetItemCount() {
203         return table.getItemCount();
204     }
205
206     protected int doIndexOf(Item item) {
207         return table.indexOf((TableItem)item);
208     }
209
210     protected void doSetItemCount(int count) {
211         table.setItemCount(count);
212     }
213
214     protected Item[] doGetItems() {
215         return table.getItems();
216     }
217
218     protected int doGetColumnCount() {
219         return table.getColumnCount();
220     }
221
222     protected Widget doGetColumn(int index) {
223         return table.getColumn(index);
224     }
225
226     protected Item doGetItem(int index) {
227         return table.getItem(index);
228     }
229
230     protected Item[] doGetSelection() {
231         return table.getSelection();
232     }
233
234     protected int[] doGetSelectionIndices() {
235         return table.getSelectionIndices();
236     }
237
238     protected void doClearAll() {
239         table.clearAll();
240     }
241
242     protected void doResetItem(Item item) {
243         TableItem tableItem = (TableItem) item;
244         int columnCount = Math.max(1, table.getColumnCount());
245         for (int i = 0; i < columnCount; i++) {
246             tableItem.setText(i, ""); //$NON-NLS-1$
247
if (tableItem.getImage(i) != null) {
248                 tableItem.setImage(i, null);
249             }
250         }
251     }
252
253     protected void doRemove(int start, int end) {
254         table.remove(start, end);
255     }
256
257     protected void doRemoveAll() {
258         table.removeAll();
259     }
260
261     protected void doRemove(int[] indices) {
262         table.remove(indices);
263     }
264
265     protected void doShowItem(Item item) {
266         table.showItem((TableItem)item);
267     }
268
269     protected void doDeselectAll() {
270         table.deselectAll();
271     }
272
273     protected void doSetSelection(Item[] items) {
274         Assert.isNotNull(items, "Items-Array can not be null"); //$NON-NLS-1$
275

276         TableItem[] t = new TableItem[items.length];
277         System.arraycopy(items, 0, t, 0, t.length);
278
279         table.setSelection(t);
280     }
281
282     protected void doShowSelection() {
283         table.showSelection();
284     }
285
286     protected void doSetSelection(int[] indices) {
287         table.setSelection(indices);
288     }
289
290     protected void doClear(int index) {
291         table.clear(index);
292     }
293
294     protected void doSelect(int[] indices) {
295         table.select(indices);
296     }
297
298     /**
299      * Refreshes this viewer starting with the given element. Labels are updated
300      * as described in <code>refresh(boolean updateLabels)</code>. The
301      * methods attempts to preserve the selection.
302      * <p>
303      * Unlike the <code>update</code> methods, this handles structural changes
304      * to the given element (e.g. addition or removal of children). If only the
305      * given element needs updating, it is more efficient to use the
306      * <code>update</code> methods.
307      * </p>
308      *
309      * <p>
310      * Subclasses who can provide this feature can open this method for the
311      * public
312      * </p>
313      *
314      * @param element
315      * the element
316      * @param updateLabels
317      * <code>true</code> to update labels for existing elements,
318      * <code>false</code> to only update labels as needed, assuming
319      * that labels for existing elements are unchanged.
320      * @param reveal
321      * <code>true</code> to make the preserved selection visible
322      * afterwards
323      *
324      * @since 3.3
325      */

326     public void refresh(final Object JavaDoc element, final boolean updateLabels,
327             boolean reveal) {
328         if (isBusy())
329             return;
330
331         if( isCellEditorActive() ) {
332             cancelEditing();
333         }
334
335         preservingSelection(new Runnable JavaDoc() {
336             public void run() {
337                 internalRefresh(element, updateLabels);
338             }
339         }, reveal);
340     }
341
342     /**
343      * Refreshes this viewer with information freshly obtained from this
344      * viewer's model. If <code>updateLabels</code> is <code>true</code>
345      * then labels for otherwise unaffected elements are updated as well.
346      * Otherwise, it assumes labels for existing elements are unchanged, and
347      * labels are only obtained as needed (for example, for new elements).
348      * <p>
349      * Calling <code>refresh(true)</code> has the same effect as
350      * <code>refresh()</code>.
351      * <p>
352      * Note that the implementation may still obtain labels for existing
353      * elements even if <code>updateLabels</code> is false. The intent is
354      * simply to allow optimization where possible.
355      *
356      * @param updateLabels
357      * <code>true</code> to update labels for existing elements,
358      * <code>false</code> to only update labels as needed, assuming
359      * that labels for existing elements are unchanged.
360      * @param reveal
361      * <code>true</code> to make the preserved selection visible
362      * afterwards
363      *
364      * @since 3.3
365      */

366     public void refresh(boolean updateLabels, boolean reveal) {
367         refresh(getRoot(), updateLabels, reveal);
368     }
369 }
370
Popular Tags