KickJava   Java API By Example, From Geeks To Geeks.

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


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: ExtendedTableTreeEditor.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.TableTree;
22 import org.eclipse.swt.custom.TableTreeEditor;
23 import org.eclipse.swt.custom.TableTreeItem;
24 import org.eclipse.swt.events.KeyEvent;
25 import org.eclipse.swt.events.KeyListener;
26 import org.eclipse.swt.events.MouseEvent;
27 import org.eclipse.swt.events.MouseListener;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.events.SelectionListener;
30 import org.eclipse.swt.graphics.Point;
31 import org.eclipse.swt.graphics.Rectangle;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Event;
34 import org.eclipse.swt.widgets.Table;
35 import org.eclipse.swt.widgets.TableItem;
36
37 import org.eclipse.emf.common.ui.viewer.ExtendedTableTreeViewer;
38
39
40 /**
41  * This base class for implementing a {@link TableTreeEditor} calls {@link #editItem}
42  * when the cell editor potentially needs to be activated.
43  * This API is under construction; please do not use it for anything more than experimentation.
44  */

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