KickJava   Java API By Example, From Geeks To Geeks.

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


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.changes;
12
13 import org.eclipse.text.edits.UndoEdit;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.SubProgressMonitor;
19
20 import org.eclipse.core.resources.IFile;
21
22 import org.eclipse.jface.text.IDocument;
23
24 import org.eclipse.ltk.core.refactoring.Change;
25 import org.eclipse.ltk.core.refactoring.ChangeDescriptor;
26 import org.eclipse.ltk.core.refactoring.ContentStamp;
27 import org.eclipse.ltk.core.refactoring.TextFileChange;
28
29 import org.eclipse.jdt.core.ICompilationUnit;
30
31 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
32
33 import org.eclipse.jdt.internal.ui.JavaPlugin;
34
35 public class CompilationUnitChange extends TextFileChange {
36
37     private final ICompilationUnit fCUnit;
38     
39     /** The (optional) refactoring descriptor */
40     private ChangeDescriptor fDescriptor;
41     
42     /**
43      * Creates a new <code>CompilationUnitChange</code>.
44      *
45      * @param name the change's name mainly used to render the change in the UI
46      * @param cunit the compilation unit this text change works on
47      */

48     public CompilationUnitChange(String JavaDoc name, ICompilationUnit cunit) {
49         super(name, getFile(cunit));
50         Assert.isNotNull(cunit);
51         fCUnit= cunit;
52         setTextType("java"); //$NON-NLS-1$
53
}
54     
55     private static IFile getFile(ICompilationUnit cunit) {
56         return (IFile) cunit.getResource();
57     }
58     
59     /* non java-doc
60      * Method declared in IChange.
61      */

62     public Object JavaDoc getModifiedElement(){
63         return fCUnit;
64     }
65     
66     /**
67      * Returns the compilation unit this change works on.
68      *
69      * @return the compilation unit this change works on
70      */

71     public ICompilationUnit getCompilationUnit() {
72         return fCUnit;
73     }
74     
75     /**
76      * {@inheritDoc}
77      */

78     protected IDocument acquireDocument(IProgressMonitor pm) throws CoreException {
79         pm.beginTask("", 2); //$NON-NLS-1$
80
fCUnit.becomeWorkingCopy(null, new SubProgressMonitor(pm, 1));
81         return super.acquireDocument(new SubProgressMonitor(pm, 1));
82     }
83     
84     /**
85      * {@inheritDoc}
86      */

87     protected void releaseDocument(IDocument document, IProgressMonitor pm) throws CoreException {
88         boolean isModified= isDocumentModified();
89         super.releaseDocument(document, pm);
90         try {
91             fCUnit.discardWorkingCopy();
92         } finally {
93             if (isModified && !isDocumentAcquired()) {
94                 if (fCUnit.isWorkingCopy())
95                     JavaModelUtil.reconcile(fCUnit);
96                 else
97                     fCUnit.makeConsistent(pm);
98             }
99         }
100     }
101     
102     /**
103      * {@inheritDoc}
104      */

105     protected Change createUndoChange(UndoEdit edit, ContentStamp stampToRestore) {
106         try {
107             return new UndoCompilationUnitChange(getName(), fCUnit, edit, stampToRestore, getSaveMode());
108         } catch (CoreException e) {
109             JavaPlugin.log(e);
110             return null;
111         }
112     }
113     
114     /**
115      * {@inheritDoc}
116      */

117     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
118         if (ICompilationUnit.class.equals(adapter))
119             return fCUnit;
120         return super.getAdapter(adapter);
121     }
122     
123     /**
124      * Sets the refactoring descriptor for this change
125      *
126      * @param descriptor the descriptor to set
127      */

128     public void setDescriptor(ChangeDescriptor descriptor) {
129         fDescriptor= descriptor;
130     }
131     
132     /**
133      * {@inheritDoc}
134      */

135     public ChangeDescriptor getDescriptor() {
136         return fDescriptor;
137     }
138 }
139
140
Popular Tags