KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > common > ui > celleditor > ExtendedTableEditor


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: ExtendedTableEditor.java,v 1.3 2005/06/12 13:24:22 emerks Exp $
16  */

17 package org.eclipse.emf.common.ui.celleditor;
18
19
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.custom.TableEditor;
22 import org.eclipse.swt.events.KeyEvent;
23 import org.eclipse.swt.events.KeyListener;
24 import org.eclipse.swt.events.MouseEvent;
25 import org.eclipse.swt.events.MouseListener;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.events.SelectionListener;
28 import org.eclipse.swt.graphics.Point;
29 import org.eclipse.swt.graphics.Rectangle;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Event;
32 import org.eclipse.swt.widgets.Table;
33 import org.eclipse.swt.widgets.TableItem;
34
35
36 /**
37  * This base class for implementing a {@link TableEditor} that calls {@link #editItem}
38  * when the cell editor potentially needs to be activated.
39  * This API is under construction; please do not use it for anything more than experimentation.
40  */

41 public abstract class ExtendedTableEditor
42   extends TableEditor
43   implements KeyListener, MouseListener, SelectionListener
44 {
45   protected Table table;
46   protected TableItem selectedTableItem;
47   protected TableItem editTableItem;
48   protected int editTableItemColumn;
49   boolean isActivating;
50   boolean isDeactivating;
51   boolean isActive;
52
53   public ExtendedTableEditor(Table table)
54   {
55     super(table);
56
57     this.table = table;
58
59     table.addKeyListener(this);
60     table.addMouseListener(this);
61     table.addSelectionListener(this);
62   }
63
64   public void mouseDoubleClick(MouseEvent event)
65   {
66     // System.out.println("*double*");
67
editTableItem = null;
68   }
69
70   public void mouseDown(MouseEvent event)
71   {
72     editTableItem = null;
73     editTableItemColumn = -1;
74
75     // System.out.println("*down*");
76
if (event.button == 1)
77     {
78       boolean wasActive = getEditor() != null && !getEditor().isDisposed();
79
80       Point point = new Point(event.x, event.y);
81       TableItem [] tableItems = table.getItems();
82       int columnCount = table.getColumnCount();
83       LOOP: for (int i = table.getTopIndex(); i < tableItems.length; ++i)
84       {
85         for (int j = 0; j < columnCount; ++j)
86         {
87           Rectangle bounds = tableItems[i].getBounds(j);
88           if (bounds.y > event.y)
89           {
90             break LOOP;
91           }
92           else if (bounds.contains(event.x, event.y))
93           {
94             if (j != 0 || !tableItems[i].getImageBounds(0).contains(event.x, event.y))
95             {
96               TableItem tableItem = tableItems[i];
97               if (tableItem == selectedTableItem || wasActive)
98               {
99                 if (tableItem != selectedTableItem)
100                 {
101                   table.setSelection(i);
102                   Event selectionEvent = new Event();
103                   selectionEvent.widget = table;
104                   selectionEvent.item = tableItem;
105                   table.notifyListeners(SWT.Selection, selectionEvent);
106                   selectedTableItem = tableItem;
107                 }
108                 editTableItem = tableItems[i];
109                 editTableItemColumn = j;
110               }
111               else
112               {
113                 TableItem mouseBasedTableItem = table.getItem(point);
114                 if (mouseBasedTableItem == null)
115                 {
116                   table.setSelection(i);
117                   Event selectionEvent = new Event();
118                   selectionEvent.widget = table;
119                   selectionEvent.item = tableItem;
120                   table.notifyListeners(SWT.Selection, selectionEvent);
121                   selectedTableItem = tableItem;
122                 }
123               }
124             }
125
126             break LOOP;
127           }
128         }
129       }
130
131       if (editTableItem == null && wasActive)
132       {
133         dismiss();
134       }
135     }
136   }
137
138   public void dismiss()
139   {
140     setEditor(null, null, -1);
141   }
142
143   public void mouseUp(MouseEvent event)
144   {
145     // System.out.println("*up*");
146
if (event.button == 1)
147     {
148       TableItem [] tableItems = table.getItems();
149       int columnCount = table.getColumnCount();
150       LOOP: for (int i = table.getTopIndex(); i < tableItems.length; ++i)
151       {
152         for (int j = 0; j < columnCount; ++j)
153         {
154           Rectangle bounds = tableItems[i].getBounds(j);
155           if (bounds.y > event.y)
156           {
157             break LOOP;
158           }
159           else if (bounds.contains(event.x, event.y))
160           {
161             if (j != 0 || !tableItems[i].getImageBounds(0).contains(event.x, event.y))
162             {
163               TableItem tableItem = tableItems[i];
164               if (tableItem == editTableItem)
165               {
166                 selectedTableItem = null;
167                 table.showSelection();
168                 editItem(editTableItem, editTableItemColumn);
169               }
170             }
171
172             break LOOP;
173           }
174         }
175       }
176     }
177   }
178
179   public void widgetDefaultSelected(SelectionEvent event)
180   {
181     widgetSelected(event);
182   }
183
184   public void widgetSelected(SelectionEvent event)
185   {
186     TableItem [] selection = table.getSelection();
187     selectedTableItem = selection.length == 1 ? selection[0] : null;
188   }
189
190   public void keyPressed(KeyEvent event)
191   {
192   }
193
194   public void keyReleased(KeyEvent event)
195   {
196     TableItem [] selection = table.getSelection();
197     selectedTableItem = selection.length == 1 ? selection[0] : null;
198     if (event.character == ' ' && selectedTableItem != null)
199     {
200       editItem(selectedTableItem, 0);
201       selectedTableItem = null;
202     }
203   }
204
205   protected abstract void editItem(TableItem tableItem, int column);
206
207   public void setEditor(Control canvas, TableItem tableItem, int column)
208   {
209     super.setEditor(canvas, tableItem, column);
210   }
211 }
212
Popular Tags