1 13 14 package org.eclipse.jface.viewers; 15 16 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Status; 20 import org.eclipse.jface.internal.InternalPolicy; 21 import org.eclipse.jface.util.Policy; 22 import org.eclipse.swt.events.MouseAdapter; 23 import org.eclipse.swt.events.MouseEvent; 24 import org.eclipse.swt.graphics.Point; 25 import org.eclipse.swt.widgets.Control; 26 import org.eclipse.swt.widgets.Item; 27 import org.eclipse.swt.widgets.Widget; 28 29 41 public abstract class ColumnViewer extends StructuredViewer { 42 private CellEditor[] cellEditors; 43 44 private ICellModifier cellModifier; 45 46 private String [] columnProperties; 47 48 51 private ViewerCell cell = new ViewerCell(null, 0, null); 52 53 private ColumnViewerEditor viewerEditor; 54 55 boolean busy; 56 boolean logWhenBusy = true; 58 61 public ColumnViewer() { 62 63 } 64 65 boolean isBusy() { 66 if (busy) { 67 if (logWhenBusy) { 68 String message = "Ignored reentrant call while viewer is busy."; if (!InternalPolicy.DEBUG_LOG_REENTRANT_VIEWER_CALLS) { 70 logWhenBusy = false; 72 message += " This is only logged once per viewer instance," + " but similar calls will still be ignored."; } 75 Policy.getLog().log( 76 new Status( 77 IStatus.WARNING, 78 Policy.JFACE, 79 message, new RuntimeException ())); 80 } 81 return true; 82 } 83 return false; 84 } 85 86 protected void hookControl(Control control) { 87 super.hookControl(control); 88 viewerEditor = createViewerEditor(); 89 hookEditingSupport(control); 90 } 91 92 98 protected void hookEditingSupport(Control control) { 99 if (viewerEditor != null) { 104 control.addMouseListener(new MouseAdapter() { 105 public void mouseDown(MouseEvent e) { 106 if( e.count != 2 ) { 108 handleMouseDown(e); 109 } 110 } 111 112 public void mouseDoubleClick(MouseEvent e) { 113 handleMouseDown(e); 114 } 115 }); 116 } 117 } 118 119 126 protected abstract ColumnViewerEditor createViewerEditor(); 127 128 137 ViewerCell getCell(Point point) { 138 ViewerRow row = getViewerRow(point); 139 if (row != null) { 140 return row.getCell(point); 141 } 142 143 return null; 144 } 145 146 154 protected ViewerRow getViewerRow(Point point) { 155 Item item = getItemAt(point); 156 157 if (item != null) { 158 return getViewerRowFromItem(item); 159 } 160 161 return null; 162 } 163 164 165 166 174 protected abstract ViewerRow getViewerRowFromItem(Widget item); 175 176 183 protected abstract Widget getColumnViewerOwner(int columnIndex); 184 185 193 ViewerColumn getViewerColumn(final int columnIndex) { 194 195 ViewerColumn viewer; 196 Widget columnOwner = getColumnViewerOwner(columnIndex); 197 198 if (columnOwner == null) { 199 return null; 200 } 201 202 viewer = (ViewerColumn) columnOwner 203 .getData(ViewerColumn.COLUMN_VIEWER_KEY); 204 205 if (viewer == null) { 206 viewer = createViewerColumn(columnOwner, CellLabelProvider 207 .createViewerLabelProvider(this, getLabelProvider())); 208 setupEditingSupport(columnIndex, viewer); 209 } 210 211 if (viewer.getEditingSupport() == null && getCellModifier() != null) { 212 setupEditingSupport(columnIndex, viewer); 213 } 214 215 return viewer; 216 } 217 218 225 private void setupEditingSupport(final int columnIndex, ViewerColumn viewer) { 226 if (getCellModifier() != null) { 227 viewer.setEditingSupport(new EditingSupport(this) { 228 229 234 public boolean canEdit(Object element) { 235 Object [] properties = getColumnProperties(); 236 237 if( columnIndex < properties.length ) { 238 return getCellModifier().canModify(element, 239 (String ) getColumnProperties()[columnIndex]); 240 } 241 242 return false; 243 } 244 245 250 public CellEditor getCellEditor(Object element) { 251 CellEditor[] editors = getCellEditors(); 252 if( columnIndex < editors.length ) { 253 return getCellEditors()[columnIndex]; 254 } 255 return null; 256 } 257 258 263 public Object getValue(Object element) { 264 Object [] properties = getColumnProperties(); 265 266 if( columnIndex < properties.length ) { 267 return getCellModifier().getValue(element, 268 (String ) getColumnProperties()[columnIndex]); 269 } 270 271 return null; 272 } 273 274 280 public void setValue(Object element, Object value) { 281 Object [] properties = getColumnProperties(); 282 283 if( columnIndex < properties.length ) { 284 getCellModifier().modify(findItem(element), 285 (String ) getColumnProperties()[columnIndex], value); 286 } 287 } 288 289 boolean isLegacySupport() { 290 return true; 291 } 292 }); 293 } 294 } 295 296 306 private ViewerColumn createViewerColumn(Widget columnOwner, 307 CellLabelProvider labelProvider) { 308 ViewerColumn column = new ViewerColumn(this, columnOwner) { 309 }; 310 column.setLabelProvider(labelProvider, false); 311 return column; 312 } 313 314 324 ViewerCell updateCell(ViewerRow rowItem, int column, Object element) { 325 cell.update(rowItem, column, element); 326 return cell; 327 } 328 329 338 protected abstract Item getItemAt(Point point); 339 340 345 protected Item getItem(int x, int y) { 346 return getItemAt(getControl().toControl(x, y)); 347 } 348 349 370 public void setLabelProvider(IBaseLabelProvider labelProvider) { 371 Assert.isTrue(labelProvider instanceof ITableLabelProvider 372 || labelProvider instanceof ILabelProvider 373 || labelProvider instanceof CellLabelProvider); 374 updateColumnParts(labelProvider); super.setLabelProvider(labelProvider); 377 } 378 379 382 private void updateColumnParts(IBaseLabelProvider labelProvider) { 383 ViewerColumn column; 384 int i = 0; 385 386 while ((column = getViewerColumn(i++)) != null) { 387 column.setLabelProvider(CellLabelProvider 388 .createViewerLabelProvider(this, labelProvider), false); 389 } 390 } 391 392 398 public void cancelEditing() { 399 if (viewerEditor != null) { 400 viewerEditor.cancelEditing(); 401 } 402 } 403 404 409 protected void applyEditorValue() { 410 if (viewerEditor != null) { 411 viewerEditor.applyEditorValue(); 412 } 413 } 414 415 424 public void editElement(Object element, int column) { 425 if (viewerEditor != null) { 426 try { 427 getControl().setRedraw(false); 428 setSelection(new StructuredSelection(element),true); 431 432 Widget item = findItem(element); 433 if (item != null) { 434 ViewerRow row = getViewerRowFromItem(item); 435 if (row != null) { 436 ViewerCell cell = row.getCell(column); 437 if (cell != null) { 438 triggerEditorActivationEvent(new ColumnViewerEditorActivationEvent( 439 cell)); 440 } 441 } 442 } 443 } finally { 444 getControl().setRedraw(true); 445 } 446 } 447 } 448 449 463 public CellEditor[] getCellEditors() { 464 return cellEditors; 465 } 466 467 482 public ICellModifier getCellModifier() { 483 return cellModifier; 484 } 485 486 502 public Object [] getColumnProperties() { 503 return columnProperties; 504 } 505 506 521 public boolean isCellEditorActive() { 522 if (viewerEditor != null) { 523 return viewerEditor.isCellEditorActive(); 524 } 525 return false; 526 } 527 528 public void refresh(Object element) { 529 if (isBusy()) 530 return; 531 532 if( isCellEditorActive() ) { 533 cancelEditing(); 534 } 535 536 super.refresh(element); 537 } 538 539 public void refresh(Object element, boolean updateLabels) { 540 if (isBusy()) 541 return; 542 543 if( isCellEditorActive() ) { 544 cancelEditing(); 545 } 546 547 super.refresh(element, updateLabels); 548 } 549 550 public void update(Object element, String [] properties) { 551 if (isBusy()) 552 return; 553 super.update(element, properties); 554 } 555 556 572 public void setCellEditors(CellEditor[] editors) { 573 this.cellEditors = editors; 574 } 575 576 592 public void setCellModifier(ICellModifier modifier) { 593 this.cellModifier = modifier; 594 } 595 596 614 public void setColumnProperties(String [] columnProperties) { 615 this.columnProperties = columnProperties; 616 } 617 618 629 protected abstract int doGetColumnCount(); 630 631 642 public CellLabelProvider getLabelProvider(int columnIndex) { 643 ViewerColumn column = getViewerColumn(columnIndex); 644 if (column != null) { 645 return column.getLabelProvider(); 646 } 647 return null; 648 } 649 650 private void handleMouseDown(MouseEvent e) { 651 ViewerCell cell = getCell(new Point(e.x, e.y)); 652 653 if (cell != null) { 654 triggerEditorActivationEvent(new ColumnViewerEditorActivationEvent( 655 cell, e)); 656 } 657 } 658 659 668 protected void triggerEditorActivationEvent( 669 ColumnViewerEditorActivationEvent event) { 670 viewerEditor.handleEditorActivationEvent(event); 671 } 672 673 677 public void setColumnViewerEditor(ColumnViewerEditor columnViewerEditor) { 678 Assert.isNotNull(viewerEditor); 679 this.viewerEditor = columnViewerEditor; 680 } 681 682 685 public ColumnViewerEditor getColumnViewerEditor() { 686 return viewerEditor; 687 } 688 689 protected Object [] getRawChildren(Object parent) { 690 boolean oldBusy = busy; 691 busy = true; 692 try { 693 return super.getRawChildren(parent); 694 } finally { 695 busy = oldBusy; 696 } 697 } 698 699 705 void clearLegacyEditingSetup() { 706 int count = doGetColumnCount(); 707 708 for( int i = 0; i < count || i == 0; i++ ) { 709 Widget owner = getColumnViewerOwner(i); 710 711 if( owner != null && ! owner.isDisposed() ) { 712 ViewerColumn column = (ViewerColumn) owner.getData(ViewerColumn.COLUMN_VIEWER_KEY); 713 if( column != null ) { 714 EditingSupport e = column.getEditingSupport(); 715 if (e != null && e.isLegacySupport()) { 718 column.setEditingSupport(null); 719 } 720 } 721 } 722 } 723 } 724 } | Popular Tags |