KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > RemoteFileEditorInput


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.ccvs.ui;
12
13  
14 import org.eclipse.core.resources.IStorage;
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.osgi.util.NLS;
18 import org.eclipse.team.core.TeamException;
19 import org.eclipse.team.core.history.IFileRevision;
20 import org.eclipse.team.core.variants.IResourceVariant;
21 import org.eclipse.team.internal.ccvs.core.*;
22 import org.eclipse.team.internal.ccvs.core.filehistory.CVSResourceVariantFileRevision;
23 import org.eclipse.team.ui.history.IHistoryPageSource;
24 import org.eclipse.ui.IPersistableElement;
25 import org.eclipse.ui.IStorageEditorInput;
26 import org.eclipse.ui.model.IWorkbenchAdapter;
27
28 /**
29  * An editor input for a file in a repository.
30  */

31 public class RemoteFileEditorInput extends PlatformObject implements IWorkbenchAdapter, IStorageEditorInput {
32     ICVSRemoteFile file;
33     IStorage storage;
34
35     /**
36      * Creates FileEditionEditorInput on the given file.
37      */

38     public RemoteFileEditorInput(ICVSRemoteFile file, IProgressMonitor monitor) {
39         this.file = file;
40         try {
41             initializeStorage(file, monitor);
42         } catch (TeamException e) {
43             // Log and continue
44
CVSUIPlugin.log(e);
45         }
46     }
47     
48     /**
49      * Initialize the strogae of this instance from the given file.
50      * @param file the file being displayed
51      * @param monitor a progress monitor
52      */

53     protected void initializeStorage(ICVSRemoteFile file, IProgressMonitor monitor) throws TeamException {
54         // Cache the contents of the file for use in the editor
55
storage = ((IResourceVariant)file).getStorage(monitor);
56     }
57     
58     /**
59      * Returns whether the editor input exists.
60      * <p>
61      * This method is primarily used to determine if an editor input should
62      * appear in the "File Most Recently Used" menu. An editor input will appear
63      * in the list until the return value of <code>exists</code> becomes
64      * <code>false</code> or it drops off the bottom of the list.
65      *
66      * @return <code>true</code> if the editor input exists; <code>false</code>
67      * otherwise
68      */

69     public boolean exists() {
70         return true;
71     }
72     public boolean equals(Object JavaDoc o) {
73         if (!(o instanceof RemoteFileEditorInput)) return false;
74         RemoteFileEditorInput input = (RemoteFileEditorInput)o;
75         return file.equals(input.file);
76     }
77     /**
78      * Returns an object which is an instance of the given class
79      * associated with this object. Returns <code>null</code> if
80      * no such object can be found.
81      *
82      * @param adapter the adapter class to look up
83      * @return a object castable to the given class,
84      * or <code>null</code> if this object does not
85      * have an adapter for the given class
86      */

87     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
88         if (adapter == IWorkbenchAdapter.class) {
89             return this;
90         }
91         
92         if (adapter == IHistoryPageSource.class)
93             return file.getAdapter(IHistoryPageSource.class);
94         
95         if (adapter == ICVSFile.class || adapter == IResourceVariant.class)
96             return file;
97         
98         if (adapter == IFileRevision.class)
99             return getFileRevision();
100         
101         return super.getAdapter(adapter);
102     }
103
104     public CVSResourceVariantFileRevision getFileRevision() {
105         return new CVSResourceVariantFileRevision((IResourceVariant)file);
106     }
107     /**
108      * Returns the children of this object. When this object
109      * is displayed in a tree, the returned objects will be this
110      * element's children. Returns an empty array if this
111      * object has no children.
112      *
113      * @param object The object to get the children for.
114      */

115     public Object JavaDoc[] getChildren(Object JavaDoc o) {
116         return new Object JavaDoc[0];
117     }
118     /**
119      * Returns the content type of the input. For instance, if the input
120      * wraps an <code>IFile</code> the content type would be derived from
121      * the extension or mime type. If the input wraps another object it
122      * may just be the object type. The content type is used for
123      * editor mapping.
124      */

