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 * Port to the D programming language: 11 * Frank Benoit <benoit@tionex.de> 12 *******************************************************************************/ 13 module org.eclipse.swt.custom.TableTreeEditor; 14 15 import java.lang.all; 16 17 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.events.ControlEvent; 20 import org.eclipse.swt.events.ControlListener; 21 import org.eclipse.swt.events.TreeEvent; 22 import org.eclipse.swt.events.TreeListener; 23 import org.eclipse.swt.graphics.Rectangle; 24 import org.eclipse.swt.widgets.Control; 25 import org.eclipse.swt.widgets.Table; 26 import org.eclipse.swt.widgets.TableColumn; 27 import org.eclipse.swt.custom.ControlEditor; 28 import org.eclipse.swt.custom.TableTree; 29 import org.eclipse.swt.custom.TableTreeItem; 30 31 32 /** 33 * 34 * A TableTreeEditor is a manager for a Control that appears above a cell in a TableTree 35 * and tracks with the moving and resizing of that cell. It can be used to display a 36 * text widget above a cell in a TableTree so that the user can edit the contents of 37 * that cell. It can also be used to display a button that can launch a dialog for 38 * modifying the contents of the associated cell. 39 * 40 * <p> Here is an example of using a TableTreeEditor: 41 * <code><pre> 42 * final TableTree tableTree = new TableTree(shell, SWT.FULL_SELECTION | SWT.HIDE_SELECTION); 43 * final Table table = tableTree.getTable(); 44 * TableColumn column1 = new TableColumn(table, SWT.NONE); 45 * TableColumn column2 = new TableColumn(table, SWT.NONE); 46 * for (int i = 0; i < 10; i++) { 47 * TableTreeItem item = new TableTreeItem(tableTree, SWT.NONE); 48 * item.setText(0, "item " + i); 49 * item.setText(1, "edit this value"); 50 * for (int j = 0; j < 3; j++) { 51 * TableTreeItem subitem = new TableTreeItem(item, SWT.NONE); 52 * subitem.setText(0, "subitem " + i + " " + j); 53 * subitem.setText(1, "edit this value"); 54 * } 55 * } 56 * column1.setWidth(100); 57 * column2.pack(); 58 * 59 * final TableTreeEditor editor = new TableTreeEditor(tableTree); 60 * //The editor must have the same size as the cell and must 61 * //not be any smaller than 50 pixels. 62 * editor.horizontalAlignment = SWT.LEFT; 63 * editor.grabHorizontal = true; 64 * editor.minimumWidth = 50; 65 * // editing the second column 66 * final int EDITABLECOLUMN = 1; 67 * 68 * tableTree.addSelectionListener(new SelectionAdapter() { 69 * public void widgetSelected(SelectionEvent e) { 70 * // Clean up any previous editor control 71 * Control oldEditor = editor.getEditor(); 72 * if (oldEditor !is null) oldEditor.dispose(); 73 * 74 * // Identify the selected row 75 * TableTreeItem item = (TableTreeItem)e.item; 76 * if (item is null) return; 77 * 78 * // The control that will be the editor must be a child of the Table 79 * Text newEditor = new Text(table, SWT.NONE); 80 * newEditor.setText(item.getText(EDITABLECOLUMN)); 81 * newEditor.addModifyListener(new ModifyListener() { 82 * public void modifyText(ModifyEvent e) { 83 * Text text = (Text)editor.getEditor(); 84 * editor.getItem().setText(EDITABLECOLUMN, text.getText()); 85 * } 86 * }); 87 * newEditor.selectAll(); 88 * newEditor.setFocus(); 89 * editor.setEditor(newEditor, item, EDITABLECOLUMN); 90 * } 91 * }); 92 * </pre></code> 93 * 94 * @deprecated As of 3.1 use TreeEditor with Tree, TreeItem and TreeColumn 95 */ 96 public class TableTreeEditor : ControlEditor { 97 98 alias ControlEditor.setEditor setEditor; 99 100 TableTree tableTree; 101 TableTreeItem item; 102 int column = -1; 103 ControlListener columnListener; 104 TreeListener treeListener; 105 /** 106 * Creates a TableTreeEditor for the specified TableTree. 107 * 108 * @param tableTree the TableTree Control above which this editor will be displayed 109 * 110 */ 111 public this (TableTree tableTree) { 112 super(tableTree.getTable()); 113 this.tableTree = tableTree; 114 115 treeListener = new class() TreeListener { 116 Runnable runnable; 117 this() { 118 runnable = new class() Runnable { 119 public void run() { 120 if (editor is null || editor.isDisposed()) return; 121 if (this.outer.outer.tableTree.isDisposed()) return; 122 layout(); 123 editor.setVisible(true); 124 } 125 }; 126 } 127 public void treeCollapsed(TreeEvent e) { 128 if (editor is null || editor.isDisposed ()) return; 129 editor.setVisible(false); 130 e.display.asyncExec(runnable); 131 } 132 public void treeExpanded(TreeEvent e) { 133 if (editor is null || editor.isDisposed ()) return; 134 editor.setVisible(false); 135 e.display.asyncExec(runnable); 136 } 137 }; 138 tableTree.addTreeListener(treeListener); 139 140 columnListener = new class() ControlListener { 141 public void controlMoved(ControlEvent e){ 142 layout (); 143 } 144 public void controlResized(ControlEvent e){ 145 layout (); 146 } 147 }; 148 149 // To be consistent with older versions of SWT, grabVertical defaults to true 150 grabVertical = true; 151 } 152 153 override Rectangle computeBounds () { 154 if (item is null || column is -1 || item.isDisposed() || item.tableItem is null) return new Rectangle(0, 0, 0, 0); 155 Rectangle cell = item.getBounds(column); 156 Rectangle area = tableTree.getClientArea(); 157 if (cell.x < area.x + area.width) { 158 if (cell.x + cell.width > area.x + area.width) { 159 cell.width = area.x + area.width - cell.x; 160 } 161 } 162 Rectangle editorRect = new Rectangle(cell.x, cell.y, minimumWidth, minimumHeight); 163 164 if (grabHorizontal) { 165 editorRect.width = Math.max(cell.width, minimumWidth); 166 } 167 168 if (grabVertical) { 169 editorRect.height = Math.max(cell.height, minimumHeight); 170 } 171 172 if (horizontalAlignment is SWT.RIGHT) { 173 editorRect.x += cell.width - editorRect.width; 174 } else if (horizontalAlignment is SWT.LEFT) { 175 // do nothing - cell.x is the right answer 176 } else { // default is CENTER 177 editorRect.x += (cell.width - editorRect.width)/2; 178 } 179 180 if (verticalAlignment is SWT.BOTTOM) { 181 editorRect.y += cell.height - editorRect.height; 182 } else if (verticalAlignment is SWT.TOP) { 183 // do nothing - cell.y is the right answer 184 } else { // default is CENTER 185 editorRect.y += (cell.height - editorRect.height)/2; 186 } 187 return editorRect; 188 } 189 /** 190 * Removes all associations between the TableTreeEditor and the cell in the table tree. The 191 * TableTree and the editor Control are <b>not</b> disposed. 192 */ 193 public override void dispose () { 194 if (tableTree !is null && !tableTree.isDisposed()) { 195 Table table = tableTree.getTable(); 196 if (table !is null && !table.isDisposed()) { 197 if (this.column > -1 && this.column < table.getColumnCount()){ 198 TableColumn tableColumn = table.getColumn(this.column); 199 tableColumn.removeControlListener(columnListener); 200 } 201 } 202 if (treeListener !is null) tableTree.removeTreeListener(treeListener); 203 } 204 treeListener = null; 205 columnListener = null; 206 tableTree = null; 207 item = null; 208 column = -1; 209 super.dispose(); 210 } 211 /** 212 * Returns the zero based index of the column of the cell being tracked by this editor. 213 * 214 * @return the zero based index of the column of the cell being tracked by this editor 215 */ 216 public int getColumn () { 217 return column; 218 } 219 /** 220 * Returns the TableTreeItem for the row of the cell being tracked by this editor. 221 * 222 * @return the TableTreeItem for the row of the cell being tracked by this editor 223 */ 224 public TableTreeItem getItem () { 225 return item; 226 } 227 public void setColumn(int column) { 228 Table table = tableTree.getTable(); 229 int columnCount = table.getColumnCount(); 230 // Separately handle the case where the table has no TableColumns. 231 // In this situation, there is a single default column. 232 if (columnCount is 0) { 233 this.column = (column is 0) ? 0 : -1; 234 layout(); 235 return; 236 } 237 if (this.column > -1 && this.column < columnCount){ 238 TableColumn tableColumn = table.getColumn(this.column); 239 tableColumn.removeControlListener(columnListener); 240 this.column = -1; 241 } 242 243 if (column < 0 || column >= table.getColumnCount()) return; 244 245 this.column = column; 246 TableColumn tableColumn = table.getColumn(this.column); 247 tableColumn.addControlListener(columnListener); 248 layout(); 249 } 250 public void setItem (TableTreeItem item) { 251 this.item = item; 252 layout(); 253 } 254 255 /** 256 * Specify the Control that is to be displayed and the cell in the table that it is to be positioned above. 257 * 258 * <p>Note: The Control provided as the editor <b>must</b> be created with its parent being the Table control 259 * specified in the TableEditor constructor. 260 * 261 * @param editor the Control that is displayed above the cell being edited 262 * @param item the TableItem for the row of the cell being tracked by this editor 263 * @param column the zero based index of the column of the cell being tracked by this editor 264 */ 265 alias ControlEditor.setEditor setEditor; 266 public void setEditor (Control editor, TableTreeItem item, int column) { 267 setItem(item); 268 setColumn(column); 269 setEditor(editor); 270 } 271 public override void layout () { 272 if (tableTree is null || tableTree.isDisposed()) return; 273 if (item is null || item.isDisposed()) return; 274 Table table = tableTree.getTable(); 275 int columnCount = table.getColumnCount(); 276 if (columnCount is 0 && column !is 0) return; 277 if (columnCount > 0 && (column < 0 || column >= columnCount)) return; 278 super.layout(); 279 } 280 281 }