Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a3b4d4fcc247c576d30a67f4562117371dcaf94a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*******************************************************************************
 * Copyright (c) 2007, 2011 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.compare.internal;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.compare.ICompareContainer;
import org.eclipse.core.expressions.Expression;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.commands.ActionHandler;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.*;
import org.eclipse.ui.handlers.IHandlerActivation;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.services.IServiceLocator;

public class CompareHandlerService {
	
	private final List fActivations = new ArrayList();
	private final Expression fExpression;
	private ICompareContainer fContainer;
	private boolean fDisposed;
	private List fPaneActivations = new ArrayList();
	private IHandlerService fHandlerService;

	public static CompareHandlerService createFor(ICompareContainer container, Shell shell) {
		IServiceLocator serviceLocator = container.getServiceLocator();
		if (serviceLocator != null) {
			IHandlerService service = serviceLocator.getService(IHandlerService.class);
			if (service != null)
				return new CompareHandlerService(container, null);
		}
		if (container.getWorkbenchPart() == null && shell != null) {
			// We're in a dialog so we can use an active shell expression
			IHandlerService service = PlatformUI.getWorkbench().getService(IHandlerService.class);
			if (service != null) {
				Expression e = new ActiveShellExpression(shell);
				return new CompareHandlerService(container, e);
			}
		}
		return new CompareHandlerService(null, null);
	}
	
	private CompareHandlerService(ICompareContainer container,
			Expression expression) {
		fContainer = container;
		fExpression = expression;
		initialize();
	}
	
	public void registerAction(IAction action, String commandId) {
		IHandlerService handlerService = getHandlerService();
		if (handlerService == null)
			return;
		action.setActionDefinitionId(commandId);
		IHandlerActivation activation;
		if (fExpression == null) {
			activation = handlerService.activateHandler(commandId, new ActionHandler(action));
		} else {
			activation = handlerService.activateHandler(commandId, new ActionHandler(action), fExpression);
		}
		if (activation != null) {
			fActivations .add(activation);
		}
	}
	
	private IHandlerService getHandlerService() {
		if (fDisposed)
			return null;
		return fHandlerService;
	}

	private void initialize() {
		if (fHandlerService == null) {
			IServiceLocator serviceLocator = fContainer.getServiceLocator();
			if (serviceLocator != null) {
				IHandlerService service = serviceLocator.getService(IHandlerService.class);
				if (service != null)
					fHandlerService = service;
			}
			if (fHandlerService == null && fContainer.getWorkbenchPart() == null && fExpression != null) {
				// We're in a dialog so we can use an active shell expression
				IHandlerService service = PlatformUI.getWorkbench().getService(IHandlerService.class);
				if (service != null) {
					fHandlerService = service;
				}
			}
		}
	}

	public void setGlobalActionHandler(String actionId, IAction actionHandler) {
		IActionBars bars = getActionBars();
		if (bars != null) {
			bars.setGlobalActionHandler(actionId, actionHandler);
			return;
		} else if (fExpression != null && actionHandler != null && actionHandler.getActionDefinitionId() != null) {
			IHandlerService service = getHandlerService();
			if (service != null) {
				IHandlerActivation activation = service.activateHandler(actionHandler.getActionDefinitionId(), new ActionHandler(actionHandler), fExpression);
				fPaneActivations.add(activation);
				return;
			}
		}
		// Remove the action definition id since we won't get key bindings
		if (actionHandler != null)
			actionHandler.setActionDefinitionId(null);
	}
	
	private void updateActionBars() {
		IActionBars bars = getActionBars();
		if (bars != null)
			bars.updateActionBars();
	}

	private void clearPaneActionHandlers() {
		if (!fPaneActivations.isEmpty()) {
			IHandlerService service = getHandlerService();
			if (service != null) {
				service.deactivateHandlers(fPaneActivations);
				fPaneActivations.clear();
			}
		}
	}
	
	private IActionBars getActionBars() {
		return fContainer.getActionBars();
	}

	public void dispose() {
		clearPaneActionHandlers();
		IHandlerService service = getHandlerService();
		if (service == null)
			return;
		service.deactivateHandlers(fActivations);
		fActivations.clear();
		fDisposed = true;
	}

	public void updatePaneActionHandlers(Runnable runnable) {
		clearPaneActionHandlers();
		runnable.run();
		updateActionBars();
	}
}

Back to the top