KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > undo > ProjectDescription


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.ui.internal.ide.undo;
13
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.resources.IProjectDescription;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.ResourcesPlugin;
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.OperationCanceledException;
22 import org.eclipse.core.runtime.SubProgressMonitor;
23
24 /**
25  * ProjectDescription is a lightweight description that describes a project to
26  * be created.
27  *
28  * This class is not intended to be instantiated or used by clients.
29  *
30  * @since 3.3
31  *
32  */

33 public class ProjectDescription extends ContainerDescription {
34
35     private IProjectDescription projectDescription;
36     private boolean openOnCreate = true;
37
38     /**
39      * Create a project description from a specified project.
40      *
41      * @param project
42      * The project to be described. The project must exist.
43      */

44     public ProjectDescription(IProject project) {
45         super(project);
46         Assert.isLegal(project.exists());
47         if (project.isOpen()) {
48             try {
49                 this.projectDescription = project.getDescription();
50             } catch (CoreException e) {
51                 // Eat this exception because it only occurs when the project
52
// is not accessible and we have already checked this. We
53
// don't want to propagate the CoreException into the
54
// constructor
55
// API.
56
}
57         } else {
58             openOnCreate = false;
59         }
60     }
61
62     /**
63      * Create a project description from a specified IProjectDescription. Used
64      * when the project does not yet exist.
65      *
66      * @param projectDescription
67      * the project description for the future project
68      */

69     public ProjectDescription(IProjectDescription projectDescription) {
70         super();
71         this.projectDescription = projectDescription;
72     }
73
74     /*
75      * (non-Javadoc)
76      *
77      * @see org.eclipse.ui.internal.ide.undo.ContainerDescription#createResourceHandle()
78      */

79     public IResource createResourceHandle() {
80         return ResourcesPlugin.getWorkspace().getRoot().getProject(getName());
81     }
82
83     /*
84      * (non-Javadoc)
85      *
86      * @see org.eclipse.ui.internal.ide.undo.ResourceDescription#createExistentResourceFromHandle(org.eclipse.core.resources.IResource,
87      * org.eclipse.core.runtime.IProgressMonitor)
88      */

89     public void createExistentResourceFromHandle(IResource resource,
90             IProgressMonitor monitor) throws CoreException {
91         Assert.isLegal(resource instanceof IProject);
92         if (resource.exists()) {
93             return;
94         }
95         IProject projectHandle = (IProject) resource;
96         monitor.beginTask("", 200); //$NON-NLS-1$
97
monitor.setTaskName(UndoMessages.FolderDescription_NewFolderProgress);
98         if (projectDescription == null) {
99             projectHandle.create(new SubProgressMonitor(monitor, 100));
100         } else {
101             projectHandle.create(projectDescription, new SubProgressMonitor(
102                     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 JavaDoc 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 }
Popular Tags