1 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 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 36 public class WorkingCopyManager implements IWorkingCopyManager, IWorkingCopyManagerExtension { 37 38 private ICompilationUnitDocumentProvider fDocumentProvider; 39 private Map fMap; 40 private boolean fIsShuttingDown; 41 42 48 public WorkingCopyManager(ICompilationUnitDocumentProvider provider) { 49 Assert.isNotNull(provider); 50 fDocumentProvider= provider; 51 } 52 53 56 public void connect(IEditorInput input) throws CoreException { 57 fDocumentProvider.connect(input); 58 } 59 60 63 public void disconnect(IEditorInput input) { 64 fDocumentProvider.disconnect(input); 65 } 66 67 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 88 public ICompilationUnit getWorkingCopy(IEditorInput input) { 89 return getWorkingCopy(input, true); 90 } 91 92 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 118 public void setWorkingCopy(IEditorInput input, ICompilationUnit workingCopy) { 119 if (fDocumentProvider.getDocument(input) != null) { 120 if (fMap == null) 121 fMap= new HashMap (); 122 fMap.put(input, workingCopy); 123 } 124 } 125 126 129 public void removeWorkingCopy(IEditorInput input) { 130 fMap.remove(input); 131 if (fMap.isEmpty()) 132 fMap= null; 133 } 134 } 135 | Popular Tags |