1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 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 org.eclipse.swt.custom.TableEditor;
14 
15 import java.lang.all;
16 
17 
18 
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.ControlEvent;
21 import org.eclipse.swt.events.ControlListener;
22 import org.eclipse.swt.graphics.Rectangle;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.swt.widgets.Table;
26 import org.eclipse.swt.widgets.TableColumn;
27 import org.eclipse.swt.widgets.TableItem;
28 import org.eclipse.swt.custom.ControlEditor;
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, SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
40 *   TableColumn column1 = new TableColumn(table, SWT.NONE);
41 *   TableColumn column2 = new TableColumn(table, SWT.NONE);
42 *   for (int i = 0; i &lt; 10; i++) {
43 *       TableItem item = new TableItem(table, SWT.NONE);
44 *       item.setText(new String[] {"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 = SWT.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, SWT.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 * @see <a href="http://www.eclipse.org/swt/snippets/#tableeditor">TableEditor snippets</a>
85 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
86 */
87 public class TableEditor : ControlEditor {
88     Table table;
89     TableItem item;
90     int column = -1;
91     ControlListener columnListener;
92     Runnable timer;
93     static const int TIMEOUT = 1500;
94 /**
95 * Creates a TableEditor for the specified Table.
96 *
97 * @param table the Table Control above which this editor will be displayed
98 *
99 */
100 public this (Table table) {
101     super(table);
102     this.table = table;
103 
104     columnListener = new class() ControlListener {
105         public void controlMoved(ControlEvent e){
106             layout ();
107         }
108         public void controlResized(ControlEvent e){
109             layout ();
110         }
111     };
112     timer = new class() Runnable {
113         public void run() {
114             layout ();
115         }
116     };
117 
118     // To be consistent with older versions of SWT, grabVertical defaults to true
119     grabVertical = true;
120 }
121 override Rectangle computeBounds () {
122     if (item is null || column is -1 || item.isDisposed()) return new Rectangle(0, 0, 0, 0);
123     Rectangle cell = item.getBounds(column);
124     Rectangle rect = item.getImageBounds(column);
125     cell.x = rect.x + rect.width;
126     cell.width -= rect.width;
127     Rectangle area = table.getClientArea();
128     if (cell.x < area.x + area.width) {
129         if (cell.x + cell.width > area.x + area.width) {
130             cell.width = area.x + area.width - cell.x;
131         }
132     }
133     Rectangle editorRect = new Rectangle(cell.x, cell.y, minimumWidth, minimumHeight);
134 
135     if (grabHorizontal) {
136         editorRect.width = Math.max(cell.width, minimumWidth);
137     }
138 
139     if (grabVertical) {
140         editorRect.height = Math.max(cell.height, minimumHeight);
141     }
142 
143     if (horizontalAlignment is SWT.RIGHT) {
144         editorRect.x += cell.width - editorRect.width;
145     } else if (horizontalAlignment is SWT.LEFT) {
146         // do nothing - cell.x is the right answer
147     } else { // default is CENTER
148         editorRect.x += (cell.width - editorRect.width)/2;
149     }
150 
151     if (verticalAlignment is SWT.BOTTOM) {
152         editorRect.y += cell.height - editorRect.height;
153     } else if (verticalAlignment is SWT.TOP) {
154         // do nothing - cell.y is the right answer
155     } else { // default is CENTER
156         editorRect.y += (cell.height - editorRect.height)/2;
157     }
158     return editorRect;
159 }
160 /**
161  * Removes all associations between the TableEditor and the cell in the table.  The
162  * Table and the editor Control are <b>not</b> disposed.
163  */
164 public override void dispose () {
165     if (table !is null && !table.isDisposed()) {
166         if (this.column > -1 && this.column < table.getColumnCount()){
167             TableColumn tableColumn = table.getColumn(this.column);
168             tableColumn.removeControlListener(columnListener);
169         }
170     }
171     columnListener = null;
172     table = null;
173     item = null;
174     column = -1;
175     timer = null;
176     super.dispose();
177 }
178 /**
179 * Returns the zero based index of the column of the cell being tracked by this editor.
180 *
181 * @return the zero based index of the column of the cell being tracked by this editor
182 */
183 public int getColumn () {
184     return column;
185 }
186 /**
187 * Returns the TableItem for the row of the cell being tracked by this editor.
188 *
189 * @return the TableItem for the row of the cell being tracked by this editor
190 */
191 public TableItem getItem () {
192     return item;
193 }
194 void resize () {
195     layout();
196     /*
197      * On some platforms, the table scrolls when an item that
198      * is partially visible at the bottom of the table is
199      * selected.  Ensure that the correct row is edited by
200      * laying out one more time in a timerExec().
201      */
202     if (table !is null) {
203         Display display = table.getDisplay();
204         display.timerExec(-1, timer);
205         display.timerExec(TIMEOUT, timer);
206     }
207 }
208 /**
209 * Sets the zero based index of the column of the cell being tracked by this editor.
210 *
211 * @param column the zero based index of the column of the cell being tracked by this editor
212 */
213 public void setColumn(int column) {
214     int columnCount = table.getColumnCount();
215     // Separately handle the case where the table has no TableColumns.
216     // In this situation, there is a single default column.
217     if (columnCount is 0) {
218         this.column = (column is 0) ? 0 : -1;
219         resize();
220         return;
221     }
222     if (this.column > -1 && this.column < columnCount){
223         TableColumn tableColumn = table.getColumn(this.column);
224         tableColumn.removeControlListener(columnListener);
225         this.column = -1;
226     }
227 
228     if (column < 0  || column >= table.getColumnCount()) return;
229 
230     this.column = column;
231     TableColumn tableColumn = table.getColumn(this.column);
232     tableColumn.addControlListener(columnListener);
233     resize();
234 }
235 /**
236 * Specifies the <code>TableItem</code> that is to be edited.
237 *
238 * @param item the item to be edited
239 */
240 public void setItem (TableItem item) {
241     this.item = item;
242     resize();
243 }
244 public override void setEditor (Control editor) {
245     super.setEditor(editor);
246     resize();
247 }
248 /**
249 * Specify the Control that is to be displayed and the cell in the table that it is to be positioned above.
250 *
251 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Table control
252 * specified in the TableEditor constructor.
253 *
254 * @param editor the Control that is displayed above the cell being edited
255 * @param item the TableItem for the row of the cell being tracked by this editor
256 * @param column the zero based index of the column of the cell being tracked by this editor
257 */
258 public void setEditor (Control editor, TableItem item, int column) {
259     setItem(item);
260     setColumn(column);
261     setEditor(editor);
262 }
263 public override void layout () {
264     if (table is null || table.isDisposed()) return;
265     if (item is null || item.isDisposed()) return;
266     int columnCount = table.getColumnCount();
267     if (columnCount is 0 && column !is 0) return;
268     if (columnCount > 0 && (column < 0 || column >= columnCount)) return;
269     super.layout();
270 }
271 }