]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/custom/TableEditor.java
7692fbd46605ff27ffe795acd31232730eca5b65
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / custom / TableEditor.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2016 IBM Corporation and others.
3  *
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/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.custom;
15
16
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.events.*;
19 import org.eclipse.swt.graphics.*;
20 import org.eclipse.swt.widgets.*;
21
22 /**
23 *
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.
28 *
29 * <p> Here is an example of using a TableEditor:</p>
30 * <pre><code>
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 &lt; 10; i++) {
35 *               TableItem item = new TableItem(table, SWT.NONE);
36 *               item.setText(new String[] {"item " + i, "edit this value"});
37 *       }
38 *       column1.pack();
39 *       column2.pack();
40 *
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;
49 *
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();
55 *
56 *                       // Identify the selected row
57 *                       TableItem item = (TableItem)e.item;
58 *                       if (item == null) return;
59 *
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());
67 *                               }
68 *                       });
69 *                       newEditor.selectAll();
70 *                       newEditor.setFocus();
71 *                       editor.setEditor(newEditor, item, EDITABLECOLUMN);
72 *               }
73 *       });
74 * </code></pre>
75 *
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>
78 */
79 public class TableEditor extends ControlEditor {
80         Table table;
81         TableItem item;
82         int column = -1;
83         ControlListener columnListener;
84         Runnable timer;
85         static final int TIMEOUT = 1500;
86 /**
87 * Creates a TableEditor for the specified Table.
88 *
89 * @param table the Table Control above which this editor will be displayed
90 *
91 */
92 public TableEditor (Table table) {
93         super(table);
94         this.table = table;
95
96         columnListener = new ControlListener() {
97                 @Override
98                 public void controlMoved(ControlEvent e){
99                         layout ();
100                 }
101                 @Override
102                 public void controlResized(ControlEvent e){
103                         layout ();
104                 }
105         };
106         timer = () -> layout ();
107
108         // To be consistent with older versions of SWT, grabVertical defaults to true
109         grabVertical = true;
110 }
111 @Override
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);
120         }
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;
125                 }
126         }
127         Rectangle editorRect = new Rectangle(cell.x, cell.y, minimumWidth, minimumHeight);
128
129         if (grabHorizontal) {
130                 editorRect.width = Math.max(cell.width, minimumWidth);
131         }
132
133         if (grabVertical) {
134                 editorRect.height = Math.max(cell.height, minimumHeight);
135         }
136
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;
143         }
144
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;
151         }
152         return editorRect;
153 }
154 /**
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.
157  */
158 @Override
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);
164                 }
165         }
166         columnListener = null;
167         table = null;
168         item = null;
169         column = -1;
170         timer = null;
171         super.dispose();
172 }
173 /**
174 * Returns the zero based index of the column of the cell being tracked by this editor.
175 *
176 * @return the zero based index of the column of the cell being tracked by this editor
177 */
178 public int getColumn () {
179         return column;
180 }
181 /**
182 * Returns the TableItem for the row of the cell being tracked by this editor.
183 *
184 * @return the TableItem for the row of the cell being tracked by this editor
185 */
186 public TableItem getItem () {
187         return item;
188 }
189 void resize () {
190         layout();
191         /*
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().
196          */
197         if (table != null) {
198                 Display display = table.getDisplay();
199                 display.timerExec(-1, timer);
200                 display.timerExec(TIMEOUT, timer);
201         }
202 }
203 /**
204 * Sets the zero based index of the column of the cell being tracked by this editor.
205 *
206 * @param column the zero based index of the column of the cell being tracked by this editor
207 */
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;
214                 resize();
215                 return;
216         }
217         if (this.column > -1 && this.column < columnCount){
218                 TableColumn tableColumn = table.getColumn(this.column);
219                 tableColumn.removeControlListener(columnListener);
220                 this.column = -1;
221         }
222
223         if (column < 0  || column >= table.getColumnCount()) return;
224
225         this.column = column;
226         TableColumn tableColumn = table.getColumn(this.column);
227         tableColumn.addControlListener(columnListener);
228         resize();
229 }
230 /**
231 * Specifies the <code>TableItem</code> that is to be edited.
232 *
233 * @param item the item to be edited
234 */
235 public void setItem (TableItem item) {
236         this.item = item;
237         resize();
238 }
239 @Override
240 public void setEditor (Control editor) {
241         super.setEditor(editor);
242         resize();
243 }
244 /**
245 * Specify the Control that is to be displayed and the cell in the table that it is to be positioned above.
246 *
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.
249 *
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
253 */
254 public void setEditor (Control editor, TableItem item, int column) {
255         setItem(item);
256         setColumn(column);
257         setEditor(editor);
258 }
259 @Override
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;
266         super.layout();
267 }
268 }