KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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> - initial API and implementation
11  * fixes in bug 198665
12  ******************************************************************************/

13
14 package org.eclipse.jface.viewers;
15
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jface.viewers.CellEditor.LayoutData;
19 import org.eclipse.swt.custom.TableEditor;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Item;
22 import org.eclipse.swt.widgets.Table;
23 import org.eclipse.swt.widgets.TableItem;
24
25 /**
26  * This is an editor-implementation for {@link Table}
27  *
28  * @since 3.3
29  *
30  */

31 public final class TableViewerEditor extends ColumnViewerEditor {
32     /**
33      * This viewer's table editor.
34      */

35     private TableEditor tableEditor;
36
37     private SWTFocusCellManager focusCellManager;
38
39     /**
40      * @param viewer
41      * the viewer the editor is attached to
42      * @param focusCellManager
43      * the cell focus manager if one used or <code>null</code>
44      * @param editorActivationStrategy
45      * the strategy used to decide about the editor activation
46      * @param feature
47      * the feature mask
48      */

49     TableViewerEditor(TableViewer viewer, SWTFocusCellManager focusCellManager,
50             ColumnViewerEditorActivationStrategy editorActivationStrategy,
51             int feature) {
52         super(viewer, editorActivationStrategy, feature);
53         tableEditor = new TableEditor(viewer.getTable());
54         this.focusCellManager = focusCellManager;
55     }
56
57     /**
58      * Create a customized editor with focusable cells
59      *
60      * @param viewer
61      * the viewer the editor is created for
62      * @param focusCellManager
63      * the cell focus manager if one needed else <code>null</code>
64      * @param editorActivationStrategy
65      * activation strategy to control if an editor activated
66      * @param feature
67      * bit mask controlling the editor
68      * <ul>
69      * <li>{@link ColumnViewerEditor#DEFAULT}</li>
70      * <li>{@link ColumnViewerEditor#TABBING_CYCLE_IN_ROW}</li>
71      * <li>{@link ColumnViewerEditor#TABBING_HORIZONTAL}</li>
72      * <li>{@link ColumnViewerEditor#TABBING_MOVE_TO_ROW_NEIGHBOR}</li>
73      * <li>{@link ColumnViewerEditor#TABBING_VERTICAL}</li>
74      * </ul>
75      * @see #create(TableViewer, ColumnViewerEditorActivationStrategy, int)
76      */

77     public static void create(TableViewer viewer,
78             SWTFocusCellManager focusCellManager,
79             ColumnViewerEditorActivationStrategy editorActivationStrategy,
80             int feature) {
81         TableViewerEditor editor = new TableViewerEditor(viewer,
82                 focusCellManager, editorActivationStrategy, feature);
83         viewer.setColumnViewerEditor(editor);
84         if (focusCellManager != null) {
85             focusCellManager.init();
86         }
87     }
88
89     /**
90      * Create a customized editor whose activation process is customized
91      *
92      * @param viewer
93      * the viewer the editor is created for
94      * @param editorActivationStrategy
95      * activation strategy to control if an editor activated
96      * @param feature
97      * bit mask controlling the editor
98      * <ul>
99      * <li>{@link ColumnViewerEditor#DEFAULT}</li>
100      * <li>{@link ColumnViewerEditor#TABBING_CYCLE_IN_ROW}</li>
101      * <li>{@link ColumnViewerEditor#TABBING_HORIZONTAL}</li>
102      * <li>{@link ColumnViewerEditor#TABBING_MOVE_TO_ROW_NEIGHBOR}</li>
103      * <li>{@link ColumnViewerEditor#TABBING_VERTICAL}</li>
104      * </ul>
105      */

106     public static void create(TableViewer viewer,
107             ColumnViewerEditorActivationStrategy editorActivationStrategy,
108             int feature) {
109         create(viewer, null, editorActivationStrategy, feature);
110     }
111
112     protected void setEditor(Control w, Item item, int columnNumber) {
113         tableEditor.setEditor(w, (TableItem) item, columnNumber);
114     }
115
116     protected void setLayoutData(LayoutData layoutData) {
117         tableEditor.grabHorizontal = layoutData.grabHorizontal;
118         tableEditor.horizontalAlignment = layoutData.horizontalAlignment;
119         tableEditor.minimumWidth = layoutData.minimumWidth;
120     }
121
122     public ViewerCell getFocusCell() {
123         if (focusCellManager != null) {
124             return focusCellManager.getFocusCell();
125         }
126
127         return super.getFocusCell();
128     }
129
130     protected void updateFocusCell(ViewerCell focusCell,
131             ColumnViewerEditorActivationEvent event) {
132         // Update the focus cell when we activated the editor with these 2
133
// events
134
if (event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC
135                 || event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL) {
136
137             List JavaDoc l = getViewer().getSelectionFromWidget();
138
139             if (focusCellManager != null) {
140                 focusCellManager.setFocusCell(focusCell);
141             }
142
143             if (!l.contains(focusCell.getElement())) {
144                 getViewer().setSelection(
145                         new StructuredSelection(focusCell.getElement()),true);
146             }
147         }
148     }
149 }
150
Popular Tags