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