KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > StorageTypedElement


1 /*******************************************************************************
2  * Copyright (c) 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 package org.eclipse.team.internal.ui;
12
13 import java.io.InputStream JavaDoc;
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 JavaDoc localEncoding;
29     private ISharedDocumentAdapter sharedDocumentAdapter;
30     
31     public StorageTypedElement(String JavaDoc localEncoding){
32         this.localEncoding = localEncoding;
33     }
34     
35     public InputStream JavaDoc 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     /**
46      * Cache the contents for the remote resource in a local buffer.
47      * This method should be invoked before {@link #getContents()}
48      * to ensure that a round trip is not made in that method.
49      * @param monitor a progress monitor.
50      * @throws CoreException
51      */

52     public void cacheContents(IProgressMonitor monitor) throws CoreException {
53         bufferedContents = fetchContents(monitor);
54     }
55
56     /**
57      * Returns an IStorage for the element.
58      * @param monitor
59      * @return a storage
60      * @throws TeamException
61      */

62     abstract protected IStorage fetchContents(IProgressMonitor monitor) throws CoreException;
63
64     /**
65      * Return the {@link IStorage} that has been buffered for this element.
66      * @return the buffered storage
67      */

68     public IStorage getBufferedStorage() {
69         return bufferedContents;
70     }
71     
72     /* (non-Javadoc)
73      * @see org.eclipse.compare.ITypedElement#getImage()
74      */

75     public Image getImage() {
76         return CompareUI.getImage(getType());
77     }
78
79     /* (non-Javadoc)
80      * @see org.eclipse.compare.ITypedElement#getType()
81      */

82     public String JavaDoc getType() {
83         String JavaDoc name = getName();
84         if (name != null) {
85             int index = name.lastIndexOf('.');
86             if (index == -1)
87                 return ""; //$NON-NLS-1$
88
if (index == (name.length() - 1))
89                 return ""; //$NON-NLS-1$
90
return name.substring(index + 1);
91         }
92         return ITypedElement.FOLDER_TYPE;
93     }
94
95     /* (non-Javadoc)
96      * @see org.eclipse.compare.IEncodedStreamContentAccessor#getCharset()
97      */

98     public String JavaDoc 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 JavaDoc charset = ((IEncodedStorage)bufferedContents).getCharset();
106             return charset;
107         }
108         return null;
109     }
110     
111     /* (non-Javadoc)
112      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
113      */

114     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
115         if (adapter == ISharedDocumentAdapter.class) {
116             synchronized (this) {
117                 if (sharedDocumentAdapter == null)
118                     sharedDocumentAdapter = new SharedDocumentAdapter() {
119                         public IEditorInput getDocumentKey(Object JavaDoc 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                             // The document is read-only
127
}
128                     };
129                 return sharedDocumentAdapter;
130             }
131         }
132         return Platform.getAdapterManager().getAdapter(this, adapter);
133     }
134
135     /**
136      * Method called from the shared document adapter to get the document key.
137      * @param element the element
138      * @return the document key
139      */

140     protected abstract IEditorInput getDocumentKey(Object JavaDoc element);
141
142     public String JavaDoc getLocalEncoding() {
143         return localEncoding;
144     }
145
146 }
147
Popular Tags