comparison dwt/custom/TableTreeEditor.d @ 155:a5afe31f5cdd

Added custom controls
author Frank Benoit <benoit@tionex.de>
date Thu, 14 Feb 2008 19:18:37 +0100
parents
children 349b8c12e243
comparison
equal deleted inserted replaced
154:b0279a69d976 155:a5afe31f5cdd
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.TableTreeEditor;
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.Table;
25 import dwt.widgets.TableColumn;
26 import dwt.custom.ControlEditor;
27 import dwt.custom.TableTree;
28 import dwt.custom.TableTreeItem;
29
30 import dwt.dwthelper.Runnable;
31 import dwt.dwthelper.utils;
32
33 /**
34 *
35 * A TableTreeEditor is a manager for a Control that appears above a cell in a TableTree
36 * and tracks with the moving and resizing of that cell. It can be used to display a
37 * text widget above a cell in a TableTree so that the user can edit the contents of
38 * that cell. It can also be used to display a button that can launch a dialog for
39 * modifying the contents of the associated cell.
40 *
41 * <p> Here is an example of using a TableTreeEditor:
42 * <code><pre>
43 * final TableTree tableTree = new TableTree(shell, DWT.FULL_SELECTION | DWT.HIDE_SELECTION);
44 * final Table table = tableTree.getTable();
45 * TableColumn column1 = new TableColumn(table, DWT.NONE);
46 * TableColumn column2 = new TableColumn(table, DWT.NONE);
47 * for (int i = 0; i &lt; 10; i++) {
48 * TableTreeItem item = new TableTreeItem(tableTree, DWT.NONE);
49 * item.setText(0, "item " + i);
50 * item.setText(1, "edit this value");
51 * for (int j = 0; j &lt; 3; j++) {
52 * TableTreeItem subitem = new TableTreeItem(item, DWT.NONE);
53 * subitem.setText(0, "subitem " + i + " " + j);
54 * subitem.setText(1, "edit this value");
55 * }
56 * }
57 * column1.setWidth(100);
58 * column2.pack();
59 *
60 * final TableTreeEditor editor = new TableTreeEditor(tableTree);
61 * //The editor must have the same size as the cell and must
62 * //not be any smaller than 50 pixels.
63 * editor.horizontalAlignment = DWT.LEFT;
64 * editor.grabHorizontal = true;
65 * editor.minimumWidth = 50;
66 * // editing the second column
67 * final int EDITABLECOLUMN = 1;
68 *
69 * tableTree.addSelectionListener(new SelectionAdapter() {
70 * public void widgetSelected(SelectionEvent e) {
71 * // Clean up any previous editor control
72 * Control oldEditor = editor.getEditor();
73 * if (oldEditor !is null) oldEditor.dispose();
74 *
75 * // Identify the selected row
76 * TableTreeItem item = (TableTreeItem)e.item;
77 * if (item is null) return;
78 *
79 * // The control that will be the editor must be a child of the Table
80 * Text newEditor = new Text(table, DWT.NONE);
81 * newEditor.setText(item.getText(EDITABLECOLUMN));
82 * newEditor.addModifyListener(new ModifyListener() {
83 * public void modifyText(ModifyEvent e) {
84 * Text text = (Text)editor.getEditor();
85 * editor.getItem().setText(EDITABLECOLUMN, text.getText());
86 * }
87 * });
88 * newEditor.selectAll();
89 * newEditor.setFocus();
90 * editor.setEditor(newEditor, item, EDITABLECOLUMN);
91 * }
92 * });
93 * </pre></code>
94 *
95 * @deprecated As of 3.1 use TreeEditor with Tree, TreeItem and TreeColumn
96 */
97 public class TableTreeEditor : ControlEditor {
98
99 alias ControlEditor.setEditor setEditor;
100
101 TableTree tableTree;
102 TableTreeItem item;
103 int column = -1;
104 ControlListener columnListener;
105 TreeListener treeListener;
106 /**
107 * Creates a TableTreeEditor for the specified TableTree.
108 *
109 * @param tableTree the TableTree Control above which this editor will be displayed
110 *
111 */
112 public this (TableTree tableTree) {
113 super(tableTree.getTable());
114 this.tableTree = tableTree;
115
116 treeListener = new class() TreeListener {
117 Runnable runnable;
118 this() {
119 runnable = new class() Runnable {
120 public void run() {
121 if (editor is null || editor.isDisposed()) return;
122 if (this.outer.outer.tableTree.isDisposed()) return;
123 layout();
124 editor.setVisible(true);
125 }
126 };
127 }
128 public void treeCollapsed(TreeEvent e) {
129 if (editor is null || editor.isDisposed ()) return;
130 editor.setVisible(false);
131 e.display.asyncExec(runnable);
132 }
133 public void treeExpanded(TreeEvent e) {
134 if (editor is null || editor.isDisposed ()) return;
135 editor.setVisible(false);
136 e.display.asyncExec(runnable);
137 }
138 };
139 tableTree.addTreeListener(treeListener);
140
141 columnListener = new class() ControlListener {
142 public void controlMoved(ControlEvent e){
143 layout ();
144 }
145 public void controlResized(ControlEvent e){
146 layout ();
147 }
148 };
149
150 // To be consistent with older versions of DWT, grabVertical defaults to true
151 grabVertical = true;
152 }
153
154 override Rectangle computeBounds () {
155 if (item is null || column is -1 || item.isDisposed() || item.tableItem is null) return new Rectangle(0, 0, 0, 0);
156 Rectangle cell = item.getBounds(column);
157 Rectangle area = tableTree.getClientArea();
158 if (cell.x < area.x + area.width) {
159 if (cell.x + cell.width > area.x + area.width) {
160 cell.width = area.x + area.width - cell.x;
161 }
162 }
163 Rectangle editorRect = new Rectangle(cell.x, cell.y, minimumWidth, minimumHeight);
164
165 if (grabHorizontal) {
166 editorRect.width = Math.max(cell.width, minimumWidth);
167 }
168
169 if (grabVertical) {
170 editorRect.height = Math.max(cell.height, minimumHeight);
171 }
172
173 if (horizontalAlignment is DWT.RIGHT) {
174 editorRect.x += cell.width - editorRect.width;
175 } else if (horizontalAlignment is DWT.LEFT) {
176 // do nothing - cell.x is the right answer
177 } else { // default is CENTER
178 editorRect.x += (cell.width - editorRect.width)/2;
179 }
180
181 if (verticalAlignment is DWT.BOTTOM) {
182 editorRect.y += cell.height - editorRect.height;
183 } else if (verticalAlignment is DWT.TOP) {
184 // do nothing - cell.y is the right answer
185 } else { // default is CENTER
186 editorRect.y += (cell.height - editorRect.height)/2;
187 }
188 return editorRect;
189 }
190 /**
191 * Removes all associations between the TableTreeEditor and the cell in the table tree. The
192 * TableTree and the editor Control are <b>not</b> disposed.
193 */
194 public override void dispose () {
195 if (tableTree !is null && !tableTree.isDisposed()) {
196 Table table = tableTree.getTable();
197 if (table !is null && !table.isDisposed()) {
198 if (this.column > -1 && this.column < table.getColumnCount()){
199 TableColumn tableColumn = table.getColumn(this.column);
200 tableColumn.removeControlListener(columnListener);
201 }
202 }
203 if (treeListener !is null) tableTree.removeTreeListener(treeListener);
204 }
205 treeListener = null;
206 columnListener = null;
207 tableTree = null;
208 item = null;
209 column = -1;
210 super.dispose();
211 }
212 /**
213 * Returns the zero based index of the column of the cell being tracked by this editor.
214 *
215 * @return the zero based index of the column of the cell being tracked by this editor
216 */
217 public int getColumn () {
218 return column;
219 }
220 /**
221 * Returns the TableTreeItem for the row of the cell being tracked by this editor.
222 *
223 * @return the TableTreeItem for the row of the cell being tracked by this editor
224 */
225 public TableTreeItem getItem () {
226 return item;
227 }
228 public void setColumn(int column) {
229 Table table = tableTree.getTable();
230 int columnCount = table.getColumnCount();
231 // Separately handle the case where the table has no TableColumns.
232 // In this situation, there is a single default column.
233 if (columnCount is 0) {
234 this.column = (column is 0) ? 0 : -1;
235 layout();
236 return;
237 }
238 if (this.column > -1 && this.column < columnCount){
239 TableColumn tableColumn = table.getColumn(this.column);
240 tableColumn.removeControlListener(columnListener);
241 this.column = -1;
242 }
243
244 if (column < 0 || column >= table.getColumnCount()) return;
245
246 this.column = column;
247 TableColumn tableColumn = table.getColumn(this.column);
248 tableColumn.addControlListener(columnListener);
249 layout();
250 }
251 public void setItem (TableTreeItem item) {
252 this.item = item;
253 layout();
254 }
255
256 /**
257 * Specify the Control that is to be displayed and the cell in the table that it is to be positioned above.
258 *
259 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Table control
260 * specified in the TableEditor constructor.
261 *
262 * @param editor the Control that is displayed above the cell being edited
263 * @param item the TableItem for the row of the cell being tracked by this editor
264 * @param column the zero based index of the column of the cell being tracked by this editor
265 */
266 alias ControlEditor.setEditor setEditor;
267 public void setEditor (Control editor, TableTreeItem item, int column) {
268 setItem(item);
269 setColumn(column);
270 setEditor(editor);
271 }
272 public override void layout () {
273 if (tableTree is null || tableTree.isDisposed()) return;
274 if (item is null || item.isDisposed()) return;
275 Table table = tableTree.getTable();
276 int columnCount = table.getColumnCount();
277 if (columnCount is 0 && column !is 0) return;
278 if (columnCount > 0 && (column < 0 || column >= columnCount)) return;
279 super.layout();
280 }
281
282 }