KickJava   Java API By Example, From Geeks To Geeks.

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


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

82 public class TableTreeEditor extends ControlEditor {
83
84     TableTree tableTree;
85     TableTreeItem item;
86     int column = -1;
87     ControlListener columnListener;
88     TreeListener treeListener;
89 /**
90 * Creates a TableTreeEditor for the specified TableTree.
91 *
92 * @param tableTree the TableTree Control above which this editor will be displayed
93 *
94 */

95 public TableTreeEditor (TableTree tableTree) {
96     super(tableTree.getTable());
97     this.tableTree = tableTree;
98
99     treeListener = new TreeListener () {
100         final Runnable JavaDoc runnable = new Runnable JavaDoc() {
101             public void run() {
102                 if (editor == null || editor.isDisposed()) return;
103                 if (TableTreeEditor.this.tableTree.isDisposed()) return;
104                 layout();
105                 editor.setVisible(true);
106             }
107         };
108         public void treeCollapsed(TreeEvent e) {
109             if (editor == null || editor.isDisposed ()) return;
110             editor.setVisible(false);
111             e.display.asyncExec(runnable);
112         }
113         public void treeExpanded(TreeEvent e) {
114             if (editor == null || editor.isDisposed ()) return;
115             editor.setVisible(false);
116             e.display.asyncExec(runnable);
117         }
118     };
119     tableTree.addTreeListener(treeListener);
120     
121     columnListener = new ControlListener() {
122         public void controlMoved(ControlEvent e){
123             layout ();
124         }
125         public void controlResized(ControlEvent e){
126             layout ();
127         }
128     };
129     
130     // To be consistent with older versions of SWT, grabVertical defaults to true
131
grabVertical = true;
132 }
133 Rectangle computeBounds () {
134     if (item == null || column == -1 || item.isDisposed() || item.tableItem == null) return new Rectangle(0, 0, 0, 0);
135     Rectangle cell = item.getBounds(column);
136     Rectangle area = tableTree.getClientArea();
137     if (cell.x < area.x + area.width) {
138         if (cell.x + cell.width > area.x + area.width) {
139             cell.width = area.x + area.width - cell.x;
140         }
141     }
142     Rectangle editorRect = new Rectangle(cell.x, cell.y, minimumWidth, minimumHeight);
143
144     if (grabHorizontal) {
145         editorRect.width = Math.max(cell.width, minimumWidth);
146     }
147     
148     if (grabVertical) {
149         editorRect.height = Math.max(cell.height, minimumHeight);
150     }
151     
152     if (horizontalAlignment == SWT.RIGHT) {
153         editorRect.x += cell.width - editorRect.width;
154     } else if (horizontalAlignment == SWT.LEFT) {
155         // do nothing - cell.x is the right answer
156
} else { // default is CENTER
157
editorRect.x += (cell.width - editorRect.width)/2;
158     }
159     
160     if (verticalAlignment == SWT.BOTTOM) {
161         editorRect.y += cell.height - editorRect.height;
162     } else if (verticalAlignment == SWT.TOP) {
163         // do nothing - cell.y is the right answer
164
} else { // default is CENTER
165
editorRect.y += (cell.height - editorRect.height)/2;
166     }
167     return editorRect;
168 }
169 /**
170  * Removes all associations between the TableTreeEditor and the cell in the table tree. The
171  * TableTree and the editor Control are <b>not</b> disposed.
172  */

173 public void dispose () {
174     if (tableTree != null && !tableTree.isDisposed()) {
175         Table table = tableTree.getTable();
176         if (table != null && !table.isDisposed()) {
177             if (this.column > -1 && this.column < table.getColumnCount()){
178                 TableColumn tableColumn = table.getColumn(this.column);
179                 tableColumn.removeControlListener(columnListener);
180             }
181         }
182         if (treeListener != null) tableTree.removeTreeListener(treeListener);
183     }
184     treeListener = null;
185     columnListener = null;
186     tableTree = null;
187     item = null;
188     column = -1;
189     super.dispose();
190 }
191 /**
192 * Returns the zero based index of the column of the cell being tracked by this editor.
193 *
194 * @return the zero based index of the column of the cell being tracked by this editor
195 */

196 public int getColumn () {
197     return column;
198 }
199 /**
200 * Returns the TableTreeItem for the row of the cell being tracked by this editor.
201 *
202 * @return the TableTreeItem for the row of the cell being tracked by this editor
203 */

204 public TableTreeItem getItem () {
205     return item;
206 }
207 public void setColumn(int column) {
208     Table table = tableTree.getTable();
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         layout();
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     layout();
229 }
230 public void setItem (TableTreeItem item) {
231     this.item = item;
232     layout();
233 }
234
235 /**
236 * Specify the Control that is to be displayed and the cell in the table that it is to be positioned above.
237 *
238 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Table control
239 * specified in the TableEditor constructor.
240 *
241 * @param editor the Control that is displayed above the cell being edited
242 * @param item the TableItem for the row of the cell being tracked by this editor
243 * @param column the zero based index of the column of the cell being tracked by this editor
244 */

245 public void setEditor (Control editor, TableTreeItem item, int column) {
246     setItem(item);
247     setColumn(column);
248     setEditor(editor);
249 }
250 public void layout () {
251     if (tableTree == null || tableTree.isDisposed()) return;
252     if (item == null || item.isDisposed()) return;
253     Table table = tableTree.getTable();
254     int columnCount = table.getColumnCount();
255     if (columnCount == 0 && column != 0) return;
256     if (columnCount > 0 && (column < 0 || column >= columnCount)) return;
257     super.layout();
258 }
259 }
260
Popular Tags