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