001: /*******************************************************************************
002: * Copyright (c) 2006, 2007 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.IContainer;
013: import org.eclipse.core.resources.IMarker;
014: import org.eclipse.core.resources.IResource;
015: import org.eclipse.core.resources.IWorkspace;
016: import org.eclipse.core.resources.ResourceAttributes;
017: import org.eclipse.core.resources.ResourcesPlugin;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.ui.ide.undo.ResourceDescription;
021:
022: /**
023: * Base implementation of ResourceDescription that describes the common
024: * attributes of a resource to be created.
025: *
026: * This class is not intended to be instantiated or used by clients.
027: *
028: * @since 3.3
029: *
030: */
031: abstract class AbstractResourceDescription extends ResourceDescription {
032: IContainer parent;
033:
034: long modificationStamp = IResource.NULL_STAMP;
035:
036: long localTimeStamp = IResource.NULL_STAMP;
037:
038: ResourceAttributes resourceAttributes;
039:
040: MarkerDescription[] markerDescriptions;
041:
042: /**
043: * Create a resource description with no initial attributes
044: */
045: protected AbstractResourceDescription() {
046: super ();
047: }
048:
049: /**
050: * Create a resource description from the specified resource.
051: *
052: * @param resource
053: * the resource to be described
054: */
055: protected AbstractResourceDescription(IResource resource) {
056: super ();
057: parent = resource.getParent();
058: if (resource.isAccessible()) {
059: modificationStamp = resource.getModificationStamp();
060: localTimeStamp = resource.getLocalTimeStamp();
061: resourceAttributes = resource.getResourceAttributes();
062: try {
063: IMarker[] markers = resource.findMarkers(null, true,
064: IResource.DEPTH_INFINITE);
065: markerDescriptions = new MarkerDescription[markers.length];
066: for (int i = 0; i < markers.length; i++) {
067: markerDescriptions[i] = new MarkerDescription(
068: markers[i]);
069: }
070: } catch (CoreException e) {
071: // Eat this exception because it only occurs when the resource
072: // does not exist and we have already checked this.
073: // We do not want to throw exceptions on the simple constructor,
074: // as no one has actually tried to do anything yet.
075: }
076: }
077: }
078:
079: /* (non-Javadoc)
080: * @see org.eclipse.ui.ide.undo.ResourceDescription#createResource(org.eclipse.core.runtime.IProgressMonitor)
081: */
082: public IResource createResource(IProgressMonitor monitor)
083: throws CoreException {
084: IResource resource = createResourceHandle();
085: createExistentResourceFromHandle(resource, monitor);
086: restoreResourceAttributes(resource);
087: return resource;
088: }
089:
090: /* (non-Javadoc)
091: * @see org.eclipse.ui.ide.undo.ResourceDescription#isValid()
092: */
093: public boolean isValid() {
094: return parent == null || parent.exists();
095: }
096:
097: /**
098: * Restore any saved attributed of the specified resource. This method is
099: * called after the existent resource represented by the receiver has been
100: * created.
101: *
102: * @param resource
103: * the newly created resource
104: * @throws CoreException
105: */
106: protected void restoreResourceAttributes(IResource resource)
107: throws CoreException {
108: if (modificationStamp != IResource.NULL_STAMP) {
109: resource.revertModificationStamp(modificationStamp);
110: }
111: if (localTimeStamp != IResource.NULL_STAMP) {
112: resource.setLocalTimeStamp(localTimeStamp);
113: }
114: if (resourceAttributes != null) {
115: resource.setResourceAttributes(resourceAttributes);
116: }
117: if (markerDescriptions != null) {
118: for (int i = 0; i < markerDescriptions.length; i++) {
119: markerDescriptions[i].resource = resource;
120: markerDescriptions[i].createMarker();
121: }
122: }
123: }
124:
125: /*
126: * Return the workspace.
127: */
128: IWorkspace getWorkspace() {
129: return ResourcesPlugin.getWorkspace();
130: }
131:
132: /* (non-Javadoc)
133: * @see org.eclipse.ui.ide.undo.ResourceDescription#verifyExistence(boolean)
134: */
135: public boolean verifyExistence(boolean checkMembers) {
136: IContainer p = parent;
137: if (p == null) {
138: p = getWorkspace().getRoot();
139: }
140: IResource handle = p.findMember(getName());
141: return handle != null;
142: }
143: }
|