001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal.ide.undo;
011:
012: import org.eclipse.core.resources.IProject;
013: import org.eclipse.core.resources.IProjectDescription;
014: import org.eclipse.core.resources.IResource;
015: import org.eclipse.core.resources.ResourcesPlugin;
016: import org.eclipse.core.runtime.Assert;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IProgressMonitor;
019: import org.eclipse.core.runtime.OperationCanceledException;
020: import org.eclipse.core.runtime.SubProgressMonitor;
021:
022: /**
023: * ProjectDescription is a lightweight description that describes a project to
024: * be created.
025: *
026: * This class is not intended to be instantiated or used by clients.
027: *
028: * @since 3.3
029: *
030: */
031: public class ProjectDescription extends ContainerDescription {
032:
033: private IProjectDescription projectDescription;
034: private boolean openOnCreate = true;
035:
036: /**
037: * Create a project description from a specified project.
038: *
039: * @param project
040: * The project to be described. The project must exist.
041: */
042: public ProjectDescription(IProject project) {
043: super (project);
044: Assert.isLegal(project.exists());
045: if (project.isOpen()) {
046: try {
047: this .projectDescription = project.getDescription();
048: } catch (CoreException e) {
049: // Eat this exception because it only occurs when the project
050: // is not accessible and we have already checked this. We
051: // don't want to propagate the CoreException into the
052: // constructor
053: // API.
054: }
055: } else {
056: openOnCreate = false;
057: }
058: }
059:
060: /**
061: * Create a project description from a specified IProjectDescription. Used
062: * when the project does not yet exist.
063: *
064: * @param projectDescription
065: * the project description for the future project
066: */
067: public ProjectDescription(IProjectDescription projectDescription) {
068: super ();
069: this .projectDescription = projectDescription;
070: }
071:
072: /*
073: * (non-Javadoc)
074: *
075: * @see org.eclipse.ui.internal.ide.undo.ContainerDescription#createResourceHandle()
076: */
077: public IResource createResourceHandle() {
078: return ResourcesPlugin.getWorkspace().getRoot().getProject(
079: getName());
080: }
081:
082: /*
083: * (non-Javadoc)
084: *
085: * @see org.eclipse.ui.internal.ide.undo.ResourceDescription#createExistentResourceFromHandle(org.eclipse.core.resources.IResource,
086: * org.eclipse.core.runtime.IProgressMonitor)
087: */
088: public void createExistentResourceFromHandle(IResource resource,
089: IProgressMonitor monitor) throws CoreException {
090: Assert.isLegal(resource instanceof IProject);
091: if (resource.exists()) {
092: return;
093: }
094: IProject projectHandle = (IProject) resource;
095: monitor.beginTask("", 200); //$NON-NLS-1$
096: monitor
097: .setTaskName(UndoMessages.FolderDescription_NewFolderProgress);
098: if (projectDescription == null) {
099: projectHandle.create(new SubProgressMonitor(monitor, 100));
100: } else {
101: projectHandle.create(projectDescription,
102: new SubProgressMonitor(monitor, 100));
103: }
104:
105: if (monitor.isCanceled()) {
106: throw new OperationCanceledException();
107: }
108: if (openOnCreate) {
109: projectHandle.open(IResource.BACKGROUND_REFRESH,
110: new SubProgressMonitor(monitor, 100));
111: }
112: monitor.done();
113: }
114:
115: /*
116: * (non-Javadoc)
117: *
118: * @see org.eclipse.ui.internal.ide.undo.ContainerDescription#getName()
119: */
120: public String getName() {
121: if (projectDescription != null) {
122: return projectDescription.getName();
123: }
124: return super .getName();
125: }
126:
127: /*
128: * (non-Javadoc)
129: *
130: * @see org.eclipse.ui.internal.ide.undo.ResourceDescription#verifyExistence(boolean)
131: */
132: public boolean verifyExistence(boolean checkMembers) {
133: // We can only check members if the project is open.
134: IProject projectHandle = (IProject) createResourceHandle();
135: if (projectHandle.isAccessible()) {
136: return super .verifyExistence(checkMembers);
137: }
138: return super .verifyExistence(false);
139: }
140: }
|