Update org.apache.commons:commons-compress to 1.25.0
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / clone / ProjectRecord.java
blob95638ee5902b40cd34e8ee7afc511f2ea2958b84
1 /*******************************************************************************
2 * Copyright (c) 2004, 2013 IBM Corporation and others.
3 * Copyright (C) 2007, Martin Oberhuber (martin.oberhuber@windriver.com)
4 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
5 * Copyright (C) 2009, Mykola Nikishov <mn@mn.com.ua>
6 * Copyright (C) 2010, Wim Jongman <wim.jongman@remainsoftware.com>
7 * Copyright (C) 2010, Benjamin Muskalla <bmuskalla@eclipsesource.com>
8 * Copyright (C) 2011, Mathias Kinzler <mathias.kinzler@sap.com>
10 * All rights reserved. This program and the accompanying materials
11 * are made available under the terms of the Eclipse Public License 2.0
12 * which accompanies this distribution, and is available at
13 * https://www.eclipse.org/legal/epl-2.0/
15 * SPDX-License-Identifier: EPL-2.0
16 *******************************************************************************/
18 package org.eclipse.egit.ui.internal.clone;
20 import java.io.File;
22 import org.eclipse.core.resources.IProjectDescription;
23 import org.eclipse.core.resources.ResourcesPlugin;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.core.runtime.Platform;
28 import org.eclipse.osgi.util.NLS;
30 /**
31 * Used for creating projects out of .project files
33 public class ProjectRecord {
34 private final File projectSystemFile;
36 private String projectName;
38 private IProjectDescription description;
40 /**
41 * Create a record for a project based on the info in the file.
43 * @param file
45 public ProjectRecord(File file) {
46 projectSystemFile = file;
47 IPath path = new Path(projectSystemFile.getPath());
48 try {
49 // If the file is in the default location, use the directory
50 // name as the project name. Otherwise we will get an error like
51 // "foo overlaps the location of another project foo" when importing
52 // in case the directory name and the name in .project do not match.
53 if (isDefaultLocation(path)) {
54 projectName = path.segment(path.segmentCount() - 2);
55 description = ResourcesPlugin.getWorkspace()
56 .newProjectDescription(projectName);
57 } else {
58 description = ResourcesPlugin.getWorkspace()
59 .loadProjectDescription(path);
60 projectName = description.getName();
62 } catch (CoreException e) {
63 description = null;
64 projectName = path.lastSegment();
68 /**
69 * Get the name of the project
71 * @return String
73 public String getProjectName() {
74 return projectName;
77 /**
78 * Gets the label to be used when rendering this project record in the UI.
80 * @return String the label
82 public String getProjectLabel() {
83 String path = projectSystemFile.getParent();
85 return NLS.bind("{0} ({1})", projectName, path); //$NON-NLS-1$
88 /**
89 * @return the project description
91 public IProjectDescription getProjectDescription() {
92 return description;
95 /**
96 * @param description
98 public void setProjectDescription(IProjectDescription description) {
99 this.description = description;
103 * @return the file used in the constructor
105 public File getProjectSystemFile() {
106 return projectSystemFile;
109 @Override
110 public String toString() {
111 return projectName;
115 * Returns whether the given project description file path is in the default
116 * location for a project
118 * @param path
119 * The path to examine
120 * @return Whether the given path is the default location for a project
122 private boolean isDefaultLocation(IPath path) {
123 // The project description file must at least be within the project,
124 // which is within the workspace location
125 if (path.segmentCount() < 2)
126 return false;
127 return path.removeLastSegments(2).toFile()
128 .equals(Platform.getLocation().toFile());