KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
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  * @since 3.1
29  */

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

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

120     private void activateCellEditor(MouseEvent event) {
121         if (tableItem == null || tableItem.isDisposed()) {
122             //item no longer exists
123
return;
124         }
125         int columnToEdit;
126         int columns = getColumnCount();
127         if (columns == 0) {
128             // If no TableColumn, Table acts as if it has a single column
129
// which takes the whole width.
130
columnToEdit = 0;
131         } else {
132             columnToEdit = -1;
133             for (int i = 0; i < columns; i++) {
134                 Rectangle bounds = getBounds(tableItem, i);
135                 if (bounds.contains(event.x, event.y)) {
136                     columnToEdit = i;
137                     break;
138                 }
139             }
140             if (columnToEdit == -1) {
141                 return;
142             }
143         }
144
145         columnNumber = columnToEdit;
146         activateCellEditor();
147     }
148
149     /**
150      * Deactivates the currently active cell editor.
151      */

152     public void applyEditorValue() {
153         CellEditor c = this.cellEditor;
154         if (c != null) {
155             // null out cell editor before calling save
156
// in case save results in applyEditorValue being re-entered
157
// see 1GAHI8Z: ITPUI:ALL - How to code event notification when using cell editor ?
158
this.cellEditor = null;
159             Item t = this.tableItem;
160             // don't null out table item -- same item is still selected
161
if (t != null && !t.isDisposed()) {
162                 saveEditorValue(c, t);
163             }
164             setEditor(null, null, 0);
165             c.removeListener(cellEditorListener);
166             Control control = c.getControl();
167             if (control != null) {
168                 if (mouseListener != null) {
169                     control.removeMouseListener(mouseListener);
170                 }
171                 if (focusListener != null) {
172                     control.removeFocusListener(focusListener);
173                 }
174             }
175             c.deactivate();
176         }
177     }
178
179     /**
180      * Cancels the active cell editor, without saving the value
181      * back to the domain model.
182      */

183     public void cancelEditing() {
184         if (cellEditor != null) {
185             setEditor(null, null, 0);
186             cellEditor.removeListener(cellEditorListener);
187             CellEditor oldEditor = cellEditor;
188             cellEditor = null;
189             oldEditor.deactivate();
190         }
191     }
192
193     /**
194      * Start editing the given element.
195      * @param element
196      * @param column
197      */

198     public void editElement(Object JavaDoc element, int column) {
199         if (cellEditor != null) {
200             applyEditorValue();
201         }
202
203         setSelection(new StructuredSelection(element), true);
204         Item[] selection = getSelection();
205         if (selection.length != 1) {
206             return;
207         }
208
209         tableItem = selection[0];
210
211         // Make sure selection is visible
212
showSelection();
213         columnNumber = column;
214         activateCellEditor();
215
216     }
217
218     abstract Rectangle getBounds(Item item, int columnNumber);
219
220     /**
221      * Return the array of CellEditors used in the viewer
222      * @return the cell editors
223      */

224     public CellEditor[] getCellEditors() {
225         return cellEditors;
226     }
227
228     /**
229      * Get the cell modifier
230      * @return the cell modifier
231      */

232     public ICellModifier getCellModifier() {
233         return cellModifier;
234     }
235
236     abstract int getColumnCount();
237
238     /**
239      * Return the properties for the column
240      * @return the array of column properties
241      */

242     public Object JavaDoc[] getColumnProperties() {
243         return columnProperties;
244     }
245
246     abstract Item[] getSelection();
247
248     /**
249      * Handles the mouse down event; activates the cell editor.
250      * @param event the mouse event that should be handled
251      */

252     public void handleMouseDown(MouseEvent event) {
253         if (event.button != 1) {
254             return;
255         }
256
257         if (cellEditor != null) {
258             applyEditorValue();
259         }
260
261         // activate the cell editor immediately. If a second mouseDown
262
// is received prior to the expiration of the doubleClick time then
263
// the cell editor will be deactivated and a doubleClick event will
264
// be processed.
265
//
266
doubleClickExpirationTime = event.time
267                 + Display.getCurrent().getDoubleClickTime();
268
269         Item[] items = getSelection();
270         // Do not edit if more than one row is selected.
271
if (items.length != 1) {
272             tableItem = null;
273             return;
274         }
275         tableItem = items[0];
276         activateCellEditor(event);
277     }
278
279     private void initCellEditorListener() {
280         cellEditorListener = new ICellEditorListener() {
281             public void editorValueChanged(boolean oldValidState,
282                     boolean newValidState) {
283                 // Ignore.
284
}
285
286             public void cancelEditor() {
287                 TableEditorImpl.this.cancelEditing();
288             }
289
290             public void applyEditorValue() {
291                 TableEditorImpl.this.applyEditorValue();
292             }
293         };
294     }
295
296     /**
297      * Return whether there is an active cell editor.
298      *
299      * @return <code>true</code> if there is an active cell editor; otherwise
300      * <code>false</code> is returned.
301      */

302     public boolean isCellEditorActive() {
303         return cellEditor != null;
304     }
305
306     /**
307      * Saves the value of the currently active cell editor,
308      * by delegating to the cell modifier.
309      */

310     private void saveEditorValue(CellEditor cellEditor, Item tableItem) {
311         if (cellModifier != null) {
312             if (!cellEditor.isValueValid()) {
313                 ///Do what ???
314
}
315             String JavaDoc property = null;
316             if (columnProperties != null
317                     && columnNumber < columnProperties.length) {
318                 property = columnProperties[columnNumber];
319             }
320             cellModifier.modify(tableItem, property, cellEditor.getValue());
321         }
322     }
323
324     /**
325      * Set the cell editors
326      * @param editors
327      */

328     public void setCellEditors(CellEditor[] editors) {
329         this.cellEditors = editors;
330     }
331
332     /**
333      * Set the cell modifier
334      * @param modifier
335      */

336     public void setCellModifier(ICellModifier modifier) {
337         this.cellModifier = modifier;
338     }
339
340     /**
341      * Set the column properties
342      * @param columnProperties
343      */

344     public void setColumnProperties(String JavaDoc[] columnProperties) {
345         this.columnProperties = columnProperties;
346     }
347
348     abstract void setEditor(Control w, Item item, int fColumnNumber);
349
350     abstract void setLayoutData(CellEditor.LayoutData layoutData);
351
352     abstract void setSelection(StructuredSelection selection, boolean b);
353
354     abstract void showSelection();
355
356     abstract void handleDoubleClickEvent();
357 }
358
Popular Tags