Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 41a4355a9f8d6abffcf12f2780080d9696dfade1 (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
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * 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:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.core.common.ui.linking;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.etrice.core.common.scoping.StandardModelLocator;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.ide.IDE;
import org.eclipse.xtext.ui.editor.GlobalURIEditorOpener;

/**
 * This class is needed since the URIs we get are non-platform URIs. In this case we have to convert them if possible.
 * 
 * @author Henrik Rentz-Reichert
 *
 */
public class GlobalNonPlatformURIEditorOpener extends GlobalURIEditorOpener {

	@Override
	public IEditorPart open(URI referenceOwnerURI, EReference reference, int indexInList, boolean select) {
		URI platformURI = getPlatformURI(referenceOwnerURI);
		if (platformURI != null)
			return super.open(platformURI, reference, indexInList, select);
		else
			return openExternalFile(referenceOwnerURI);
	}

	@Override
	public IEditorPart open(URI uri, boolean select) {
		URI platformURI = getPlatformURI(uri);
		if (platformURI != null)
			return super.open(platformURI, select);
		else
			return openExternalFile(uri);
	}
	
	/**
	 *  Returns a platformURI which underlying file is accessible
	 */
	public static URI getPlatformURI(URI uri) {
		return StandardModelLocator.getPlatformURI(uri);
	}

	private IEditorPart openExternalFile(URI referenceOwnerURI) {
		URI folder = referenceOwnerURI.trimSegments(1);
		IFileStore fileStore = EFS.getLocalFileSystem().getStore(new Path(folder.toFileString()));
		fileStore = fileStore.getFileStore(new Path(referenceOwnerURI.lastSegment()));
		if (!fileStore.fetchInfo().isDirectory() && fileStore.fetchInfo().exists()) {
			IWorkbenchPage page = getWorkbench().getActiveWorkbenchWindow().getActivePage();
			try {
				return IDE.openEditorOnFileStore(page, fileStore);
			}
			catch (PartInitException e) {
			}
		}
		return null;
	}
}

Back to the top