KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.resources.IContainer;
15 import org.eclipse.core.resources.IMarker;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.IWorkspace;
18 import org.eclipse.core.resources.ResourceAttributes;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.ui.ide.undo.ResourceDescription;
23
24 /**
25  * Base implementation of ResourceDescription that describes the common
26  * attributes of a resource to be created.
27  *
28  * This class is not intended to be instantiated or used by clients.
29  *
30  * @since 3.3
31  *
32  */

33 abstract class AbstractResourceDescription extends ResourceDescription {
34     IContainer parent;
35
36     long modificationStamp = IResource.NULL_STAMP;
37
38     long localTimeStamp = IResource.NULL_STAMP;
39
40     ResourceAttributes resourceAttributes;
41
42     MarkerDescription[] markerDescriptions;
43
44     /**
45      * Create a resource description with no initial attributes
46      */

47     protected AbstractResourceDescription() {
48         super();
49     }
50
51     /**
52      * Create a resource description from the specified resource.
53      *
54      * @param resource
55      * the resource to be described
56      */

57     protected AbstractResourceDescription(IResource resource) {
58         super();
59         parent = resource.getParent();
60         if (resource.isAccessible()) {
61             modificationStamp = resource.getModificationStamp();
62             localTimeStamp = resource.getLocalTimeStamp();
63             resourceAttributes = resource.getResourceAttributes();
64             try {
65                 IMarker[] markers = resource.findMarkers(null, true,
66                         IResource.DEPTH_INFINITE);
67                 markerDescriptions = new MarkerDescription[markers.length];
68                 for (int i = 0; i < markers.length; i++) {
69                     markerDescriptions[i] = new MarkerDescription(markers[i]);
70                 }
71             } catch (CoreException e) {
72                 // Eat this exception because it only occurs when the resource
73
// does not exist and we have already checked this.
74
// We do not want to throw exceptions on the simple constructor,
75
// as no one has actually tried to do anything yet.
76
}
77         }
78     }
79
80     
81     /* (non-Javadoc)
82      * @see org.eclipse.ui.ide.undo.ResourceDescription#createResource(org.eclipse.core.runtime.IProgressMonitor)
83      */

84     public IResource createResource(IProgressMonitor monitor)
85             throws CoreException {
86         IResource resource = createResourceHandle();
87         createExistentResourceFromHandle(resource, monitor);
88         restoreResourceAttributes(resource);
89         return resource;
90     }
91
92     /* (non-Javadoc)
93      * @see org.eclipse.ui.ide.undo.ResourceDescription#isValid()
94      */

95     public boolean isValid() {
96         return parent == null || parent.exists();
97     }
98
99     /**
100      * Restore any saved attributed of the specified resource. This method is
101      * called after the existent resource represented by the receiver has been
102      * created.
103      *
104      * @param resource
105      * the newly created resource
106      * @throws CoreException
107      */

108     protected void restoreResourceAttributes(IResource resource) throws CoreException {
109         if (modificationStamp != IResource.NULL_STAMP) {
110             resource.revertModificationStamp(modificationStamp);
111         }
112         if (localTimeStamp != IResource.NULL_STAMP) {
113             resource.setLocalTimeStamp(localTimeStamp);
114         }
115         if (resourceAttributes != null) {
116             resource.setResourceAttributes(resourceAttributes);
117         }
118         if (markerDescriptions != null) {
119             for (int i = 0; i < markerDescriptions.length; i++) {
120                 markerDescriptions[i].resource = resource;
121                 markerDescriptions[i].createMarker();
122             }
123         }
124     }
125
126     /*
127      * Return the workspace.
128      */

129     IWorkspace getWorkspace() {
130         return ResourcesPlugin.getWorkspace();
131     }
132
133     /* (non-Javadoc)
134      * @see org.eclipse.ui.ide.undo.ResourceDescription#verifyExistence(boolean)
135      */

136     public boolean verifyExistence(boolean checkMembers) {
137         IContainer p = parent;
138         if (p == null) {
139             p = getWorkspace().getRoot();
140         }
141         IResource handle = p.findMember(getName());
142         return handle != null;
143     }
144 }
145
Popular Tags