1 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 82 public class TableTreeEditor extends ControlEditor { 83 84 TableTree tableTree; 85 TableTreeItem item; 86 int column = -1; 87 ControlListener columnListener; 88 TreeListener treeListener; 89 95 public TableTreeEditor (TableTree tableTree) { 96 super(tableTree.getTable()); 97 this.tableTree = tableTree; 98 99 treeListener = new TreeListener () { 100 final Runnable runnable = new Runnable () { 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 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 } else { 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 } else { editorRect.y += (cell.height - editorRect.height)/2; 166 } 167 return editorRect; 168 } 169 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 196 public int getColumn () { 197 return column; 198 } 199 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 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 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 |