inital
[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
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import net.sourceforge.phpdt.core.ICompilationUnit;
19 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
20 import net.sourceforge.phpdt.ui.IWorkingCopyManagerExtension;
21
22 import org.eclipse.core.runtime.CoreException;
23 import net.sourceforge.phpeclipse.phpeditor.ICompilationUnitDocumentProvider;
24 import org.eclipse.jface.text.Assert;
25 import org.eclipse.ui.IEditorInput;
26
27
28 /**
29  * This working copy manager works together with a given compilation unit document provider and
30  * additionally offers to "overwrite" the working copy provided by this document provider.
31  */
32 public class WorkingCopyManager implements IWorkingCopyManager, IWorkingCopyManagerExtension {
33         
34         private ICompilationUnitDocumentProvider fDocumentProvider;
35         private Map fMap;
36         private boolean fIsShuttingDown;
37
38         /**
39          * Creates a new working copy manager that co-operates with the given
40          * compilation unit document provider.
41          * 
42          * @param provider the provider
43          */
44         public WorkingCopyManager(ICompilationUnitDocumentProvider provider) {
45                 Assert.isNotNull(provider);
46                 fDocumentProvider= provider;
47         }
48
49         /*
50          * @see org.eclipse.jdt.ui.IWorkingCopyManager#connect(org.eclipse.ui.IEditorInput)
51          */
52         public void connect(IEditorInput input) throws CoreException {
53                 fDocumentProvider.connect(input);
54         }
55         
56         /*
57          * @see org.eclipse.jdt.ui.IWorkingCopyManager#disconnect(org.eclipse.ui.IEditorInput)
58          */
59         public void disconnect(IEditorInput input) {
60                 fDocumentProvider.disconnect(input);
61         }
62         
63         /*
64          * @see org.eclipse.jdt.ui.IWorkingCopyManager#shutdown()
65          */
66         public void shutdown() {
67                 if (!fIsShuttingDown) {
68                         fIsShuttingDown= true;
69                         try {
70                                 if (fMap != null) {
71                                         fMap.clear();
72                                         fMap= null;
73                                 }
74                                 fDocumentProvider.shutdown();
75                         } finally {
76                                 fIsShuttingDown= false;
77                         }
78                 }
79         }
80
81         /*
82          * @see org.eclipse.jdt.ui.IWorkingCopyManager#getWorkingCopy(org.eclipse.ui.IEditorInput)
83          */
84         public ICompilationUnit getWorkingCopy(IEditorInput input) {
85                 ICompilationUnit unit= fMap == null ? null : (ICompilationUnit) fMap.get(input);
86                 return unit != null ? unit : fDocumentProvider.getWorkingCopy(input);
87         }
88         
89         /*
90          * @see org.eclipse.jdt.internal.ui.javaeditor.IWorkingCopyManagerExtension#setWorkingCopy(org.eclipse.ui.IEditorInput, org.eclipse.jdt.core.ICompilationUnit)
91          */
92         public void setWorkingCopy(IEditorInput input, ICompilationUnit workingCopy) {
93                 if (fDocumentProvider.getDocument(input) != null) {
94                         if (fMap == null)
95                                 fMap= new HashMap();
96                         fMap.put(input, workingCopy);
97                 }
98         }
99         /*
100          * @see org.eclipse.jdt.internal.ui.javaeditor.IWorkingCopyManagerExtension#removeWorkingCopy(org.eclipse.ui.IEditorInput)
101          */
102         public void removeWorkingCopy(IEditorInput input) {
103                 fMap.remove(input);
104                 if (fMap.isEmpty())
105                         fMap= null;
106         }
107 }