01: /*******************************************************************************
02: * Copyright (c) 2000, 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.jdt.internal.corext.refactoring.util;
11:
12: import org.eclipse.core.runtime.Assert;
13: import org.eclipse.core.runtime.CoreException;
14: import org.eclipse.core.runtime.IPath;
15: import org.eclipse.core.runtime.NullProgressMonitor;
16:
17: import org.eclipse.core.resources.IResource;
18:
19: import org.eclipse.core.filebuffers.FileBuffers;
20: import org.eclipse.core.filebuffers.ITextFileBuffer;
21: import org.eclipse.core.filebuffers.LocationKind;
22:
23: import org.eclipse.jdt.core.ICompilationUnit;
24:
25: /**
26: * Helper methods to deal with file buffers in refactorings.
27: */
28: public final class RefactoringFileBuffers {
29:
30: /**
31: * Connects to and acquires a text file buffer for the specified compilation unit.
32: * <p>
33: * All text file buffers acquired by a call to {@link RefactoringFileBuffers#acquire(ICompilationUnit)}
34: * must be released using {@link RefactoringFileBuffers#release(ICompilationUnit)}.
35: * </p>
36: *
37: * @param unit the compilation unit to acquire a text file buffer for
38: * @return the text file buffer, or <code>null</code> if no buffer could be acquired
39: * @throws CoreException if no buffer could be acquired
40: */
41: public static ITextFileBuffer acquire(final ICompilationUnit unit)
42: throws CoreException {
43: Assert.isNotNull(unit);
44: final IResource resource = unit.getResource();
45: if (resource != null && resource.getType() == IResource.FILE) {
46: final IPath path = resource.getFullPath();
47: FileBuffers.getTextFileBufferManager().connect(path,
48: LocationKind.IFILE, new NullProgressMonitor());
49: return FileBuffers.getTextFileBufferManager()
50: .getTextFileBuffer(path, LocationKind.IFILE);
51: }
52: return null;
53: }
54:
55: /**
56: * Returns the text file buffer for the specified compilation unit.
57: *
58: * @param unit the compilation unit whose text file buffer to retrieve
59: * @return the associated text file buffer, or <code>null</code> if no text file buffer is managed for the compilation unit
60: */
61: public static ITextFileBuffer getTextFileBuffer(
62: final ICompilationUnit unit) {
63: Assert.isNotNull(unit);
64: final IResource resource = unit.getResource();
65: if (resource == null || resource.getType() != IResource.FILE)
66: return null;
67: return FileBuffers.getTextFileBufferManager()
68: .getTextFileBuffer(resource.getFullPath(),
69: LocationKind.IFILE);
70: }
71:
72: /**
73: * Releases the text file buffer associated with the compilation unit.
74: *
75: * @param unit the compilation unit whose text file buffer has to be released
76: * @throws CoreException if the buffer could not be successfully released
77: */
78: public static void release(final ICompilationUnit unit)
79: throws CoreException {
80: Assert.isNotNull(unit);
81: final IResource resource = unit.getResource();
82: if (resource != null && resource.getType() == IResource.FILE)
83: FileBuffers.getTextFileBufferManager().disconnect(
84: resource.getFullPath(), LocationKind.IFILE,
85: new NullProgressMonitor());
86: }
87:
88: private RefactoringFileBuffers() {
89: // Not for instantiation
90: }
91: }
|