Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b231fb1ae584faef0834655c4e4446ef4a47cd57 (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
/*****************************************************************************
 * Copyright (c) 2011, 2016 Atos Origin Integration, Christian W. Damus, 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:
 *  Tristan Faure (Atos Origin Integration) tristan.faure@atosorigin.com - Initial API and implementation
 *  Christian W. Damus - bug 485220
 *  
 *****************************************************************************/
package org.eclipse.papyrus.infra.onefile.ui.utils;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.papyrus.infra.onefile.internal.ui.Activator;
import org.eclipse.papyrus.infra.onefile.model.IPapyrusFile;
import org.eclipse.ui.IEditorDescriptor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.part.FileEditorInput;

/**
 * Utility methods for the UI
 *
 * @author tristan.faure@atosorigin.com
 *
 */
public class OneFileUIUtils {

	/**
	 * Check if the object is in an already opened editor
	 *
	 * @param inputElement
	 * @return
	 */
	public static IEditorPart isOpenInEditor(Object inputElement) {
		IEditorPart editor = findEditor(inputElement, false);
		if (editor != null) {
			return editor;
		}
		IEditorInput input = getEditorInput(inputElement);
		if (input != null) {

			IWorkbenchPage p = getActivePage();
			if (p != null) {
				return p.findEditor(input);
			}
		}
		return null;
	}

	/**
	 * Find an editor opening the input element
	 *
	 * @param inputElement
	 * @param activate
	 *            , if activate is true, once the editor is found it is
	 *            activated
	 * @return null if no editor is found
	 */
	private static IEditorPart findEditor(Object inputElement, boolean activate) {
		if (inputElement instanceof IPapyrusFile) {
			IPapyrusFile cu = (IPapyrusFile) inputElement;
			if (cu != null) {
				IWorkbenchPage page = getActivePage();
				for (IEditorReference ref : page.getEditorReferences()) {
					IEditorPart editor = ref.getEditor(false);
					if (editor != null) {
						IEditorInput editorInput;
						editorInput = editor.getEditorInput();
						if (cu.getMainFile().equals(editorInput.getAdapter(IFile.class))) {
							if (activate && page.getActivePart() != editor) {
								page.activate(editor);
							}
							return editor;
						}
					}
				}
			}
		}
		return null;
	}

	/**
	 * Open the editor corresponding to the inpur element
	 *
	 * @param inputElement
	 * @param activate
	 * @return
	 * @throws PartInitException
	 */
	public static IEditorPart openInEditor(Object inputElement, boolean activate) throws PartInitException {

		if (inputElement instanceof IFile) {
			return openInEditor((IFile) inputElement, activate);
		}
		IEditorPart editor = findEditor(inputElement, activate);
		if (editor != null) {
			return editor;
		}
		IEditorInput input = getEditorInput(inputElement);
		if (input == null) {
			throw new PartInitException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "not found"));
		}
		return openInEditor(input, getEditorID(input), activate);
	}

	/**
	 * Get the editor input for the given element
	 *
	 * @param inputElement
	 * @return
	 */
	private static IEditorInput getEditorInput(Object inputElement) {
		if (inputElement instanceof IFile) {
			IFile file = (IFile) inputElement;
			return new FileEditorInput(file);
		}
		return null;
	}

	/**
	 * Get the editor id for an editor input
	 *
	 * @param input
	 * @return
	 * @throws PartInitException
	 */
	public static String getEditorID(IEditorInput input) throws PartInitException {

		Assert.isNotNull(input);

		IEditorDescriptor editorDescriptor;

		if (input instanceof IFileEditorInput) {
			editorDescriptor = IDE.getEditorDescriptor(((IFileEditorInput) input).getFile());
		} else {
			editorDescriptor = IDE.getEditorDescriptor(input.getName());
		}
		return editorDescriptor.getId();

	}

	/**
	 * @param file
	 * @param activate
	 * @return
	 * @throws PartInitException
	 */
	private static IEditorPart openInEditor(IFile file, boolean activate) throws PartInitException {

		if (file == null) {
			throw new PartInitException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "not found"));
		}

		IWorkbenchPage p = getActivePage();

		if (p == null) {
			throw new PartInitException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "not found"));
		}

		IEditorPart editorPart = IDE.openEditor(p, file, activate);

		return editorPart;

	}

	/**
	 * Get the active Page
	 *
	 * @return
	 */
	public static IWorkbenchPage getActivePage() {
		IWorkbench workbench = PlatformUI.getWorkbench();
		if (workbench == null) {
			return null;
		}
		IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow();
		if (activeWorkbenchWindow == null) {
			return null;
		}
		return activeWorkbenchWindow.getActivePage();
	}

	private static IEditorPart openInEditor(IEditorInput input, String editorID, boolean activate) throws PartInitException {
		Assert.isNotNull(input);
		Assert.isNotNull(editorID);
		IWorkbenchPage p = getActivePage();
		if (p == null) {
			throw new PartInitException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "not found"));
		}
		IEditorPart editorPart = p.openEditor(input, editorID, activate);
		return editorPart;

	}

}

Back to the top