1 /*******************************************************************************
2 * Copyright (c) 2000, 2009 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 *******************************************************************************/
11 package org.eclipse.cdt.internal.ui.editor;
14 import java.util.HashMap;
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.ui.IEditorInput;
22 import org.eclipse.cdt.core.model.CModelException;
23 import org.eclipse.cdt.core.model.IProblemRequestor;
24 import org.eclipse.cdt.core.model.ITranslationUnit;
25 import org.eclipse.cdt.core.model.IWorkingCopy;
26 import org.eclipse.cdt.ui.IWorkingCopyManager;
27 import org.eclipse.cdt.ui.IWorkingCopyManagerExtension;
29 import org.eclipse.cdt.internal.core.model.CModelManager;
30 import org.eclipse.cdt.internal.core.model.IBufferFactory;
34 * This working copy manager works together with a given compilation unit document provider and
35 * additionally offers to "overwrite" the working copy provided by this document provider.
37 public class WorkingCopyManager implements IWorkingCopyManager, IWorkingCopyManagerExtension {
39 private CDocumentProvider fDocumentProvider;
40 private Map<IEditorInput, IWorkingCopy> fMap;
41 private boolean fIsShuttingDown;
42 private IBufferFactory fBufferFactory;
45 * Creates a new working copy manager that co-operates with the given
46 * compilation unit document provider.
48 * @param provider the provider
50 public WorkingCopyManager(CDocumentProvider provider) {
51 Assert.isNotNull(provider);
52 fDocumentProvider= provider;
56 * @see org.eclipse.cdt.ui.IWorkingCopyManager#connect(org.eclipse.ui.IEditorInput)
58 public void connect(IEditorInput input) throws CoreException {
59 fDocumentProvider.connect(input);
63 * @see org.eclipse.cdt.ui.IWorkingCopyManager#disconnect(org.eclipse.ui.IEditorInput)
65 public void disconnect(IEditorInput input) {
66 fDocumentProvider.disconnect(input);
70 * @see org.eclipse.cdt.ui.IWorkingCopyManager#shutdown()
72 public void shutdown() {
73 if (!fIsShuttingDown) {
74 fIsShuttingDown= true;
80 fDocumentProvider.shutdown();
82 fIsShuttingDown= false;
88 * @see org.eclipse.cdt.ui.IWorkingCopyManager#getWorkingCopy(org.eclipse.ui.IEditorInput)
90 public IWorkingCopy getWorkingCopy(IEditorInput input) {
91 IWorkingCopy unit= fMap == null ? null : fMap.get(input);
92 return unit != null ? unit : fDocumentProvider.getWorkingCopy(input);
96 * @see org.eclipse.cdt.internal.ui.editor.IWorkingCopyManagerExtension#setWorkingCopy(org.eclipse.ui.IEditorInput, org.eclipse.cdt.core.model.ITranslationUnit)
98 public void setWorkingCopy(IEditorInput input, IWorkingCopy workingCopy) {
99 if (fDocumentProvider.getDocument(input) != null) {
101 fMap= new HashMap<IEditorInput, IWorkingCopy>();
102 fMap.put(input, workingCopy);
107 * @see org.eclipse.cdt.internal.ui.editor.IWorkingCopyManagerExtension#removeWorkingCopy(org.eclipse.ui.IEditorInput)
109 public void removeWorkingCopy(IEditorInput input) {
115 public IBufferFactory getBufferFactory() {
116 if (fBufferFactory == null) {
117 synchronized (this) {
118 if (fBufferFactory == null)
119 fBufferFactory= new CustomBufferFactory();
122 return fBufferFactory;
125 public IWorkingCopy findSharedWorkingCopy(ITranslationUnit tu) {
126 return CModelManager.getDefault().findSharedWorkingCopy(getBufferFactory(), tu);
129 public IWorkingCopy[] getSharedWorkingCopies() {
130 return CModelManager.getDefault().getSharedWorkingCopies(getBufferFactory());
133 public IWorkingCopy getSharedWorkingCopy(ITranslationUnit original, IProblemRequestor requestor,
134 IProgressMonitor progressMonitor) throws CModelException {
135 return CModelManager.getDefault().getSharedWorkingCopy(getBufferFactory(), original, requestor, progressMonitor);