KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > nls > changes > CreateFileChange


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.jdt.internal.corext.refactoring.nls.changes;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.UnsupportedEncodingException JavaDoc;
17 import java.net.URI JavaDoc;
18
19 import org.eclipse.core.filesystem.EFS;
20 import org.eclipse.core.filesystem.IFileInfo;
21
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.OperationCanceledException;
27 import org.eclipse.core.runtime.Platform;
28 import org.eclipse.core.runtime.SubProgressMonitor;
29 import org.eclipse.core.runtime.content.IContentType;
30
31 import org.eclipse.core.resources.IFile;
32 import org.eclipse.core.resources.IResource;
33 import org.eclipse.core.resources.ResourcesPlugin;
34
35 import org.eclipse.ltk.core.refactoring.Change;
36 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
37
38 import org.eclipse.jdt.core.IJavaModelStatusConstants;
39 import org.eclipse.jdt.core.JavaModelException;
40
41 import org.eclipse.jdt.internal.corext.refactoring.base.JDTChange;
42 import org.eclipse.jdt.internal.corext.util.Messages;
43
44 public class CreateFileChange extends JDTChange {
45
46     private String JavaDoc fChangeName;
47
48     private IPath fPath;
49     private String JavaDoc fSource;
50     private String JavaDoc fEncoding;
51     private boolean fExplicitEncoding;
52     private long fStampToRestore;
53
54     public CreateFileChange(IPath path, String JavaDoc source, String JavaDoc encoding) {
55         this(path, source, encoding, IResource.NULL_STAMP);
56     }
57
58     public CreateFileChange(IPath path, String JavaDoc source, String JavaDoc encoding, long stampToRestore) {
59         Assert.isNotNull(path, "path"); //$NON-NLS-1$
60
Assert.isNotNull(source, "source"); //$NON-NLS-1$
61
fPath= path;
62         fSource= source;
63         fEncoding= encoding;
64         fExplicitEncoding= fEncoding != null;
65         fStampToRestore= stampToRestore;
66     }
67
68     /*
69     private CreateFileChange(IPath path, String source, String encoding, long stampToRestore, boolean explicit) {
70         Assert.isNotNull(path, "path"); //$NON-NLS-1$
71         Assert.isNotNull(source, "source"); //$NON-NLS-1$
72         Assert.isNotNull(encoding, "encoding"); //$NON-NLS-1$
73         fPath= path;
74         fSource= source;
75         fEncoding= encoding;
76         fStampToRestore= stampToRestore;
77         fExplicitEncoding= explicit;
78     }
79     */

80
81     protected void setEncoding(String JavaDoc encoding, boolean explicit) {
82         Assert.isNotNull(encoding, "encoding"); //$NON-NLS-1$
83
fEncoding= encoding;
84         fExplicitEncoding= explicit;
85     }
86
87     public String JavaDoc getName() {
88         if (fChangeName == null)
89             return Messages.format(NLSChangesMessages.createFile_Create_file, fPath.toOSString());
90         else
91             return fChangeName;
92     }
93
94     public void setName(String JavaDoc name) {
95         fChangeName= name;
96     }
97     
98     protected void setSource(String JavaDoc source) {
99         fSource= source;
100     }
101
102     protected String JavaDoc getSource() {
103         return fSource;
104     }
105
106     protected void setPath(IPath path) {
107         fPath= path;
108     }
109
110     protected IPath getPath() {
111         return fPath;
112     }
113
114     public Object JavaDoc getModifiedElement() {
115         return ResourcesPlugin.getWorkspace().getRoot().getFile(fPath);
116     }
117
118     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
119         RefactoringStatus result= new RefactoringStatus();
120         IFile file= ResourcesPlugin.getWorkspace().getRoot().getFile(fPath);
121         
122         URI JavaDoc location= file.getLocationURI();
123         if (location == null) {
124             result.addFatalError(Messages.format(
125                 NLSChangesMessages.CreateFileChange_error_unknownLocation,
126                 file.getFullPath().toString()));
127             return result;
128         }
129         
130         IFileInfo jFile= EFS.getStore(location).fetchInfo();
131         if (jFile.exists()) {
132             result.addFatalError(Messages.format(
133                 NLSChangesMessages.CreateFileChange_error_exists,
134                 file.getFullPath().toString()));
135             return result;
136         }
137         return result;
138     }
139
140     public Change perform(IProgressMonitor pm) throws CoreException, OperationCanceledException {
141
142         InputStream JavaDoc is= null;
143         try {
144             pm.beginTask(NLSChangesMessages.createFile_creating_resource, 3);
145
146             initializeEncoding();
147             IFile file= getOldFile(new SubProgressMonitor(pm, 1));
148             /*
149             if (file.exists()) {
150                 CompositeChange composite= new CompositeChange(getName());
151                 composite.add(new DeleteFileChange(file));
152                 composite.add(new CreateFileChange(fPath, fSource, fEncoding, fStampToRestore, fExplicitEncoding));
153                 pm.worked(1);
154                 return composite.perform(new SubProgressMonitor(pm, 1));
155             } else { */

156             try {
157                 is= new ByteArrayInputStream JavaDoc(fSource.getBytes(fEncoding));
158                 file.create(is, false, new SubProgressMonitor(pm, 1));
159                 if (fStampToRestore != IResource.NULL_STAMP) {
160                     file.revertModificationStamp(fStampToRestore);
161                 }
162                 if (fExplicitEncoding) {
163                     file.setCharset(fEncoding, new SubProgressMonitor(pm, 1));
164                 } else {
165                     pm.worked(1);
166                 }
167                 return new DeleteFileChange(file);
168             } catch (UnsupportedEncodingException JavaDoc e) {
169                 throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
170             }
171         } finally {
172             try {
173                 if (is != null)
174                     is.close();
175             } catch (IOException JavaDoc ioe) {
176                 throw new JavaModelException(ioe, IJavaModelStatusConstants.IO_EXCEPTION);
177             } finally {
178                 pm.done();
179             }
180         }
181     }
182
183     protected IFile getOldFile(IProgressMonitor pm) throws OperationCanceledException {
184         pm.beginTask("", 1); //$NON-NLS-1$
185
try {
186             return ResourcesPlugin.getWorkspace().getRoot().getFile(fPath);
187         } finally {
188             pm.done();
189         }
190     }
191
192     private void initializeEncoding() {
193         if (fEncoding == null) {
194             fExplicitEncoding= false;
195             IFile file= ResourcesPlugin.getWorkspace().getRoot().getFile(fPath);
196             if (file != null) {
197                 try {
198                     if (file.exists()) {
199                         fEncoding= file.getCharset(false);
200                         if (fEncoding == null) {
201                             fEncoding= file.getCharset(true);
202                         } else {
203                             fExplicitEncoding= true;
204                         }
205                     } else {
206                         IContentType contentType= Platform.getContentTypeManager().findContentTypeFor(file.getName());
207                         if (contentType != null)
208                             fEncoding= contentType.getDefaultCharset();
209                         if (fEncoding == null)
210                             fEncoding= file.getCharset(true);
211                     }
212                 } catch (CoreException e) {
213                     fEncoding= ResourcesPlugin.getEncoding();
214                     fExplicitEncoding= true;
215                 }
216             } else {
217                 fEncoding= ResourcesPlugin.getEncoding();
218                 fExplicitEncoding= true;
219             }
220         }
221         Assert.isNotNull(fEncoding);
222     }
223 }
224
Popular Tags