KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > WorkingCopyOwner


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.core;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.Path;
15 import org.eclipse.jdt.internal.core.BufferManager;
16 import org.eclipse.jdt.internal.core.CompilationUnit;
17 import org.eclipse.jdt.internal.core.DefaultWorkingCopyOwner;
18 import org.eclipse.jdt.internal.core.ExternalJavaProject;
19 import org.eclipse.jdt.internal.core.PackageFragment;
20
21 /**
22  * The owner of an {@link ICompilationUnit} handle in working copy mode.
23  * An owner is used to identify a working copy and to create its buffer.
24  * <p>
25  * Clients should subclass this class to instantiate a working copy owner that is specific to their need and that
26  * they can pass in to various APIs (e.g. {@link IType#resolveType(String, WorkingCopyOwner)}.
27  * Clients can also override the default implementation of {@link #createBuffer(ICompilationUnit)}.
28  * </p><p>
29  * Note: even though this class has no abstract method, which means that it provides functional default behavior,
30  * it is still an abstract class, as clients are intended to own their owner implementation.
31  * </p>
32  * @see ICompilationUnit#becomeWorkingCopy(org.eclipse.core.runtime.IProgressMonitor)
33  * @see ICompilationUnit#discardWorkingCopy()
34  * @see ICompilationUnit#getWorkingCopy(org.eclipse.core.runtime.IProgressMonitor)
35  * @since 3.0
36  */

