1 /*******************************************************************************
2  * Copyright (c) 2006, 2015 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  ******************************************************************************/
14 
15 package org.eclipse.ui.internal.ide.undo;
16 
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IProjectDescription;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.Assert;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.SubMonitor;
25 
26 /**
27  * ProjectDescription is a lightweight description that describes a project to
28  * be created.
29  *
30  * This class is not intended to be instantiated or used by clients.
31  *
32  * @since 3.3
33  *
34  */
35 public class ProjectDescription extends ContainerDescription {
36 
37 	private IProjectDescription projectDescription;
38 	private boolean openOnCreate = true;
39 
40 	/**
41 	 * Create a project description from a specified project.
42 	 *
43 	 * @param project
44 	 *            The project to be described. The project must exist.
45 	 */
ProjectDescription(IProject project)46 	public ProjectDescription(IProject project) {
47 		super(project);
48 		Assert.isLegal(project.exists());
49 		if (project.isOpen()) {
50 			try {
51 				this.projectDescription = project.getDescription();
52 			} catch (CoreException e) {
53 				// Eat this exception because it only occurs when the project
54 				// is not accessible and we have already checked this. We
55 				// don't want to propagate the CoreException into the
56 				// constructor
57 				// API.
58 			}
59 		} else {
60 			openOnCreate = false;
61 		}
62 	}
63 
64 	/**
65 	 * Create a project description from a specified IProjectDescription. Used
66 	 * when the project does not yet exist.
67 	 *
68 	 * @param projectDescription
69 	 *            the project description for the future project
70 	 */
ProjectDescription(IProjectDescription projectDescription)71 	public ProjectDescription(IProjectDescription projectDescription) {
72 		super();
73 		this.projectDescription = projectDescription;
74 	}
75 
76 	@Override
createResourceHandle()77 	public IResource createResourceHandle() {
78 		return ResourcesPlugin.getWorkspace().getRoot().getProject(getName());
79 	}
80 
81 	@Override
createExistentResourceFromHandle(IResource resource, IProgressMonitor monitor)82 	public void createExistentResourceFromHandle(IResource resource,
83 			IProgressMonitor monitor) throws CoreException {
84 		SubMonitor subMonitor = SubMonitor.convert(monitor, 200);
85 		Assert.isLegal(resource instanceof IProject);
86 		if (resource.exists()) {
87 			return;
88 		}
89 		IProject projectHandle = (IProject) resource;
90 		subMonitor.setTaskName(UndoMessages.FolderDescription_NewFolderProgress);
91 		if (projectDescription == null) {
92 			projectHandle.create(subMonitor.split(100));
93 		} else {
94 			projectHandle.create(projectDescription, subMonitor.split(100));
95 		}
96 
97 		if (openOnCreate) {
98 			projectHandle.open(IResource.NONE, subMonitor.split(100));
99 		}
100 	}
101 
102 	@Override
getName()103 	public String getName() {
104 		if (projectDescription != null) {
105 			return projectDescription.getName();
106 		}
107 		return super.getName();
108 	}
109 
110 	@Override
verifyExistence(boolean checkMembers)111 	public boolean verifyExistence(boolean checkMembers) {
112 		// We can only check members if the project is open.
113 		IProject projectHandle = (IProject) createResourceHandle();
114 		if (projectHandle.isAccessible()) {
115 			return super.verifyExistence(checkMembers);
116 		}
117 		return super.verifyExistence(false);
118 	}
119 }