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