KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > custom > TableEditor


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  *******************************************************************************/

11 package org.eclipse.swt.custom;
12
13
14 import org.eclipse.swt.*;
15 import org.eclipse.swt.events.*;
16 import org.eclipse.swt.graphics.*;
17 import org.eclipse.swt.widgets.*;
18
19 /**
20 *
21 * A TableEditor is a manager for a Control that appears above a cell in a Table and tracks with the
22 * moving and resizing of that cell. It can be used to display a text widget above a cell
23 * in a Table so that the user can edit the contents of that cell. It can also be used to display
24 * a button that can launch a dialog for modifying the contents of the associated cell.
25 *
26 * <p> Here is an example of using a TableEditor:
27 * <code><pre>
28 * final Table table = new Table(shell, SWT.FULL_SELECTION | SWT.HIDE_SELECTION);
29 * TableColumn column1 = new TableColumn(table, SWT.NONE);
30 * TableColumn column2 = new TableColumn(table, SWT.NONE);
31 * for (int i = 0; i &lt; 10; i++) {
32 * TableItem item = new TableItem(table, SWT.NONE);
33 * item.setText(new String[] {"item " + i, "edit this value"});
34 * }
35 * column1.pack();
36 * column2.pack();
37 *
38 * final TableEditor editor = new TableEditor(table);
39 * //The editor must have the same size as the cell and must
40 * //not be any smaller than 50 pixels.
41 * editor.horizontalAlignment = SWT.LEFT;
42 * editor.grabHorizontal = true;
43 * editor.minimumWidth = 50;
44 * // editing the second column
45 * final int EDITABLECOLUMN = 1;
46 *
47 * table.addSelectionListener(new SelectionAdapter() {
48 * public void widgetSelected(SelectionEvent e) {
49 * // Clean up any previous editor control
50 * Control oldEditor = editor.getEditor();
51 * if (oldEditor != null) oldEditor.dispose();
52 *
53 * // Identify the selected row
54 * TableItem item = (TableItem)e.item;
55 * if (item == null) return;
56 *
57 * // The control that will be the editor must be a child of the Table
58 * Text newEditor = new Text(table, SWT.NONE);
59 * newEditor.setText(item.getText(EDITABLECOLUMN));
60 * newEditor.addModifyListener(new ModifyListener() {
61 * public void modifyText(ModifyEvent e) {
62 * Text text = (Text)editor.getEditor();
63 * editor.getItem().setText(EDITABLECOLUMN, text.getText());
64 * }
65 * });
66 * newEditor.selectAll();
67 * newEditor.setFocus();
68 * editor.setEditor(newEditor, item, EDITABLECOLUMN);
69 * }
70 * });
71 * </pre></code>
72 */

