1 11 package org.eclipse.ui.part; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.jface.action.Action; 18 import org.eclipse.jface.action.IAction; 19 import org.eclipse.jface.util.IPropertyChangeListener; 20 import org.eclipse.jface.util.PropertyChangeEvent; 21 import org.eclipse.jface.viewers.CellEditor; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.widgets.Control; 24 import org.eclipse.swt.widgets.Event; 25 import org.eclipse.swt.widgets.Listener; 26 import org.eclipse.ui.IActionBars; 27 import org.eclipse.ui.PlatformUI; 28 import org.eclipse.ui.actions.ActionFactory; 29 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 30 import org.eclipse.ui.internal.WorkbenchMessages; 31 32 48 public class CellEditorActionHandler { 49 private CutActionHandler cellCutAction = new CutActionHandler(); 50 51 private CopyActionHandler cellCopyAction = new CopyActionHandler(); 52 53 private PasteActionHandler cellPasteAction = new PasteActionHandler(); 54 55 private DeleteActionHandler cellDeleteAction = new DeleteActionHandler(); 56 57 private SelectAllActionHandler cellSelectAllAction = new SelectAllActionHandler(); 58 59 private FindActionHandler cellFindAction = new FindActionHandler(); 60 61 private UndoActionHandler cellUndoAction = new UndoActionHandler(); 62 63 private RedoActionHandler cellRedoAction = new RedoActionHandler(); 64 65 private IAction cutAction; 66 67 private IAction copyAction; 68 69 private IAction pasteAction; 70 71 private IAction deleteAction; 72 73 private IAction selectAllAction; 74 75 private IAction findAction; 76 77 private IAction undoAction; 78 79 private IAction redoAction; 80 81 private IPropertyChangeListener cutActionListener = new ActionEnabledChangeListener( 82 cellCutAction); 83 84 private IPropertyChangeListener copyActionListener = new ActionEnabledChangeListener( 85 cellCopyAction); 86 87 private IPropertyChangeListener pasteActionListener = new ActionEnabledChangeListener( 88 cellPasteAction); 89 90 private IPropertyChangeListener deleteActionListener = new ActionEnabledChangeListener( 91 cellDeleteAction); 92 93 private IPropertyChangeListener selectAllActionListener = new ActionEnabledChangeListener( 94 cellSelectAllAction); 95 96 private IPropertyChangeListener findActionListener = new ActionEnabledChangeListener( 97 cellFindAction); 98 99 private IPropertyChangeListener undoActionListener = new ActionEnabledChangeListener( 100 cellUndoAction); 101 102 private IPropertyChangeListener redoActionListener = new ActionEnabledChangeListener( 103 cellRedoAction); 104 105 private CellEditor activeEditor; 106 107 private IPropertyChangeListener cellListener = new CellChangeListener(); 108 109 private Listener controlListener = new ControlListener(); 110 111 private HashMap controlToEditor = new HashMap (); 112 113 private class ControlListener implements Listener { 114 public void handleEvent(Event event) { 115 switch (event.type) { 116 case SWT.Activate: 117 activeEditor = (CellEditor) controlToEditor.get(event.widget); 118 if (activeEditor != null) { 119 activeEditor.addPropertyChangeListener(cellListener); 120 } 121 updateActionsEnableState(); 122 break; 123 case SWT.Deactivate: 124 if (activeEditor != null) { 125 activeEditor.removePropertyChangeListener(cellListener); 126 } 127 activeEditor = null; 128 updateActionsEnableState(); 129 break; 130 default: 131 break; 132 } 133 } 134 } 135 136 private class ActionEnabledChangeListener implements 137 IPropertyChangeListener { 138 private IAction actionHandler; 139 140 protected ActionEnabledChangeListener(IAction actionHandler) { 141 super(); 142 this.actionHandler = actionHandler; 143 } 144 145 public void propertyChange(PropertyChangeEvent event) { 146 if (activeEditor != null) { 147 return; 148 } 149 if (event.getProperty().equals(IAction.ENABLED)) { 150 Boolean bool = (Boolean ) event.getNewValue(); 151 actionHandler.setEnabled(bool.booleanValue()); 152 return; 153 } 154 if (event.getProperty().equals(IAction.TEXT)) { 158 String text = (String ) event.getNewValue(); 159 actionHandler.setText(text); 160 return; 161 } 162 if (event.getProperty().equals(IAction.TOOL_TIP_TEXT)) { 163 String text = (String ) event.getNewValue(); 164 actionHandler.setToolTipText(text); 165 return; 166 } 167 } 168 } 169 170 private class CellChangeListener implements IPropertyChangeListener { 171 public void propertyChange(PropertyChangeEvent event) { 172 if (activeEditor == null) { 173 return; 174 } 175 if (event.getProperty().equals(CellEditor.CUT)) { 176 cellCutAction.setEnabled(activeEditor.isCutEnabled()); 177 return; 178 } 179 if (event.getProperty().equals(CellEditor.COPY)) { 180 cellCopyAction.setEnabled(activeEditor.isCopyEnabled()); 181 return; 182 } 183 if (event.getProperty().equals(CellEditor.PASTE)) { 184 cellPasteAction.setEnabled(activeEditor.isPasteEnabled()); 185 return; 186 } 187 if (event.getProperty().equals(CellEditor.DELETE)) { 188 cellDeleteAction.setEnabled(activeEditor.isDeleteEnabled()); 189 return; 190 } 191 if (event.getProperty().equals(CellEditor.SELECT_ALL)) { 192 cellSelectAllAction.setEnabled(activeEditor 193 .isSelectAllEnabled()); 194 return; 195 } 196 if (event.getProperty().equals(CellEditor.FIND)) { 197 cellFindAction.setEnabled(activeEditor.isFindEnabled()); 198 return; 199 } 200 if (event.getProperty().equals(CellEditor.UNDO)) { 201 cellUndoAction.setEnabled(activeEditor.isUndoEnabled()); 202 return; 203 } 204 if (event.getProperty().equals(CellEditor.REDO)) { 205 cellRedoAction.setEnabled(activeEditor.isRedoEnabled()); 206 return; 207 } 208 } 209 } 210 211 private class CutActionHandler extends Action { 212 protected CutActionHandler() { 213 setId("CellEditorCutActionHandler"); setEnabled(false); 215 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IWorkbenchHelpContextIds.CELL_CUT_ACTION); 216 } 217 218 public void runWithEvent(Event event) { 219 if (activeEditor != null) { 220 activeEditor.performCut(); 221 return; 222 } 223 if (cutAction != null) { 224 cutAction.runWithEvent(event); 225 return; 226 } 227 } 228 229 public void updateEnabledState() { 230 if (activeEditor != null) { 231 setEnabled(activeEditor.isCutEnabled()); 232 return; 233 } 234 if (cutAction != null) { 235 setEnabled(cutAction.isEnabled()); 236 return; 237 } 238 setEnabled(false); 239 } 240 } 241 242 private class CopyActionHandler extends Action { 243 protected CopyActionHandler() { 244 setId("CellEditorCopyActionHandler"); setEnabled(false); 246 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IWorkbenchHelpContextIds.CELL_COPY_ACTION); 247 } 248 249 public void runWithEvent(Event event) { 250 if (activeEditor != null) { 251 activeEditor.performCopy(); 252 return; 253 } 254 if (copyAction != null) { 255 copyAction.runWithEvent(event); 256 return; 257 } 258 } 259 260 public void updateEnabledState() { 261 if (activeEditor != null) { 262 setEnabled(activeEditor.isCopyEnabled()); 263 return; 264 } 265 if (copyAction != null) { 266 setEnabled(copyAction.isEnabled()); 267 return; 268 } 269 setEnabled(false); 270 } 271 } 272 273 private class PasteActionHandler extends Action { 274 protected PasteActionHandler() { 275 setId("CellEditorPasteActionHandler"); setEnabled(false); 277 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IWorkbenchHelpContextIds.CELL_PASTE_ACTION); 278 } 279 280 public void runWithEvent(Event event) { 281 if (activeEditor != null) { 282 activeEditor.performPaste(); 283 return; 284 } 285 if (pasteAction != null) { 286 pasteAction.runWithEvent(event); 287 return; 288 } 289 } 290 291 public void updateEnabledState() { 292 if (activeEditor != null) { 293 setEnabled(activeEditor.isPasteEnabled()); 294 return; 295 } 296 if (pasteAction != null) { 297 setEnabled(pasteAction.isEnabled()); 298 return; 299 } 300 setEnabled(false); 301 } 302 } 303 304 private class DeleteActionHandler extends Action { 305 protected DeleteActionHandler() { 306 setId("CellEditorDeleteActionHandler"); setEnabled(false); 308 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IWorkbenchHelpContextIds.CELL_DELETE_ACTION); 309 } 310 311 public void runWithEvent(Event event) { 312 if (activeEditor != null) { 313 activeEditor.performDelete(); 314 return; 315 } 316 if (deleteAction != null) { 317 deleteAction.runWithEvent(event); 318 return; 319 } 320 } 321 322 public void updateEnabledState() { 323 if (activeEditor != null) { 324 setEnabled(activeEditor.isDeleteEnabled()); 325 return; 326 } 327 if (deleteAction != null) { 328 setEnabled(deleteAction.isEnabled()); 329 return; 330 } 331 setEnabled(false); 332 } 333 } 334 335 private class SelectAllActionHandler extends Action { 336 protected SelectAllActionHandler() { 337 setId("CellEditorSelectAllActionHandler"); setEnabled(false); 339 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IWorkbenchHelpContextIds.CELL_SELECT_ALL_ACTION); 340 } 341 342 public void runWithEvent(Event event) { 343 if (activeEditor != null) { 344 activeEditor.performSelectAll(); 345 return; 346 } 347 if (selectAllAction != null) { 348 selectAllAction.runWithEvent(event); 349 return; 350 } 351 } 352 353 public void updateEnabledState() { 354 if (activeEditor != null) { 355 setEnabled(activeEditor.isSelectAllEnabled()); 356 return; 357 } 358 if (selectAllAction != null) { 359 setEnabled(selectAllAction.isEnabled()); 360 return; 361 } 362 setEnabled(false); 363 } 364 } 365 366 private class FindActionHandler extends Action { 367 protected FindActionHandler() { 368 setId("CellEditorFindActionHandler"); setEnabled(false); 370 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IWorkbenchHelpContextIds.CELL_FIND_ACTION); 371 } 372 373 public void runWithEvent(Event event) { 374 if (activeEditor != null) { 375 activeEditor.performFind(); 376 return; 377 } 378 if (findAction != null) { 379 findAction.runWithEvent(event); 380 return; 381 } 382 } 383 384 public void updateEnabledState() { 385 if (activeEditor != null) { 386 setEnabled(activeEditor.isFindEnabled()); 387 return; 388 } 389 if (findAction != null) { 390 setEnabled(findAction.isEnabled()); 391 return; 392 } 393 setEnabled(false); 394 } 395 } 396 397 private class UndoActionHandler extends Action { 398 protected UndoActionHandler() { 399 setId("CellEditorUndoActionHandler"); setEnabled(false); 401 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IWorkbenchHelpContextIds.CELL_UNDO_ACTION); 402 } 403 404 public void runWithEvent(Event event) { 405 if (activeEditor != null) { 406 activeEditor.performUndo(); 407 return; 408 } 409 if (undoAction != null) { 410 undoAction.runWithEvent(event); 411 return; 412 } 413 } 414 415 public void updateEnabledState() { 416 if (activeEditor != null) { 417 setEnabled(activeEditor.isUndoEnabled()); 418 setText(WorkbenchMessages.Workbench_undo); 419 setToolTipText(WorkbenchMessages.Workbench_undoToolTip); 420 return; 421 } 422 if (undoAction != null) { 423 setEnabled(undoAction.isEnabled()); 424 setText(undoAction.getText()); 425 setToolTipText(undoAction.getToolTipText()); 426 return; 427 } 428 setEnabled(false); 429 } 430 } 431 432 private class RedoActionHandler extends Action { 433 protected RedoActionHandler() { 434 setId("CellEditorRedoActionHandler"); setEnabled(false); 436 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IWorkbenchHelpContextIds.CELL_REDO_ACTION); 437 } 438 439 public void runWithEvent(Event event) { 440 if (activeEditor != null) { 441 activeEditor.performRedo(); 442 return; 443 } 444 if (redoAction != null) { 445 redoAction.runWithEvent(event); 446 return; 447 } 448 } 449 450 public void updateEnabledState() { 451 if (activeEditor != null) { 452 setEnabled(activeEditor.isRedoEnabled()); 453 setText(WorkbenchMessages.Workbench_redo); 454 setToolTipText(WorkbenchMessages.Workbench_redoToolTip); 455 return; 456 } 457 if (redoAction != null) { 458 setEnabled(redoAction.isEnabled()); 459 setText(redoAction.getText()); 460 setToolTipText(redoAction.getToolTipText()); 461 return; 462 } 463 setEnabled(false); 464 } 465 } 466 467 475 public CellEditorActionHandler(IActionBars actionBar) { 476 super(); 477 actionBar.setGlobalActionHandler(ActionFactory.CUT.getId(), 478 cellCutAction); 479 actionBar.setGlobalActionHandler(ActionFactory.COPY.getId(), 480 cellCopyAction); 481 actionBar.setGlobalActionHandler(ActionFactory.PASTE.getId(), 482 cellPasteAction); 483 actionBar.setGlobalActionHandler(ActionFactory.DELETE.getId(), 484 cellDeleteAction); 485 actionBar.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), 486 cellSelectAllAction); 487 actionBar.setGlobalActionHandler(ActionFactory.FIND.getId(), 488 cellFindAction); 489 actionBar.setGlobalActionHandler(ActionFactory.UNDO.getId(), 490 cellUndoAction); 491 actionBar.setGlobalActionHandler(ActionFactory.REDO.getId(), 492 cellRedoAction); 493 } 494 495 502 public void addCellEditor(CellEditor editor) { 503 if (editor == null) { 504 return; 505 } 506 507 Control control = editor.getControl(); 508 Assert.isNotNull(control); 509 controlToEditor.put(control, editor); 510 control.addListener(SWT.Activate, controlListener); 511 control.addListener(SWT.Deactivate, controlListener); 512 513 if (control.isFocusControl()) { 514 activeEditor = editor; 515 editor.addPropertyChangeListener(cellListener); 516 updateActionsEnableState(); 517 } 518 } 519 520 523 public void dispose() { 524 setCutAction(null); 525 setCopyAction(null); 526 setPasteAction(null); 527 setDeleteAction(null); 528 setSelectAllAction(null); 529 setFindAction(null); 530 setUndoAction(null); 531 setRedoAction(null); 532 533 Iterator itr = controlToEditor.keySet().iterator(); 534 while (itr.hasNext()) { 535 Control control = (Control) itr.next(); 536 if (!control.isDisposed()) { 537 control.removeListener(SWT.Activate, controlListener); 538 control.removeListener(SWT.Deactivate, controlListener); 539 } 540 } 541 controlToEditor.clear(); 542 543 if (activeEditor != null) { 544 activeEditor.removePropertyChangeListener(cellListener); 545 } 546 activeEditor = null; 547 548 } 549 550 557 public void removeCellEditor(CellEditor editor) { 558 if (editor == null) { 559 return; 560 } 561 562 if (activeEditor == editor) { 563 activeEditor.removePropertyChangeListener(cellListener); 564 activeEditor = null; 565 } 566 567 Control control = editor.getControl(); 568 if (control != null) { 569 controlToEditor.remove(control); 570 if (!control.isDisposed()) { 571 control.removeListener(SWT.Activate, controlListener); 572 control.removeListener(SWT.Deactivate, controlListener); 573 } 574 } 575 } 576 577 585 public void setCopyAction(IAction action) { 586 if (copyAction == action) { 587 return; 588 } 589 590 if (copyAction != null) { 591 copyAction.removePropertyChangeListener(copyActionListener); 592 } 593 594 copyAction = action; 595 596 if (copyAction != null) { 597 copyAction.addPropertyChangeListener(copyActionListener); 598 } 599 600 cellCopyAction.updateEnabledState(); 601 } 602 603 611 public void setCutAction(IAction action) { 612 if (cutAction == action) { 613 return; 614 } 615 616 if (cutAction != null) { 617 cutAction.removePropertyChangeListener(cutActionListener); 618 } 619 620 cutAction = action; 621 622 if (cutAction != null) { 623 cutAction.addPropertyChangeListener(cutActionListener); 624 } 625 626 cellCutAction.updateEnabledState(); 627 } 628 629 637 public void setDeleteAction(IAction action) { 638 if (deleteAction == action) { 639 return; 640 } 641 642 if (deleteAction != null) { 643 deleteAction.removePropertyChangeListener(deleteActionListener); 644 } 645 646 deleteAction = action; 647 648 if (deleteAction != null) { 649 deleteAction.addPropertyChangeListener(deleteActionListener); 650 } 651 652 cellDeleteAction.updateEnabledState(); 653 } 654 655 663 public void setFindAction(IAction action) { 664 if (findAction == action) { 665 return; 666 } 667 668 if (findAction != null) { 669 findAction.removePropertyChangeListener(findActionListener); 670 } 671 672 findAction = action; 673 674 if (findAction != null) { 675 findAction.addPropertyChangeListener(findActionListener); 676 } 677 678 cellFindAction.updateEnabledState(); 679 } 680 681 689 public void setPasteAction(IAction action) { 690 if (pasteAction == action) { 691 return; 692 } 693 694 if (pasteAction != null) { 695 pasteAction.removePropertyChangeListener(pasteActionListener); 696 } 697 698 pasteAction = action; 699 700 if (pasteAction != null) { 701 pasteAction.addPropertyChangeListener(pasteActionListener); 702 } 703 704 cellPasteAction.updateEnabledState(); 705 } 706 707 715 public void setRedoAction(IAction action) { 716 if (redoAction == action) { 717 return; 718 } 719 720 if (redoAction != null) { 721 redoAction.removePropertyChangeListener(redoActionListener); 722 } 723 724 redoAction = action; 725 726 if (redoAction != null) { 727 redoAction.addPropertyChangeListener(redoActionListener); 728 } 729 730 cellRedoAction.updateEnabledState(); 731 } 732 733 741 public void setSelectAllAction(IAction action) { 742 if (selectAllAction == action) { 743 return; 744 } 745 746 if (selectAllAction != null) { 747 selectAllAction 748 .removePropertyChangeListener(selectAllActionListener); 749 } 750 751 selectAllAction = action; 752 753 if (selectAllAction != null) { 754 selectAllAction.addPropertyChangeListener(selectAllActionListener); 755 } 756 757 cellSelectAllAction.updateEnabledState(); 758 } 759 760 768 public void setUndoAction(IAction action) { 769 if (undoAction == action) { 770 return; 771 } 772 773 if (undoAction != null) { 774 undoAction.removePropertyChangeListener(undoActionListener); 775 } 776 777 undoAction = action; 778 779 if (undoAction != null) { 780 undoAction.addPropertyChangeListener(undoActionListener); 781 } 782 783 cellUndoAction.updateEnabledState(); 784 } 785 786 791 private void updateActionsEnableState() { 792 cellCutAction.updateEnabledState(); 793 cellCopyAction.updateEnabledState(); 794 cellPasteAction.updateEnabledState(); 795 cellDeleteAction.updateEnabledState(); 796 cellSelectAllAction.updateEnabledState(); 797 cellFindAction.updateEnabledState(); 798 cellUndoAction.updateEnabledState(); 799 cellRedoAction.updateEnabledState(); 800 } 801 } 802 | Popular Tags |