Eclipse 3M7
[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 org.eclipse.core.runtime.CoreException;
19
20 import org.eclipse.jface.text.Assert;
21
22 import org.eclipse.ui.IEditorInput;
23
24 import net.sourceforge.phpdt.core.ICompilationUnit;
25
26 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
27 import net.sourceforge.phpdt.ui.IWorkingCopyManagerExtension;
28
29
30 /**
31  * This working copy manager works together with a given compilation unit document provider and
32  * additionally offers to "overwrite" the working copy provided by this document provider.
33  */
34 public class WorkingCopyManager implements IWorkingCopyManager, IWorkingCopyManagerExtension {
35         
36         private PHPDocumentProvider fDocumentProvider;
37         private Map fMap;
38         private boolean fIsShuttingDown;
39
40         /**
41          * Creates a new working copy manager that co-operates with the given
42          * compilation unit document provider.
43          * 
44          * @param provider the provider
45          */
46         public WorkingCopyManager(PHPDocumentProvider provider) {
47                 Assert.isNotNull(provider);
48                 fDocumentProvider= provider;
49         }
50
51         /*
52          * @see org.eclipse.jdt.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 org.eclipse.jdt.ui.IWorkingCopyManager#disconnect(org.eclipse.ui.IEditorInput)
60          */
61         public void disconnect(IEditorInput input) {
62                 fDocumentProvider.disconnect(input);
63         }
64         
65         /*
66          * @see org.eclipse.jdt.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 org.eclipse.jdt.ui.IWorkingCopyManager#getWorkingCopy(org.eclipse.ui.IEditorInput)
85          */
86         public ICompilationUnit getWorkingCopy(IEditorInput input) {
87                 ICompilationUnit unit= fMap == null ? null : (ICompilationUnit) fMap.get(input);
88                 return unit != null ? unit : fDocumentProvider.getWorkingCopy(input);
89         }
90         
91         /*
92          * @see org.eclipse.jdt.internal.ui.javaeditor.IWorkingCopyManagerExtension#setWorkingCopy(org.eclipse.ui.IEditorInput, org.eclipse.jdt.core.ICompilationUnit)
93          */
94         public void setWorkingCopy(IEditorInput input, ICompilationUnit workingCopy) {
95                 if (fDocumentProvider.isConnected(input)) {
96                         if (fMap == null)
97                                 fMap= new HashMap();
98                         fMap.put(input, workingCopy);
99                 }
100         }
101         
102         /*
103          * @see org.eclipse.jdt.internal.ui.javaeditor.IWorkingCopyManagerExtension#removeWorkingCopy(org.eclipse.ui.IEditorInput)
104          */
105         public void removeWorkingCopy(IEditorInput input) {
106                 fMap.remove(input);
107                 if (fMap.isEmpty())
108                         fMap= null;
109         }
110 }