]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/javaeditor/WorkingCopyManager.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / javaeditor / WorkingCopyManager.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package org.eclipse.jdt.internal.ui.javaeditor;
13
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.CoreException;
20
21 import org.eclipse.ui.IEditorInput;
22
23 import org.eclipse.jdt.core.ICompilationUnit;
24
25 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
26
27 import org.eclipse.jdt.ui.IWorkingCopyManager;
28 import org.eclipse.jdt.ui.IWorkingCopyManagerExtension;
29
30
31 /**
32  * This working copy manager works together with a given compilation unit document provider and
33  * additionally offers to "overwrite" the working copy provided by this document provider.
34  */
35 public class WorkingCopyManager implements IWorkingCopyManager, IWorkingCopyManagerExtension {
36
37         private ICompilationUnitDocumentProvider fDocumentProvider;
38         private Map<IEditorInput, ICompilationUnit> fMap;
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 the provider
46          */
47         public WorkingCopyManager(ICompilationUnitDocumentProvider provider) {
48                 Assert.isNotNull(provider);
49                 fDocumentProvider= provider;
50         }
51
52         /*
53          * @see org.eclipse.jdt.ui.IWorkingCopyManager#connect(org.eclipse.ui.IEditorInput)
54          */
55         public void connect(IEditorInput input) throws CoreException {
56                 fDocumentProvider.connect(input);
57         }
58
59         /*
60          * @see org.eclipse.jdt.ui.IWorkingCopyManager#disconnect(org.eclipse.ui.IEditorInput)
61          */
62         public void disconnect(IEditorInput input) {
63                 fDocumentProvider.disconnect(input);
64         }
65
66         /*
67          * @see org.eclipse.jdt.ui.IWorkingCopyManager#shutdown()
68          */
69         public void shutdown() {
70                 if (!fIsShuttingDown) {
71                         fIsShuttingDown= true;
72                         try {
73                                 if (fMap != null) {
74                                         fMap.clear();
75                                         fMap= null;
76                                 }
77                                 fDocumentProvider.shutdown();
78                         } finally {
79                                 fIsShuttingDown= false;
80                         }
81                 }
82         }
83
84         /*
85          * @see org.eclipse.jdt.ui.IWorkingCopyManager#getWorkingCopy(org.eclipse.ui.IEditorInput)
86          */
87         public ICompilationUnit getWorkingCopy(IEditorInput input) {
88                 return getWorkingCopy(input, true);
89         }
90
91         /**
92          * Returns the working copy remembered for the compilation unit encoded in the
93          * given editor input.
94          * <p>
95          * Note: This method must not be part of the public {@link IWorkingCopyManager} API.
96          * </p>
97          *
98          * @param input the editor input
99          * @param primaryOnly if <code>true</code> only primary working copies will be returned
100          * @return the working copy of the compilation unit, or <code>null</code> if the
101          *   input does not encode an editor input, or if there is no remembered working
102          *   copy for this compilation unit
103          * @since 3.2
104          */
105         public ICompilationUnit getWorkingCopy(IEditorInput input, boolean primaryOnly) {
106                 ICompilationUnit unit= fMap == null ? null : fMap.get(input);
107                 if (unit == null)
108                         unit= fDocumentProvider.getWorkingCopy(input);
109                 if (unit != null && (!primaryOnly || JavaModelUtil.isPrimary(unit)))
110                         return unit;
111                 return null;
112         }
113
114         /*
115          * @see org.eclipse.jdt.internal.ui.javaeditor.IWorkingCopyManagerExtension#setWorkingCopy(org.eclipse.ui.IEditorInput, org.eclipse.jdt.core.ICompilationUnit)
116          */
117         public void setWorkingCopy(IEditorInput input, ICompilationUnit workingCopy) {
118                 if (fDocumentProvider.getDocument(input) != null) {
119                         if (fMap == null)
120                                 fMap= new HashMap<IEditorInput, ICompilationUnit>();
121                         fMap.put(input, workingCopy);
122                 }
123         }
124
125         /*
126          * @see org.eclipse.jdt.internal.ui.javaeditor.IWorkingCopyManagerExtension#removeWorkingCopy(org.eclipse.ui.IEditorInput)
127          */
128         public void removeWorkingCopy(IEditorInput input) {
129                 fMap.remove(input);
130                 if (fMap.isEmpty())
131                         fMap= null;
132         }
133 }