Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 145fef08aa2a0b46c5852a3fed5072e616bb43bc (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
/*****************************************************************************
 * Copyright (c) 2012, 2015 CEA LIST, 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 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:
 *  Vincent Lorenzo (CEA LIST) Vincent.Lorenzo@cea.fr - Initial API and implementation
 *  Christian W. Damus - bug 434983
 *
 *****************************************************************************/
package org.eclipse.papyrus.junit.utils;

import org.eclipse.core.resources.IFile;
import org.eclipse.papyrus.editor.PapyrusMultiDiagramEditor;
import org.eclipse.papyrus.infra.ui.editor.IMultiDiagramEditor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.junit.Assert;

/**
 * 
 * useful methods for Editors
 * 
 */
public class EditorUtils {

	private EditorUtils() {
		// to prevent instanciation
	}

	/**
	 * 
	 * @param file
	 *            a file
	 * @return
	 * 		the opened editor for this file
	 * @throws PartInitException
	 */
	public static final IEditorPart openEditor(final IFile file) throws PartInitException {
		return withoutLayoutStoragePopup(() -> {
			GenericUtils.closeIntroPart();
			final IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
			IEditorPart editor = null;
			editor = IDE.openEditor(activePage, file);
			Assert.assertNotNull(editor);
			return editor;
		});
	}

	/**
	 * Opens the file with the Papyrus Editor
	 * 
	 * @param file
	 * @return
	 * @throws PartInitException
	 */
	public static final IMultiDiagramEditor openPapyrusEditor(final IFile file) throws PartInitException {
		return withoutLayoutStoragePopup(() -> {
			GenericUtils.closeIntroPart();
			final IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
			IEditorPart editor = null;
			editor = IDE.openEditor(activePage, file, PapyrusMultiDiagramEditor.EDITOR_ID);
			Assert.assertNotNull(editor);
			return (IMultiDiagramEditor) editor;
		});
	}

	/**
	 * Opens an editor without the possibility of it showing a prompt dialog to convert
	 * DI-file storage of the page layout to private sash-file storage.
	 */
	@SuppressWarnings("restriction")
	private static <E extends IEditorPart> E withoutLayoutStoragePopup(EditorOpener<E> editorOpener) throws PartInitException {
		E result;
		boolean posted = false;

		org.eclipse.papyrus.infra.ui.internal.preferences.YesNo originalPreference = org.eclipse.papyrus.infra.ui.internal.preferences.EditorPreferences.getInstance().getConvertSharedPageLayoutToPrivate();
		org.eclipse.papyrus.infra.ui.internal.preferences.EditorPreferences.getInstance().setConvertSharedPageLayoutToPrivate(org.eclipse.papyrus.infra.ui.internal.preferences.YesNo.NO);

		try {
			result = editorOpener.openEditor();
			result.getSite().getShell().getDisplay().asyncExec(() -> org.eclipse.papyrus.infra.ui.internal.preferences.EditorPreferences.getInstance().setConvertSharedPageLayoutToPrivate(originalPreference));
			posted = true;
		} finally {
			if (!posted) {
				// Revert now because the editor failed to open and we won't be reverting asynchronously
				org.eclipse.papyrus.infra.ui.internal.preferences.EditorPreferences.getInstance().setConvertSharedPageLayoutToPrivate(originalPreference);
			}
		}

		return result;
	}

	//
	// Nested types
	//

	@FunctionalInterface
	private interface EditorOpener<E extends IEditorPart> {
		E openEditor() throws PartInitException;
	}
}

Back to the top