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