Merge branch 'master' of ssh://git.phpeclipse.com/phpeclipse
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / WorkingCopyManager.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpeclipse.phpeditor;
13
14 import java.util.HashMap;
15 import java.util.Map;
16
17 import net.sourceforge.phpdt.core.ICompilationUnit;
18 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
19 import net.sourceforge.phpdt.ui.IWorkingCopyManagerExtension;
20
21 import org.eclipse.core.runtime.CoreException;
22 //incastrix
23 //import org.eclipse.jface.text.Assert;
24 import org.eclipse.core.runtime.Assert;
25 import org.eclipse.ui.IEditorInput;
26
27 /**
28  * This working copy manager works together with a given compilation unit
29  * document provider and additionally offers to "overwrite" the working copy
30  * provided by this document provider.
31  */
32 public class WorkingCopyManager implements IWorkingCopyManager,
33                 IWorkingCopyManagerExtension {
34
35         private ICompilationUnitDocumentProvider fDocumentProvider;
36
37         private Map fMap;
38
39         private boolean fIsShuttingDown;
40
41         /**
42          * Creates a new working copy manager that co-operates with the given
43          * compilation unit document provider.
44          * 
45          * @param provider
46          *            the provider
47          */
48         public WorkingCopyManager(ICompilationUnitDocumentProvider provider) {
49                 Assert.isNotNull(provider);
50                 fDocumentProvider = provider;
51         }
52
53         /*
54          * @see net.sourceforge.phpdt.ui.IWorkingCopyManager#connect(org.eclipse.ui.IEditorInput)
55          */
56         public void connect(IEditorInput input) throws CoreException {
57                 fDocumentProvider.connect(input);
58         }
59
60         /*
61          * @see net.sourceforge.phpdt.ui.IWorkingCopyManager#disconnect(org.eclipse.ui.IEditorInput)
62          */
63         public void disconnect(IEditorInput input) {
64                 fDocumentProvider.disconnect(input);
65         }
66
67         /*
68          * @see net.sourceforge.phpdt.ui.IWorkingCopyManager#shutdown()
69          */
70         public void shutdown() {
71                 if (!fIsShuttingDown) {
72                         fIsShuttingDown = true;
73                         try {
74                                 if (fMap != null) {
75                                         fMap.clear();
76                                         fMap = null;
77                                 }
78                                 fDocumentProvider.shutdown();
79                         } finally {
80                                 fIsShuttingDown = false;
81                         }
82                 }
83         }
84
85         /*
86          * @see net.sourceforge.phpdt.ui.IWorkingCopyManager#getWorkingCopy(org.eclipse.ui.IEditorInput)
87          */
88         public ICompilationUnit getWorkingCopy(IEditorInput input) {
89                 ICompilationUnit unit = fMap == null ? null : (ICompilationUnit) fMap
90                                 .get(input);
91                 return unit != null ? unit : fDocumentProvider.getWorkingCopy(input);
92         }
93
94         /*
95          * @see net.sourceforge.phpdt.internal.ui.javaeditor.IWorkingCopyManagerExtension#setWorkingCopy(org.eclipse.ui.IEditorInput,
96          *      net.sourceforge.phpdt.core.ICompilationUnit)
97          */
98         public void setWorkingCopy(IEditorInput input, ICompilationUnit workingCopy) {
99                 if (fDocumentProvider.getDocument(input) != null) {
100                         if (fMap == null)
101                                 fMap = new HashMap();
102                         fMap.put(input, workingCopy);
103                 }
104         }
105
106         /*
107          * @see net.sourceforge.phpdt.internal.ui.javaeditor.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 }