KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > history > FileRevisionEditorInput


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.ui.history;
12
13 import java.io.InputStream JavaDoc;
14 import java.util.Date JavaDoc;
15
16 import org.eclipse.core.resources.*;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.team.core.history.IFileRevision;
21 import org.eclipse.team.internal.ui.TeamUIMessages;
22 import org.eclipse.team.internal.ui.Utils;
23 import org.eclipse.ui.IPersistableElement;
24 import org.eclipse.ui.IStorageEditorInput;
25 import org.eclipse.ui.model.IWorkbenchAdapter;
26
27 import com.ibm.icu.text.DateFormat;
28
29 public class FileRevisionEditorInput extends PlatformObject implements IWorkbenchAdapter, IStorageEditorInput {
30
31     private final Object JavaDoc fileRevision;
32     private final IStorage storage;
33     
34     /**
35      * Creates FileRevisionEditorInput on the given revision.
36      * @param revision the file revision
37      * @param monitor
38      * @return a file revision editor input
39      * @throws CoreException
40      */

41     public static FileRevisionEditorInput createEditorInputFor(IFileRevision revision, IProgressMonitor monitor) throws CoreException {
42         IStorage storage = revision.getStorage(monitor);
43         return new FileRevisionEditorInput(revision, storage);
44     }
45     
46     private static IStorage wrapStorage(final IStorage storage, final String JavaDoc charset) {
47         if (charset == null)
48             return storage;
49         return new IEncodedStorage() {
50             public Object JavaDoc getAdapter(Class JavaDoc adapter) {
51                 return storage.getAdapter(adapter);
52             }
53             public boolean isReadOnly() {
54                 return storage.isReadOnly();
55             }
56             public String JavaDoc getName() {
57                 return storage.getName();
58             }
59             public IPath getFullPath() {
60                 return storage.getFullPath();
61             }
62             public InputStream JavaDoc getContents() throws CoreException {
63                 return storage.getContents();
64             }
65             public String JavaDoc getCharset() throws CoreException {
66                 return charset;
67             }
68         };
69     }
70     
71     /**
72      * Creates FileRevisionEditorInput on the given revision.
73      * @param revision the file revision
74      * @param storage the contents of the file revision
75      */

76     public FileRevisionEditorInput(Object JavaDoc revision, IStorage storage) {
77         Assert.isNotNull(revision);
78         Assert.isNotNull(storage);
79         this.fileRevision = revision;
80         this.storage = storage;
81     }
82     
83     public FileRevisionEditorInput(IFileState state) {
84         this(state, state);
85     }
86     
87     public FileRevisionEditorInput(Object JavaDoc revision, IStorage storage, String JavaDoc charset) {
88         this(revision, wrapStorage(storage, charset));
89     }
90
91     public IStorage getStorage() throws CoreException {
92         return storage;
93     }
94
95     public boolean exists() {
96         return true;
97     }
98
99     public ImageDescriptor getImageDescriptor() {
100         return null;
101     }
102
103     public String JavaDoc getName() {
104         IFileRevision rev = (IFileRevision)getAdapter(IFileRevision.class);
105         if (rev != null)
106             return NLS.bind(TeamUIMessages.nameAndRevision, new String JavaDoc[] { rev.getName(), rev.getContentIdentifier()});
107         IFileState state = (IFileState)getAdapter(IFileState.class);
108         if (state != null)
109             return state.getName() + " " + DateFormat.getInstance().format(new Date JavaDoc(state.getModificationTime())) ; //$NON-NLS-1$
110
return storage.getName();
111         
112     }
113
114     public IPersistableElement getPersistable() {
115         //can't persist
116
return null;
117     }
118
119     public String JavaDoc getToolTipText() {
120         return storage.getFullPath().toString();
121     }
122
123     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
124         if (adapter == IWorkbenchAdapter.class)
125             return this;
126         if (adapter == IStorage.class)
127             return storage;
128         Object JavaDoc object = super.getAdapter(adapter);
129         if (object != null)
130             return object;
131         return Utils.getAdapter(fileRevision, adapter);
132     }
133
134     public Object JavaDoc[] getChildren(Object JavaDoc o) {
135         return new Object JavaDoc[0];
136     }
137
138     public ImageDescriptor getImageDescriptor(Object JavaDoc object) {
139         return null;
140     }
141
142     public String JavaDoc getLabel(Object JavaDoc o) {
143         IFileRevision rev = (IFileRevision)getAdapter(IFileRevision.class);
144         if (rev != null)
145             return rev.getName();
146         return storage.getName();
147     }
148
149     public Object JavaDoc getParent(Object JavaDoc o) {
150         return null;
151     }
152     
153     public boolean equals(Object JavaDoc obj) {
154         if (obj instanceof FileRevisionEditorInput) {
155             FileRevisionEditorInput other = (FileRevisionEditorInput) obj;
156             return (other.fileRevision.equals(this.fileRevision));
157         }
158         return false;
159     }
160     
161     public int hashCode() {
162         return fileRevision.hashCode();
163     }
164
165     public IFileRevision getFileRevision() {
166         if (fileRevision instanceof IFileRevision) {
167             return (IFileRevision) fileRevision;
168         }
169         return null;
170     }
171
172 }
173
Popular Tags