1 11 package org.eclipse.debug.internal.ui.viewers; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Status; 20 import org.eclipse.core.runtime.jobs.Job; 21 import org.eclipse.jface.resource.ImageDescriptor; 22 import org.eclipse.jface.viewers.CellEditor; 23 import org.eclipse.jface.viewers.DoubleClickEvent; 24 import org.eclipse.jface.viewers.ICellModifier; 25 import org.eclipse.jface.viewers.ISelection; 26 import org.eclipse.jface.viewers.IStructuredSelection; 27 import org.eclipse.jface.viewers.OpenEvent; 28 import org.eclipse.jface.viewers.StructuredSelection; 29 import org.eclipse.jface.viewers.Viewer; 30 import org.eclipse.jface.viewers.ViewerSorter; 31 import org.eclipse.swt.SWT; 32 import org.eclipse.swt.custom.TableEditor; 33 import org.eclipse.swt.events.MouseAdapter; 34 import org.eclipse.swt.events.MouseEvent; 35 import org.eclipse.swt.graphics.Color; 36 import org.eclipse.swt.graphics.Font; 37 import org.eclipse.swt.graphics.FontData; 38 import org.eclipse.swt.graphics.Image; 39 import org.eclipse.swt.graphics.RGB; 40 import org.eclipse.swt.graphics.Rectangle; 41 import org.eclipse.swt.widgets.Composite; 42 import org.eclipse.swt.widgets.Control; 43 import org.eclipse.swt.widgets.Item; 44 import org.eclipse.swt.widgets.Listener; 45 import org.eclipse.swt.widgets.Table; 46 import org.eclipse.swt.widgets.TableItem; 47 import org.eclipse.swt.widgets.Widget; 48 import org.eclipse.ui.progress.WorkbenchJob; 49 50 53 public class AsynchronousTableViewer extends AsynchronousViewer implements Listener { 54 55 private Table fTable; 56 57 private TableEditor fTableEditor; 58 59 private TableEditorImpl fTableEditorImpl; 60 61 public AsynchronousTableViewer(Composite parent) { 62 this(parent, SWT.VIRTUAL); 63 } 64 65 public AsynchronousTableViewer(Composite parent, int style) { 66 this(new Table(parent, style)); 67 } 68 69 76 public AsynchronousTableViewer(Table table) { 77 Assert.isTrue((table.getStyle() & SWT.VIRTUAL) != 0); 78 fTable = table; 79 hookControl(fTable); 80 fTableEditor = new TableEditor(fTable); 81 fTableEditorImpl = createTableEditorImpl(); 82 } 83 84 protected void hookControl(Control control) { 85 super.hookControl(control); 86 control.addMouseListener(new MouseAdapter() { 87 public void mouseDown(MouseEvent e) { 88 fTableEditorImpl.handleMouseDown(e); 89 } 90 }); 91 } 92 93 public synchronized void dispose() { 94 fTableEditor.dispose(); 95 fTable.dispose(); 96 super.dispose(); 97 } 98 99 protected ISelection doAttemptSelectionToWidget(ISelection selection, boolean reveal) { 100 if (acceptsSelection(selection)) { 101 List list = ((IStructuredSelection) selection).toList(); 102 if (list == null) { 103 fTable.deselectAll(); 104 return StructuredSelection.EMPTY; 105 } 106 107 int[] indices = new int[list.size()]; 108 ModelNode[] nodes = getModel().getRootNode().getChildrenNodes(); 109 if (nodes != null) { 110 int index = 0; 111 112 for (int i = 0; i < nodes.length; i++) { 114 Object element = nodes[i].getElement(); 115 if (list.contains(element)) { 116 indices[index] = i; 117 index++; 118 } 119 } 120 121 fTable.setSelection(indices); 122 if (reveal && indices.length > 0) { 123 TableItem item = fTable.getItem(indices[0]); 124 fTable.showItem(item); 125 } 126 } 127 } 128 return StructuredSelection.EMPTY; 129 } 130 131 protected boolean acceptsSelection(ISelection selection) { 132 return selection instanceof IStructuredSelection; 133 } 134 135 protected ISelection getEmptySelection() { 136 return StructuredSelection.EMPTY; 137 } 138 139 protected Widget getParent(Widget widget) { 140 if (widget instanceof TableItem) { 141 return fTable; 142 } 143 return null; 144 } 145 146 protected List getSelectionFromWidget() { 147 TableItem[] selection = fTable.getSelection(); 148 List datas = new ArrayList (selection.length); 149 for (int i = 0; i < selection.length; i++) { 150 datas.add(selection[i].getData()); 151 } 152 return datas; 153 } 154 155 public Control getControl() { 156 return fTable; 157 } 158 159 public Table getTable() { 160 return (Table) getControl(); 161 } 162 163 166 protected void internalRefresh(ModelNode node) { 167 super.internalRefresh(node); 168 if (node.getElement().equals(getInput())) { 169 updateChildren(node); 170 } 171 } 172 173 protected void restoreLabels(Item item) { 174 TableItem tableItem = (TableItem) item; 175 String [] values = (String []) tableItem.getData(OLD_LABEL); 176 Image[] images = (Image[]) tableItem.getData(OLD_IMAGE); 177 if (values != null) { 178 tableItem.setText(values); 179 tableItem.setImage(images); 180 } 181 } 182 183 public void setLabels(Widget widget, String [] labels, ImageDescriptor[] imageDescriptors) { 184 TableItem item = (TableItem) widget; 185 item.setText(labels); 186 item.setData(OLD_LABEL, labels); 187 Image[] images = new Image[labels.length]; 188 item.setData(OLD_IMAGE, images); 189 if (imageDescriptors != null) { 190 for (int i = 0; i < images.length; i++) { 191 if (i < imageDescriptors.length) 192 images[i] = getImage(imageDescriptors[i]); 193 } 194 } 195 item.setImage(images); 196 } 197 198 public void setColors(Widget widget, RGB[] foregrounds, RGB[] backgrounds) { 199 TableItem item = (TableItem) widget; 200 if (foregrounds == null) { 201 foregrounds = new RGB[fTable.getColumnCount()]; 202 } 203 if (backgrounds == null) { 204 backgrounds = new RGB[fTable.getColumnCount()]; 205 } 206 207 for (int i = 0; i < foregrounds.length; i++) { 208 Color fg = getColor(foregrounds[i]); 209 item.setForeground(i, fg); 210 } 211 for (int i = 0; i < backgrounds.length; i++) { 212 Color bg = getColor(backgrounds[i]); 213 item.setBackground(i, bg); 214 } 215 } 216 217 public void setFonts(Widget widget, FontData[] fontDatas) { 218 TableItem item = (TableItem) widget; 219 if (fontDatas != null) { 220 for (int i = 0; i < fontDatas.length; i++) { 221 Font font = getFont(fontDatas[i]); 222 item.setFont(i, font); 223 } 224 } 225 } 226 227 public void setColumnHeaders(final String [] headers) { 228 fTableEditorImpl.setColumnProperties(headers); 229 } 230 231 public Object [] getColumnProperties() { 232 return fTableEditorImpl.getColumnProperties(); 233 } 234 235 public void showColumnHeader(final boolean showHeaders) { 236 WorkbenchJob job = new WorkbenchJob("Set Header Visibility") { public IStatus runInUIThread(IProgressMonitor monitor) { 238 if (!fTable.isDisposed()) 239 { 240 fTable.setHeaderVisible(showHeaders); 241 } 242 return Status.OK_STATUS; 243 } 244 }; 245 job.setPriority(Job.INTERACTIVE); 246 job.setSystem(true); 247 job.schedule(); 248 } 249 250 255 public void reveal(Object element) { 256 Assert.isNotNull(element); 257 Widget w = findItem(element); 258 if (w instanceof TableItem) 259 getTable().showItem((TableItem) w); 260 } 261 262 268 public void setCellEditors(CellEditor[] editors) { 269 fTableEditorImpl.setCellEditors(editors); 270 } 271 272 278 public void setCellModifier(ICellModifier modifier) { 279 fTableEditorImpl.setCellModifier(modifier); 280 } 281 282 protected TableEditorImpl createTableEditorImpl() { 283 return new TableEditorImpl(this) { 284 Rectangle getBounds(Item item, int columnNumber) { 285 return ((TableItem) item).getBounds(columnNumber); 286 } 287 288 int getColumnCount() { 289 return getTable().getColumnCount(); 290 } 291 292 Item[] getSelection() { 293 return getTable().getSelection(); 294 } 295 296 void setEditor(Control w, Item item, int columnNumber) { 297 fTableEditor.setEditor(w, (TableItem) item, columnNumber); 298 } 299 300 void setSelection(StructuredSelection selection, boolean b) { 301 AsynchronousTableViewer.this.setSelection(selection, b); 302 } 303 304 void showSelection() { 305 getTable().showSelection(); 306 } 307 308 void setLayoutData(CellEditor.LayoutData layoutData) { 309 fTableEditor.grabHorizontal = layoutData.grabHorizontal; 310 fTableEditor.horizontalAlignment = layoutData.horizontalAlignment; 311 fTableEditor.minimumWidth = layoutData.minimumWidth; 312 } 313 314 void handleDoubleClickEvent() { 315 Viewer viewer = getViewer(); 316 fireDoubleClick(new DoubleClickEvent(viewer, viewer.getSelection())); 317 fireOpen(new OpenEvent(viewer, viewer.getSelection())); 318 } 319 }; 320 } 321 322 protected ISelection newSelectionFromWidget() { 323 Control control = getControl(); 324 if (control == null || control.isDisposed()) { 325 return StructuredSelection.EMPTY; 326 } 327 List list = getSelectionFromWidget(); 328 return new StructuredSelection(list); 329 } 330 331 public CellEditor[] getCellEditors() { 332 return fTableEditorImpl.getCellEditors(); 333 } 334 335 public ICellModifier getCellModifier() { 336 return fTableEditorImpl.getCellModifier(); 337 } 338 339 public boolean isCellEditorActive() { 340 return fTableEditorImpl.isCellEditorActive(); 341 } 342 343 346 public void cancelEditing() { 347 fTableEditorImpl.cancelEditing(); 348 } 349 350 358 public void editElement(Object element, int column) { 359 fTableEditorImpl.editElement(element, column); 360 } 361 362 protected int indexForElement(Object element) { 363 ViewerSorter sorter = getSorter(); 364 if (sorter == null) 365 return fTable.getItemCount(); 366 int count = fTable.getItemCount(); 367 int min = 0, max = count - 1; 368 while (min <= max) { 369 int mid = (min + max) / 2; 370 Object data = fTable.getItem(mid).getData(); 371 int compare = sorter.compare(this, data, element); 372 if (compare == 0) { 373 while (compare == 0) { 375 ++mid; 376 if (mid >= count) { 377 break; 378 } 379 data = fTable.getItem(mid).getData(); 380 compare = sorter.compare(this, data, element); 381 } 382 return mid; 383 } 384 if (compare < 0) 385 min = mid + 1; 386 else 387 max = mid - 1; 388 } 389 return min; 390 } 391 392 public void add(Object element) { 393 if (element != null) 394 add(new Object [] { element }); 395 } 396 397 public void add(Object [] elements) { 398 if (elements == null || elements.length == 0) 399 return; ((AsynchronousTableModel)getModel()).add(elements); 401 } 402 403 public void remove(Object element) { 404 if (element != null) 405 remove(new Object [] { element }); 406 } 407 408 public void remove(final Object [] elements) { 409 if (elements == null || elements.length == 0) 410 return; ((AsynchronousTableModel)getModel()).remove(elements); 412 } 413 414 public void insert(Object element, int position) { 415 if (element != null) 416 insert(new Object [] { element }, position); 417 } 418 419 public void insert(Object [] elements, int position) { 420 if (elements == null || elements.length == 0) 421 return; 422 ((AsynchronousTableModel)getModel()).insert(elements, position); 423 } 424 425 public void replace(Object element, Object replacement) { 426 if (element == null || replacement == null) 427 throw new IllegalArgumentException ("unexpected null parameter"); ((AsynchronousTableModel)getModel()).replace(element, replacement); 429 } 430 431 434 protected AsynchronousModel createModel() { 435 return new AsynchronousTableModel(this); 436 } 437 438 441 protected void setItemCount(Widget parent, int itemCount) { 442 fTable.setItemCount(itemCount); 443 } 444 445 protected int getVisibleItemCount(int top) { 446 int itemCount = fTable.getItemCount(); 447 return Math.min((fTable.getBounds().height / fTable.getItemHeight()) + 2, itemCount - top); 448 } 449 450 453 public AbstractUpdatePolicy createUpdatePolicy() { 454 return new TableUpdatePolicy(); 455 } 456 457 460 protected Widget getParentWidget(Widget widget) { 461 if (widget instanceof TableItem) { 462 return ((TableItem)widget).getParent(); 463 } 464 return null; 465 } 466 467 470 protected Widget getChildWidget(Widget parent, int index) { 471 if (index < fTable.getItemCount()) { 472 return fTable.getItem(index); 473 } 474 return null; 475 } 476 477 480 protected void clear(Widget item) { 481 if (item instanceof TableItem) { 482 int i = fTable.indexOf((TableItem)item); 483 if (i >= 0) { 484 fTable.clear(i); 485 } 486 } 487 } 488 489 492 protected void clearChild(Widget parent, int childIndex) { 493 if (parent instanceof Table) { 494 fTable.clear(childIndex); 495 } 496 } 497 498 501 protected void clearChildren(Widget item) { 502 if (item instanceof Table) { 503 fTable.clearAll(); 504 } 505 } 506 507 510 protected int indexOf(Widget parent, Widget child) { 511 if (parent instanceof Table) { 512 return ((Table)parent).indexOf((TableItem)child); 513 } 514 return -1; 515 } 516 517 } 518 | Popular Tags |