KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > custom > TreeEditor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.swt.custom;
12
13
14 import org.eclipse.swt.*;
15 import org.eclipse.swt.events.*;
16 import org.eclipse.swt.graphics.*;
17 import org.eclipse.swt.widgets.*;
18
19 /**
20 *
21 * A TreeEditor is a manager for a Control that appears above a cell in a Tree and tracks with the
22 * moving and resizing of that cell. It can be used to display a text widget above a cell
23 * in a Tree so that the user can edit the contents of that cell. It can also be used to display
24 * a button that can launch a dialog for modifying the contents of the associated cell.
25 *
26 * <p> Here is an example of using a TreeEditor:
27 * <code><pre>
28 * final Tree tree = new Tree(shell, SWT.BORDER);
29 * for (int i = 0; i &lt; 3; i++) {
30 * TreeItem item = new TreeItem(tree, SWT.NONE);
31 * item.setText("item " + i);
32 * for (int j = 0; j &lt; 3; j++) {
33 * TreeItem subItem = new TreeItem(item, SWT.NONE);
34 * subItem.setText("item " + i + " " + j);
35 * }
36 * }
37 *
38 * final TreeEditor editor = new TreeEditor(tree);
39 * //The editor must have the same size as the cell and must
40 * //not be any smaller than 50 pixels.
41 * editor.horizontalAlignment = SWT.LEFT;
42 * editor.grabHorizontal = true;
43 * editor.minimumWidth = 50;
44 *
45 * tree.addSelectionListener(new SelectionAdapter() {
46 * public void widgetSelected(SelectionEvent e) {
47 * // Clean up any previous editor control
48 * Control oldEditor = editor.getEditor();
49 * if (oldEditor != null) oldEditor.dispose();
50 *
51 * // Identify the selected row
52 * TreeItem item = (TreeItem)e.item;
53 * if (item == null) return;
54 *
55 * // The control that will be the editor must be a child of the Tree
56 * Text newEditor = new Text(tree, SWT.NONE);
57 * newEditor.setText(item.getText());
58 * newEditor.addModifyListener(new ModifyListener() {
59 * public void modifyText(ModifyEvent e) {
60 * Text text = (Text)editor.getEditor();
61 * editor.getItem().setText(text.getText());
62 * }
63 * });
64 * newEditor.selectAll();
65 * newEditor.setFocus();
66 * editor.setEditor(newEditor, item);
67 * }
68 * });
69 * </pre></code>
70 */

