comparison dwt/custom/TableEditor.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 07399639c0c8
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.TableEditor;
12 14
13 15 import dwt.dwthelper.utils;
14 import dwt.*; 16
15 import dwt.events.*; 17
16 import dwt.graphics.*; 18
17 import dwt.widgets.*; 19 import dwt.DWT;
20 import dwt.events.ControlEvent;
21 import dwt.events.ControlListener;
22 import dwt.graphics.Rectangle;
23 import dwt.widgets.Control;
24 import dwt.widgets.Display;
25 import dwt.widgets.Table;
26 import dwt.widgets.TableColumn;
27 import dwt.widgets.TableItem;
28 import dwt.custom.ControlEditor;
29 import dwt.dwthelper.Runnable;
18 30
19 /** 31 /**
20 * 32 *
21 * A TableEditor is a manager for a Control that appears above a cell in a Table and tracks with the 33 * A TableEditor is a manager for a Control that appears above a cell in a Table and tracks with the
22 * moving and resizing of that cell. It can be used to display a text widget above a cell 34 * moving and resizing of that cell. It can be used to display a text widget above a cell
32 * TableItem item = new TableItem(table, DWT.NONE); 44 * TableItem item = new TableItem(table, DWT.NONE);
33 * item.setText(new String[] {"item " + i, "edit this value"}); 45 * item.setText(new String[] {"item " + i, "edit this value"});
34 * } 46 * }
35 * column1.pack(); 47 * column1.pack();
36 * column2.pack(); 48 * column2.pack();
37 * 49 *
38 * final TableEditor editor = new TableEditor(table); 50 * final TableEditor editor = new TableEditor(table);
39 * //The editor must have the same size as the cell and must 51 * //The editor must have the same size as the cell and must
40 * //not be any smaller than 50 pixels. 52 * //not be any smaller than 50 pixels.
41 * editor.horizontalAlignment = DWT.LEFT; 53 * editor.horizontalAlignment = DWT.LEFT;
42 * editor.grabHorizontal = true; 54 * editor.grabHorizontal = true;
43 * editor.minimumWidth = 50; 55 * editor.minimumWidth = 50;
44 * // editing the second column 56 * // editing the second column
45 * final int EDITABLECOLUMN = 1; 57 * final int EDITABLECOLUMN = 1;
46 * 58 *
47 * table.addSelectionListener(new SelectionAdapter() { 59 * table.addSelectionListener(new SelectionAdapter() {
48 * public void widgetSelected(SelectionEvent e) { 60 * public void widgetSelected(SelectionEvent e) {
49 * // Clean up any previous editor control 61 * // Clean up any previous editor control
50 * Control oldEditor = editor.getEditor(); 62 * Control oldEditor = editor.getEditor();
51 * if (oldEditor !is null) oldEditor.dispose(); 63 * if (oldEditor !is null) oldEditor.dispose();
52 * 64 *
53 * // Identify the selected row 65 * // Identify the selected row
54 * TableItem item = cast(TableItem)e.item; 66 * TableItem item = (TableItem)e.item;
55 * if (item is null) return; 67 * if (item is null) return;
56 * 68 *
57 * // The control that will be the editor must be a child of the Table 69 * // The control that will be the editor must be a child of the Table
58 * Text newEditor = new Text(table, DWT.NONE); 70 * Text newEditor = new Text(table, DWT.NONE);
59 * newEditor.setText(item.getText(EDITABLECOLUMN)); 71 * newEditor.setText(item.getText(EDITABLECOLUMN));
60 * newEditor.addModifyListener(new ModifyListener() { 72 * newEditor.addModifyListener(new ModifyListener() {
61 * public void modifyText(ModifyEvent e) { 73 * public void modifyText(ModifyEvent e) {
62 * Text text = cast(Text)editor.getEditor(); 74 * Text text = (Text)editor.getEditor();
63 * editor.getItem().setText(EDITABLECOLUMN, text.getText()); 75 * editor.getItem().setText(EDITABLECOLUMN, text.getText());
64 * } 76 * }
65 * }); 77 * });
66 * newEditor.selectAll(); 78 * newEditor.selectAll();
67 * newEditor.setFocus(); 79 * newEditor.setFocus();
68 * editor.setEditor(newEditor, item, EDITABLECOLUMN); 80 * editor.setEditor(newEditor, item, EDITABLECOLUMN);
69 * } 81 * }
70 * }); 82 * });
71 * </pre></code> 83 * </pre></code>
84 *
85 * @see <a href="http://www.eclipse.org/swt/snippets/#tableeditor">TableEditor snippets</a>
86 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
72 */ 87 */
73 public class TableEditor : ControlEditor { 88 public class TableEditor : ControlEditor {
74 Table table; 89 Table table;
75 TableItem item; 90 TableItem item;
76 int column = -1; 91 int column = -1;
84 * 99 *
85 */ 100 */
86 public this (Table table) { 101 public this (Table table) {
87 super(table); 102 super(table);
88 this.table = table; 103 this.table = table;
89 104
90 columnListener = new ControlListener() { 105 columnListener = new class() ControlListener {
91 public void controlMoved(ControlEvent e){ 106 public void controlMoved(ControlEvent e){
92 layout (); 107 layout ();
93 } 108 }
94 public void controlResized(ControlEvent e){ 109 public void controlResized(ControlEvent e){
95 layout (); 110 layout ();
96 } 111 }
97 }; 112 };
98 timer = new Runnable () { 113 timer = new class() Runnable {
99 public void run() { 114 public void run() {
100 layout (); 115 layout ();
101 } 116 }
102 }; 117 };
103 118
104 // To be consistent with older versions of DWT, grabVertical defaults to true 119 // To be consistent with older versions of DWT, grabVertical defaults to true
105 grabVertical = true; 120 grabVertical = true;
106 } 121 }
107 Rectangle computeBounds () { 122 override Rectangle computeBounds () {
108 if (item is null || column is -1 || item.isDisposed()) return new Rectangle(0, 0, 0, 0); 123 if (item is null || column is -1 || item.isDisposed()) return new Rectangle(0, 0, 0, 0);
109 Rectangle cell = item.getBounds(column); 124 Rectangle cell = item.getBounds(column);
110 Rectangle rect = item.getImageBounds(column); 125 Rectangle rect = item.getImageBounds(column);
111 cell.x = rect.x + rect.width; 126 cell.x = rect.x + rect.width;
112 cell.width -= rect.width; 127 cell.width -= rect.width;
119 Rectangle editorRect = new Rectangle(cell.x, cell.y, minimumWidth, minimumHeight); 134 Rectangle editorRect = new Rectangle(cell.x, cell.y, minimumWidth, minimumHeight);
120 135
121 if (grabHorizontal) { 136 if (grabHorizontal) {
122 editorRect.width = Math.max(cell.width, minimumWidth); 137 editorRect.width = Math.max(cell.width, minimumWidth);
123 } 138 }
124 139
125 if (grabVertical) { 140 if (grabVertical) {
126 editorRect.height = Math.max(cell.height, minimumHeight); 141 editorRect.height = Math.max(cell.height, minimumHeight);
127 } 142 }
128 143
129 if (horizontalAlignment is DWT.RIGHT) { 144 if (horizontalAlignment is DWT.RIGHT) {
130 editorRect.x += cell.width - editorRect.width; 145 editorRect.x += cell.width - editorRect.width;
131 } else if (horizontalAlignment is DWT.LEFT) { 146 } else if (horizontalAlignment is DWT.LEFT) {
132 // do nothing - cell.x is the right answer 147 // do nothing - cell.x is the right answer
133 } else { // default is CENTER 148 } else { // default is CENTER
134 editorRect.x += (cell.width - editorRect.width)/2; 149 editorRect.x += (cell.width - editorRect.width)/2;
135 } 150 }
136 151
137 if (verticalAlignment is DWT.BOTTOM) { 152 if (verticalAlignment is DWT.BOTTOM) {
138 editorRect.y += cell.height - editorRect.height; 153 editorRect.y += cell.height - editorRect.height;
139 } else if (verticalAlignment is DWT.TOP) { 154 } else if (verticalAlignment is DWT.TOP) {
140 // do nothing - cell.y is the right answer 155 // do nothing - cell.y is the right answer
141 } else { // default is CENTER 156 } else { // default is CENTER
145 } 160 }
146 /** 161 /**
147 * Removes all associations between the TableEditor and the cell in the table. The 162 * Removes all associations between the TableEditor and the cell in the table. The
148 * Table and the editor Control are <b>not</b> disposed. 163 * Table and the editor Control are <b>not</b> disposed.
149 */ 164 */
150 public void dispose () { 165 public override void dispose () {
151 if (table !is null && !table.isDisposed()) { 166 if (table !is null && !table.isDisposed()) {
152 if (this.column > -1 && this.column < table.getColumnCount()){ 167 if (this.column > -1 && this.column < table.getColumnCount()){
153 TableColumn tableColumn = table.getColumn(this.column); 168 TableColumn tableColumn = table.getColumn(this.column);
154 tableColumn.removeControlListener(columnListener); 169 tableColumn.removeControlListener(columnListener);
155 } 170 }
191 display.timerExec(TIMEOUT, timer); 206 display.timerExec(TIMEOUT, timer);
192 } 207 }
193 } 208 }
194 /** 209 /**
195 * Sets the zero based index of the column of the cell being tracked by this editor. 210 * Sets the zero based index of the column of the cell being tracked by this editor.
196 * 211 *
197 * @param column the zero based index of the column of the cell being tracked by this editor 212 * @param column the zero based index of the column of the cell being tracked by this editor
198 */ 213 */
199 public void setColumn(int column) { 214 public void setColumn(int column) {
200 int columnCount = table.getColumnCount(); 215 int columnCount = table.getColumnCount();
201 // Separately handle the case where the table has no TableColumns. 216 // Separately handle the case where the table has no TableColumns.
202 // In this situation, there is a single default column. 217 // In this situation, there is a single default column.
209 TableColumn tableColumn = table.getColumn(this.column); 224 TableColumn tableColumn = table.getColumn(this.column);
210 tableColumn.removeControlListener(columnListener); 225 tableColumn.removeControlListener(columnListener);
211 this.column = -1; 226 this.column = -1;
212 } 227 }
213 228
214 if (column < 0 || column >= table.getColumnCount()) return; 229 if (column < 0 || column >= table.getColumnCount()) return;
215 230
216 this.column = column; 231 this.column = column;
217 TableColumn tableColumn = table.getColumn(this.column); 232 TableColumn tableColumn = table.getColumn(this.column);
218 tableColumn.addControlListener(columnListener); 233 tableColumn.addControlListener(columnListener);
219 resize(); 234 resize();
220 } 235 }
221 public void setItem (TableItem item) { 236 /**
237 * Specifies the <code>TableItem</code> that is to be edited.
238 *
239 * @param item the item to be edited
240 */
241 public void setItem (TableItem item) {
222 this.item = item; 242 this.item = item;
223 resize(); 243 resize();
224 } 244 }
225 public void setEditor (Control editor) { 245 public override void setEditor (Control editor) {
226 super.setEditor(editor); 246 super.setEditor(editor);
227 resize(); 247 resize();
228 } 248 }
229 /** 249 /**
230 * Specify the Control that is to be displayed and the cell in the table that it is to be positioned above. 250 * Specify the Control that is to be displayed and the cell in the table that it is to be positioned above.
231 * 251 *
232 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Table control 252 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Table control
233 * specified in the TableEditor constructor. 253 * specified in the TableEditor constructor.
234 * 254 *
235 * @param editor the Control that is displayed above the cell being edited 255 * @param editor the Control that is displayed above the cell being edited
236 * @param item the TableItem for the row of the cell being tracked by this editor 256 * @param item the TableItem for the row of the cell being tracked by this editor
237 * @param column the zero based index of the column of the cell being tracked by this editor 257 * @param column the zero based index of the column of the cell being tracked by this editor
238 */ 258 */
239 public void setEditor (Control editor, TableItem item, int column) { 259 public void setEditor (Control editor, TableItem item, int column) {
240 setItem(item); 260 setItem(item);
241 setColumn(column); 261 setColumn(column);
242 setEditor(editor); 262 setEditor(editor);
243 } 263 }
244 public void layout () { 264 public override void layout () {
245 if (table is null || table.isDisposed()) return; 265 if (table is null || table.isDisposed()) return;
246 if (item is null || item.isDisposed()) return; 266 if (item is null || item.isDisposed()) return;
247 int columnCount = table.getColumnCount(); 267 int columnCount = table.getColumnCount();
248 if (columnCount is 0 && column !is 0) return; 268 if (columnCount is 0 && column !is 0) return;
249 if (columnCount > 0 && (column < 0 || column >= columnCount)) return; 269 if (columnCount > 0 && (column < 0 || column >= columnCount)) return;