Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 18a21ad2da5c383a5e4c926832a035039b419450 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*******************************************************************************
 * Copyright (c) 2009, 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
 ******************************************************************************/
package org.eclipse.e4.ui.internal.workbench;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.inject.Named;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.contexts.RunAndTrack;
import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.core.internal.contexts.EclipseContext;
import org.eclipse.e4.core.internal.contexts.IContextDisposalListener;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.core.services.log.Logger;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.e4.ui.workbench.UIEvents;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.e4.ui.workbench.modeling.ISelectionListener;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;

public class SelectionAggregator {

	static final String OUT_SELECTION = "org.eclipse.ui.output.selection"; //$NON-NLS-1$
	static final String OUT_POST_SELECTION = "org.eclipse.ui.output.postSelection"; //$NON-NLS-1$

	private ListenerList genericListeners = new ListenerList();
	private ListenerList genericPostListeners = new ListenerList();
	private Map<String, ListenerList> targetedListeners = new HashMap<String, ListenerList>();
	private Map<String, ListenerList> targetedPostListeners = new HashMap<String, ListenerList>();
	private Set<IEclipseContext> tracked = new HashSet<IEclipseContext>();

	private EventHandler eventHandler = new EventHandler() {
		public void handleEvent(Event event) {
			Object element = event.getProperty(UIEvents.EventTags.ELEMENT);
			if (element instanceof MPart) {
				MPart part = (MPart) element;

				String partId = part.getElementId();
				if (targetedListeners.containsKey(partId))
					track(part);
			}
		}
	};

	private MPart activePart;

	private IEclipseContext context;

	private EPartService partService;

	private IEventBroker eventBroker;

	private Logger logger;

	@Inject
	SelectionAggregator(IEclipseContext context, EPartService partService,
			IEventBroker eventBroker, Logger logger) {
		super();
		this.context = context;
		this.partService = partService;
		this.eventBroker = eventBroker;
		this.logger = logger;
	}

	@PreDestroy
	void preDestroy() {
		genericListeners.clear();
		genericPostListeners.clear();
		targetedListeners.clear();
		targetedPostListeners.clear();

		eventBroker.unsubscribe(eventHandler);
	}

	@PostConstruct
	void postConstruct() {
		eventBroker.subscribe(UIEvents.Context.TOPIC_CONTEXT, eventHandler);
	}

	@Inject
	void setPart(@Optional @Named(IServiceConstants.ACTIVE_PART) final MPart part) {
		if ((part != null) && (activePart != part)) {
			activePart = part;
			IEclipseContext partContext = part.getContext();
			// only notify listeners if the part actually posts selections
			if (partContext.containsKey(OUT_POST_SELECTION)) {
				Object selection = partContext.get(OUT_POST_SELECTION);
				context.set(IServiceConstants.ACTIVE_SELECTION, selection);
				notifyPostListeners(part, selection);
			} else if (partContext.containsKey(OUT_SELECTION)) {
				Object selection = partContext.get(OUT_SELECTION);
				context.set(IServiceConstants.ACTIVE_SELECTION, selection);
				notifyListeners(part, selection);
			}
			track(part);
		}
	}

	private void notifyListeners(final MPart part, final Object selection) {
		for (Object listener : genericListeners.getListeners()) {
			final ISelectionListener myListener = (ISelectionListener) listener;
			SafeRunner.run(new ISafeRunnable() {
				public void run() throws Exception {
					myListener.selectionChanged(part, selection);
				}

				public void handleException(Throwable exception) {
					logger.error(exception);
				}
			});
		}
		notifyTargetedListeners(part, selection);
	}

	private void notifyTargetedListeners(final MPart part, final Object selection) {
		String id = part.getElementId();
		if (id != null) {
			ListenerList listenerList = targetedListeners.get(id);
			if (listenerList != null) {
				for (Object listener : listenerList.getListeners()) {
					final ISelectionListener myListener = (ISelectionListener) listener;
					SafeRunner.run(new ISafeRunnable() {
						public void run() throws Exception {
							myListener.selectionChanged(part, selection);
						}

						public void handleException(Throwable exception) {
							logger.error(exception);
						}
					});
				}
			}
		}
	}

	private void notifyPostListeners(final MPart part, final Object selection) {
		for (Object listener : genericPostListeners.getListeners()) {
			final ISelectionListener myListener = (ISelectionListener) listener;
			SafeRunner.run(new ISafeRunnable() {
				public void run() throws Exception {
					myListener.selectionChanged(part, selection);
				}

				public void handleException(Throwable exception) {
					logger.error(exception);
				}
			});
		}
		notifyTargetedPostListeners(part, selection);
	}

