1 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 71 public class TreeEditor extends ControlEditor { 72 Tree tree; 73 TreeItem item; 74 int column = 0; 75 ControlListener columnListener; 76 TreeListener treeListener; 77 Runnable timer; 78 static final int TIMEOUT = 1500; 79 80 86 public TreeEditor (Tree tree) { 87 super(tree); 88 this.tree = tree; 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 () { 99 public void run() { 100 layout (); 101 } 102 }; 103 treeListener = new TreeListener () { 104 final Runnable runnable = new Runnable () { 105 public void run() { 106 if (editor == null || editor.isDisposed()) return; 107 if (TreeEditor.this.tree.isDisposed()) return; 108 layout(); 109 editor.setVisible(true); 110 } 111 }; 112 public void treeCollapsed(TreeEvent e) { 113 if (editor == null || editor.isDisposed ()) return; 114 editor.setVisible(false); 115 e.display.asyncExec(runnable); 116 } 117 public void treeExpanded(TreeEvent e) { 118 if (editor == null || editor.isDisposed ()) return; 119 editor.setVisible(false); 120 e.display.asyncExec(runnable); 121 } 122 }; 123 tree.addTreeListener(treeListener); 124 125 grabVertical = true; 127 } 128 129 Rectangle computeBounds () { 130 if (item == null || column == -1 || item.isDisposed()) return new Rectangle(0, 0, 0, 0); 131 Rectangle cell = item.getBounds(column); 132 Rectangle rect = item.getImageBounds(column); 133 cell.x = rect.x + rect.width; 134 cell.width -= rect.width; 135 Rectangle area = tree.getClientArea(); 136 if (cell.x < area.x + area.width) { 137 if (cell.x + cell.width > area.x + area.width) { 138 cell.width = area.x + area.width - cell.x; 139 } 140 } 141 Rectangle editorRect = new Rectangle(cell.x, cell.y, minimumWidth, minimumHeight); 142 143 if (grabHorizontal) { 144 if (tree.getColumnCount() == 0) { 145 cell.width = area.x + area.width - cell.x; 148 } 149 editorRect.width = Math.max(cell.width, minimumWidth); 150 } 151 152 if (grabVertical) { 153 editorRect.height = Math.max(cell.height, minimumHeight); 154 } 155 156 if (horizontalAlignment == SWT.RIGHT) { 157 editorRect.x += cell.width - editorRect.width; 158 } else if (horizontalAlignment == SWT.LEFT) { 159 } else { editorRect.x += (cell.width - editorRect.width)/2; 162 } 163 editorRect.x = Math.max(cell.x, editorRect.x); 165 166 if (verticalAlignment == SWT.BOTTOM) { 167 editorRect.y += cell.height - editorRect.height; 168 } else if (verticalAlignment == SWT.TOP) { 169 } else { editorRect.y += (cell.height - editorRect.height)/2; 172 } 173 return editorRect; 174 } 175 176 180 public void dispose () { 181 if (tree != null && !tree.isDisposed()) { 182 if (this.column > -1 && this.column < tree.getColumnCount()){ 183 TreeColumn treeColumn = tree.getColumn(this.column); 184 treeColumn.removeControlListener(columnListener); 185 } 186 if (treeListener != null) tree.removeTreeListener(treeListener); 187 } 188 columnListener = null; 189 treeListener = null; 190 tree = null; 191 item = null; 192 column = 0; 193 timer = null; 194 super.dispose(); 195 } 196 197 204 public int getColumn () { 205 return column; 206 } 207 208 213 public TreeItem getItem () { 214 return item; 215 } 216 217 void resize () { 218 layout(); 219 225 if (tree != null) { 226 Display display = tree.getDisplay(); 227 display.timerExec(-1, timer); 228 display.timerExec(TIMEOUT, timer); 229 } 230 } 231 232 239 public void setColumn(int column) { 240 int columnCount = tree.getColumnCount(); 241 if (columnCount == 0) { 244 this.column = (column == 0) ? 0 : -1; 245 resize(); 246 return; 247 } 248 if (this.column > -1 && this.column < columnCount){ 249 TreeColumn treeColumn = tree.getColumn(this.column); 250 treeColumn.removeControlListener(columnListener); 251 this.column = -1; 252 } 253 254 if (column < 0 || column >= tree.getColumnCount()) return; 255 256 this.column = column; 257 TreeColumn treeColumn = tree.getColumn(this.column); 258 treeColumn.addControlListener(columnListener); 259 resize(); 260 } 261 262 public void setItem (TreeItem item) { 263 this.item = item; 264 resize(); 265 } 266 267 279 public void setEditor (Control editor, TreeItem item, int column) { 280 setItem(item); 281 setColumn(column); 282 setEditor(editor); 283 } 284 public void setEditor (Control editor) { 285 super.setEditor(editor); 286 resize(); 287 } 288 289 298 public void setEditor (Control editor, TreeItem item) { 299 setItem(item); 300 setEditor(editor); 301 } 302 303 public void layout () { 304 if (tree == null || tree.isDisposed()) return; 305 if (item == null || item.isDisposed()) return; 306 int columnCount = tree.getColumnCount(); 307 if (columnCount == 0 && column != 0) return; 308 if (columnCount > 0 && (column < 0 || column >= columnCount)) return; 309 super.layout(); 310 } 311 } 312 | Popular Tags |