1 /*******************************************************************************
2 * Copyright (c) 2000, 2016 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.custom;
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.events.*;
19 import org.eclipse.swt.graphics.*;
20 import org.eclipse.swt.widgets.*;
24 * A TableEditor is a manager for a Control that appears above a cell in a Table and tracks with the
25 * moving and resizing of that cell. It can be used to display a text widget above a cell
26 * in a Table so that the user can edit the contents of that cell. It can also be used to display
27 * a button that can launch a dialog for modifying the contents of the associated cell.
29 * <p> Here is an example of using a TableEditor:</p>
31 * final Table table = new Table(shell, SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
32 * TableColumn column1 = new TableColumn(table, SWT.NONE);
33 * TableColumn column2 = new TableColumn(table, SWT.NONE);
34 * for (int i = 0; i < 10; i++) {
35 * TableItem item = new TableItem(table, SWT.NONE);
36 * item.setText(new String[] {"item " + i, "edit this value"});
41 * final TableEditor editor = new TableEditor(table);
42 * //The editor must have the same size as the cell and must
43 * //not be any smaller than 50 pixels.
44 * editor.horizontalAlignment = SWT.LEFT;
45 * editor.grabHorizontal = true;
46 * editor.minimumWidth = 50;
47 * // editing the second column
48 * final int EDITABLECOLUMN = 1;
50 * table.addSelectionListener(new SelectionAdapter() {
51 * public void widgetSelected(SelectionEvent e) {
52 * // Clean up any previous editor control
53 * Control oldEditor = editor.getEditor();
54 * if (oldEditor != null) oldEditor.dispose();
56 * // Identify the selected row
57 * TableItem item = (TableItem)e.item;
58 * if (item == null) return;
60 * // The control that will be the editor must be a child of the Table
61 * Text newEditor = new Text(table, SWT.NONE);
62 * newEditor.setText(item.getText(EDITABLECOLUMN));
63 * newEditor.addModifyListener(new ModifyListener() {
64 * public void modifyText(ModifyEvent e) {
65 * Text text = (Text)editor.getEditor();
66 * editor.getItem().setText(EDITABLECOLUMN, text.getText());
69 * newEditor.selectAll();
70 * newEditor.setFocus();
71 * editor.setEditor(newEditor, item, EDITABLECOLUMN);
76 * @see <a href="http://www.eclipse.org/swt/snippets/#tableeditor">TableEditor snippets</a>
77 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
79 public class TableEditor extends ControlEditor {
83 ControlListener columnListener;
85 static final int TIMEOUT = 1500;
87 * Creates a TableEditor for the specified Table.
89 * @param table the Table Control above which this editor will be displayed
92 public TableEditor (Table table) {
96 columnListener = new ControlListener() {
98 public void controlMoved(ControlEvent e){
102 public void controlResized(ControlEvent e){
106 timer = () -> layout ();
108 // To be consistent with older versions of SWT, grabVertical defaults to true
112 Rectangle computeBounds () {
113 if (item == null || column == -1 || item.isDisposed()) return new Rectangle(0, 0, 0, 0);
114 Rectangle cell = item.getBounds(column);
115 Rectangle rect = item.getImageBounds(column);
116 if (rect.width != 0) {
117 int imageGap = Math.max(rect.x - cell.x, 0);
118 cell.x = rect.x + rect.width;
119 cell.width -= (imageGap + rect.width);
121 Rectangle area = table.getClientArea();
122 if (cell.x < area.x + area.width) {
123 if (cell.x + cell.width > area.x + area.width) {
124 cell.width = area.x + area.width - cell.x;
127 Rectangle editorRect = new Rectangle(cell.x, cell.y, minimumWidth, minimumHeight);
129 if (grabHorizontal) {
130 editorRect.width = Math.max(cell.width, minimumWidth);
134 editorRect.height = Math.max(cell.height, minimumHeight);
137 if (horizontalAlignment == SWT.RIGHT) {
138 editorRect.x += cell.width - editorRect.width;
139 } else if (horizontalAlignment == SWT.LEFT) {
140 // do nothing - cell.x is the right answer
141 } else { // default is CENTER
142 editorRect.x += (cell.width - editorRect.width)/2;
145 if (verticalAlignment == SWT.BOTTOM) {
146 editorRect.y += cell.height - editorRect.height;
147 } else if (verticalAlignment == SWT.TOP) {
148 // do nothing - cell.y is the right answer
149 } else { // default is CENTER
150 editorRect.y += (cell.height - editorRect.height)/2;
155 * Removes all associations between the TableEditor and the cell in the table. The
156 * Table and the editor Control are <b>not</b> disposed.
159 public void dispose () {
160 if (table != null && !table.isDisposed()) {
161 if (this.column > -1 && this.column < table.getColumnCount()){
162 TableColumn tableColumn = table.getColumn(this.column);
163 tableColumn.removeControlListener(columnListener);
166 columnListener = null;
174 * Returns the zero based index of the column of the cell being tracked by this editor.
176 * @return the zero based index of the column of the cell being tracked by this editor
178 public int getColumn () {
182 * Returns the TableItem for the row of the cell being tracked by this editor.
184 * @return the TableItem for the row of the cell being tracked by this editor
186 public TableItem getItem () {
192 * On some platforms, the table scrolls when an item that
193 * is partially visible at the bottom of the table is
194 * selected. Ensure that the correct row is edited by
195 * laying out one more time in a timerExec().
198 Display display = table.getDisplay();
199 display.timerExec(-1, timer);
200 display.timerExec(TIMEOUT, timer);
204 * Sets the zero based index of the column of the cell being tracked by this editor.
206 * @param column the zero based index of the column of the cell being tracked by this editor
208 public void setColumn(int column) {
209 int columnCount = table.getColumnCount();
210 // Separately handle the case where the table has no TableColumns.
211 // In this situation, there is a single default column.
212 if (columnCount == 0) {
213 this.column = (column == 0) ? 0 : -1;
217 if (this.column > -1 && this.column < columnCount){
218 TableColumn tableColumn = table.getColumn(this.column);
219 tableColumn.removeControlListener(columnListener);
223 if (column < 0 || column >= table.getColumnCount()) return;
225 this.column = column;
226 TableColumn tableColumn = table.getColumn(this.column);
227 tableColumn.addControlListener(columnListener);
231 * Specifies the <code>TableItem</code> that is to be edited.
233 * @param item the item to be edited
235 public void setItem (TableItem item) {
240 public void setEditor (Control editor) {
241 super.setEditor(editor);
245 * Specify the Control that is to be displayed and the cell in the table that it is to be positioned above.
247 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Table control
248 * specified in the TableEditor constructor.
250 * @param editor the Control that is displayed above the cell being edited
251 * @param item the TableItem for the row of the cell being tracked by this editor
252 * @param column the zero based index of the column of the cell being tracked by this editor
254 public void setEditor (Control editor, TableItem item, int column) {
260 public void layout () {
261 if (table == null || table.isDisposed()) return;
262 if (item == null || item.isDisposed()) return;
263 int columnCount = table.getColumnCount();
264 if (columnCount == 0 && column != 0) return;
265 if (columnCount > 0 && (column < 0 || column >= columnCount)) return;