KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > WorkingCopyManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.CoreException;
20
21
22 import org.eclipse.ui.IEditorInput;
23
24 import org.eclipse.jdt.core.ICompilationUnit;
25
26 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
27
28 import org.eclipse.jdt.ui.IWorkingCopyManager;
29 import org.eclipse.jdt.ui.IWorkingCopyManagerExtension;
30
31
32 /**
33  * This working copy manager works together with a given compilation unit document provider and
34  * additionally offers to "overwrite" the working copy provided by this document provider.
35  */

36 public class WorkingCopyManager implements IWorkingCopyManager, IWorkingCopyManagerExtension {
37
38     private ICompilationUnitDocumentProvider fDocumentProvider;
39     private Map JavaDoc fMap;
40     private boolean fIsShuttingDown;
41
42     /**
43      * Creates a new working copy manager that co-operates with the given
44      * compilation unit document provider.
45      *
46      * @param provider the provider
47      */

48     public WorkingCopyManager(ICompilationUnitDocumentProvider provider) {
49         Assert.isNotNull(provider);
50         fDocumentProvider= provider;
51     }
52
53     /*
54      * @see org.eclipse.jdt.ui.IWorkingCopyManager#connect(org.eclipse.ui.IEditorInput)
55      */

56     public void connect(IEditorInput input) throws CoreException {
57         fDocumentProvider.connect(input);
58     }
59
60     /*
61      * @see org.eclipse.jdt.ui.IWorkingCopyManager#disconnect(org.eclipse.ui.IEditorInput)
62      */

63     public void disconnect(IEditorInput input) {
64         fDocumentProvider.disconnect(input);
65     }
66
67     /*
68      * @see org.eclipse.jdt.ui.IWorkingCopyManager#shutdown()
69      */

70     public void shutdown() {
71         if (!fIsShuttingDown) {
72             fIsShuttingDown= true;
73             try {
74                 if (fMap != null) {
75                     fMap.clear();
76                     fMap= null;
77                 }
78                 fDocumentProvider.shutdown();
79             } finally {
80                 fIsShuttingDown= false;
81             }
82         }
83     }
84
85     /*
86      * @see org.eclipse.jdt.ui.IWorkingCopyManager#getWorkingCopy(org.eclipse.ui.IEditorInput)
87      */

88     public ICompilationUnit getWorkingCopy(IEditorInput input) {
89         return getWorkingCopy(input, true);
90     }
91
92     /**
93      * Returns the working copy remembered for the compilation unit encoded in the
94      * given editor input.
95      * <p>
96      * Note: This method must not be part of the public {@link IWorkingCopyManager} API.
97      * </p>
98      *
99      * @param input the editor input
100      * @param primaryOnly if <code>true</code> only primary working copies will be returned
101      * @return the working copy of the compilation unit, or <code>null</code> if the
102      * input does not encode an editor input, or if there is no remembered working
103      * copy for this compilation unit
104      * @since 3.2
105      */

106     public ICompilationUnit getWorkingCopy(IEditorInput input, boolean primaryOnly) {
107         ICompilationUnit unit= fMap == null ? null : (ICompilationUnit) fMap.get(input);
108         if (unit == null)
109             unit= fDocumentProvider.getWorkingCopy(input);
110         if (unit != null && (!primaryOnly || JavaModelUtil.isPrimary(unit)))
111             return unit;
112         return null;
113     }
114
115     /*
116      * @see org.eclipse.jdt.internal.ui.javaeditor.IWorkingCopyManagerExtension#setWorkingCopy(org.eclipse.ui.IEditorInput, org.eclipse.jdt.core.ICompilationUnit)
117      */

118     public void setWorkingCopy(IEditorInput input, ICompilationUnit workingCopy) {
119         if (fDocumentProvider.getDocument(input) != null) {
120             if (fMap == null)
121                 fMap= new HashMap JavaDoc();
122             fMap.put(input, workingCopy);
123         }
124     }
125
126     /*
127      * @see org.eclipse.jdt.internal.ui.javaeditor.IWorkingCopyManagerExtension#removeWorkingCopy(org.eclipse.ui.IEditorInput)
128      */

129     public void removeWorkingCopy(IEditorInput input) {
130         fMap.remove(input);
131         if (fMap.isEmpty())
132             fMap= null;
133     }
134 }
135
Popular Tags