Bug 471247 - Import (de)resolve is FILE-based in room + diagram

Change-Id: I11557cead6c3c100369abe689b1613f96abbac03
diff --git a/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/linking/GlobalNonPlatformURIEditorOpener.java b/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/linking/GlobalNonPlatformURIEditorOpener.java
index 762215f..0fcb331 100644
--- a/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/linking/GlobalNonPlatformURIEditorOpener.java
+++ b/plugins/org.eclipse.etrice.core.common.ui/src/org/eclipse/etrice/core/common/ui/linking/GlobalNonPlatformURIEditorOpener.java
@@ -52,6 +52,9 @@
 			return openExternalFile(uri);
 	}
 	
+	/**
+	 *  Returns a platformURI which underlying file is accessible
+	 */
 	public static URI getPlatformURI(URI uri) {
 		if (uri.isPlatform())
 			return uri;
@@ -59,14 +62,18 @@
 		// HOWTO: find absolute path location in workspace (as platform URI)
 		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
 		IFile[] files = root.findFilesForLocationURI(java.net.URI.create(uri.toString()));
-		for (IFile file : files) { // which file to choose ?
+		
+		URI minLength = null;
+		for (IFile file : files) {
 			if (!file.isAccessible())	// avoid closed or other bad files
 				continue;
 			
-			return URI.createPlatformResourceURI(file.getFullPath().toString(), true).appendFragment(uri.fragment());
-		}
+			URI platURI = URI.createPlatformResourceURI(file.getFullPath().toString(), true).appendFragment(uri.fragment());
+			if(minLength == null || platURI.toString().length() < minLength.toString().length())
+				minLength = platURI;
+		}	
 
-		return null;
+		return minLength;
 	}
 
 	private IEditorPart openExternalFile(URI referenceOwnerURI) {
diff --git a/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/scoping/StandardModelLocator.java b/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/scoping/StandardModelLocator.java
index 89f0f82..db866f7 100644
--- a/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/scoping/StandardModelLocator.java
+++ b/plugins/org.eclipse.etrice.core.common/src/org/eclipse/etrice/core/common/scoping/StandardModelLocator.java
@@ -29,6 +29,9 @@
 import org.eclipse.emf.ecore.resource.URIConverter;
 import org.eclipse.xtext.resource.ClasspathUriResolutionException;
 
+/**
+ *  TODO import resolve (and deresolve in diagrams) is not consistent 
+ */
 public class StandardModelLocator implements IModelLocator {
 
 	private static final String CLASSPATH = "classpath:/";
@@ -111,33 +114,26 @@
 			return null;
 
 		URI uri = URI.createURI(resolve);
-		if (uri.isRelative()) {
+		if (uri.isFile() && uri.hasRelativePath()) {
+			// NOTE: deresolve (== reverse direction) is implemented in ui.RelativeFileURIHandler
 			URI base = baseUri.trimSegments(1);
 			if (base.isPlatformResource() && EMFPlugin.IS_ECLIPSE_RUNNING) {
-				URI platUri = uri.resolve(baseUri);
-				if (!existsInPlatform(platUri)) {
-					// platUri does not exist in the workspace as a platform
-					// resource
-					URI fileUri = resolveFileUriFromPlatformBase(uri, baseUri);
-					if (existsInFileSys(fileUri)) {
-						// corresponding fileUri does exist in the file system,
-						// so try to create a mapping in URIMap
-						if (!updateURIMapEntry(res, platUri, fileUri))
-							return null;
-					} else {
-						// corresponding fileUri does not exist in the file
-						// system either, so fail and return null
-						return null;
-					}
+				URI fileUri = resolveFileUriFromPlatformBase(uri, baseUri); // relative path ?-> baseFileURI => absolute file uri
+				URI platUri = getPlatformURI(fileUri);						// absolute file uri => accessible platform uri
+				if(platUri == null){
+					// JH: Fix this here ? Why need we an URI mapping ? Room import does not support any logicals URIs ?
+//					if(existsInFileSys(fileUri)){
+//						// corresponding fileUri does exist in the file system,
+//						// so try to create a mapping in URIMap
+//						if (!updateURIMapEntry(res, platUri, fileUri))
+//							return null;
+//					}
+					
+					// room import is file based => can only return an fileURI here
+					return fileUri.toString();	
 				}
 				else {
-					// platUri exists in the workspace
-					
-					// check whether a simple concatenation with the base gives a valid file system path
-					URI fileUri = resolveFileUriFromPlatformBase(uri, baseUri);
-					if (!existsInFileSys(fileUri))
-						return null;
-					
+					// platUri exists in the workspace					
 					removeURIMapEntry(res, platUri);
 					return platUri.toString();
 				}
@@ -250,6 +246,7 @@
 	private boolean existsInPlatform(URI uri) {
 		if(!EMFPlugin.IS_ECLIPSE_RUNNING)
 			return false;
+		
 		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
 		if (root == null)
 			return false;
@@ -325,4 +322,30 @@
 	protected File locateFile(File f) {
 		return f;
 	}
+	
+	/**
+	 *  Returns a platformURI which underlying file is accessible.
+	 *  Copied from GlobalNonPlatformURIEditorOpener
+	 */
+	public static URI getPlatformURI(URI uri) {
+		if (uri.isPlatform())
+			return uri;
+
+		// HOWTO: find absolute path location in workspace (as platform URI)
+		IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
+		IFile[] files = root.findFilesForLocationURI(java.net.URI.create(uri.toString()));
+		
+		URI minLength = null;
+		for (IFile file : files) {
+			if (!file.isAccessible())	// avoid closed or other bad files
+				continue;
+			
+			URI platURI = URI.createPlatformResourceURI(file.getFullPath().toString(), true).appendFragment(uri.fragment());
+			if(minLength == null || platURI.toString().length() < minLength.toString().length())
+				minLength = platURI;
+		}	
+
+		return minLength;
+	}
+
 }
diff --git a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/RelativeFileURIHandler.java b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/RelativeFileURIHandler.java
index 5f3381e..ae71b3a 100644
--- a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/RelativeFileURIHandler.java
+++ b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/editor/RelativeFileURIHandler.java
@@ -22,6 +22,7 @@
 import org.eclipse.emf.ecore.xmi.XMIResource;
 import org.eclipse.emf.ecore.xmi.XMLResource.URIHandler;
 import org.eclipse.emf.ecore.xmi.impl.URIHandlerImpl;
+import org.eclipse.etrice.core.common.ui.linking.GlobalNonPlatformURIEditorOpener;
 
 /**
  *  {@linkplain URIHandler} which deresolves to relative file uri and resolves to absolute file uri. <br>
@@ -55,7 +56,9 @@
 			
 			URI resolvedFileURI = fileURI.resolve(baseFileURI, true);	
 		//	System.out.println("resolve: " + fileURI + " -> " + baseFileURI + " = " + resolvedFileURI);
-			return resolvedFileURI;
+			URI platURI = GlobalNonPlatformURIEditorOpener.getPlatformURI(resolvedFileURI);
+			
+			return (platURI != null) ? platURI : resolvedFileURI;
 		}
 		
 		return (fallback != null) ? fallback.resolve(uri) : uri;
diff --git a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/support/DiagramAccessBase.java b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/support/DiagramAccessBase.java
index d8acb04..771dc0d 100644
--- a/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/support/DiagramAccessBase.java
+++ b/plugins/org.eclipse.etrice.ui.common.base/src/org/eclipse/etrice/ui/common/base/support/DiagramAccessBase.java
@@ -209,7 +209,7 @@
 	public DiagramEditorBase findDiagramEditor(EObject rootObject) {
 		IFileEditorInput input = getEditorInput(rootObject);
 	
-		IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findEditor(input);
+		IEditorPart part = (input == null) ? null : PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findEditor(input);
 		if(part instanceof DiagramEditorBase) {
 			return (DiagramEditorBase) part;
 		}
@@ -220,16 +220,14 @@
 	public DiagramEditorBase openDiagramEditor(EObject rootObject) {
 		IFileEditorInput input = getEditorInput(rootObject);
 	
-		if(input != null) {
-			try {
-				IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(input, getEditorId());
-				if(part instanceof DiagramEditorBase) {
-					return (DiagramEditorBase) part;
-				}
-			} catch (PartInitException e) {
-				String error = "Error while opening diagram editor";
-				System.err.println(error);
+		try {
+			IEditorPart part = (input == null) ? null : PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(input, getEditorId());
+			if(part instanceof DiagramEditorBase) {
+				return (DiagramEditorBase) part;
 			}
+		} catch (PartInitException e) {
+			String error = "Error while opening diagram editor";
+			System.err.println(error);
 		}
 		
 		return null;