01: /*******************************************************************************
02: * Copyright (c) 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.ui.refactoring;
11:
12: import org.eclipse.core.filebuffers.FileBuffers;
13: import org.eclipse.core.filebuffers.ITextFileBuffer;
14: import org.eclipse.core.filebuffers.ITextFileBufferManager;
15: import org.eclipse.core.filebuffers.LocationKind;
16: import org.eclipse.core.resources.IFile;
17: import org.eclipse.core.runtime.CoreException;
18: import org.eclipse.core.runtime.IPath;
19: import org.eclipse.core.runtime.IProgressMonitor;
20: import org.eclipse.core.runtime.NullProgressMonitor;
21: import org.eclipse.jface.text.IDocument;
22: import org.eclipse.ltk.core.refactoring.TextFileChange;
23:
24: /*
25: * Class is meant to be used to perform a TextFileChange on a file which will be moved during the refactoring execution. This
26: * is useful for editing text files when a project is renamed, since the resource will be moved during the project refactoring.
27: */
28:
29: public class MovedTextFileChange extends TextFileChange {
30:
31: private IFile fCurrentFile;
32:
33: public MovedTextFileChange(String name, IFile newFile,
34: IFile currentFile) {
35: super (name, newFile);
36: fCurrentFile = currentFile;
37: }
38:
39: /*
40: * (non-Javadoc)
41: * @see org.eclipse.ltk.core.refactoring.TextChange#getCurrentDocument(org.eclipse.core.runtime.IProgressMonitor)
42: *
43: * Override getCurrentDocument to return the document of the fCurrentFile instead of the fFile. Since fFile
44: * does not exist, it creates problems when displaying preview information
45: */
46: public IDocument getCurrentDocument(IProgressMonitor pm)
47: throws CoreException {
48: if (pm == null)
49: pm = new NullProgressMonitor();
50: IDocument result = null;
51: pm.beginTask("", 2); //$NON-NLS-1$
52: ITextFileBufferManager manager = FileBuffers
53: .getTextFileBufferManager();
54: try {
55: IPath path = fCurrentFile.getFullPath();
56: manager.connect(path, LocationKind.NORMALIZE, pm);
57: ITextFileBuffer buffer = manager.getTextFileBuffer(path,
58: LocationKind.NORMALIZE);
59: result = buffer.getDocument();
60: } finally {
61: if (result != null)
62: manager.disconnect(fCurrentFile.getFullPath(),
63: LocationKind.NORMALIZE, pm);
64: }
65: pm.done();
66: return result;
67: }
68:
69: }
|