Bug 302152 - New path variable created unexpectedly
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/IDEResourceInfoUtils.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/IDEResourceInfoUtils.java
index 8e3d588..2664d5d 100644
--- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/IDEResourceInfoUtils.java
+++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/IDEResourceInfoUtils.java
@@ -21,6 +21,7 @@
 import org.eclipse.core.filesystem.EFS;
 import org.eclipse.core.filesystem.IFileInfo;
 import org.eclipse.core.filesystem.IFileStore;
+import org.eclipse.core.filesystem.URIUtil;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
@@ -255,7 +256,8 @@
 			}
 		}
 		if (isLinked && isPathVariable) {
-			return resource.getRawLocationURI().toString();
+			String tmp = URIUtil.toPath(resource.getRawLocationURI()).toOSString();
+			return resource.getProject().getPathVariableManager().convertToUserEditableFormat(tmp, true);
 		}
 		if (store != null) {
 			return store.toString();
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/LinkedResourceEditor.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/LinkedResourceEditor.java
index 4a6fb88..df79da9 100644
--- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/LinkedResourceEditor.java
+++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/LinkedResourceEditor.java
@@ -275,7 +275,7 @@
 				else {
 					IPath rawLocation = ((IResource) obj).getRawLocation();
 					if (rawLocation != null)
-						return rawLocation.toPortableString();
+						return rawLocation.toOSString();
 				}
 			} else if ((obj instanceof String) && index == 0)
 				return (String) obj;
@@ -522,8 +522,8 @@
 												res.getProjectRelativePath()
 														.toPortableString(),
 												res.getRawLocation()
-														.toPortableString(),
-												location.toPortableString() }));
+														.toOSString(),
+												location.toOSString() }));
 			} catch (CoreException e) {
 				report
 						.add(NLS
@@ -642,9 +642,9 @@
 													res
 															.getProjectRelativePath()
 															.toPortableString(),
-													location.toPortableString(),
+													location.toOSString(),
 													newLocation
-															.toPortableString() }));
+															.toOSString() }));
 				}
 			} catch (CoreException e) {
 				remaining.add(res);
@@ -667,7 +667,7 @@
 			for (int i = 0; i < variables.length; i++) {
 				IPath resolvePath = URIUtil.toPath(fProject
 				.getPathVariableManager().resolveURI(
-						URIUtil.toURI(Path.fromPortableString(variables[i])), res));
+						URIUtil.toURI(Path.fromOSString(variables[i])), res));
 				if (resolvePath
 						.isPrefixOf(convertToProperCase(location))) {
 					int count = location
@@ -691,9 +691,9 @@
 													res
 															.getProjectRelativePath()
 															.toPortableString(),
-													location.toPortableString(),
+													location.toOSString(),
 													newLocation
-															.toPortableString() }));
+															.toOSString() }));
 				} catch (CoreException e) {
 					variable = -1;
 				}
