KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.ByteArrayInputStream JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.net.URI JavaDoc;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IFileState;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.resources.IResourceStatus;
22 import org.eclipse.core.resources.IWorkspaceRoot;
23 import org.eclipse.core.runtime.Assert;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.OperationCanceledException;
28 import org.eclipse.core.runtime.SubProgressMonitor;
29
30 /**
31  * FileDescription is a lightweight description that describes a file to be
32  * created.
33  *
34  * This class is not intended to be instantiated or used by clients.
35  *
36  * @since 3.3
37  *
38  */

39 public class FileDescription extends AbstractResourceDescription {
40
41     String JavaDoc name;
42
43     URI JavaDoc location;
44
45     String JavaDoc charset;
46
47     private IFileContentDescription fileContentDescription;
48
49     /**
50      * Create a FileDescription that can be used to later restore the given
51      * file. The file typically already exists, but this constructor will not
52      * fail if the file does not exist.
53      *
54      * @param file
55      * the file to be restored.
56      */

57     public FileDescription(IFile file) {
58         super(file);
59         this.name = file.getName();
60         try {
61             this.charset = file.getCharset(false);
62         } catch (CoreException e) {
63             // we don't care, a null charset is fine.
64
}
65         if (file.isLinked()) {
66             location = file.getLocationURI();
67         }
68
69     }
70
71     /**
72      * Create a file description from the specified file handle. The handle does
73      * not exist, so no information should be derived from it. If a location
74      * path is specified, this file should represent a link to another location.
75      * The content description describes any state that should be used when the
76      * file resource is created.
77      *
78      * @param file
79      * the file to be described
80      * @param linkLocation
81      * the location of the file's link, or <code>null</code> if the
82      * file is not linked
83      * @param fileContentDescription
84      * the file content description that can be used to get
85      * information about the file, such as its initial content
86      */

87     public FileDescription(IFile file, URI JavaDoc linkLocation,
88             IFileContentDescription fileContentDescription) {
89         super(file);
90         this.name = file.getName();
91         this.location = linkLocation;
92         this.charset = null;
93         this.fileContentDescription = fileContentDescription;
94     }
95
96     /*
97      * (non-Javadoc)
98      *
99      * @see org.eclipse.ui.internal.ide.undo.ResourceDescription#recordStateFromHistory(org.eclipse.core.resources.IResource,
100      * org.eclipse.core.runtime.IProgressMonitor)
101      */

102     public void recordStateFromHistory(IResource resource,
103             IProgressMonitor monitor) throws CoreException {
104         Assert.isLegal(resource.getType() == IResource.FILE);
105
106         if (location != null) {
107             // file is linked, no need to record any history
108
return;
109         }
110         IFileState[] states = ((IFile) resource).getHistory(monitor);
111         if (states.length > 0) {
112             final IFileState state = getMatchingFileState(states);
113             this.fileContentDescription = new IFileContentDescription() {
114                 /*
115                  * (non-Javadoc)
116                  *
117                  * @see org.eclipse.ui.internal.ide.undo.IFileContentDescription#exists()
118                  */

119                 public boolean exists() {
120                     return state.exists();
121                 }
122
123                 /*
124                  * (non-Javadoc)
125                  *
126                  * @see org.eclipse.ui.internal.ide.undo.IFileContentDescription#getContents()
127                  */

128                 public InputStream JavaDoc getContents() throws CoreException {
129                     return state.getContents();
130                 }
131
132                 /*
133                  * (non-Javadoc)
134                  *
135                  * @see org.eclipse.ui.internal.ide.undo.IFileContentDescription#getCharset()
136                  */

137                 public String JavaDoc getCharset() throws CoreException {
138                     return state.getCharset();
139                 }
140             };
141         }
142     }
143
144     /*
145      * (non-Javadoc)
146      *
147      * @see org.eclipse.ui.internal.ide.undo.ResourceDescription#createResourceHandle()
148      */

149     public IResource createResourceHandle() {
150         IWorkspaceRoot workspaceRoot = parent.getWorkspace().getRoot();
151         IPath fullPath = parent.getFullPath().append(name);
152         return workspaceRoot.getFile(fullPath);
153     }
154
155     /*
156      * (non-Javadoc)
157      *
158      * @see org.eclipse.ui.internal.ide.undo.ResourceDescription#createExistentResourceFromHandle(org.eclipse.core.resources.IResource,
159      * org.eclipse.core.runtime.IProgressMonitor)
160      */

161     public void createExistentResourceFromHandle(IResource resource,
162             IProgressMonitor monitor) throws CoreException {
163
164         Assert.isLegal(resource instanceof IFile);
165         if (resource.exists()) {
166             return;
167         }
168         IFile fileHandle = (IFile) resource;
169         monitor.beginTask("", 200); //$NON-NLS-1$
170
monitor.setTaskName(UndoMessages.FileDescription_NewFileProgress);
171         try {
172             if (monitor.isCanceled()) {
173                 throw new OperationCanceledException();
174             }
175             if (location != null) {
176                 fileHandle.createLink(location, IResource.ALLOW_MISSING_LOCAL,
177                         new SubProgressMonitor(monitor, 200));
178             } else {
179                 InputStream JavaDoc contents = new ByteArrayInputStream JavaDoc(
180                         UndoMessages.FileDescription_ContentsCouldNotBeRestored
181                                 .getBytes());
182                 // Retrieve the contents from the file content
183
// description. Other file state attributes, such as timestamps,
184
// have already been retrieved from the original IResource
185
// object and are restored in #restoreResourceAttributes
186
if (fileContentDescription != null
187                         && fileContentDescription.exists()) {
188                     contents = fileContentDescription.getContents();
189                 }
190                 fileHandle.create(contents, false, new SubProgressMonitor(
191                         monitor, 100));
192                 fileHandle.setCharset(charset, new SubProgressMonitor(monitor,
193                         100));
194             }
195             if (monitor.isCanceled()) {
196                 throw new OperationCanceledException();
197             }
198         } catch (CoreException e) {
199             if (e.getStatus().getCode() == IResourceStatus.PATH_OCCUPIED) {
200                 fileHandle.refreshLocal(IResource.DEPTH_ZERO, null);
201             } else {
202                 throw e;
203             }
204         } finally {
205             monitor.done();
206         }
207     }
208
209     /*
210      * (non-Javadoc)
211      *
212      * @see org.eclipse.ui.internal.ide.undo.ResourceDescription#isValid()
213      */

214     public boolean isValid() {
215         if (location != null) {
216             return super.isValid();
217         }
218         return super.isValid() && fileContentDescription != null
219                 && fileContentDescription.exists();
220     }
221
222     /*
223      * (non-Javadoc)
224      *
225      * @see org.eclipse.ui.internal.ide.undo.ResourceDescription#getName()
226      */

227     public String JavaDoc getName() {
228         return name;
229     }
230
231     /*
232      * Get the file state that matches this file description. The local time
233      * stamp is used to try to find a matching file state. If none can be found,
234      * the most recent copy of the file state is used.
235      */

236     private IFileState getMatchingFileState(IFileState[] states) {
237         for (int i = 0; i < states.length; i++) {
238             if (localTimeStamp == states[i].getModificationTime()) {
239                 return states[i];
240             }
241         }
242         return states[0];
243
244     }
245     
246     /*
247      * (non-Javadoc)
248      *
249      * @see org.eclipse.ui.internal.ide.undo.ResourceDescription#restoreResourceAttributes(org.eclipse.core.resources.IResource)
250      */

251     protected void restoreResourceAttributes(IResource resource)
252             throws CoreException {
253         super.restoreResourceAttributes(resource);
254         Assert.isLegal(resource instanceof IFile);
255         IFile file = (IFile) resource;
256         if (charset != null) {
257             file.setCharset(charset, null);
258         }
259     }
260 }
261
Popular Tags