125     public String JavaDoc getContentType() {
126         String JavaDoc name = file.getName();
127         return name.substring(name.lastIndexOf('.')+1);
128     }
129     /**
130      * Returns the fully qualified path name of the input.
131      */

132     public String JavaDoc getFullPath() {
133         //use path to make sure slashes are correct
134
ICVSRepositoryLocation location = file.getRepository();
135         IPath path = new Path(null, location.getRootDirectory());
136         path = path.setDevice(location.getHost() + IPath.DEVICE_SEPARATOR);
137         path = path.append(file.getRepositoryRelativePath());
138         String JavaDoc fullPath;
139         try {
140             String JavaDoc revision = file.getRevision();
141             fullPath = NLS.bind(CVSUIMessages.RemoteFileEditorInput_fullPathAndRevision, new String JavaDoc[] { path.toString(), revision });
142         } catch (TeamException e) {
143             CVSUIPlugin.log(e);
144             fullPath = path.toString();
145         }
146         return fullPath;
147     }
148     /**
149      * Returns the image descriptor for this input.
150      *
151      * @return the image descriptor for this input
152      */

153     public ImageDescriptor getImageDescriptor() {
154         IWorkbenchAdapter fileAdapter = (IWorkbenchAdapter)file.getAdapter(IWorkbenchAdapter.class);
155         return fileAdapter == null ? null : fileAdapter.getImageDescriptor(file);
156     }
157     /**
158      * @see IWorkbenchAdapter#getImageDescriptor
159      */

160     public ImageDescriptor getImageDescriptor(Object JavaDoc object) {
161         IWorkbenchAdapter fileAdapter = (IWorkbenchAdapter)file.getAdapter(IWorkbenchAdapter.class);
162         return fileAdapter == null ? null : fileAdapter.getImageDescriptor(file);
163     }
164     /**
165      * @see IWorkbenchAdapter#getLabel
166      */

167     public String JavaDoc getLabel(Object JavaDoc o) {
168         return file.getName();
169     }
170     /**
171      * Returns the input name for display purposes. For instance, if
172      * the fully qualified input name is "a\b\MyFile.gif" the return value for
173      * <code>getName</code> is "MyFile.gif".
174      */

175     public String JavaDoc getName() {
176         String JavaDoc name = file.getName();
177         try {
178             return NLS.bind(CVSUIMessages.nameAndRevision, new String JavaDoc[] { name, file.getRevision() });
179         } catch (TeamException e) {
180             return name;
181         }
182     }
183     /**
184      * Returns the logical parent of the given object in its tree.
185      * Returns <code>null</code> if there is no parent, or if this object doesn't
186      * belong to a tree.
187      *
188      * @param object The object to get the parent for.
189      */

190     public Object JavaDoc getParent(Object JavaDoc o) {
191         return null;
192     }
193     /*
194      * Returns an interface used to persist the object. If the editor input
195      * cannot be persisted this method returns <code>null</code>.
196      */

197     public IPersistableElement getPersistable() {
198         //not persistable
199
return null;
200     }
201     /**
202      * Returns the underlying IStorage object.
203      *
204      * @return an IStorage object.
205      * @exception CoreException if this method fails
206      */

207     public IStorage getStorage() throws CoreException {
208         if (storage == null) {
209             initializeStorage(file, new NullProgressMonitor());
210         }
211         return storage;
212     }
213     /**
214      * Returns the tool tip text for this editor input. This text
215      * is used to differentiate between two input with the same name.
216      * For instance, MyClass.java in folder X and MyClass.java in folder Y.
217      * <p>
218      * The format of the path will vary with each input type. For instance,
219      * if the editor input is of type <code>IFileEditorInput</code> this method
220      * should return the fully qualified resource path. For editor input of
221      * other types it may be different.
222      * </p>
223      * @return the tool tip text
224      */

225     public String JavaDoc getToolTipText() {
226         return getFullPath();
227     }
228     
229     /**
230      * Returns the remote CVS file shown in this editor input.
231      * @return the remote file handle.
232      */

233     public ICVSRemoteFile getCVSRemoteFile() {
234         return file;
235     }
236
237 }
238
Popular Tags