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