KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > base > JDTChange


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.jdt.internal.corext.refactoring.base;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.SubProgressMonitor;
17
18 import org.eclipse.core.filebuffers.FileBuffers;
19 import org.eclipse.core.filebuffers.ITextFileBuffer;
20 import org.eclipse.core.filebuffers.ITextFileBufferManager;
21 import org.eclipse.core.filebuffers.LocationKind;
22
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.core.resources.IResource;
25
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.IDocumentExtension4;
28
29 import org.eclipse.ltk.core.refactoring.Change;
30 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
31
32 import org.eclipse.jdt.core.ICompilationUnit;
33 import org.eclipse.jdt.core.IJavaElement;
34
35 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
36 import org.eclipse.jdt.internal.corext.util.Messages;
37 import org.eclipse.jdt.internal.corext.util.Resources;
38
39 /**
40  * JDT specific change object.
41  */

42 public abstract class JDTChange extends Change {
43
44     private long fModificationStamp;
45     private boolean fReadOnly;
46     
47     private static class ValidationState {
48         private IResource fResource;
49         private int fKind;
50         private boolean fDirty;
51         private boolean fReadOnly;
52         private long fModificationStamp;
53         private ITextFileBuffer fTextFileBuffer;
54         public static final int RESOURCE= 1;
55         public static final int DOCUMENT= 2;
56         public ValidationState(IResource resource) {
57             fResource= resource;
58             if (resource instanceof IFile) {
59                 initializeFile((IFile)resource);
60             } else {
61                 initializeResource(resource);
62             }
63         }
64         public void checkDirty(RefactoringStatus status, long stampToMatch, IProgressMonitor pm) throws CoreException {
65             if (fDirty) {
66                 if (fKind == DOCUMENT && fTextFileBuffer != null && stampToMatch == fModificationStamp) {
67                     fTextFileBuffer.commit(pm, false);
68                 } else {
69                     status.addFatalError(Messages.format(
70                         RefactoringCoreMessages.Change_is_unsaved, fResource.getFullPath().toString()));
71                 }
72             }
73         }
74         public void checkDirty(RefactoringStatus status) {
75             if (fDirty) {
76                 status.addFatalError(Messages.format(
77                     RefactoringCoreMessages.Change_is_unsaved, fResource.getFullPath().toString()));
78             }
79         }
80         public void checkReadOnly(RefactoringStatus status) {
81             if (fReadOnly) {
82                 status.addFatalError(Messages.format(
83                     RefactoringCoreMessages.Change_is_read_only, fResource.getFullPath().toString()));
84             }
85         }
86         public void checkSameReadOnly(RefactoringStatus status, boolean valueToMatch) {
87             if (fReadOnly != valueToMatch) {
88                 status.addFatalError(Messages.format(
89                     RefactoringCoreMessages.Change_same_read_only,
90                     fResource.getFullPath().toString()));
91             }
92         }
93         public void checkModificationStamp(RefactoringStatus status, long stampToMatch) {
94             if (fKind == DOCUMENT) {
95                 if (stampToMatch != IDocumentExtension4.UNKNOWN_MODIFICATION_STAMP && fModificationStamp != stampToMatch) {
96                     status.addFatalError(Messages.format(
97                         RefactoringCoreMessages.Change_has_modifications, fResource.getFullPath().toString()));
98                 }
99             } else {
100                 if (stampToMatch != IResource.NULL_STAMP && fModificationStamp != stampToMatch) {
101                     status.addFatalError(Messages.format(
102                         RefactoringCoreMessages.Change_has_modifications, fResource.getFullPath().toString()));
103                     
104                 }
105             }
106         }
107         private void initializeFile(IFile file) {
108             fTextFileBuffer= getBuffer(file);
109             if (fTextFileBuffer == null) {
110                 initializeResource(file);
111             } else {
112                 IDocument document= fTextFileBuffer.getDocument();
113                 fDirty= fTextFileBuffer.isDirty();
114                 fReadOnly= Resources.isReadOnly(file);
115                 if (document instanceof IDocumentExtension4) {
116                     fKind= DOCUMENT;
117                     fModificationStamp= ((IDocumentExtension4)document).getModificationStamp();
118                 } else {
119                     fKind= RESOURCE;
120                     fModificationStamp= file.getModificationStamp();
121                 }
122             }
123             
124         }
125         private void initializeResource(IResource resource) {
126             fKind= RESOURCE;
127             fDirty= false;
128             fReadOnly= Resources.isReadOnly(resource);
129             fModificationStamp= resource.getModificationStamp();
130         }
131     }
132
133     protected static final int NONE= 0;
134     protected static final int READ_ONLY= 1 << 0;
135     protected static final int DIRTY= 1 << 1;
136     private static final int SAVE= 1 << 2;
137     protected static final int SAVE_IF_DIRTY= SAVE | DIRTY;
138     
139     protected JDTChange() {
140         fModificationStamp= IResource.NULL_STAMP;
141         fReadOnly= false;
142     }
143     
144     public void initializeValidationData(IProgressMonitor pm) {
145         IResource resource= getResource(getModifiedElement());
146         if (resource != null) {
147             fModificationStamp= getModificationStamp(resource);
148             fReadOnly= Resources.isReadOnly(resource);
149         }
150     }
151
152     // protected final RefactoringStatus isValid(IProgressMonitor pm, boolean checkReadOnly, boolean checkDirty) throws CoreException {
153
protected final RefactoringStatus isValid(IProgressMonitor pm, int flags) throws CoreException {
154         pm.beginTask("", 2); //$NON-NLS-1$
155
try {
156             RefactoringStatus result= new RefactoringStatus();
157             Object JavaDoc modifiedElement= getModifiedElement();
158             checkExistence(result, modifiedElement);
159             if (result.hasFatalError())
160                 return result;
161             if (flags == NONE)
162                 return result;
163             IResource resource= getResource(modifiedElement);
164             if (resource != null) {
165                 ValidationState state= new ValidationState(resource);
166                 state.checkModificationStamp(result, fModificationStamp);
167                 if (result.hasFatalError())
168                     return result;
169                 state.checkSameReadOnly(result, fReadOnly);
170                 if (result.hasFatalError())
171                     return result;
172                 if ((flags & READ_ONLY) != 0) {
173                     state.checkReadOnly(result);
174                     if (result.hasFatalError())
175                         return result;
176                 }
177                 if ((flags & DIRTY) != 0) {
178                     if ((flags & SAVE) != 0) {
179                         state.checkDirty(result, fModificationStamp, new SubProgressMonitor(pm, 1));
180                     } else {
181                         state.checkDirty(result);
182                     }
183                 }
184             }
185             return result;
186         } finally {
187             pm.done();
188         }
189     }
190
191     protected static void checkIfModifiable(RefactoringStatus status, Object JavaDoc element, int flags) {
192         checkIfModifiable(status, getResource(element), flags);
193     }
194
195     protected static void checkIfModifiable(RefactoringStatus result, IResource resource, int flags) {
196         checkExistence(result, resource);
197         if (result.hasFatalError())
198             return;
199         if (flags == NONE)
200             return;
201         ValidationState state= new ValidationState(resource);
202         if ((flags & READ_ONLY) != 0) {
203             state.checkReadOnly(result);
204             if (result.hasFatalError())
205                 return;
206         }
207         if ((flags & DIRTY) != 0) {
208             state.checkDirty(result);
209         }
210     }
211
212     protected static void checkExistence(RefactoringStatus status, Object JavaDoc element) {
213         if (element == null) {
214             status.addFatalError(RefactoringCoreMessages.DynamicValidationStateChange_workspace_changed);
215             
216         } else if (element instanceof IResource && !((IResource)element).exists()) {
217             status.addFatalError(Messages.format(
218                 RefactoringCoreMessages.Change_does_not_exist, ((IResource)element).getFullPath().toString()));
219         } else if (element instanceof IJavaElement && !((IJavaElement)element).exists()) {
220             status.addFatalError(Messages.format(
221                 RefactoringCoreMessages.Change_does_not_exist, ((IJavaElement)element).getElementName()));
222         }
223     }
224
225     private static IResource getResource(Object JavaDoc element) {
226         if (element instanceof IResource) {
227             return (IResource)element;
228         }
229         if (element instanceof ICompilationUnit) {
230             return ((ICompilationUnit)element).getPrimary().getResource();
231         }
232         if (element instanceof IJavaElement) {
233             return ((IJavaElement)element).getResource();
234         }
235         if (element instanceof IAdaptable) {
236             return (IResource)((IAdaptable)element).getAdapter(IResource.class);
237         }
238         return null;
239     }
240
241     public String JavaDoc toString() {
242         return getName();
243     }
244     
245     public long getModificationStamp(IResource resource) {
246         if (!(resource instanceof IFile))
247             return resource.getModificationStamp();
248         IFile file= (IFile)resource;
249         ITextFileBuffer buffer= getBuffer(file);
250         if (buffer == null) {
251             return file.getModificationStamp();
252         } else {
253             IDocument document= buffer.getDocument();
254             if (document instanceof IDocumentExtension4) {
255                 return ((IDocumentExtension4)document).getModificationStamp();
256             } else {
257                 return file.getModificationStamp();
258             }
259         }
260     }
261     
262     private static ITextFileBuffer getBuffer(IFile file) {
263         ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
264         return manager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
265     }
266 }
267
Popular Tags