KickJava   Java API By Example, From Geeks To Geeks.

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


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

11
12 package org.eclipse.jface.viewers;
13
14 import org.eclipse.swt.events.FocusAdapter;
15 import org.eclipse.swt.events.FocusEvent;
16 import org.eclipse.swt.events.FocusListener;
17 import org.eclipse.swt.events.MouseAdapter;
18 import org.eclipse.swt.events.MouseEvent;
19 import org.eclipse.swt.events.MouseListener;
20 import org.eclipse.swt.graphics.Rectangle;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.swt.widgets.Item;
24
25 /**
26  * Internal table viewer implementation.
27  */

28 /* package */ abstract class TableViewerImpl {
29     
30     private CellEditor cellEditor;
31     private CellEditor[] cellEditors;
32     private ICellModifier cellModifier;
33     private String JavaDoc[] columnProperties;
34     private Item tableItem;
35     private int columnNumber;
36     private ICellEditorListener cellEditorListener;
37     private FocusListener focusListener;
38     private MouseListener mouseListener;
39     private int doubleClickExpirationTime;
40     private StructuredViewer viewer;
41     
42
43 TableViewerImpl(StructuredViewer viewer) {
44     this.viewer = viewer;
45     initCellEditorListener();
46 }
47
48 /**
49  * Returns this <code>TableViewerImpl</code> viewer
50  *
51  * @return the viewer
52  */

53 public StructuredViewer getViewer() {
54     return viewer;
55 }
56
57 private void activateCellEditor() {
58     if (cellEditors != null) {
59         if(cellEditors[columnNumber] != null && cellModifier != null) {
60             Object JavaDoc element = tableItem.getData();
61             String JavaDoc property = columnProperties[columnNumber];
62             if (cellModifier.canModify(element, property)) {
63                 cellEditor = cellEditors[columnNumber];
64                 //table.showSelection();
65
cellEditor.addListener(cellEditorListener);
66                 Object JavaDoc value = cellModifier.getValue(element, property);
67                 cellEditor.setValue(value);
68                 // Tricky flow of control here:
69
// activate() can trigger callback to cellEditorListener which will clear cellEditor
70
// so must get control first, but must still call activate() even if there is no control.
71
final Control control = cellEditor.getControl();
72                 cellEditor.activate();
73                 if (control == null)
74                     return;
75                 setLayoutData(cellEditor.getLayoutData());
76                 setEditor(control, tableItem, columnNumber);
77                 cellEditor.setFocus();
78                 if(focusListener == null) {
79                     focusListener = new FocusAdapter() {
80                         public void focusLost(FocusEvent e) {
81                             applyEditorValue();
82                         }
83                     };
84                 }
85                 control.addFocusListener(focusListener);
86                 mouseListener = new MouseAdapter() {
87                     public void mouseDown(MouseEvent e) {
88                         // time wrap?
89
// check for expiration of doubleClickTime
90
if (e.time <= doubleClickExpirationTime ) {
91                             control.removeMouseListener(mouseListener);
92                             cancelEditing();
93                             handleDoubleClickEvent();
94                         } else if (mouseListener != null) {
95                             control.removeMouseListener(mouseListener);
96                         }
97                     }
98                 };
99                 control.addMouseListener(mouseListener);
100             }
101         }
102     }
103 }
104 /**
105  * Activate a cell editor for the given mouse position.
106  */

107 private void activateCellEditor(MouseEvent event) {
108     if (tableItem == null || tableItem.isDisposed()) {
109         //item no longer exists
110
return;
111     }
112     int columnToEdit;
113     int columns = getColumnCount();
114     if (columns == 0) {
115         // If no TableColumn, Table acts as if it has a single column
116
// which takes the whole width.
117
columnToEdit = 0;
118     }
119     else {
120         columnToEdit = -1;
121         for (int i = 0; i < columns; i++) {
122             Rectangle bounds = getBounds(tableItem, i);
123             if (bounds.contains(event.x, event.y)) {
124                 columnToEdit = i;
125                 break;
126             }
127         }
128         if (columnToEdit == -1) {
129             return;
130         }
131     }
132     
133     columnNumber = columnToEdit;
134     activateCellEditor();
135 }
136 /**
137  * Deactivates the currently active cell editor.
138  */

139 public void applyEditorValue() {
140     CellEditor c = this.cellEditor;
141     if (c != null) {
142         // null out cell editor before calling save
143
// in case save results in applyEditorValue being re-entered
144
// see 1GAHI8Z: ITPUI:ALL - How to code event notification when using cell editor ?
145
this.cellEditor = null;
146         Item t = this.tableItem;
147         // don't null out table item -- same item is still selected
148
if (t != null && !t.isDisposed()) {
149             saveEditorValue(c, t);
150         }
151         setEditor(null, null, 0);
152         c.removeListener(cellEditorListener);
153         Control control = c.getControl();
154         if (control != null) {
155             if (mouseListener != null) {
156                 control.removeMouseListener(mouseListener);
157             }
158             if (focusListener != null) {
159                 control.removeFocusListener(focusListener);
160             }
161         }
162         c.deactivate();
163     }
164 }
165 /**
166  * Cancels the active cell editor, without saving the value
167  * back to the domain model.
168  */

169 public void cancelEditing() {
170     if (cellEditor != null) {
171         setEditor(null, null, 0);
172         cellEditor.removeListener(cellEditorListener);
173         CellEditor oldEditor = cellEditor;
174         cellEditor = null;
175         oldEditor.deactivate();
176     }
177 }
178 /**
179  * Start editing the given element.
180  */

181 public void editElement(Object JavaDoc element, int column) {
182     if (cellEditor != null)
183         applyEditorValue();
184
185     setSelection(new StructuredSelection(element), true);
186     Item[] selection = getSelection();
187     if (selection.length != 1)
188         return;
189
190     tableItem = selection[0];
191
192     // Make sure selection is visible
193
showSelection();
194     columnNumber = column;
195     activateCellEditor();
196
197 }
198 abstract Rectangle getBounds(Item item, int columnNumber);
199 public CellEditor[] getCellEditors() {
200     return cellEditors;
201 }
202 public ICellModifier getCellModifier() {
203     return cellModifier;
204 }
205 abstract int getColumnCount();
206 public Object JavaDoc[] getColumnProperties() {
207     return columnProperties;
208 }
209 abstract Item[] getSelection();
210 /**
211  * Handles the mouse down event; activates the cell editor.
212  */

213 public void handleMouseDown(MouseEvent event) {
214     if (event.button != 1)
215         return;
216
217     if (cellEditor != null)
218         applyEditorValue();
219     
220     // activate the cell editor immediately. If a second mouseDown
221
// is received prior to the expiration of the doubleClick time then
222
// the cell editor will be deactivated and a doubleClick event will
223
// be processed.
224
//
225
doubleClickExpirationTime = event.time + Display.getCurrent().getDoubleClickTime();
226
227     Item[] items = getSelection();
228     // Do not edit if more than one row is selected.
229
if (items.length != 1) {
230         tableItem = null;
231         return;
232     }
233     tableItem = items[0];
234     activateCellEditor(event);
235 }
236 private void initCellEditorListener() {
237     cellEditorListener = new ICellEditorListener() {
238         public void editorValueChanged(boolean oldValidState, boolean newValidState) {
239             // Ignore.
240
}
241         
242         public void cancelEditor() {
243             TableViewerImpl.this.cancelEditing();
244         }
245         
246         public void applyEditorValue() {
247             TableViewerImpl.this.applyEditorValue();
248         }
249     };
250 }
251 /**
252  * Returns <code>true</code> if there is an active cell editor; otherwise
253  * <code>false</code> is returned.
254  */

255 public boolean isCellEditorActive() {
256     return cellEditor != null;
257 }
258 /**
259  * Saves the value of the currently active cell editor,
260  * by delegating to the cell modifier.
261  */

262 private void saveEditorValue(CellEditor cellEditor, Item tableItem) {
263     if (cellModifier != null) {
264         if (!cellEditor.isValueValid()) {
265             ///Do what ???
266
}
267         String JavaDoc property = null;
268         if (columnProperties != null && columnNumber < columnProperties.length)
269             property = columnProperties[columnNumber];
270         cellModifier.modify(tableItem, property, cellEditor.getValue());
271     }
272 }
273 public void setCellEditors(CellEditor[] editors) {
274     this.cellEditors = editors;
275 }
276 public void setCellModifier(ICellModifier modifier) {
277     this.cellModifier = modifier;
278 }
279 public void setColumnProperties(String JavaDoc[] columnProperties) {
280     this.columnProperties = columnProperties;
281 }
282 abstract void setEditor(Control w, Item item, int fColumnNumber);
283 abstract void setLayoutData(CellEditor.LayoutData layoutData);
284 abstract void setSelection(StructuredSelection selection, boolean b);
285 abstract void showSelection();
286 abstract void handleDoubleClickEvent();
287 }
288
Popular Tags