Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 07a0cd970f205b38c20a5f0a0dd9f33b157ce2e6 (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) 2010 CEA LIST.
 *
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *  Itemis - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.uml.xtext.integration;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.core.resources.IProject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IPageListener;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IWindowListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * Get the last active editor in general or of a specific type.
 *
 * Please note that the ActiveEditorTracker currently only supports one single
 * workbench window properly.
 *
 * @author patrick.koenemann@itemis.de
 * @author alexander.nyssen@itemis.de
 *
 */
public class ActiveEditorTracker implements IPageListener, IPartListener,
		IWindowListener {

	private static final String SINGLETON_MSG = "This class is a singleton and may only be instantiated once!";

	private IWorkbenchWindow workbenchWindow;

	private Map<String, IEditorPart> activeEditors = new HashMap<String, IEditorPart>();

	private String lastActiveEditorId;

	private IWorkbenchPage activePage;

	private static ActiveEditorTracker INSTANCE;

	public ActiveEditorTracker() {
		if (INSTANCE != null) {
			throw new IllegalStateException(SINGLETON_MSG);
		}
		INSTANCE = this;
	}

	public void earlyStartup() {
		PlatformUI.getWorkbench().addWindowListener(this);
	}

	/**
	 * @return The last active editor in the current active workbench page.
	 */
	public static IEditorPart getLastActiveEditor() {
		if (INSTANCE == null) {
			// not yet initialized, e.g. when another early startups blocks us!
			// Let's try to get the current active editor instead.
			if (PlatformUI.getWorkbench() != null) {
				if (PlatformUI.getWorkbench().getActiveWorkbenchWindow() != null) {
					return PlatformUI.getWorkbench().getActiveWorkbenchWindow()
							.getActivePage().getActiveEditor();
				}
			}
			return null;
		}
		return INSTANCE.getLastActiveEditorInternal();
	}

	/**
	 * @return The last active editor with the given editor ID in the current
	 *         active workbench page.
	 */
	public static IEditorPart getLastEditor(String editorId) {
		if (INSTANCE == null) {
			// not yet initialized, e.g. when another early startups blocks us!
			// Let's try to get any editor with the specified id instead.
			if (PlatformUI.getWorkbench() != null) {
				final IWorkbenchWindow window = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow();
				if (window != null) {
					final IWorkbenchPage page = window.getActivePage();
					if (page != null) {
						for (IEditorReference ref : page.getEditorReferences()) {
							if (ref.getId().equals(editorId)) {
								return ref.getEditor(false);
							}
						}
					}
				}
			}
			return null;
		}
		return INSTANCE.getEditorById(editorId);
	}

	/**
	 *
	 * @return The EMF resource set of the last active editor (if it is still
	 *         open).
	 */
	public static ResourceSet getLastActiveEditorResourceSet() {
		final IEditorPart editor = getLastActiveEditor();
		if (editor == null) {
			return null;
		}
		EditingDomain domain = null;
		if (editor instanceof IEditingDomainProvider) {
			domain = ((IEditingDomainProvider) editor).getEditingDomain();
		} else if (editor.getAdapter(IEditingDomainProvider.class) != null) {
			domain = editor
					.getAdapter(IEditingDomainProvider.class)
					.getEditingDomain();
		} else if (editor.getAdapter(EditingDomain.class) != null) {
			domain = editor.getAdapter(EditingDomain.class);
		}
		if (domain == null) {
			return null;
		}

		return domain.getResourceSet();
	}

	/**
	 * @return The project which contains the file that is open in the last
	 *         active editor in the current workbench page.
	 */
	public static IProject getLastActiveEditorProject() {
		final IEditorPart editor = getLastActiveEditor();
		if (editor == null) {
			return null;
		}
		final IEditorInput editorInput = editor.getEditorInput();
		if (editorInput instanceof IFileEditorInput) {
			final IFileEditorInput input = (IFileEditorInput) editorInput;
			return input.getFile().getProject();
		}
		return null;
	}

	public void pageActivated(IWorkbenchPage page) {
		this.activePage = page;
	}

	public void pageClosed(IWorkbenchPage page) {
		if (page == activePage) {
			activePage = null;
		}
		lastActiveEditorId = null;
	}

	public void pageOpened(IWorkbenchPage page) {
		// do nothing
		System.out.println("PAGE OPENED: " + page);
	}

	public void partActivated(IWorkbenchPart part) {
		if (part instanceof IEditorPart) {
			setActiveEditor((IEditorPart) part);
		}
	}

	public void partBroughtToTop(IWorkbenchPart part) {
		if (part instanceof IEditorPart) {
			setActiveEditor((IEditorPart) part);
		}
	}

	public void partClosed(IWorkbenchPart part) {
		if (part instanceof IEditorPart) {
			String id = null;
			for (Entry<String, IEditorPart> entry : activeEditors.entrySet()) {
				if (entry.getValue().equals(part)) {
					id = entry.getKey();
					break;
				}
			}
			if (id != null) {
				activeEditors.remove(id);
				if (id.equals(lastActiveEditorId)) {
					lastActiveEditorId = null;
				}
			}
		}
	}

	public void partDeactivated(IWorkbenchPart part) {
		if (part instanceof IEditorPart) {
			// do nothing
		}
	}

	private IEditorPart getLastActiveEditorInternal() {
		if (activePage == null) {
			initialize(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
			if (activePage == null) {
				return null;
			}
		}
		boolean updated = false;
		if (lastActiveEditorId == null) {
			final IEditorPart editor = activePage.getActiveEditor();
			if (editor != null) {
				setActiveEditor(editor);
			}
			updated = true;
		}
		IEditorPart editor = getEditorById(lastActiveEditorId);
		if (editor == null && !updated) {
			editor = activePage.getActiveEditor();
			if (editor != null) {
				setActiveEditor(editor);
			}
		}
		return editor;
	}

	private IEditorPart getEditorById(String editorId) {
		if (activePage == null || editorId == null) {
			return null;
		}
		final IEditorPart editor = activeEditors.get(editorId);
		final String id = checkEditorAndGetId(editor);
		if (id != null && id.equals(editorId)) {
			return editor;
		}
		return null;
	}

	private String checkEditorAndGetId(IEditorPart editor) {
		if (editor == null) {
			return null;
		}
		for (IEditorReference ref : activePage.getEditorReferences()) {
			if (editor.equals(ref.getEditor(false))) {
				return ref.getId();
			}
		}
		return null;
	}

	public IWorkbenchPage getActivePage() {
		return activePage;
	}

	/**
	 * Set the active editor
	 */
	private void setActiveEditor(IEditorPart part) {
		if (part == null) {
			lastActiveEditorId = null;
			return;
		}
		final IWorkbenchPartReference reference = activePage.getReference(part);
		if (reference == null) {
			throw new IllegalStateException("Impossible?!");
		}
		lastActiveEditorId = reference.getId();
		activeEditors.put(lastActiveEditorId, part);
	}

	public void partOpened(IWorkbenchPart part) {
		if (part instanceof IEditorPart) {
			setActiveEditor((IEditorPart) part);
		}
	}

	public void dispose() {
		if (workbenchWindow == null) {
			return;
		}
		workbenchWindow.removePageListener(this);
		workbenchWindow.getPartService().removePartListener(this);
		workbenchWindow = null;
	}

	public void windowActivated(IWorkbenchWindow window) {
		initialize(window);
	}

	protected void initialize(IWorkbenchWindow window) {
		if (workbenchWindow != null && !workbenchWindow.equals(window)) {
			/*
			 * TODO: implement logic for keeping track of editors in multiple
			 * workbench windows!
			 */
		}
		this.workbenchWindow = window;
		if (window == null) {
			return;
		}
		this.activePage = window.getActivePage();
		final IEditorPart editor = this.activePage.getActiveEditor();
		if (editor != null) {
			lastActiveEditorId = checkEditorAndGetId(editor);
			activeEditors.put(lastActiveEditorId, editor);
		}
		window.addPageListener(this);
		window.getPartService().addPartListener(this);
	}

	public void windowDeactivated(IWorkbenchWindow window) {
		// not of interest
	}

	public void windowClosed(IWorkbenchWindow window) {
		dispose();
	}

	public void windowOpened(IWorkbenchWindow window) {
		// not of interest
	}
}

Back to the top