/*******************************************************************************
 * Copyright (c) 2004, 2008 IBM Corporation and others.
 * Copyright (C) 2007, Martin Oberhuber (martin.oberhuber@windriver.com)
 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
 * Copyright (C) 2009, Mykola Nikishov <mn@mn.com.ua>
 * Copyright (C) 2010, Wim Jongman <wim.jongman@remainsoftware.com>
 * Copyright (C) 2010, Benjamin Muskalla <bmuskalla@eclipsesource.com>
 * Copyright (C) 2011, Mathias Kinzler <mathias.kinzler@sap.com>
 *
 * 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
 *******************************************************************************/

package org.eclipse.egit.ui.internal.clone;

import java.io.File;

import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.osgi.util.NLS;

/**
 * Used for creating projects out of .project files
 */
public class ProjectRecord {
	private final File projectSystemFile;

	private String projectName;

	private IProjectDescription description;

	/**
	 * Create a record for a project based on the info in the file.
	 *
	 * @param file
	 */
	public ProjectRecord(File file) {
		projectSystemFile = file;
		IPath path = new Path(projectSystemFile.getPath());
		try {
			description = ResourcesPlugin.getWorkspace()
					.loadProjectDescription(path);
			projectName = description.getName();
		} catch (CoreException e) {
			description = null;
			projectName = path.lastSegment();
		}
	}

	/**
	 * Get the name of the project
	 *
	 * @return String
	 */
	public String getProjectName() {
		return projectName;
	}

	/**
	 * Gets the label to be used when rendering this project record in the UI.
	 *
	 * @return String the label
	 */
	public String getProjectLabel() {
		String path = projectSystemFile.getParent();

		return NLS.bind("{0} ({1})", projectName, path); //$NON-NLS-1$
	}

	/**
	 * @return the project description
	 */
	public IProjectDescription getProjectDescription() {
		return description;
	}

	/**
	 * @param description
	 */
	public void setProjectDescription(IProjectDescription description) {
		this.description = description;
	}

	/**
	 * @return the file used in the constructor
	 */
	public File getProjectSystemFile() {
		return projectSystemFile;
	}

	@Override
	public String toString() {
		return projectName;
	}
}
@@ -99,18 +99,18 @@
 2004, 20
-08
+13
  IBM Cor
@@ -1154,24 +1154,66 @@
 ntime.Path;%0A
+import org.eclipse.core.runtime.Platform;%0A
 import org.e
@@ -1664,53 +1664,526 @@
 %0A%09%09%09
-description = ResourcesPlugin.getWorkspace()%0A
+// If the file is in the default location, use the directory%0A%09%09%09// name as the project name. Otherwise we will get an error like%0A%09%09%09// %22foo overlaps the location of another project foo%22 when importing%0A%09%09%09// in case the directory name and the name in .project do not match.%0A%09%09%09if (isDefaultLocation(path)) %7B%0A%09%09%09%09projectName = path.segment(path.segmentCount() - 2);%0A%09%09%09%09description = ResourcesPlugin.getWorkspace()%0A%09%09%09%09%09%09.newProjectDescription(projectName);%0A%09%09%09%7D else %7B%0A%09%09%09%09description = ResourcesPlugin.getWorkspace()%0A%09
 %09%09%09%09
@@ -2214,16 +2214,17 @@
 (path);%0A
+%09
 %09%09%09proje
@@ -2255,16 +2255,21 @@
 Name();%0A
+%09%09%09%7D%0A
 %09%09%7D catc
@@ -3207,12 +3207,563 @@
 ctName;%0A
-
 %09%7D%0A
+%0A%09/**%0A%09 * Returns whether the given project description file path is in the default%0A%09 * location for a project%0A%09 *%0A%09 * @param path%0A%09 *            The path to examine%0A%09 * @return Whether the given path is the default location for a project%0A%09 */%0A%09private boolean isDefaultLocation(IPath path) %7B%0A%09%09// The project description file must at least be within the project,%0A%09%09// which is within the workspace location%0A%09%09if (path.segmentCount() %3C 2)%0A%09%09%09return false;%0A%09%09return path.removeLastSegments(2).toFile()%0A%09%09%09%09.equals(Platform.getLocation().toFile());%0A%09%7D%0A%0A
 %7D

Fix "overlaps the location of another project" when importing