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 73 public class TableEditor extends ControlEditor { 74 Table table; 75 TableItem item; 76 int column = -1; 77 ControlListener columnListener; 78 Runnable timer; 79 static final int TIMEOUT = 1500; 80 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 () { 99 public void run() { 100 layout (); 101 } 102 }; 103 104 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 } else { 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 } else { editorRect.y += (cell.height - editorRect.height)/2; 143 } 144 return editorRect; 145 } 146 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 169 public int getColumn () { 170 return column; 171 } 172 177 public TableItem getItem () { 178 return item; 179 } 180 void resize () { 181 layout(); 182 188 if (table != null) { 189 Display display = table.getDisplay(); 190 display.timerExec(-1, timer); 191 display.timerExec(TIMEOUT, timer); 192 } 193 } 194 199 public void setColumn(int column) { 200 int columnCount = table.getColumnCount(); 201 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 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 |