KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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 java.net.URI JavaDoc;
15
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IFolder;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.IWorkspaceRoot;
21 import org.eclipse.core.resources.ResourcesPlugin;
22 import org.eclipse.core.runtime.Assert;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.core.runtime.SubProgressMonitor;
28 import org.eclipse.ui.ide.undo.ResourceDescription;
29
30 /**
31  * ContainerDescription is a lightweight description that describes a container
32  * to be created.
33  *
34  * This class is not intended to be instantiated or used by clients.
35  *
36  * @since 3.3
37  *
38  */

39 public abstract class ContainerDescription extends AbstractResourceDescription {
40
41     String JavaDoc name;
42
43     URI JavaDoc location;
44
45     String JavaDoc defaultCharSet;
46
47     AbstractResourceDescription[] members;
48
49     /**
50      * Create a container description from the specified container handle that
51      * can be used to create the container. The returned ContainerDescription
52      * should represent any non-existing parents in addition to the specified
53      * container.
54      *
55      * @param container
56      * the handle of the container to be described
57      * @return a container description describing the container and any
58      * non-existing parents.
59      */

60
61     public static ContainerDescription fromContainer(IContainer container) {
62         IPath fullPath = container.getFullPath();
63         ContainerDescription firstCreatedParent = null;
64         ContainerDescription currentContainerDescription = null;
65
66         // Does the container exist already? If so, then the parent exists and
67
// we use the normal creation constructor.
68
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
69         IContainer currentContainer = (IContainer) root.findMember(fullPath);
70         if (currentContainer != null) {
71             return (ContainerDescription) ResourceDescription
72                     .fromResource(container);
73         }
74
75         // Create container descriptions for any uncreated parents in the given
76
// path.
77
currentContainer = root;
78         for (int i = 0; i < fullPath.segmentCount(); i++) {
79             String JavaDoc currentSegment = fullPath.segment(i);
80             IResource resource = currentContainer.findMember(currentSegment);
81             if (resource != null) {
82                 // parent already exists, no need to create a description for it
83
currentContainer = (IContainer) resource;
84             } else {
85                 if (i == 0) {
86                     // parent does not exist and it is a project
87
firstCreatedParent = new ProjectDescription(root
88                             .getProject(currentSegment));
89                     currentContainerDescription = firstCreatedParent;
90                 } else {
91                     IFolder folderHandle = currentContainer.getFolder(new Path(
92                             currentSegment));
93                     ContainerDescription currentFolder = new FolderDescription(
94                             folderHandle);
95                     currentContainer = folderHandle;
96                     if (currentContainerDescription != null) {
97                         currentContainerDescription.addMember(currentFolder);
98                     }
99                     currentContainerDescription = currentFolder;
100                     if (firstCreatedParent == null) {
101                         firstCreatedParent = currentFolder;
102                     }
103                 }
104             }
105         }
106         return firstCreatedParent;
107     }
108
109     /**
110      * Create a ContainerDescription with no state.
111      */

112     public ContainerDescription() {
113
114     }
115
116     /**
117      * Create a ContainerDescription from the specified container handle.
118      * Typically used when the container handle represents a resource that
119      * actually exists, although it will not fail if the resource is
120      * non-existent.
121      *
122      * @param container
123      * the container to be described
124      */

125     public ContainerDescription(IContainer container) {
126         super(container);
127         this.name = container.getName();
128         if (container.isLinked()) {
129             this.location = container.getLocationURI();
130         }
131         try {
132             if (container.isAccessible()) {
133                 defaultCharSet = container.getDefaultCharset(false);
134                 IResource[] resourceMembers = container.members();
135                 members = new AbstractResourceDescription[resourceMembers.length];
136                 for (int i = 0; i < resourceMembers.length; i++) {
137                     members[i] = (AbstractResourceDescription) ResourceDescription
138                             .fromResource(resourceMembers[i]);
139                 }
140             }
141         } catch (CoreException e) {
142             // Eat this exception because it only occurs when the resource
143
// does not exist and we have already checked this.
144
// We do not want to throw exceptions on the simple constructor, as
145
// no one has actually tried to do anything yet.
146
}
147     }
148
149     /**
150      * Create any child resources known by this container description.
151      *
152      * @param parentHandle
153      * the handle of the created parent
154      * @param monitor
155      * the progress monitor to be used
156      * @param ticks
157      * the number of ticks allocated for creating children
158      * @throws CoreException
159      */

160     protected void createChildResources(IContainer parentHandle,
161             IProgressMonitor monitor, int ticks) throws CoreException {
162
163         // restore any children
164
if (members != null && members.length > 0) {
165             for (int i = 0; i < members.length; i++) {
166                 members[i].parent = parentHandle;
167                 members[i].createResource(new SubProgressMonitor(monitor, ticks
168                         / members.length));
169             }
170         }
171     }
172
173     /*
174      * (non-Javadoc)
175      *
176      * @see org.eclipse.ui.internal.ide.undo.ResourceDescription#recordStateFromHistory(org.eclipse.core.resources.IResource,
177      * org.eclipse.core.runtime.IProgressMonitor)
178      */

179     public void recordStateFromHistory(IResource resource,
180             IProgressMonitor monitor) throws CoreException {
181         monitor.beginTask(
182                 UndoMessages.FolderDescription_SavingUndoInfoProgress, 100);
183         if (members != null) {
184             for (int i = 0; i < members.length; i++) {
185                 if (members[i] instanceof FileDescription) {
186                     IPath path = resource.getFullPath().append(
187                             ((FileDescription) members[i]).name);
188                     IFile fileHandle = resource.getWorkspace().getRoot().getFile(
189                             path);
190                     members[i].recordStateFromHistory(fileHandle,
191                             new SubProgressMonitor(monitor, 100 / members.length));
192                 } else if (members[i] instanceof FolderDescription) {
193                     IPath path = resource.getFullPath().append(
194                             ((FolderDescription) members[i]).name);
195                     IFolder folderHandle = resource.getWorkspace().getRoot()
196                             .getFolder(path);
197                     members[i].recordStateFromHistory(folderHandle,
198                             new SubProgressMonitor(monitor, 100 / members.length));
199                 }
200             }
201         }
202         monitor.done();
203     }
204
205     /**
206      * Return the name of the container described by this ContainerDescription.
207      *
208      * @return the name of the container.
209      */

210     public String JavaDoc getName() {
211         return name;
212     }
213
214     /**
215      * Return the first folder found that has no child folders.
216      *
217      * @return the container description for the first child in the receiver
218      * that is a leaf, or this container if there are no children.
219      */

220     public ContainerDescription getFirstLeafFolder() {
221         // If there are no members, this is a leaf
222
if (members == null || members.length == 0) {
223             return this;
224         }
225         // Traverse the members and find the first potential leaf
226
for (int i = 0; i < members.length; i++) {
227             if (members[i] instanceof ContainerDescription) {
228                 return ((ContainerDescription) members[i]).getFirstLeafFolder();
229             }
230         }
231         // No child folders were found, this is a leaf
232
return this;
233     }
234
235     /**
236      * Add the specified resource description as a member of this resource
237      * description
238      *
239      * @param member
240      * the resource description considered a member of this
241      * container.
242      */

243     public void addMember(AbstractResourceDescription member) {
244         if (members == null) {
245             members = new AbstractResourceDescription[] { member };
246         } else {
247             AbstractResourceDescription[] expandedMembers = new AbstractResourceDescription[members.length + 1];
248             System.arraycopy(members, 0, expandedMembers, 0, members.length);
249             expandedMembers[members.length] = member;
250             members = expandedMembers;
251         }
252     }
253
254     /*
255      * (non-Javadoc)
256      *
257      * @see org.eclipse.ui.internal.ide.undo.ResourceDescription#restoreResourceAttributes(org.eclipse.core.resources.IResource)
258      */

259     protected void restoreResourceAttributes(IResource resource)
260             throws CoreException {
261         super.restoreResourceAttributes(resource);
262         Assert.isLegal(resource instanceof IContainer);
263         IContainer container = (IContainer) resource;
264         if (defaultCharSet != null) {
265             container.setDefaultCharset(defaultCharSet, null);
266         }
267     }
268
269     /**
270      * Set the location to which this container is linked.
271      *
272      * @param location
273      * the location URI, or <code>null</code> if there is no link
274      */

275     public void setLocation(URI JavaDoc location) {
276         this.location = location;
277     }
278
279     /*
280      * (non-Javadoc)
281      *
282      * @see org.eclipse.ui.internal.ide.undo.ResourceDescription#verifyExistence(boolean)
283      */

284     public boolean verifyExistence(boolean checkMembers) {
285         boolean existence = super.verifyExistence(checkMembers);
286         if (existence) {
287             if (checkMembers) {
288                 // restore any children
289
if (members != null && members.length > 0) {
290                     for (int i = 0; i < members.length; i++) {
291                         if (!members[i].verifyExistence(checkMembers)) {
292                             return false;
293                         }
294                     }
295                 }
296             }
297             return true;
298         }
299         return false;
300     }
301 }
302
Popular Tags