71 public class TreeEditor extends ControlEditor {
72     Tree tree;
73     TreeItem item;
74     int column = 0;
75     ControlListener columnListener;
76     TreeListener treeListener;
77     Runnable JavaDoc timer;
78     static final int TIMEOUT = 1500;
79     
80 /**
81 * Creates a TreeEditor for the specified Tree.
82 *
83 * @param tree the Tree Control above which this editor will be displayed
84 *
85 */

86 public TreeEditor (Tree tree) {
87     super(tree);
88     this.tree = tree;
89
90     columnListener = new ControlListener() {
91         public void controlMoved(ControlEvent e){
92             layout();
93         }
94         public void controlResized(ControlEvent e){
95             layout();
96         }
97     };
98     timer = new Runnable JavaDoc () {
99         public void run() {
100             layout ();
101         }
102     };
103     treeListener = new TreeListener () {
104         final Runnable JavaDoc runnable = new Runnable JavaDoc() {
105             public void run() {
106                 if (editor == null || editor.isDisposed()) return;
107                 if (TreeEditor.this.tree.isDisposed()) return;
108                 layout();
109                 editor.setVisible(true);
110             }
111         };
112         public void treeCollapsed(TreeEvent e) {
113             if (editor == null || editor.isDisposed ()) return;
114             editor.setVisible(false);
115             e.display.asyncExec(runnable);
116         }
117         public void treeExpanded(TreeEvent e) {
118             if (editor == null || editor.isDisposed ()) return;
119             editor.setVisible(false);
120             e.display.asyncExec(runnable);
121         }
122     };
123     tree.addTreeListener(treeListener);
124     
125     // To be consistent with older versions of SWT, grabVertical defaults to true
126
grabVertical = true;
127 }
128
129 Rectangle computeBounds () {
130     if (item == null || column == -1 || item.isDisposed()) return new Rectangle(0, 0, 0, 0);
131     Rectangle cell = item.getBounds(column);
132     Rectangle rect = item.getImageBounds(column);
133     cell.x = rect.x + rect.width;
134     cell.width -= rect.width;
135     Rectangle area = tree.getClientArea();
136     if (cell.x < area.x + area.width) {
137         if (cell.x + cell.width > area.x + area.width) {
138             cell.width = area.x + area.width - cell.x;
139         }
140     }
141     Rectangle editorRect = new Rectangle(cell.x, cell.y, minimumWidth, minimumHeight);
142
143     if (grabHorizontal) {
144         if (tree.getColumnCount() == 0) {
145             // Bounds of tree item only include the text area - stretch out to include
146
// entire client area
147
cell.width = area.x + area.width - cell.x;
148         }
149         editorRect.width = Math.max(cell.width, minimumWidth);
150     }
151     
152     if (grabVertical) {
153         editorRect.height = Math.max(cell.height, minimumHeight);
154     }
155     
156     if (horizontalAlignment == SWT.RIGHT) {
157         editorRect.x += cell.width - editorRect.width;
158     } else if (horizontalAlignment == SWT.LEFT) {
159         // do nothing - cell.x is the right answer
160
} else { // default is CENTER
161
editorRect.x += (cell.width - editorRect.width)/2;
162     }
163     // don't let the editor overlap with the +/- of the tree
164
editorRect.x = Math.max(cell.x, editorRect.x);
165     
166     if (verticalAlignment == SWT.BOTTOM) {
167         editorRect.y += cell.height - editorRect.height;
168     } else if (verticalAlignment == SWT.TOP) {
169         // do nothing - cell.y is the right answer
170
} else { // default is CENTER
171
editorRect.y += (cell.height - editorRect.height)/2;
172     }
173     return editorRect;
174 }
175
176 /**
177  * Removes all associations between the TreeEditor and the row in the tree. The
178  * tree and the editor Control are <b>not</b> disposed.
179  */

180 public void dispose () {
181     if (tree != null && !tree.isDisposed()) {
182         if (this.column > -1 && this.column < tree.getColumnCount()){
183             TreeColumn treeColumn = tree.getColumn(this.column);
184             treeColumn.removeControlListener(columnListener);
185         }
186         if (treeListener != null) tree.removeTreeListener(treeListener);
187     }
188     columnListener = null;
189     treeListener = null;
190     tree = null;
191     item = null;
192     column = 0;
193     timer = null;
194     super.dispose();
195 }
196
197 /**
198 * Returns the zero based index of the column of the cell being tracked by this editor.
199 *
200 * @return the zero based index of the column of the cell being tracked by this editor
201 *
202 * @since 3.1
203 */

204 public int getColumn () {
205     return column;
206 }
207
208 /**
209 * Returns the TreeItem for the row of the cell being tracked by this editor.
210 *
211 * @return the TreeItem for the row of the cell being tracked by this editor
212 */

213 public TreeItem getItem () {
214     return item;
215 }
216
217 void resize () {
218     layout();
219     /*
220      * On some platforms, the table scrolls when an item that
221      * is partially visible at the bottom of the table is
222      * selected. Ensure that the correct row is edited by
223      * laying out one more time in a timerExec().
224      */

225     if (tree != null) {
226         Display display = tree.getDisplay();
227         display.timerExec(-1, timer);
228         display.timerExec(TIMEOUT, timer);
229     }
230 }
231
232 /**
233 * Sets the zero based index of the column of the cell being tracked by this editor.
234 *
235 * @param column the zero based index of the column of the cell being tracked by this editor
236 *
237 * @since 3.1
238 */

239 public void setColumn(int column) {
240     int columnCount = tree.getColumnCount();
241     // Separately handle the case where the tree has no TreeColumns.
242
// In this situation, there is a single default column.
243
if (columnCount == 0) {
244         this.column = (column == 0) ? 0 : -1;
245         resize();
246         return;
247     }
248     if (this.column > -1 && this.column < columnCount){
249         TreeColumn treeColumn = tree.getColumn(this.column);
250         treeColumn.removeControlListener(columnListener);
251         this.column = -1;
252     }
253
254     if (column < 0 || column >= tree.getColumnCount()) return;
255         
256     this.column = column;
257     TreeColumn treeColumn = tree.getColumn(this.column);
258     treeColumn.addControlListener(columnListener);
259     resize();
260 }
261
262 public void setItem (TreeItem item) {
263     this.item = item;
264     resize();
265 }
266
267 /**
268 * Specify the Control that is to be displayed and the cell in the tree that it is to be positioned above.
269 *
270 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Tree control
271 * specified in the TreeEditor constructor.
272 *
273 * @param editor the Control that is displayed above the cell being edited
274 * @param item the TreeItem for the row of the cell being tracked by this editor
275 * @param column the zero based index of the column of the cell being tracked by this editor
276 *
277 * @since 3.1
278 */

279 public void setEditor (Control editor, TreeItem item, int column) {
280     setItem(item);
281     setColumn(column);
282     setEditor(editor);
283 }
284 public void setEditor (Control editor) {
285     super.setEditor(editor);
286     resize();
287 }
288
289 /**
290 * Specify the Control that is to be displayed and the cell in the tree that it is to be positioned above.
291 *
292 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Tree control
293 * specified in the TreeEditor constructor.
294 *
295 * @param editor the Control that is displayed above the cell being edited
296 * @param item the TreeItem for the row of the cell being tracked by this editor
297 */

298 public void setEditor (Control editor, TreeItem item) {
299     setItem(item);
300     setEditor(editor);
301 }
302
303 public void layout () {
304     if (tree == null || tree.isDisposed()) return;
305     if (item == null || item.isDisposed()) return;
306     int columnCount = tree.getColumnCount();
307     if (columnCount == 0 && column != 0) return;
308     if (columnCount > 0 && (column < 0 || column >= columnCount)) return;
309     super.layout();
310 }
311 }
312
Popular Tags