	private void notifyTargetedPostListeners(final MPart part, final Object selection) {
		String id = part.getElementId();
		if (id != null) {
			ListenerList listenerList = targetedPostListeners.get(id);
			if (listenerList != null) {
				for (Object listener : listenerList.getListeners()) {
					final ISelectionListener myListener = (ISelectionListener) listener;
					SafeRunner.run(new ISafeRunnable() {
						public void run() throws Exception {
							myListener.selectionChanged(part, selection);
						}

						public void handleException(Throwable exception) {
							logger.error(exception);
						}
					});
				}
			}
		}
	}

	private void track(final MPart part) {
		final IEclipseContext myContext = this.context;
		IEclipseContext context = part.getContext();
		if (context != null && tracked.add(context)) {
			if (context instanceof EclipseContext) {
				((EclipseContext) context).notifyOnDisposal(new IContextDisposalListener() {
					public void disposed(IEclipseContext context) {
						tracked.remove(context);
					}
				});
			}

			context.runAndTrack(new RunAndTrack() {
				private boolean initial = true;

				public boolean changed(IEclipseContext context) {
					final Object selection = context.get(OUT_SELECTION);
					final Object postSelection = context.get(OUT_POST_SELECTION);
					context.set(OUT_POST_SELECTION, null); // make sure it's gone until set again
					if (initial) {
						initial = false;
						if (selection == null) {
							return true;
						}
					}

					if (activePart == part) {
						myContext.set(IServiceConstants.ACTIVE_SELECTION, selection);
						runExternalCode(new Runnable() {
							public void run() {
								if (postSelection != null)
									notifyPostListeners(part, selection);
								else
									notifyListeners(part, selection);
							}
						});
					} else {
						runExternalCode(new Runnable() {
							public void run() {
								if (postSelection != null)
									notifyTargetedPostListeners(part, selection);
								else
									notifyTargetedListeners(part, selection);
							}
						});
						// we don't need to keep tracking non-active parts unless
						// they have targeted listeners
						String partId = part.getElementId();
						boolean continueTracking = targetedListeners.containsKey(partId)
								|| targetedPostListeners.containsKey(partId);
						if (!continueTracking) {
							tracked.remove(part.getContext());
						}
						return continueTracking;
					}
					return true;
				}
			});
		}
	}

	public Object getSelection() {
		return context.get(IServiceConstants.ACTIVE_SELECTION);
	}

	public void addSelectionListener(ISelectionListener listener) {
		genericListeners.add(listener);
	}

	public void addPostSelectionListener(ISelectionListener listener) {
		genericPostListeners.add(listener);
	}

	public void removeSelectionListener(ISelectionListener listener) {
		// we may have been destroyed already, see bug 310113
		if (context != null) {
			genericListeners.remove(listener);
		}
	}

	public void removePostSelectionListener(ISelectionListener listener) {
		// we may have been destroyed already, see bug 310113
		if (context != null) {
			genericPostListeners.remove(listener);
		}
	}

	public void addSelectionListener(String partId, ISelectionListener listener) {
		ListenerList listeners = targetedListeners.get(partId);
		if (listeners == null) {
			listeners = new ListenerList();
			targetedListeners.put(partId, listeners);
		}
		listeners.add(listener);

		MPart part = partService.findPart(partId);
		if (part != null)
			track(part);
	}

	public void addPostSelectionListener(String partId, ISelectionListener listener) {
		ListenerList listeners = targetedPostListeners.get(partId);
		if (listeners == null) {
			listeners = new ListenerList();
			targetedPostListeners.put(partId, listeners);
		}
		listeners.add(listener);

		MPart part = partService.findPart(partId);
		if (part != null)
			track(part);
	}

	public void removeSelectionListener(String partId, ISelectionListener listener) {
		// we may have been destroyed already, see bug 310113
		if (context != null) {
			ListenerList listeners = targetedListeners.get(partId);
			if (listeners != null) {
				listeners.remove(listener);
			}
		}
	}

	public void removePostSelectionListener(String partId, ISelectionListener listener) {
		// we may have been destroyed already, see bug 310113
		if (context != null) {
			ListenerList listeners = targetedPostListeners.get(partId);
			if (listeners != null) {
				listeners.remove(listener);
			}
		}
	}

	public Object getSelection(String partId) {
		MPart part = partService.findPart(partId);
		if (part == null) {
			return null;
		}

		IEclipseContext partContext = part.getContext();
		if (partContext == null) {
			return null;
		}
		return partContext.get(OUT_SELECTION);
	}

}

Back to the top