1 11 package org.eclipse.compare.internal; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.compare.ICompareContainer; 17 import org.eclipse.core.expressions.Expression; 18 import org.eclipse.jface.action.IAction; 19 import org.eclipse.jface.commands.ActionHandler; 20 import org.eclipse.swt.widgets.Shell; 21 import org.eclipse.ui.*; 22 import org.eclipse.ui.handlers.IHandlerActivation; 23 import org.eclipse.ui.handlers.IHandlerService; 24 import org.eclipse.ui.services.IServiceLocator; 25 26 public class CompareHandlerService { 27 28 private final List fActivations = new ArrayList (); 29 private final Expression fExpression; 30 private ICompareContainer fContainer; 31 private boolean fDisposed; 32 private List fPaneActivations = new ArrayList (); 33 private IHandlerService fHandlerService; 34 35 public static CompareHandlerService createFor(ICompareContainer container, Shell shell) { 36 IServiceLocator serviceLocator = container.getServiceLocator(); 37 if (serviceLocator != null) { 38 IHandlerService service = (IHandlerService)serviceLocator.getService(IHandlerService.class); 39 if (service != null) 40 return new CompareHandlerService(container, null); 41 } 42 if (container.getWorkbenchPart() == null && shell != null) { 43 IHandlerService service = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class); 45 if (service != null) { 46 Expression e = new ActiveShellExpression(shell); 47 return new CompareHandlerService(container, e); 48 } 49 } 50 return new CompareHandlerService(null, null); 51 } 52 53 private CompareHandlerService(ICompareContainer container, 54 Expression expression) { 55 fContainer = container; 56 fExpression = expression; 57 initialize(); 58 } 59 60 public void registerAction(IAction action, String commandId) { 61 IHandlerService handlerService = getHandlerService(); 62 if (handlerService == null) 63 return; 64 action.setActionDefinitionId(commandId); 65 IHandlerActivation activation; 66 if (fExpression == null) { 67 activation = handlerService.activateHandler(commandId, new ActionHandler(action)); 68 } else { 69 activation = handlerService.activateHandler(commandId, new ActionHandler(action), fExpression); 70 } 71 if (activation != null) { 72 fActivations .add(activation); 73 } 74 } 75 76 private IHandlerService getHandlerService() { 77 if (fDisposed) 78 return null; 79 return fHandlerService; 80 } 81 82 private void initialize() { 83 if (fHandlerService == null) { 84 IServiceLocator serviceLocator = fContainer.getServiceLocator(); 85 if (serviceLocator != null) { 86 IHandlerService service = (IHandlerService)serviceLocator.getService(IHandlerService.class); 87 if (service != null) 88 fHandlerService = service; 89 } 90 if (fHandlerService == null && fContainer.getWorkbenchPart() == null && fExpression != null) { 91 IHandlerService service = (IHandlerService)PlatformUI.getWorkbench().getService(IHandlerService.class); 93 if (service != null) { 94 fHandlerService = service; 95 } 96 } 97 } 98 } 99 100 public void setGlobalActionHandler(String actionId, IAction actionHandler) { 101 IActionBars bars = getActionBars(); 102 if (bars != null) { 103 bars.setGlobalActionHandler(actionId, actionHandler); 104 return; 105 } else if (fExpression != null && actionHandler != null && actionHandler.getActionDefinitionId() != null) { 106 IHandlerService service = getHandlerService(); 107 if (service != null) { 108 IHandlerActivation activation = service.activateHandler(actionHandler.getActionDefinitionId(), new ActionHandler(actionHandler), fExpression); 109 fPaneActivations.add(activation); 110 return; 111 } 112 } 113 if (actionHandler != null) 115 actionHandler.setActionDefinitionId(null); 116 } 117 118 private void updateActionBars() { 119 IActionBars bars = getActionBars(); 120 if (bars != null) 121 bars.updateActionBars(); 122 } 123 124 private void clearPaneActionHandlers() { 125 if (!fPaneActivations.isEmpty()) { 126 IHandlerService service = getHandlerService(); 127 if (service != null) { 128 service.deactivateHandlers(fPaneActivations); 129 fPaneActivations.clear(); 130 } 131 } 132 } 133 134 private IActionBars getActionBars() { 135 return fContainer.getActionBars(); 136 } 137 138 public void dispose() { 139 clearPaneActionHandlers(); 140 IHandlerService service = getHandlerService(); 141 if (service == null) 142 return; 143 service.deactivateHandlers(fActivations); 144 fActivations.clear(); 145 fDisposed = true; 146 } 147 148 public void updatePaneActionHandlers(Runnable runnable) { 149 clearPaneActionHandlers(); 150 runnable.run(); 151 updateActionBars(); 152 } 153 } 154 | Popular Tags |