1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package org.eclipse.jdt.internal.ui.javaeditor;
15 import java.util.HashMap;
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.ui.IEditorInput;
23 import org.eclipse.jdt.core.ICompilationUnit;
25 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
27 import org.eclipse.jdt.ui.IWorkingCopyManager;
28 import org.eclipse.jdt.ui.IWorkingCopyManagerExtension;
32 * This working copy manager works together with a given compilation unit document provider and
33 * additionally offers to "overwrite" the working copy provided by this document provider.
35 public class WorkingCopyManager implements IWorkingCopyManager, IWorkingCopyManagerExtension {
37 private ICompilationUnitDocumentProvider fDocumentProvider;
38 private Map<IEditorInput, ICompilationUnit> fMap;
39 private boolean fIsShuttingDown;
42 * Creates a new working copy manager that co-operates with the given
43 * compilation unit document provider.
45 * @param provider the provider
47 public WorkingCopyManager(ICompilationUnitDocumentProvider provider) {
48 Assert.isNotNull(provider);
49 fDocumentProvider= provider;
53 * @see org.eclipse.jdt.ui.IWorkingCopyManager#connect(org.eclipse.ui.IEditorInput)
55 public void connect(IEditorInput input) throws CoreException {
56 fDocumentProvider.connect(input);
60 * @see org.eclipse.jdt.ui.IWorkingCopyManager#disconnect(org.eclipse.ui.IEditorInput)
62 public void disconnect(IEditorInput input) {
63 fDocumentProvider.disconnect(input);
67 * @see org.eclipse.jdt.ui.IWorkingCopyManager#shutdown()
69 public void shutdown() {
70 if (!fIsShuttingDown) {
71 fIsShuttingDown= true;
77 fDocumentProvider.shutdown();
79 fIsShuttingDown= false;
85 * @see org.eclipse.jdt.ui.IWorkingCopyManager#getWorkingCopy(org.eclipse.ui.IEditorInput)
87 public ICompilationUnit getWorkingCopy(IEditorInput input) {
88 return getWorkingCopy(input, true);
92 * Returns the working copy remembered for the compilation unit encoded in the
95 * Note: This method must not be part of the public {@link IWorkingCopyManager} API.
98 * @param input the editor input
99 * @param primaryOnly if <code>true</code> only primary working copies will be returned
100 * @return the working copy of the compilation unit, or <code>null</code> if the
101 * input does not encode an editor input, or if there is no remembered working
102 * copy for this compilation unit
105 public ICompilationUnit getWorkingCopy(IEditorInput input, boolean primaryOnly) {
106 ICompilationUnit unit= fMap == null ? null : fMap.get(input);
108 unit= fDocumentProvider.getWorkingCopy(input);
109 if (unit != null && (!primaryOnly || JavaModelUtil.isPrimary(unit)))
115 * @see org.eclipse.jdt.internal.ui.javaeditor.IWorkingCopyManagerExtension#setWorkingCopy(org.eclipse.ui.IEditorInput, org.eclipse.jdt.core.ICompilationUnit)
117 public void setWorkingCopy(IEditorInput input, ICompilationUnit workingCopy) {
118 if (fDocumentProvider.getDocument(input) != null) {
120 fMap= new HashMap<IEditorInput, ICompilationUnit>();
121 fMap.put(input, workingCopy);
126 * @see org.eclipse.jdt.internal.ui.javaeditor.IWorkingCopyManagerExtension#removeWorkingCopy(org.eclipse.ui.IEditorInput)
128 public void removeWorkingCopy(IEditorInput input) {