upload tizen1.0 source
[sdk/ide/product.git] / org.eclipse.cdt.ui / src / org / eclipse / cdt / internal / ui / editor / WorkingCopyManager.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.cdt.internal.ui.editor;
12
13
14 import java.util.HashMap;
15 import java.util.Map;
16
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;
21
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;
28
29 import org.eclipse.cdt.internal.core.model.CModelManager;
30 import org.eclipse.cdt.internal.core.model.IBufferFactory;
31
32
33 /**
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.
36  */
37 public class WorkingCopyManager implements IWorkingCopyManager, IWorkingCopyManagerExtension {
38         
39         private CDocumentProvider fDocumentProvider;
40         private Map<IEditorInput, IWorkingCopy> fMap;
41         private boolean fIsShuttingDown;
42         private IBufferFactory fBufferFactory;
43
44         /**
45          * Creates a new working copy manager that co-operates with the given
46          * compilation unit document provider.
47          * 
48          * @param provider the provider
49          */
50         public WorkingCopyManager(CDocumentProvider provider) {
51                 Assert.isNotNull(provider);
52                 fDocumentProvider= provider;
53         }
54
55         /*
56          * @see org.eclipse.cdt.ui.IWorkingCopyManager#connect(org.eclipse.ui.IEditorInput)
57          */
58         public void connect(IEditorInput input) throws CoreException {
59                 fDocumentProvider.connect(input);
60         }
61         
62         /*
63          * @see org.eclipse.cdt.ui.IWorkingCopyManager#disconnect(org.eclipse.ui.IEditorInput)
64          */
65         public void disconnect(IEditorInput input) {
66                 fDocumentProvider.disconnect(input);
67         }
68         
69         /*
70          * @see org.eclipse.cdt.ui.IWorkingCopyManager#shutdown()
71          */
72         public void shutdown() {
73                 if (!fIsShuttingDown) {
74                         fIsShuttingDown= true;
75                         try {
76                                 if (fMap != null) {
77                                         fMap.clear();
78                                         fMap= null;
79                                 }
80                                 fDocumentProvider.shutdown();
81                         } finally {
82                                 fIsShuttingDown= false;
83                         }
84                 }
85         }
86
87         /*
88          * @see org.eclipse.cdt.ui.IWorkingCopyManager#getWorkingCopy(org.eclipse.ui.IEditorInput)
89          */
90         public IWorkingCopy getWorkingCopy(IEditorInput input) {
91                 IWorkingCopy unit= fMap == null ? null : fMap.get(input);
92                 return unit != null ? unit : fDocumentProvider.getWorkingCopy(input);
93         }
94         
95         /*
96          * @see org.eclipse.cdt.internal.ui.editor.IWorkingCopyManagerExtension#setWorkingCopy(org.eclipse.ui.IEditorInput, org.eclipse.cdt.core.model.ITranslationUnit)
97          */
98         public void setWorkingCopy(IEditorInput input, IWorkingCopy workingCopy) {
99                 if (fDocumentProvider.getDocument(input) != null) {
100                         if (fMap == null)
101                                 fMap= new HashMap<IEditorInput, IWorkingCopy>();
102                         fMap.put(input, workingCopy);
103                 }
104         }
105         
106         /*
107          * @see org.eclipse.cdt.internal.ui.editor.IWorkingCopyManagerExtension#removeWorkingCopy(org.eclipse.ui.IEditorInput)
108          */
109         public void removeWorkingCopy(IEditorInput input) {
110                 fMap.remove(input);
111                 if (fMap.isEmpty())
112                         fMap= null;
113         }
114
115         public IBufferFactory getBufferFactory() {              
116                 if (fBufferFactory == null) {
117                         synchronized (this) {
118                                 if (fBufferFactory == null)
119                                         fBufferFactory= new CustomBufferFactory();
120                         }
121                 }
122                 return fBufferFactory;
123         }
124
125         public IWorkingCopy findSharedWorkingCopy(ITranslationUnit tu) {
126                 return CModelManager.getDefault().findSharedWorkingCopy(getBufferFactory(), tu);
127         }
128
129         public IWorkingCopy[] getSharedWorkingCopies() {
130                 return CModelManager.getDefault().getSharedWorkingCopies(getBufferFactory());
131         }
132
133         public IWorkingCopy getSharedWorkingCopy(ITranslationUnit original, IProblemRequestor requestor,
134                         IProgressMonitor progressMonitor) throws CModelException {
135                 return CModelManager.getDefault().getSharedWorkingCopy(getBufferFactory(), original, requestor, progressMonitor);
136         }
137 }