@@ -737,7 +737,7 @@
 									.bind(
 											IDEWorkbenchMessages.LinkedResourceEditor_unableToCreateVariable,
 											variableName, commonPath
-													.toPortableString()));
+													.toOSString()));
 				}
 				it = resources.iterator();
 				while (it.hasNext()) {
@@ -758,9 +758,9 @@
 																.getProjectRelativePath()
 																.toPortableString(),
 														location
-																.toPortableString(),
+																.toOSString(),
 														newLocation
-																.toPortableString() }));
+																.toOSString() }));
 					} catch (CoreException e) {
 						report
 								.add(NLS
@@ -808,8 +808,8 @@
 										new Object[] {
 												res.getProjectRelativePath()
 														.toPortableString(),
-												location.toPortableString(),
-												newLocation.toPortableString() }));
+												location.toOSString(),
+												newLocation.toOSString() }));
 			} catch (CoreException e) {
 				report
 						.add(NLS
@@ -827,6 +827,13 @@
 
 	private String getSuitablePathVariable(IPath commonPath) {
 		String variableName = commonPath.lastSegment();
+		if (variableName == null) {
+			variableName = commonPath.getDevice();
+			if (variableName == null)
+				variableName = "ROOT"; //$NON-NLS-1$
+			else
+				variableName = variableName.substring(0, variableName.length() -1); // remove the tailing ':'
+		}
 		StringBuffer buf = new StringBuffer();
 		for (int i = 0; i < variableName.length(); i++) {
 			char c = variableName.charAt(i);
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/PathVariableDialog.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/PathVariableDialog.java
index b31dec0..df2d619 100644
--- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/PathVariableDialog.java
+++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/PathVariableDialog.java
@@ -382,10 +382,10 @@
         if (currentResource != null) {
         	IPathVariableManager pathVariableManager2 = currentResource.getProject().getPathVariableManager();
 			String[] variables = pathVariableManager2.getPathVariableNames(currentResource);
-    		String internalFormat = pathVariableManager2.convertFromUserEditableFormat(variableValue, currentResource);
-    		URI uri = URIUtil.toURI(Path.fromPortableString(internalFormat));
+    		String internalFormat = pathVariableManager2.convertFromUserEditableFormat(variableValue, operationMode == EDIT_LINK_LOCATION, currentResource);
+    		URI uri = URIUtil.toURI(Path.fromOSString(internalFormat));
         	URI resolvedURI = pathVariableManager2.resolveURI(uri, currentResource);
-        	String resolveValue = URIUtil.toPath(resolvedURI).toPortableString();
+        	String resolveValue = URIUtil.toPath(resolvedURI).toOSString();
         	// Delete intermediate variables that might have been created as
         	// as a side effect of converting arbitrary relative paths to an internal string. 
         	String[] newVariables = pathVariableManager2.getPathVariableNames(currentResource);
@@ -474,10 +474,10 @@
             String[] variableNames = (String[]) dialog.getResult();
             if (variableNames != null && variableNames.length == 1) {
                 String newValue = variableNames[0];
-            	IPath path = Path.fromPortableString(newValue);
+            	IPath path = Path.fromOSString(newValue);
                 if (operationMode != EDIT_LINK_LOCATION && currentResource != null && !path.isAbsolute() && path.segmentCount() > 0) {
                 	path = buildVariableMacro(path);
-                	newValue = path.toPortableString();
+                	newValue = path.toOSString();
                 }
                 variableValue = newValue;
                 variableValueField.setText(newValue);
@@ -593,7 +593,7 @@
             // contain macros such as "${foo}\etc"
             allowFinish = true;
             String resolvedValue = getVariableResolvedValue();
-            IPath resolvedPath = Path.fromPortableString(resolvedValue);
+            IPath resolvedPath = Path.fromOSString(resolvedValue);
             if (!IDEResourceInfoUtils.exists(resolvedPath.toOSString())) {
                 // the path does not exist (warning)
                 message = IDEWorkbenchMessages.PathVariableDialog_pathDoesNotExistMessage;
@@ -655,7 +655,7 @@
      */
     public String getVariableValue() {
     	if (currentResource != null) {
-    		String internalFormat = getPathVariableManager().convertFromUserEditableFormat(variableValue, currentResource);
+    		String internalFormat = getPathVariableManager().convertFromUserEditableFormat(variableValue, operationMode == EDIT_LINK_LOCATION, currentResource);
     		return internalFormat;
     	}
     	return variableValue;
@@ -677,7 +677,7 @@
      * @param variable the new variable value
      */
     public void setVariableValue(String variable) {
-    	String userEditableString = getPathVariableManager().convertToUserEditableFormat(variable);
+    	String userEditableString = getPathVariableManager().convertToUserEditableFormat(variable, operationMode == EDIT_LINK_LOCATION);
         variableValue = userEditableString;
     }
 
@@ -692,7 +692,7 @@
      * @param location
      */
     public void setLinkLocation(IPath location) {
-    	String userEditableString = getPathVariableManager().convertToUserEditableFormat(location.toPortableString());
+    	String userEditableString = getPathVariableManager().convertToUserEditableFormat(location.toOSString(), operationMode == EDIT_LINK_LOCATION);
         variableValue = userEditableString;
     }
 
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/PathVariablesGroup.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/PathVariablesGroup.java
index 8eeb971..9d78097 100644
--- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/PathVariablesGroup.java
+++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/PathVariablesGroup.java
@@ -571,7 +571,7 @@
      * @return the converted value
      */
     private String removeParentVariable(String value) {
-    	return pathVariableManager.convertToUserEditableFormat(value);
+    	return pathVariableManager.convertToUserEditableFormat(value, false);
     }
     
     /**
diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/ResourceInfoPage.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/ResourceInfoPage.java
index 0849f00..ff2a7c2 100644
--- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/ResourceInfoPage.java
+++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/dialogs/ResourceInfoPage.java
@@ -316,7 +316,8 @@
 	protected void editLinkLocation() {
 		IResource resource = (IResource) getElement().getAdapter(
 				IResource.class);
-		IPath location = Path.fromPortableString(locationValue.getText());
+		String locationFormat = resource.getProject().getPathVariableManager().convertFromUserEditableFormat(locationValue.getText(), true, resource);
+		IPath location = Path.fromOSString(locationFormat);
 
 		PathVariableDialog dialog = new PathVariableDialog(getShell(),
 				PathVariableDialog.EDIT_LINK_LOCATION, resource.getType(),
@@ -336,7 +337,8 @@
 		IResource resource = (IResource) getElement().getAdapter(
 				IResource.class);
 
-		locationValue.setText(newResourceLocation.toPortableString());
+		String userEditableFormat = resource.getProject().getPathVariableManager().convertToUserEditableFormat(newResourceLocation.toOSString(), true);
+		locationValue.setText(userEditableFormat);
 
 		URI resolvedURI = resource.getProject().getPathVariableManager()
 				.resolveURI(URIUtil.toURI(newResourceLocation), resource);