155
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwt.custom.TreeEditor;
|
|
14
|
|
15
|
|
16
|
|
17 import dwt.DWT;
|
|
18 import dwt.events.ControlEvent;
|
|
19 import dwt.events.ControlListener;
|
|
20 import dwt.events.TreeEvent;
|
|
21 import dwt.events.TreeListener;
|
|
22 import dwt.graphics.Rectangle;
|
|
23 import dwt.widgets.Control;
|
|
24 import dwt.widgets.Display;
|
|
25 import dwt.widgets.Tree;
|
|
26 import dwt.widgets.TreeColumn;
|
|
27 import dwt.widgets.TreeItem;
|
|
28 import dwt.custom.ControlEditor;
|
|
29 import dwt.dwthelper.Runnable;
|
|
30
|
|
31 import dwt.dwthelper.utils;
|
|
32
|
|
33 /**
|
|
34 *
|
|
35 * A TreeEditor is a manager for a Control that appears above a cell in a Tree and tracks with the
|
|
36 * moving and resizing of that cell. It can be used to display a text widget above a cell
|
|
37 * in a Tree so that the user can edit the contents of that cell. It can also be used to display
|
|
38 * a button that can launch a dialog for modifying the contents of the associated cell.
|
|
39 *
|
|
40 * <p> Here is an example of using a TreeEditor:
|
|
41 * <code><pre>
|
|
42 * final Tree tree = new Tree(shell, DWT.BORDER);
|
|
43 * for (int i = 0; i < 3; i++) {
|
|
44 * TreeItem item = new TreeItem(tree, DWT.NONE);
|
|
45 * item.setText("item " + i);
|
|
46 * for (int j = 0; j < 3; j++) {
|
|
47 * TreeItem subItem = new TreeItem(item, DWT.NONE);
|
|
48 * subItem.setText("item " + i + " " + j);
|
|
49 * }
|
|
50 * }
|
|
51 *
|
|
52 * final TreeEditor editor = new TreeEditor(tree);
|
|
53 * //The editor must have the same size as the cell and must
|
|
54 * //not be any smaller than 50 pixels.
|
|
55 * editor.horizontalAlignment = DWT.LEFT;
|
|
56 * editor.grabHorizontal = true;
|
|
57 * editor.minimumWidth = 50;
|
|
58 *
|
|
59 * tree.addSelectionListener(new SelectionAdapter() {
|
|
60 * public void widgetSelected(SelectionEvent e) {
|
|
61 * // Clean up any previous editor control
|
|
62 * Control oldEditor = editor.getEditor();
|
|
63 * if (oldEditor !is null) oldEditor.dispose();
|
|
64 *
|
|
65 * // Identify the selected row
|
|
66 * TreeItem item = (TreeItem)e.item;
|
|
67 * if (item is null) return;
|
|
68 *
|
|
69 * // The control that will be the editor must be a child of the Tree
|
|
70 * Text newEditor = new Text(tree, DWT.NONE);
|
|
71 * newEditor.setText(item.getText());
|
|
72 * newEditor.addModifyListener(new ModifyListener() {
|
|
73 * public void modifyText(ModifyEvent e) {
|
|
74 * Text text = (Text)editor.getEditor();
|
|
75 * editor.getItem().setText(text.getText());
|
|
76 * }
|
|
77 * });
|
|
78 * newEditor.selectAll();
|
|
79 * newEditor.setFocus();
|
|
80 * editor.setEditor(newEditor, item);
|
|
81 * }
|
|
82 * });
|
|
83 * </pre></code>
|
|
84 */
|
|
85
|
|
86 public class TreeEditor : ControlEditor {
|
|
87 Tree tree;
|
|
88 TreeItem item;
|
|
89 int column = 0;
|
|
90 ControlListener columnListener;
|
|
91 TreeListener treeListener;
|
|
92 Runnable timer;
|
|
93 static const int TIMEOUT = 1500;
|
|
94
|
|
95 /**
|
|
96 * Creates a TreeEditor for the specified Tree.
|
|
97 *
|
|
98 * @param tree the Tree Control above which this editor will be displayed
|
|
99 *
|
|
100 */
|
|
101 public this (Tree tree) {
|
|
102 super(tree);
|
|
103 this.tree = tree;
|
|
104
|
|
105 columnListener = new class() ControlListener {
|
|
106 public void controlMoved(ControlEvent e){
|
|
107 layout();
|
|
108 }
|
|
109 public void controlResized(ControlEvent e){
|
|
110 layout();
|
|
111 }
|
|
112 };
|
|
113 timer = new class() Runnable {
|
|
114 public void run() {
|
|
115 layout ();
|
|
116 }
|
|
117 };
|
|
118 treeListener = new class() TreeListener {
|
|
119 Runnable runnable;
|
|
120 this(){
|
|
121 runnable = new class() Runnable {
|
|
122 public void run() {
|
211
|
123 if (this.outer.outer.editor is null || this.outer.outer.editor.isDisposed()) return;
|
155
|
124 if (this.outer.outer.tree.isDisposed()) return;
|
|
125 layout();
|
211
|
126 this.outer.outer.editor.setVisible(true);
|
155
|
127 }
|
|
128 };
|
|
129 }
|
|
130 public void treeCollapsed(TreeEvent e) {
|
211
|
131 if (this.outer.editor is null || this.outer.editor.isDisposed ()) return;
|
|
132 this.outer.editor.setVisible(false);
|
155
|
133 e.display.asyncExec(runnable);
|
|
134 }
|
|
135 public void treeExpanded(TreeEvent e) {
|
211
|
136 if (this.outer.editor is null || this.outer.editor.isDisposed ()) return;
|
|
137 this.outer.editor.setVisible(false);
|
155
|
138 e.display.asyncExec(runnable);
|
|
139 }
|
|
140 };
|
|
141 tree.addTreeListener(treeListener);
|
|
142
|
|
143 // To be consistent with older versions of DWT, grabVertical defaults to true
|
|
144 grabVertical = true;
|
|
145 }
|
|
146
|
|
147 override Rectangle computeBounds () {
|
|
148 if (item is null || column is -1 || item.isDisposed()) return new Rectangle(0, 0, 0, 0);
|
|
149 Rectangle cell = item.getBounds(column);
|
|
150 Rectangle rect = item.getImageBounds(column);
|
|
151 cell.x = rect.x + rect.width;
|
|
152 cell.width -= rect.width;
|
|
153 Rectangle area = tree.getClientArea();
|
|
154 if (cell.x < area.x + area.width) {
|
|
155 if (cell.x + cell.width > area.x + area.width) {
|
|
156 cell.width = area.x + area.width - cell.x;
|
|
157 }
|
|
158 }
|
|
159 Rectangle editorRect = new Rectangle(cell.x, cell.y, minimumWidth, minimumHeight);
|
|
160
|
|
161 if (grabHorizontal) {
|
|
162 if (tree.getColumnCount() is 0) {
|
|
163 // Bounds of tree item only include the text area - stretch out to include
|
|
164 // entire client area
|
|
165 cell.width = area.x + area.width - cell.x;
|
|
166 }
|
|
167 editorRect.width = Math.max(cell.width, minimumWidth);
|
|
168 }
|
|
169
|
|
170 if (grabVertical) {
|
|
171 editorRect.height = Math.max(cell.height, minimumHeight);
|
|
172 }
|
|
173
|
|
174 if (horizontalAlignment is DWT.RIGHT) {
|
|
175 editorRect.x += cell.width - editorRect.width;
|
|
176 } else if (horizontalAlignment is DWT.LEFT) {
|
|
177 // do nothing - cell.x is the right answer
|
|
178 } else { // default is CENTER
|
|
179 editorRect.x += (cell.width - editorRect.width)/2;
|
|
180 }
|
|
181 // don't let the editor overlap with the + / - of the tree
|
|
182 editorRect.x = Math.max(cell.x, editorRect.x);
|
|
183
|
|
184 if (verticalAlignment is DWT.BOTTOM) {
|
|
185 editorRect.y += cell.height - editorRect.height;
|
|
186 } else if (verticalAlignment is DWT.TOP) {
|
|
187 // do nothing - cell.y is the right answer
|
|
188 } else { // default is CENTER
|
|
189 editorRect.y += (cell.height - editorRect.height)/2;
|
|
190 }
|
|
191 return editorRect;
|
|
192 }
|
|
193
|
|
194 /**
|
|
195 * Removes all associations between the TreeEditor and the row in the tree. The
|
|
196 * tree and the editor Control are <b>not</b> disposed.
|
|
197 */
|
|
198 public override void dispose () {
|
|
199 if (tree !is null && !tree.isDisposed()) {
|
|
200 if (this.column > -1 && this.column < tree.getColumnCount()){
|
|
201 TreeColumn treeColumn = tree.getColumn(this.column);
|
|
202 treeColumn.removeControlListener(columnListener);
|
|
203 }
|
|
204 if (treeListener !is null) tree.removeTreeListener(treeListener);
|
|
205 }
|
|
206 columnListener = null;
|
|
207 treeListener = null;
|
|
208 tree = null;
|
|
209 item = null;
|
|
210 column = 0;
|
|
211 timer = null;
|
|
212 super.dispose();
|
|
213 }
|
|
214
|
|
215 /**
|
|
216 * Returns the zero based index of the column of the cell being tracked by this editor.
|
|
217 *
|
|
218 * @return the zero based index of the column of the cell being tracked by this editor
|
|
219 *
|
|
220 * @since 3.1
|
|
221 */
|
|
222 public int getColumn () {
|
|
223 return column;
|
|
224 }
|
|
225
|
|
226 /**
|
|
227 * Returns the TreeItem for the row of the cell being tracked by this editor.
|
|
228 *
|
|
229 * @return the TreeItem for the row of the cell being tracked by this editor
|
|
230 */
|
|
231 public TreeItem getItem () {
|
|
232 return item;
|
|
233 }
|
|
234
|
|
235 void resize () {
|
|
236 layout();
|
|
237 /*
|
|
238 * On some platforms, the table scrolls when an item that
|
|
239 * is partially visible at the bottom of the table is
|
|
240 * selected. Ensure that the correct row is edited by
|
|
241 * laying out one more time in a timerExec().
|
|
242 */
|
|
243 if (tree !is null) {
|
|
244 Display display = tree.getDisplay();
|
|
245 display.timerExec(-1, timer);
|
|
246 display.timerExec(TIMEOUT, timer);
|
|
247 }
|
|
248 }
|
|
249
|
|
250 /**
|
|
251 * Sets the zero based index of the column of the cell being tracked by this editor.
|
|
252 *
|
|
253 * @param column the zero based index of the column of the cell being tracked by this editor
|
|
254 *
|
|
255 * @since 3.1
|
|
256 */
|
|
257 public void setColumn(int column) {
|
|
258 int columnCount = tree.getColumnCount();
|
|
259 // Separately handle the case where the tree has no TreeColumns.
|
|
260 // In this situation, there is a single default column.
|
|
261 if (columnCount is 0) {
|
|
262 this.column = (column is 0) ? 0 : -1;
|
|
263 resize();
|
|
264 return;
|
|
265 }
|
|
266 if (this.column > -1 && this.column < columnCount){
|
|
267 TreeColumn treeColumn = tree.getColumn(this.column);
|
|
268 treeColumn.removeControlListener(columnListener);
|
|
269 this.column = -1;
|
|
270 }
|
|
271
|
|
272 if (column < 0 || column >= tree.getColumnCount()) return;
|
|
273
|
|
274 this.column = column;
|
|
275 TreeColumn treeColumn = tree.getColumn(this.column);
|
|
276 treeColumn.addControlListener(columnListener);
|
|
277 resize();
|
|
278 }
|
|
279
|
|
280 public void setItem (TreeItem item) {
|
|
281 this.item = item;
|
|
282 resize();
|
|
283 }
|
|
284
|
|
285 /**
|
|
286 * Specify the Control that is to be displayed and the cell in the tree that it is to be positioned above.
|
|
287 *
|
|
288 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Tree control
|
|
289 * specified in the TreeEditor constructor.
|
|
290 *
|
|
291 * @param editor the Control that is displayed above the cell being edited
|
|
292 * @param item the TreeItem for the row of the cell being tracked by this editor
|
|
293 * @param column the zero based index of the column of the cell being tracked by this editor
|
|
294 *
|
|
295 * @since 3.1
|
|
296 */
|
|
297 public void setEditor (Control editor, TreeItem item, int column) {
|
|
298 setItem(item);
|
|
299 setColumn(column);
|
|
300 setEditor(editor);
|
|
301 }
|
|
302 public override void setEditor (Control editor) {
|
|
303 super.setEditor(editor);
|
|
304 resize();
|
|
305 }
|
|
306
|
|
307 /**
|
|
308 * Specify the Control that is to be displayed and the cell in the tree that it is to be positioned above.
|
|
309 *
|
|
310 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Tree control
|
|
311 * specified in the TreeEditor constructor.
|
|
312 *
|
|
313 * @param editor the Control that is displayed above the cell being edited
|
|
314 * @param item the TreeItem for the row of the cell being tracked by this editor
|
|
315 */
|
|
316 public void setEditor (Control editor, TreeItem item) {
|
|
317 setItem(item);
|
|
318 setEditor(editor);
|
|
319 }
|
|
320
|
|
321 public override void layout () {
|
|
322 if (tree is null || tree.isDisposed()) return;
|
|
323 if (item is null || item.isDisposed()) return;
|
|
324 int columnCount = tree.getColumnCount();
|
|
325 if (columnCount is 0 && column !is 0) return;
|
|
326 if (columnCount > 0 && (column < 0 || column >= columnCount)) return;
|
|
327 super.layout();
|
|
328 }
|
|
329
|
|
330 }
|