Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9b8cc561ee600d27bc36753fdd4e06818df3c705 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*******************************************************************************
 * Copyright (c) 2012 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
 *     Sopot Cela - initial API and implementation
 ******************************************************************************/

package org.eclipse.e4.ui.internal.workbench.addons;

import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import org.eclipse.e4.core.commands.EHandlerService;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.services.contributions.IContributionFactory;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.commands.MCommand;
import org.eclipse.e4.ui.model.application.commands.MHandler;
import org.eclipse.e4.ui.model.application.commands.MHandlerContainer;
import org.eclipse.e4.ui.model.application.ui.MContext;
import org.eclipse.e4.ui.workbench.UIEvents;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;

/**
 * Process the additions and removals of handlers on the model
 */
public class HandlerProcessingAddon {

	private EventHandler eventHandler;
	private EventHandler contextHandler;

	@Inject
	private IEventBroker eventBroker;

	/**
	 * Registers the listeners
	 * 
	 * @param application
	 * @param modelService
	 */
	@PostConstruct
	public void postConstruct(MApplication application, EModelService modelService) {
		List<MHandlerContainer> findElements = modelService.findElements(application, null,
				MHandlerContainer.class, null);
		for (MHandlerContainer mHandlerContainer : findElements) {
			if (mHandlerContainer instanceof MContext) {
				MContext mContext = (MContext) mHandlerContainer;
				IEclipseContext context = mContext.getContext();
				if (context != null) {
					for (MHandler mHandler : mHandlerContainer.getHandlers()) {
						processActiveHandler(mHandler, context);
					}
				}
			}
		}

		registerModelListeners();
	}

	private void registerModelListeners() {
		eventHandler = new EventHandler() {
			public void handleEvent(Event event) {
				if ((event.getProperty(UIEvents.EventTags.ELEMENT) instanceof MHandlerContainer)
						&& (event.getProperty(UIEvents.EventTags.ELEMENT) instanceof MContext)) {
					MHandlerContainer handlerContainer = (MHandlerContainer) event
							.getProperty(UIEvents.EventTags.ELEMENT);
					if (UIEvents.EventTypes.ADD.equals(event.getProperty(UIEvents.EventTags.TYPE))) {
						if (event.getProperty(UIEvents.EventTags.NEW_VALUE) instanceof MHandler) {
							MHandler handler = (MHandler) event
									.getProperty(UIEvents.EventTags.NEW_VALUE);
							MContext mContext = (MContext) handlerContainer;
							IEclipseContext context = mContext.getContext();
							if (context != null) {
								processActiveHandler(handler, context);
							}
						}
					} else if (UIEvents.EventTypes.REMOVE.equals(event
							.getProperty(UIEvents.EventTags.TYPE))) {
						if (event.getProperty(UIEvents.EventTags.OLD_VALUE) instanceof MHandler) {
							MHandler handler = (MHandler) event
									.getProperty(UIEvents.EventTags.OLD_VALUE);
							MContext mContext = (MContext) handlerContainer;
							IEclipseContext context = mContext.getContext();
							if (context != null) {
								MCommand command = handler.getCommand();
								if (command != null) {
									String commandId = command.getElementId();
									EHandlerService handlerService = (EHandlerService) context
											.get(EHandlerService.class.getName());
									handlerService
											.deactivateHandler(commandId, handler.getObject());
								}
							}
						}

					}

				}

			}
		};

		contextHandler = new EventHandler() {

			public void handleEvent(Event event) {
				Object origin = event.getProperty(UIEvents.EventTags.ELEMENT);
				Object context = event.getProperty(UIEvents.EventTags.NEW_VALUE);
				if ((origin instanceof MHandlerContainer)
						&& (UIEvents.EventTypes.SET.equals(event
								.getProperty(UIEvents.EventTags.TYPE)) && context instanceof IEclipseContext)) {
					MHandlerContainer handlerContainer = (MHandlerContainer) origin;
					IEclipseContext castedContext = (IEclipseContext) context;
					for (MHandler mHandler : handlerContainer.getHandlers()) {
						processActiveHandler(mHandler, castedContext);
					}

				}

			}
		};
		eventBroker.subscribe(UIEvents.HandlerContainer.TOPIC_HANDLERS, eventHandler);
		eventBroker.subscribe(UIEvents.Context.TOPIC_CONTEXT, contextHandler);
	}

	/**
	 * Clean up
	 */
	@PreDestroy
	public void preDestroy() {
		unregisterModelListeners();
	}

	/**
	 * Unregisters the listeners
	 */
	private void unregisterModelListeners() {
		eventBroker.unsubscribe(eventHandler);
		eventBroker.unsubscribe(contextHandler);
	}

	/**
	 * @param handler
	 * @param context
	 */
	private void processActiveHandler(MHandler handler, IEclipseContext context) {
		MCommand command = handler.getCommand();
		if (command != null) {
			String commandId = command.getElementId();
			if (handler.getObject() == null) {
				IContributionFactory contributionFactory = (IContributionFactory) context
						.get(IContributionFactory.class.getName());
				handler.setObject(contributionFactory.create(handler.getContributionURI(), context));
			}
			EHandlerService handlerService = (EHandlerService) context.get(EHandlerService.class
					.getName());
			handlerService.activateHandler(commandId, handler.getObject());
		}
	}

}

Back to the top