KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005 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 org.eclipse.jface.viewers.CellEditor;
14 import org.eclipse.jface.viewers.ICellEditorListener;
15 import org.eclipse.jface.viewers.ICellModifier;
16 import org.eclipse.jface.viewers.StructuredSelection;
17 import org.eclipse.jface.viewers.StructuredViewer;
18 import org.eclipse.swt.events.FocusAdapter;
19 import org.eclipse.swt.events.FocusEvent;
20 import org.eclipse.swt.events.FocusListener;
21 import org.eclipse.swt.events.MouseAdapter;
22 import org.eclipse.swt.events.MouseEvent;
23 import org.eclipse.swt.events.MouseListener;
24 import org.eclipse.swt.graphics.Rectangle;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Display;
27 import org.eclipse.swt.widgets.Item;
28
29 /**
30  * This class is copied from package org.eclipse.jface.viewers.TableEditorImpl
31  * because the original has package access only.
32  *
33  * TODO: complain to UI about package access to API class.
34  *
35  */

36 public abstract class TableEditorImpl {
37     private CellEditor fCellEditor;
38     private CellEditor[] fCellEditors;
39     private ICellModifier fCellModifier;
40     private String JavaDoc[] fColumnProperties;
41     private Item fTableItem;
42     private int fColumnNumber;
43     private ICellEditorListener fCellEditorListener;
44     private FocusListener fFocusListener;
45     private MouseListener fMouseListener;
46     private int fDoubleClickExpirationTime;
47     private StructuredViewer fViewer;
48
49     TableEditorImpl(StructuredViewer viewer) {
50         fViewer = viewer;
51         initCellEditorListener();
52     }
53
54     /**
55      * Returns this <code>TableViewerImpl</code> viewer
56      *
57      * @return the viewer
58      */

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

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

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

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

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

221     public CellEditor[] getCellEditors() {
222         return fCellEditors;
223     }
224
225     /**
226      * Get the cell modifier
227      *
228      * @return the cell modifier
229      */

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

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

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

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

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

324     public void setCellEditors(CellEditor[] editors) {
325         fCellEditors = editors;
326     }
327
328     /**
329      * Set the cell modifier
330      *
331      * @param modifier
332      */

333     public void setCellModifier(ICellModifier modifier) {
334         fCellModifier = modifier;
335     }
336
337     /**
338      * Set the column properties
339      *
340      * @param columnProperties
341      */

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