Mercurial > projects > dwt-mac
comparison dwt/custom/TreeEditor.d @ 41:6337764516f1
Sync dwt/custom with dwt-linux (took copy of complete folder)
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 07 Oct 2008 16:29:55 +0200 |
parents | e831403a80a9 |
children | 63a09873578e |
comparison
equal
deleted
inserted
replaced
40:fbe68c33eeee | 41:6337764516f1 |
---|---|
1 /******************************************************************************* | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
3 * All rights reserved. This program and the accompanying materials | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | 4 * are made available under the terms of the Eclipse Public License v1.0 |
5 * which accompanies this distribution, and is available at | 5 * which accompanies this distribution, and is available at |
6 * http://www.eclipse.org/legal/epl-v10.html | 6 * http://www.eclipse.org/legal/epl-v10.html |
7 * | 7 * |
8 * Contributors: | 8 * Contributors: |
9 * IBM Corporation - initial API and implementation | 9 * IBM Corporation - initial API and implementation |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
10 *******************************************************************************/ | 12 *******************************************************************************/ |
11 module dwt.custom; | 13 module dwt.custom.TreeEditor; |
12 | 14 |
13 | 15 import dwt.dwthelper.utils; |
14 import dwt.*; | 16 |
15 import dwt.events.*; | 17 |
16 import dwt.graphics.*; | 18 import dwt.DWT; |
17 import dwt.widgets.*; | 19 import dwt.events.ControlEvent; |
20 import dwt.events.ControlListener; | |
21 import dwt.events.TreeEvent; | |
22 import dwt.events.TreeListener; | |
23 import dwt.graphics.Rectangle; | |
24 import dwt.widgets.Control; | |
25 import dwt.widgets.Display; | |
26 import dwt.widgets.Tree; | |
27 import dwt.widgets.TreeColumn; | |
28 import dwt.widgets.TreeItem; | |
29 import dwt.custom.ControlEditor; | |
30 import dwt.dwthelper.Runnable; | |
18 | 31 |
19 /** | 32 /** |
20 * | 33 * |
21 * A TreeEditor is a manager for a Control that appears above a cell in a Tree and tracks with the | 34 * 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 | 35 * moving and resizing of that cell. It can be used to display a text widget above a cell |
32 * for (int j = 0; j < 3; j++) { | 45 * for (int j = 0; j < 3; j++) { |
33 * TreeItem subItem = new TreeItem(item, DWT.NONE); | 46 * TreeItem subItem = new TreeItem(item, DWT.NONE); |
34 * subItem.setText("item " + i + " " + j); | 47 * subItem.setText("item " + i + " " + j); |
35 * } | 48 * } |
36 * } | 49 * } |
37 * | 50 * |
38 * final TreeEditor editor = new TreeEditor(tree); | 51 * final TreeEditor editor = new TreeEditor(tree); |
39 * //The editor must have the same size as the cell and must | 52 * //The editor must have the same size as the cell and must |
40 * //not be any smaller than 50 pixels. | 53 * //not be any smaller than 50 pixels. |
41 * editor.horizontalAlignment = DWT.LEFT; | 54 * editor.horizontalAlignment = DWT.LEFT; |
42 * editor.grabHorizontal = true; | 55 * editor.grabHorizontal = true; |
43 * editor.minimumWidth = 50; | 56 * editor.minimumWidth = 50; |
44 * | 57 * |
45 * tree.addSelectionListener(new SelectionAdapter() { | 58 * tree.addSelectionListener(new SelectionAdapter() { |
46 * public void widgetSelected(SelectionEvent e) { | 59 * public void widgetSelected(SelectionEvent e) { |
47 * // Clean up any previous editor control | 60 * // Clean up any previous editor control |
48 * Control oldEditor = editor.getEditor(); | 61 * Control oldEditor = editor.getEditor(); |
49 * if (oldEditor !is null) oldEditor.dispose(); | 62 * if (oldEditor !is null) oldEditor.dispose(); |
50 * | 63 * |
51 * // Identify the selected row | 64 * // Identify the selected row |
52 * TreeItem item = cast(TreeItem)e.item; | 65 * TreeItem item = (TreeItem)e.item; |
53 * if (item is null) return; | 66 * if (item is null) return; |
54 * | 67 * |
55 * // The control that will be the editor must be a child of the Tree | 68 * // The control that will be the editor must be a child of the Tree |
56 * Text newEditor = new Text(tree, DWT.NONE); | 69 * Text newEditor = new Text(tree, DWT.NONE); |
57 * newEditor.setText(item.getText()); | 70 * newEditor.setText(item.getText()); |
58 * newEditor.addModifyListener(new ModifyListener() { | 71 * newEditor.addModifyListener(new ModifyListener() { |
59 * public void modifyText(ModifyEvent e) { | 72 * public void modifyText(ModifyEvent e) { |
60 * Text text = cast(Text)editor.getEditor(); | 73 * Text text = (Text)editor.getEditor(); |
61 * editor.getItem().setText(text.getText()); | 74 * editor.getItem().setText(text.getText()); |
62 * } | 75 * } |
63 * }); | 76 * }); |
64 * newEditor.selectAll(); | 77 * newEditor.selectAll(); |
65 * newEditor.setFocus(); | 78 * newEditor.setFocus(); |
66 * editor.setEditor(newEditor, item); | 79 * editor.setEditor(newEditor, item); |
67 * } | 80 * } |
68 * }); | 81 * }); |
69 * </pre></code> | 82 * </pre></code> |
70 */ | 83 * |
71 public class TreeEditor : ControlEditor { | 84 * @see <a href="http://www.eclipse.org/swt/snippets/#treeeditor">TreeEditor snippets</a> |
85 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
86 */ | |
87 | |
88 public class TreeEditor : ControlEditor { | |
72 Tree tree; | 89 Tree tree; |
73 TreeItem item; | 90 TreeItem item; |
74 int column = 0; | 91 int column = 0; |
75 ControlListener columnListener; | 92 ControlListener columnListener; |
76 TreeListener treeListener; | 93 TreeListener treeListener; |
77 Runnable timer; | 94 Runnable timer; |
78 static final int TIMEOUT = 1500; | 95 static const int TIMEOUT = 1500; |
79 | 96 |
80 /** | 97 /** |
81 * Creates a TreeEditor for the specified Tree. | 98 * Creates a TreeEditor for the specified Tree. |
82 * | 99 * |
83 * @param tree the Tree Control above which this editor will be displayed | 100 * @param tree the Tree Control above which this editor will be displayed |
84 * | 101 * |
85 */ | 102 */ |
86 public this (Tree tree) { | 103 public this (Tree tree) { |
87 super(tree); | 104 super(tree); |
88 this.tree = tree; | 105 this.tree = tree; |
89 | 106 |
90 columnListener = new ControlListener() { | 107 columnListener = new class() ControlListener { |
91 public void controlMoved(ControlEvent e){ | 108 public void controlMoved(ControlEvent e){ |
92 layout(); | 109 layout(); |
93 } | 110 } |
94 public void controlResized(ControlEvent e){ | 111 public void controlResized(ControlEvent e){ |
95 layout(); | 112 layout(); |
96 } | 113 } |
97 }; | 114 }; |
98 timer = new Runnable () { | 115 timer = new class() Runnable { |
99 public void run() { | 116 public void run() { |
100 layout (); | 117 layout (); |
101 } | 118 } |
102 }; | 119 }; |
103 treeListener = new TreeListener () { | 120 treeListener = new class() TreeListener { |
104 final Runnable runnable = new Runnable() { | 121 Runnable runnable; |
105 public void run() { | 122 this(){ |
106 if (editor is null || editor.isDisposed()) return; | 123 runnable = new class() Runnable { |
107 if (TreeEditor.this.tree.isDisposed()) return; | 124 public void run() { |
108 layout(); | 125 if (this.outer.outer.editor is null || this.outer.outer.editor.isDisposed()) return; |
109 editor.setVisible(true); | 126 if (this.outer.outer.tree.isDisposed()) return; |
110 } | 127 layout(); |
111 }; | 128 this.outer.outer.editor.setVisible(true); |
129 } | |
130 }; | |
131 } | |
112 public void treeCollapsed(TreeEvent e) { | 132 public void treeCollapsed(TreeEvent e) { |
113 if (editor is null || editor.isDisposed ()) return; | 133 if (this.outer.editor is null || this.outer.editor.isDisposed ()) return; |
114 editor.setVisible(false); | 134 this.outer.editor.setVisible(false); |
115 e.display.asyncExec(runnable); | 135 e.display.asyncExec(runnable); |
116 } | 136 } |
117 public void treeExpanded(TreeEvent e) { | 137 public void treeExpanded(TreeEvent e) { |
118 if (editor is null || editor.isDisposed ()) return; | 138 if (this.outer.editor is null || this.outer.editor.isDisposed ()) return; |
119 editor.setVisible(false); | 139 this.outer.editor.setVisible(false); |
120 e.display.asyncExec(runnable); | 140 e.display.asyncExec(runnable); |
121 } | 141 } |
122 }; | 142 }; |
123 tree.addTreeListener(treeListener); | 143 tree.addTreeListener(treeListener); |
124 | 144 |
125 // To be consistent with older versions of DWT, grabVertical defaults to true | 145 // To be consistent with older versions of DWT, grabVertical defaults to true |
126 grabVertical = true; | 146 grabVertical = true; |
127 } | 147 } |
128 | 148 |
129 Rectangle computeBounds () { | 149 override Rectangle computeBounds () { |
130 if (item is null || column is -1 || item.isDisposed()) return new Rectangle(0, 0, 0, 0); | 150 if (item is null || column is -1 || item.isDisposed()) return new Rectangle(0, 0, 0, 0); |
131 Rectangle cell = item.getBounds(column); | 151 Rectangle cell = item.getBounds(column); |
132 Rectangle rect = item.getImageBounds(column); | 152 Rectangle rect = item.getImageBounds(column); |
133 cell.x = rect.x + rect.width; | 153 cell.x = rect.x + rect.width; |
134 cell.width -= rect.width; | 154 cell.width -= rect.width; |
140 } | 160 } |
141 Rectangle editorRect = new Rectangle(cell.x, cell.y, minimumWidth, minimumHeight); | 161 Rectangle editorRect = new Rectangle(cell.x, cell.y, minimumWidth, minimumHeight); |
142 | 162 |
143 if (grabHorizontal) { | 163 if (grabHorizontal) { |
144 if (tree.getColumnCount() is 0) { | 164 if (tree.getColumnCount() is 0) { |
145 // Bounds of tree item only include the text area - stretch out to include | 165 // Bounds of tree item only include the text area - stretch out to include |
146 // entire client area | 166 // entire client area |
147 cell.width = area.x + area.width - cell.x; | 167 cell.width = area.x + area.width - cell.x; |
148 } | 168 } |
149 editorRect.width = Math.max(cell.width, minimumWidth); | 169 editorRect.width = Math.max(cell.width, minimumWidth); |
150 } | 170 } |
151 | 171 |
152 if (grabVertical) { | 172 if (grabVertical) { |
153 editorRect.height = Math.max(cell.height, minimumHeight); | 173 editorRect.height = Math.max(cell.height, minimumHeight); |
154 } | 174 } |
155 | 175 |
156 if (horizontalAlignment is DWT.RIGHT) { | 176 if (horizontalAlignment is DWT.RIGHT) { |
157 editorRect.x += cell.width - editorRect.width; | 177 editorRect.x += cell.width - editorRect.width; |
158 } else if (horizontalAlignment is DWT.LEFT) { | 178 } else if (horizontalAlignment is DWT.LEFT) { |
159 // do nothing - cell.x is the right answer | 179 // do nothing - cell.x is the right answer |
160 } else { // default is CENTER | 180 } else { // default is CENTER |
161 editorRect.x += (cell.width - editorRect.width)/2; | 181 editorRect.x += (cell.width - editorRect.width)/2; |
162 } | 182 } |
163 // don't let the editor overlap with the +/- of the tree | 183 // don't let the editor overlap with the + / - of the tree |
164 editorRect.x = Math.max(cell.x, editorRect.x); | 184 editorRect.x = Math.max(cell.x, editorRect.x); |
165 | 185 |
166 if (verticalAlignment is DWT.BOTTOM) { | 186 if (verticalAlignment is DWT.BOTTOM) { |
167 editorRect.y += cell.height - editorRect.height; | 187 editorRect.y += cell.height - editorRect.height; |
168 } else if (verticalAlignment is DWT.TOP) { | 188 } else if (verticalAlignment is DWT.TOP) { |
169 // do nothing - cell.y is the right answer | 189 // do nothing - cell.y is the right answer |
170 } else { // default is CENTER | 190 } else { // default is CENTER |
175 | 195 |
176 /** | 196 /** |
177 * Removes all associations between the TreeEditor and the row in the tree. The | 197 * Removes all associations between the TreeEditor and the row in the tree. The |
178 * tree and the editor Control are <b>not</b> disposed. | 198 * tree and the editor Control are <b>not</b> disposed. |
179 */ | 199 */ |
180 public void dispose () { | 200 public override void dispose () { |
181 if (tree !is null && !tree.isDisposed()) { | 201 if (tree !is null && !tree.isDisposed()) { |
182 if (this.column > -1 && this.column < tree.getColumnCount()){ | 202 if (this.column > -1 && this.column < tree.getColumnCount()){ |
183 TreeColumn treeColumn = tree.getColumn(this.column); | 203 TreeColumn treeColumn = tree.getColumn(this.column); |
184 treeColumn.removeControlListener(columnListener); | 204 treeColumn.removeControlListener(columnListener); |
185 } | 205 } |
229 } | 249 } |
230 } | 250 } |
231 | 251 |
232 /** | 252 /** |
233 * Sets the zero based index of the column of the cell being tracked by this editor. | 253 * Sets the zero based index of the column of the cell being tracked by this editor. |
234 * | 254 * |
235 * @param column the zero based index of the column of the cell being tracked by this editor | 255 * @param column the zero based index of the column of the cell being tracked by this editor |
236 * | 256 * |
237 * @since 3.1 | 257 * @since 3.1 |
238 */ | 258 */ |
239 public void setColumn(int column) { | 259 public void setColumn(int column) { |
249 TreeColumn treeColumn = tree.getColumn(this.column); | 269 TreeColumn treeColumn = tree.getColumn(this.column); |
250 treeColumn.removeControlListener(columnListener); | 270 treeColumn.removeControlListener(columnListener); |
251 this.column = -1; | 271 this.column = -1; |
252 } | 272 } |
253 | 273 |
254 if (column < 0 || column >= tree.getColumnCount()) return; | 274 if (column < 0 || column >= tree.getColumnCount()) return; |
255 | 275 |
256 this.column = column; | 276 this.column = column; |
257 TreeColumn treeColumn = tree.getColumn(this.column); | 277 TreeColumn treeColumn = tree.getColumn(this.column); |
258 treeColumn.addControlListener(columnListener); | 278 treeColumn.addControlListener(columnListener); |
259 resize(); | 279 resize(); |
260 } | 280 } |
261 | 281 |
282 /** | |
283 * Specifies the <code>TreeItem</code> that is to be edited. | |
284 * | |
285 * @param item the item to be edited | |
286 */ | |
262 public void setItem (TreeItem item) { | 287 public void setItem (TreeItem item) { |
263 this.item = item; | 288 this.item = item; |
264 resize(); | 289 resize(); |
265 } | 290 } |
266 | 291 |
267 /** | 292 /** |
268 * Specify the Control that is to be displayed and the cell in the tree that it is to be positioned above. | 293 * Specify the Control that is to be displayed and the cell in the tree that it is to be positioned above. |
269 * | 294 * |
270 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Tree control | 295 * <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. | 296 * specified in the TreeEditor constructor. |
272 * | 297 * |
273 * @param editor the Control that is displayed above the cell being edited | 298 * @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 | 299 * @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 | 300 * @param column the zero based index of the column of the cell being tracked by this editor |
276 * | 301 * |
277 * @since 3.1 | 302 * @since 3.1 |
279 public void setEditor (Control editor, TreeItem item, int column) { | 304 public void setEditor (Control editor, TreeItem item, int column) { |
280 setItem(item); | 305 setItem(item); |
281 setColumn(column); | 306 setColumn(column); |
282 setEditor(editor); | 307 setEditor(editor); |
283 } | 308 } |
284 public void setEditor (Control editor) { | 309 public override void setEditor (Control editor) { |
285 super.setEditor(editor); | 310 super.setEditor(editor); |
286 resize(); | 311 resize(); |
287 } | 312 } |
288 | 313 |
289 /** | 314 /** |
290 * Specify the Control that is to be displayed and the cell in the tree that it is to be positioned above. | 315 * Specify the Control that is to be displayed and the cell in the tree that it is to be positioned above. |
291 * | 316 * |
292 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Tree control | 317 * <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. | 318 * specified in the TreeEditor constructor. |
294 * | 319 * |
295 * @param editor the Control that is displayed above the cell being edited | 320 * @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 | 321 * @param item the TreeItem for the row of the cell being tracked by this editor |
297 */ | 322 */ |
298 public void setEditor (Control editor, TreeItem item) { | 323 public void setEditor (Control editor, TreeItem item) { |
299 setItem(item); | 324 setItem(item); |
300 setEditor(editor); | 325 setEditor(editor); |
301 } | 326 } |
302 | 327 |
303 public void layout () { | 328 public override void layout () { |
304 if (tree is null || tree.isDisposed()) return; | 329 if (tree is null || tree.isDisposed()) return; |
305 if (item is null || item.isDisposed()) return; | 330 if (item is null || item.isDisposed()) return; |
306 int columnCount = tree.getColumnCount(); | 331 int columnCount = tree.getColumnCount(); |
307 if (columnCount is 0 && column !is 0) return; | 332 if (columnCount is 0 && column !is 0) return; |
308 if (columnCount > 0 && (column < 0 || column >= columnCount)) return; | 333 if (columnCount > 0 && (column < 0 || column >= columnCount)) return; |
309 super.layout(); | 334 super.layout(); |
310 } | 335 } |
311 } | 336 |
337 } |