KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.CoreException;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.core.runtime.SubProgressMonitor;
20
21 import org.eclipse.core.resources.IFile;
22
23 import org.eclipse.jdt.core.ICompilationUnit;
24
25 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
26 import org.eclipse.jdt.internal.corext.util.Messages;
27
28 import org.eclipse.jdt.internal.ui.JavaPlugin;
29
30 import org.eclipse.ltk.core.refactoring.Change;
31 import org.eclipse.ltk.core.refactoring.ContentStamp;
32 import org.eclipse.ltk.core.refactoring.UndoTextFileChange;
33
34 /* package */ class UndoCompilationUnitChange extends UndoTextFileChange {
35     
36     private ICompilationUnit fCUnit;
37
38     public UndoCompilationUnitChange(String JavaDoc name, ICompilationUnit unit, UndoEdit undo, ContentStamp stampToRestore, int saveMode) throws CoreException {
39         super(name, getFile(unit), undo, stampToRestore, saveMode);
40         fCUnit= unit;
41     }
42
43     private static IFile getFile(ICompilationUnit cunit) throws CoreException {
44         IFile file= (IFile)cunit.getResource();
45         if (file == null)
46             throw new CoreException(new Status(
47                 IStatus.ERROR,
48                 JavaPlugin.getPluginId(),
49                 IStatus.ERROR,
50                 Messages.format(
51                     RefactoringCoreMessages.UndoCompilationUnitChange_no_resource,
52                     cunit.getElementName()),
53                 null)
54             );
55         return file;
56     }
57     
58     /**
59      * {@inheritDoc}
60      */

61     public Object JavaDoc getModifiedElement() {
62         return fCUnit;
63     }
64     
65     /**
66      * {@inheritDoc}
67      */

68     protected Change createUndoChange(UndoEdit edit, ContentStamp stampToRestore) throws CoreException {
69         return new UndoCompilationUnitChange(getName(), fCUnit, edit, stampToRestore, getSaveMode());
70     }
71     
72     /**
73      * {@inheritDoc}
74      */

75     public Change perform(IProgressMonitor pm) throws CoreException {
76         pm.beginTask("", 2); //$NON-NLS-1$
77
fCUnit.becomeWorkingCopy(null, new SubProgressMonitor(pm,1));
78         try {
79             return super.perform(new SubProgressMonitor(pm,1));
80         } finally {
81             fCUnit.discardWorkingCopy();
82         }
83     }
84 }
85
Popular Tags