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