1 11 package org.eclipse.team.internal.ui; 12 13 import java.io.InputStream ; 14 15 import org.eclipse.compare.*; 16 import org.eclipse.core.resources.IEncodedStorage; 17 import org.eclipse.core.resources.IStorage; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.jface.text.IDocument; 20 import org.eclipse.swt.graphics.Image; 21 import org.eclipse.team.core.TeamException; 22 import org.eclipse.ui.IEditorInput; 23 import org.eclipse.ui.texteditor.IDocumentProvider; 24 25 public abstract class StorageTypedElement implements ITypedElement, IEncodedStreamContentAccessor, IAdaptable { 26 27 private IStorage bufferedContents; 28 private final String localEncoding; 29 private ISharedDocumentAdapter sharedDocumentAdapter; 30 31 public StorageTypedElement(String localEncoding){ 32 this.localEncoding = localEncoding; 33 } 34 35 public InputStream getContents() throws CoreException { 36 if (bufferedContents == null) { 37 cacheContents(new NullProgressMonitor()); 38 } 39 if (bufferedContents != null) { 40 return bufferedContents.getContents(); 41 } 42 return null; 43 } 44 45 52 public void cacheContents(IProgressMonitor monitor) throws CoreException { 53 bufferedContents = fetchContents(monitor); 54 } 55 56 62 abstract protected IStorage fetchContents(IProgressMonitor monitor) throws CoreException; 63 64 68 public IStorage getBufferedStorage() { 69 return bufferedContents; 70 } 71 72 75 public Image getImage() { 76 return CompareUI.getImage(getType()); 77 } 78 79 82 public String getType() { 83 String name = getName(); 84 if (name != null) { 85 int index = name.lastIndexOf('.'); 86 if (index == -1) 87 return ""; if (index == (name.length() - 1)) 89 return ""; return name.substring(index + 1); 91 } 92 return ITypedElement.FOLDER_TYPE; 93 } 94 95 98 public String getCharset() throws CoreException { 99 if (localEncoding != null) 100 return localEncoding; 101 if (bufferedContents == null) { 102 cacheContents(new NullProgressMonitor()); 103 } 104 if (bufferedContents instanceof IEncodedStorage) { 105 String charset = ((IEncodedStorage)bufferedContents).getCharset(); 106 return charset; 107 } 108 return null; 109 } 110 111 114 public Object getAdapter(Class adapter) { 115 if (adapter == ISharedDocumentAdapter.class) { 116 synchronized (this) { 117 if (sharedDocumentAdapter == null) 118 sharedDocumentAdapter = new SharedDocumentAdapter() { 119 public IEditorInput getDocumentKey(Object element) { 120 return StorageTypedElement.this.getDocumentKey(element); 121 } 122 public void flushDocument(IDocumentProvider provider, 123 IEditorInput documentKey, IDocument document, 124 boolean overwrite) 125 throws CoreException { 126 } 128 }; 129 return sharedDocumentAdapter; 130 } 131 } 132 return Platform.getAdapterManager().getAdapter(this, adapter); 133 } 134 135 140 protected abstract IEditorInput getDocumentKey(Object element); 141 142 public String getLocalEncoding() { 143 return localEncoding; 144 } 145 146 } 147 | Popular Tags |