KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > ui > celleditor > AdapterFactoryTreeEditor


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: AdapterFactoryTreeEditor.java,v 1.2 2005/06/08 06:20:52 nickb Exp $
16  */

17 package org.eclipse.emf.edit.ui.celleditor;
18
19
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.FocusAdapter;
22 import org.eclipse.swt.events.FocusEvent;
23 import org.eclipse.swt.events.KeyAdapter;
24 import org.eclipse.swt.events.KeyEvent;
25 import org.eclipse.swt.events.ModifyEvent;
26 import org.eclipse.swt.events.ModifyListener;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.widgets.Combo;
29 import org.eclipse.swt.widgets.Text;
30 import org.eclipse.swt.widgets.Tree;
31 import org.eclipse.swt.widgets.TreeItem;
32
33 import org.eclipse.emf.common.notify.AdapterFactory;
34 import org.eclipse.emf.common.ui.celleditor.ExtendedTreeEditor;
35 import org.eclipse.emf.edit.provider.IUpdateableItemText;
36
37
38 /**
39  * This base class for implementing {@link org.eclipse.swt.custom.TreeEditor}s that delegate
40  * to adapters produced by an {@link AdapterFactory}.
41  */

42 public class AdapterFactoryTreeEditor extends ExtendedTreeEditor
43 {
44   protected AdapterFactory adapterFactory;
45   protected TreeItem currentTreeItem;
46
47   public AdapterFactoryTreeEditor(Tree tree, AdapterFactory adapterFactory)
48   {
49     super(tree);
50     this.adapterFactory = adapterFactory;
51   }
52
53   public AdapterFactory getAdapterFactory()
54   {
55     return adapterFactory;
56   }
57
58   public void setAdapterFactory(AdapterFactory adapterFactory)
59   {
60     this.adapterFactory = adapterFactory;
61   }
62
63   protected void editItem(final TreeItem treeItem)
64   {
65     final Object JavaDoc object = treeItem.getData();
66     final IUpdateableItemText updateableItemText = (IUpdateableItemText)adapterFactory.adapt(object, IUpdateableItemText.class);
67     if (updateableItemText != null)
68     {
69       String JavaDoc string = updateableItemText.getUpdateableText(object);
70
71       if (string != null)
72       {
73         horizontalAlignment = SWT.LEFT;
74         // grabHorizontal = true;
75
minimumWidth = Math.max(50, treeItem.getBounds().width);
76     
77         if (System.getProperty("EMF_COMBO_TEST") == null)
78         {
79           final Text text = new Text(tree, SWT.BORDER);
80           setEditor(text, treeItem);
81           text.setFocus();
82           text.setText(string);
83           text.setSelection(0, string.length());
84       
85           text.addFocusListener
86            (new FocusAdapter()
87             {
88               public void focusLost(FocusEvent event)
89               {
90                 updateableItemText.setText(object, text.getText());
91                 text.setVisible(false);
92               }
93             });
94           text.addKeyListener
95            (new KeyAdapter()
96             {
97               public void keyPressed(KeyEvent event)
98               {
99                 if (event.character == '\r' || event.character == '\n')
100                 {
101                   updateableItemText.setText(object, text.getText());
102                   setEditor(null);
103                   text.dispose();
104                 }
105                 else if (event.character == '\033')
106                 {
107                   setEditor(null);
108                   text.dispose();
109                 }
110               }
111             });
112         }
113         else
114         {
115           final Combo combo = new Combo(tree, SWT.BORDER);
116           setEditor(combo, treeItem);
117           combo.setFocus();
118           combo.setText(string);
119           combo.setSelection(new Point(0,string.length()));
120           combo.add("Item 1");
121           combo.add("Item 2");
122           combo.add("Item 3");
123           combo.add("Item 4");
124       
125           combo.addFocusListener
126            (new FocusAdapter()
127             {
128               public void focusLost(FocusEvent event)
129               {
130                 System.out.println("Combo lost focus");
131                 updateableItemText.setText(object, combo.getText());
132                 combo.setVisible(false);
133               }
134             });
135
136           combo.addKeyListener
137            (new KeyAdapter()
138             {
139               public void keyPressed(KeyEvent event)
140               {
141                 System.out.println("Combo key event");
142                 if (event.character == '\r' || event.character == '\n')
143                 {
144                   updateableItemText.setText(object, combo.getText());
145                   setEditor(null);
146                   combo.dispose();
147                 }
148                 else if (event.character == '\033')
149                 {
150                   setEditor(null);
151                   combo.dispose();
152                 }
153               }
154             });
155
156           combo.addModifyListener
157             (new ModifyListener()
158              {
159                public void modifyText(ModifyEvent event)
160                {
161                  System.out.println("Combo modified");
162                }
163              });
164         }
165       }
166     }
167   }
168 }
169
Popular Tags