comparison org.eclipse.swt.win32.win32.x86/src/org/eclipse/swt/custom/TreeEditor.d @ 0:6dd524f61e62

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