KickJava   Java API By Example, From Geeks To Geeks.

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


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: ExtendedTreeEditor.java,v 1.2 2005/06/08 06:24:33 nickb Exp $
16  */

17 package org.eclipse.emf.common.ui.celleditor;
18
19
20 import org.eclipse.swt.custom.TreeEditor;
21 import org.eclipse.swt.events.KeyEvent;
22 import org.eclipse.swt.events.KeyListener;
23 import org.eclipse.swt.events.MouseEvent;
24 import org.eclipse.swt.events.MouseListener;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.events.SelectionListener;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Tree;
30 import org.eclipse.swt.widgets.TreeItem;
31
32
33 /**
34  * This base class for implementing a {@link TreeEditor} calls {@link #editItem}
35  * when the cell editor potentially needs to be activated.
36  * Activation is determined by checking for when a click happens on the single selection that was previously in effect,
37  * or when the user hits the space key when a single selection is in effect.
38  */

39 public abstract class ExtendedTreeEditor extends TreeEditor implements SelectionListener, MouseListener, KeyListener
40 {
41   protected Tree tree;
42   protected TreeItem selectedTreeItem;
43   protected TreeItem editTreeItem;
44
45   public ExtendedTreeEditor(Tree tree)
46   {
47     super(tree);
48     this.tree = tree;
49     tree.addKeyListener(this);
50     tree.addMouseListener(this);
51     tree.addSelectionListener(this);
52   }
53
54   public void mouseDoubleClick(MouseEvent event)
55   {
56   }
57
58   public void mouseDown(MouseEvent event)
59   {
60     if (event.button == 1)
61     {
62       TreeItem treeItem = tree.getItem(new Point(event.x, event.y));
63       editTreeItem = treeItem == selectedTreeItem ? treeItem : null;
64     }
65   }
66
67   public void mouseUp(MouseEvent event)
68   {
69     if (event.button == 1)
70     {
71       TreeItem treeItem = tree.getItem(new Point(event.x, event.y));
72       if (editTreeItem == treeItem && editTreeItem != null)
73       {
74         editTreeItem = null;
75         editItem(treeItem);
76       }
77     }
78   }
79
80   public void widgetDefaultSelected(SelectionEvent event)
81   {
82     widgetSelected(event);
83   }
84
85   public void widgetSelected(SelectionEvent event)
86   {
87     Control control = getEditor();
88     if (control != null && !control.isDisposed())
89     {
90       setEditor(null);
91       control.dispose();
92     }
93
94     TreeItem [] selection = tree.getSelection();
95     selectedTreeItem = selection.length == 1 ? selection[0] : null;
96   }
97
98   public void keyPressed(KeyEvent event)
99   {
100   }
101
102   public void keyReleased(KeyEvent event)
103   {
104     if (event.character == ' ' && selectedTreeItem != null)
105     {
106       editItem(selectedTreeItem);
107     }
108   }
109
110   protected abstract void editItem(TreeItem treeItem);
111 }
112
Popular Tags