37 public abstract class WorkingCopyOwner {
38
39     /**
40      * Sets the buffer provider of the primary working copy owner. Note that even if the
41      * buffer provider is a working copy owner, only its <code>createBuffer(ICompilationUnit)</code>
42      * method is used by the primary working copy owner. It doesn't replace the internal primary
43      * working owner.
44      * <p>
45      * This method is for internal use by the jdt-related plug-ins.
46      * Clients outside of the jdt should not reference this method.
47      * </p>
48      *
49      * @param primaryBufferProvider the primary buffer provider
50      */

51     public static void setPrimaryBufferProvider(WorkingCopyOwner primaryBufferProvider) {
52         DefaultWorkingCopyOwner.PRIMARY.primaryBufferProvider = primaryBufferProvider;
53     }
54
55     /**
56      * Creates a buffer for the given working copy.
57      * The new buffer will be initialized with the contents of the underlying file
58      * if and only if it was not already initialized by the compilation owner (a buffer is
59      * uninitialized if its content is <code>null</code>).
60      * <p>
61      * Note: This buffer will be associated to the working copy for its entire life-cycle. Another
62      * working copy on same unit but owned by a different owner would not share the same buffer
63      * unless its owner decided to implement such a sharing behaviour.
64      * </p>
65      *
66      * @param workingCopy the working copy of the buffer
67      * @return IBuffer the created buffer for the given working copy
68      * @see IBuffer
69      */

70     public IBuffer createBuffer(ICompilationUnit workingCopy) {
71
72         return BufferManager.createBuffer(workingCopy);
73     }
74
75     /**
76      * Returns the problem requestor used by a working copy of this working copy owner.
77      * <p>
78      * By default, no problem requestor is configured. Clients can override this
79      * method to provide a requestor.
80      * </p>
81      *
82      * @param workingCopy The problem requestor used for the given working copy.
83      * @return the problem requestor to be used by working copies of this working
84      * copy owner or <code>null</code> if no problem requestor is configured.
85      *
86      * @since 3.3
87      */

88     public IProblemRequestor getProblemRequestor(ICompilationUnit workingCopy) {
89         return null;
90     }
91
92     /**
93      * Returns a new working copy with the given name using this working copy owner to
94      * create its buffer.
95      * <p>
96      * This working copy always belongs to the default package in a package
97      * fragment root that corresponds to its Java project, and this Java project never exists.
98      * However this Java project has the given classpath that is used when resolving names
99      * in this working copy.
100      * </p><p>
101      * A DOM AST created using this working copy will have bindings resolved using the given
102      * classpath, and problem are reported to the given problem requestor.
103      * <p></p>
104      * <code>JavaCore#getOptions()</code> is used to create the DOM AST as it is not
105      * possible to set the options on the non-existing Java project.
106      * </p><p>
107      * When the working copy instance is created, an {@link IJavaElementDelta#ADDED added delta} is
108      * reported on this working copy.
109      * </p><p>
110      * Once done with the working copy, users of this method must discard it using
111      * {@link ICompilationUnit#discardWorkingCopy()}.
112      * </p><p>
113      * Note that when such working copy is committed, only its buffer is saved (see
114      * {@link IBuffer#save(IProgressMonitor, boolean)}) but no resource is created.
115      * </p><p>
116      * This method is not intended to be overriden by clients.
117      * </p>
118      *
119      * @param name the name of the working copy (e.g. "X.java")
120      * @param classpath the classpath used to resolve names in this working copy
121      * @param problemRequestor a requestor which will get notified of problems detected during
122      * reconciling as they are discovered. The requestor can be set to <code>null</code> indicating
123      * that the client is not interested in problems.
124      * @param monitor a progress monitor used to report progress while opening the working copy
125      * or <code>null</code> if no progress should be reported
126      * @throws JavaModelException if the contents of this working copy can
127      * not be determined.
128      * @return a new working copy
129      * @see ICompilationUnit#becomeWorkingCopy(IProblemRequestor, IProgressMonitor)
130      * @since 3.2
131      *
132      * @deprecated Use {@link #newWorkingCopy(String, IClasspathEntry[], IProgressMonitor)} instead.
133      * Note that if this deprecated method is used, problems may be reported twice
134      * if the given requestor is not the same as the current working copy owner one.
135      */

136     public final ICompilationUnit newWorkingCopy(String JavaDoc name, IClasspathEntry[] classpath, IProblemRequestor problemRequestor, IProgressMonitor monitor) throws JavaModelException {
137         ExternalJavaProject project = new ExternalJavaProject(classpath);
138         IPackageFragment parent = project.getPackageFragmentRoot(Path.EMPTY).getPackageFragment(IPackageFragment.DEFAULT_PACKAGE_NAME);
139         CompilationUnit result = new CompilationUnit((PackageFragment) parent, name, this);
140         result.becomeWorkingCopy(problemRequestor, monitor);
141         return result;
142     }
143
144     /**
145      * Returns a new working copy with the given name using this working copy owner to
146      * create its buffer.
147      * <p>
148      * This working copy always belongs to the default package in a package
149      * fragment root that corresponds to its Java project, and this Java project never exists.
150      * However this Java project has the given classpath that is used when resolving names
151      * in this working copy.
152      * </p><p>
153      * If a DOM AST is created using this working copy, then given classpath will be used
154      * if bindings need to be resolved. Problems will be reported to the problem requestor
155      * of the current working copy owner problem if it is not <code>null</code>.
156      * <p></p>
157      * Options used to create the DOM AST are got from {@link JavaCore#getOptions()}
158      * as it is not possible to set the options on a non-existing Java project.
159      * </p><p>
160      * When the working copy instance is created, an {@link IJavaElementDelta#ADDED added delta} is
161      * reported on this working copy.
162      * </p><p>
163      * Once done with the working copy, users of this method must discard it using
164      * {@link ICompilationUnit#discardWorkingCopy()}.
165      * </p><p>
166      * Note that when such working copy is committed, only its buffer is saved (see
167      * {@link IBuffer#save(IProgressMonitor, boolean)}) but no resource is created.
168      * </p><p>
169      * This method is not intended to be overriden by clients.
170      * </p>
171      *
172      * @param name the name of the working copy (e.g. "X.java")
173      * @param classpath the classpath used to resolve names in this working copy
174      * @param monitor a progress monitor used to report progress while opening the working copy
175      * or <code>null</code> if no progress should be reported
176      * @throws JavaModelException if the contents of this working copy can
177      * not be determined.
178      * @return a new working copy
179      * @see ICompilationUnit#becomeWorkingCopy(IProgressMonitor)
180      *
181      * @since 3.3
182      */

183     public final ICompilationUnit newWorkingCopy(String JavaDoc name, IClasspathEntry[] classpath, IProgressMonitor monitor) throws JavaModelException {
184         ExternalJavaProject project = new ExternalJavaProject(classpath);
185         IPackageFragment parent = project.getPackageFragmentRoot(Path.EMPTY).getPackageFragment(IPackageFragment.DEFAULT_PACKAGE_NAME);
186         CompilationUnit result = new CompilationUnit((PackageFragment) parent, name, this);
187         result.becomeWorkingCopy(getProblemRequestor(result), monitor);
188         return result;
189     }
190
191 }
192
Popular Tags