73 public class TableEditor extends ControlEditor {
74     Table table;
75     TableItem item;
76     int column = -1;
77     ControlListener columnListener;
78     Runnable JavaDoc timer;
79     static final int TIMEOUT = 1500;
80 /**
81 * Creates a TableEditor for the specified Table.
82 *
83 * @param table the Table Control above which this editor will be displayed
84 *
85 */

86 public TableEditor (Table table) {
87     super(table);
88     this.table = table;
89     
90     columnListener = new ControlListener() {
91         public void controlMoved(ControlEvent e){
92             layout ();
93         }
94         public void controlResized(ControlEvent e){
95             layout ();
96         }
97     };
98     timer = new Runnable JavaDoc () {
99         public void run() {
100             layout ();
101         }
102     };
103     
104     // To be consistent with older versions of SWT, grabVertical defaults to true
105
grabVertical = true;
106 }
107 Rectangle computeBounds () {
108     if (item == null || column == -1 || item.isDisposed()) return new Rectangle(0, 0, 0, 0);
109     Rectangle cell = item.getBounds(column);
110     Rectangle rect = item.getImageBounds(column);
111     cell.x = rect.x + rect.width;
112     cell.width -= rect.width;
113     Rectangle area = table.getClientArea();
114     if (cell.x < area.x + area.width) {
115         if (cell.x + cell.width > area.x + area.width) {
116             cell.width = area.x + area.width - cell.x;
117         }
118     }
119     Rectangle editorRect = new Rectangle(cell.x, cell.y, minimumWidth, minimumHeight);
120
121     if (grabHorizontal) {
122         editorRect.width = Math.max(cell.width, minimumWidth);
123     }
124     
125     if (grabVertical) {
126         editorRect.height = Math.max(cell.height, minimumHeight);
127     }
128     
129     if (horizontalAlignment == SWT.RIGHT) {
130         editorRect.x += cell.width - editorRect.width;
131     } else if (horizontalAlignment == SWT.LEFT) {
132         // do nothing - cell.x is the right answer
133
} else { // default is CENTER
134
editorRect.x += (cell.width - editorRect.width)/2;
135     }
136     
137     if (verticalAlignment == SWT.BOTTOM) {
138         editorRect.y += cell.height - editorRect.height;
139     } else if (verticalAlignment == SWT.TOP) {
140         // do nothing - cell.y is the right answer
141
} else { // default is CENTER
142
editorRect.y += (cell.height - editorRect.height)/2;
143     }
144     return editorRect;
145 }
146 /**
147  * Removes all associations between the TableEditor and the cell in the table. The
148  * Table and the editor Control are <b>not</b> disposed.
149  */

150 public void dispose () {
151     if (table != null && !table.isDisposed()) {
152         if (this.column > -1 && this.column < table.getColumnCount()){
153             TableColumn tableColumn = table.getColumn(this.column);
154             tableColumn.removeControlListener(columnListener);
155         }
156     }
157     columnListener = null;
158     table = null;
159     item = null;
160     column = -1;
161     timer = null;
162     super.dispose();
163 }
164 /**
165 * Returns the zero based index of the column of the cell being tracked by this editor.
166 *
167 * @return the zero based index of the column of the cell being tracked by this editor
168 */

169 public int getColumn () {
170     return column;
171 }
172 /**
173 * Returns the TableItem for the row of the cell being tracked by this editor.
174 *
175 * @return the TableItem for the row of the cell being tracked by this editor
176 */

177 public TableItem getItem () {
178     return item;
179 }
180 void resize () {
181     layout();
182     /*
183      * On some platforms, the table scrolls when an item that
184      * is partially visible at the bottom of the table is
185      * selected. Ensure that the correct row is edited by
186      * laying out one more time in a timerExec().
187      */

188     if (table != null) {
189         Display display = table.getDisplay();
190         display.timerExec(-1, timer);
191         display.timerExec(TIMEOUT, timer);
192     }
193 }
194 /**
195 * Sets the zero based index of the column of the cell being tracked by this editor.
196 *
197 * @param column the zero based index of the column of the cell being tracked by this editor
198 */

199 public void setColumn(int column) {
200     int columnCount = table.getColumnCount();
201     // Separately handle the case where the table has no TableColumns.
202
// In this situation, there is a single default column.
203
if (columnCount == 0) {
204         this.column = (column == 0) ? 0 : -1;
205         resize();
206         return;
207     }
208     if (this.column > -1 && this.column < columnCount){
209         TableColumn tableColumn = table.getColumn(this.column);
210         tableColumn.removeControlListener(columnListener);
211         this.column = -1;
212     }
213
214     if (column < 0 || column >= table.getColumnCount()) return;
215         
216     this.column = column;
217     TableColumn tableColumn = table.getColumn(this.column);
218     tableColumn.addControlListener(columnListener);
219     resize();
220 }
221 public void setItem (TableItem item) {
222     this.item = item;
223     resize();
224 }
225 public void setEditor (Control editor) {
226     super.setEditor(editor);
227     resize();
228 }
229 /**
230 * Specify the Control that is to be displayed and the cell in the table that it is to be positioned above.
231 *
232 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Table control
233 * specified in the TableEditor constructor.
234 *
235 * @param editor the Control that is displayed above the cell being edited
236 * @param item the TableItem for the row of the cell being tracked by this editor
237 * @param column the zero based index of the column of the cell being tracked by this editor
238 */

239 public void setEditor (Control editor, TableItem item, int column) {
240     setItem(item);
241     setColumn(column);
242     setEditor(editor);
243 }
244 public void layout () {
245     if (table == null || table.isDisposed()) return;
246     if (item == null || item.isDisposed()) return;
247     int columnCount = table.getColumnCount();
248     if (columnCount == 0 && column != 0) return;
249     if (columnCount > 0 && (column < 0 || column >= columnCount)) return;
250     super.layout();
251 }
252 }
253
Popular Tags