KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > refactoring > MovedTextFileChange


1 /*******************************************************************************
2  * Copyright (c) 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.pde.internal.ui.refactoring;
12
13 import org.eclipse.core.filebuffers.FileBuffers;
14 import org.eclipse.core.filebuffers.ITextFileBuffer;
15 import org.eclipse.core.filebuffers.ITextFileBufferManager;
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 JavaDoc name, IFile newFile, IFile currentFile) {
34         super(name, newFile);
35         fCurrentFile = currentFile;
36     }
37
38     /*
39      * (non-Javadoc)
40      * @see org.eclipse.ltk.core.refactoring.TextChange#getCurrentDocument(org.eclipse.core.runtime.IProgressMonitor)
41      *
42      * Override getCurrentDocument to return the document of the fCurrentFile instead of the fFile. Since fFile
43      * does not exist, it creates problems when displaying preview information
44      */

45     public IDocument getCurrentDocument(IProgressMonitor pm)
46             throws CoreException {
47         if (pm == null)
48             pm= new NullProgressMonitor();
49         IDocument result= null;
50         pm.beginTask("", 2); //$NON-NLS-1$
51
ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
52         try{
53             IPath path= fCurrentFile.getFullPath();
54             manager.connect(path, pm);
55             ITextFileBuffer buffer = manager.getTextFileBuffer(path);
56             result= buffer.getDocument();
57         } finally {
58             if (result != null)
59                 manager.disconnect(fCurrentFile.getFullPath(), pm);
60         }
61         pm.done();
62         return result;
63     }
64
65 }
66
Popular Tags