Skip to main content
summaryrefslogtreecommitdiffstats
blob: cfbe12bd56572512e9c2cd1fb2a9e75afb9a45e4 (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
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 *    
 * 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:
 *  CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.moka.launch;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;

/**
 * Some utilities for dealing with IEditorParts and File editor inputs 
 *
 */
public class EditorUtils {

	/**
	 * Returns the IEditorPart associated with the given model element.
	 * If no IEditorPart is available in the workbench, a new one is created.
	 * 
	 * @param modelElement The modelElement for which an IEditorPart is requested
	 * @return The IEditorPart associated with the given model element.
	 */
	public static IEditorPart getEditorPart(EObject modelElement) {
		if (! modelElement.eIsProxy())
			return getEditorPart(modelElement.eResource().getURI().toString()) ;
		IEditorPart part = null ;
		// Need to open a new editor
		final FileEditorInput input = getFileEditorInput(modelElement) ;
		OpenPapyrusEditorRunnable runnable = new OpenPapyrusEditorRunnable(input) ;
		Display.getDefault().syncExec(runnable) ;
		part = runnable.getEditorPart() ;

		return part ;
	}

	/**
	 * Returns the IEditorPart associated with the Resource depicted by the given resourceURI string.
	 * If no IEditorPart is available in the workbench, a new one is created.
	 * 
	 * @param resourceURI The string representation of the resource URI for which an IEditorPart is requested
	 * @return The IEditorPart associated with the given resource URI.
	 */
	public static IEditorPart getEditorPart(String resourceURI) {
		// FIXME this snippet of code is not robust
		String[] splitted = resourceURI.split("/") ;
		String resourceName = splitted[splitted.length - 1].replaceAll("\\.uml$", ".di") ;
		IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows() ;
		IEditorPart part = null ;
		for (int i = 0 ; i < windows.length && part == null ; i++) {
			IWorkbenchPage[] pages = windows[i].getPages() ;
			for (int j = 0 ; j < pages.length && part == null ; j++) {
				IEditorReference[] editorReferences = pages[j].getEditorReferences() ;
				for (int k = 0 ; k < editorReferences.length && part == null ; k++) {
					IEditorReference ref = editorReferences[k] ;
					IEditorPart cdd = ref.getEditor(true) ;
					if (cdd instanceof IMultiDiagramEditor) {
						String cddName = cdd.getEditorInput().getName() ;
						if (cddName.equals(resourceName))
							part = cdd ;
					}
				}
			}
		}
		if (part == null) {
			// Need to open a new editor
			final FileEditorInput input = getFileEditorInput(resourceURI) ;
			OpenPapyrusEditorRunnable runnable = new OpenPapyrusEditorRunnable(input) ;
			Display.getDefault().syncExec(runnable) ;
			part = runnable.getEditorPart() ;

		}

		return part ;
	}

	/**
	 * Returns a File Editor Input for the given model element.
	 * This shall be understood as "The FileEditorInput which shall be used 
	 * to retrieve or construct the appropriate IEditorPart"
	 * 
	 * @param modelElement The model element for which a File Editor Input is requested
	 * @return The FileEditorInput which shall be used to retrieve or construct the appropriate IEditorPart for editing this model element
	 */
	public static FileEditorInput getFileEditorInput(EObject modelElement) {
		EObject resolvedElement = modelElement ;
		if (resolvedElement.eIsProxy()) {
			// Tries to resolve in a new resource set
			resolvedElement = EcoreUtil.resolve(modelElement, new ResourceSetImpl()) ;
		}
		return getFileEditorInput(resolvedElement.eResource().getURI().toString()) ;
	}

	/**
	 * Returns a File Editor Input for the given resource URI string.
	 * 
	 * @param resourceURI The string representing the resource URI for which a FileEditorInput is requested
	 * @return a File Editor Input for the given resource URI string.
	 */
	public static FileEditorInput getFileEditorInput(String resourceURI) {
		Resource newResource = new ResourceSetImpl().createResource(URI.createURI(resourceURI.replaceAll("\\.uml$", ".di"))) ;
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IWorkspaceRoot workspaceRoot = workspace.getRoot();
		IFile modelElementIFile = (IFile) workspaceRoot.findMember(newResource.getURI().toPlatformString(true));
		if(modelElementIFile == null)
			return null ;
		return new FileEditorInput(modelElementIFile) ;
	}

}

Back to the top