KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > HistoryItem


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.compare;
12
13 import java.io.InputStream JavaDoc;
14 import java.io.BufferedInputStream JavaDoc;
15
16 import org.eclipse.swt.graphics.Image;
17
18 import org.eclipse.compare.IResourceProvider;
19 import org.eclipse.core.resources.IEncodedStorage;
20 import org.eclipse.core.resources.IFileState;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.ResourcesPlugin;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IPath;
25
26 /**
27  * A combination <code>IFileState</code> and <code>ITypedElement</code> that can be used as
28  * an input to a compare viewer or other places where an <code>IStreamContentAccessor</code>
29  * is needed.
30  * <p>
31  * <p>
32  * Clients may instantiate this class; it is not intended to be subclassed.
33  * </p>
34  */

35 public class HistoryItem implements IEncodedStreamContentAccessor, ITypedElement, IModificationDate, IResourceProvider {
36     
37     private ITypedElement fBase;
38     private IFileState fFileState;
39
40     /**
41      * Creates a <code>HistoryItem</code> object which combines the given <code>IFileState</code>
42      * and <code>ITypedElement</code> into an object
43      * which is suitable as input for a compare viewer or <code>ReplaceWithEditionDialog</code>.
44      *
45      * @param base the implementation of the <code>ITypedElement</code> interface delegates to this base <code>ITypedElement</code>
46      * @param fileState the <code>IFileState</code> from which the streamable contents and the modification time is derived from
47      */

48     public HistoryItem(ITypedElement base, IFileState fileState) {
49         fBase= base;
50         fFileState= fileState;
51     }
52     
53     /* (non-Javadoc)
54      * see ITypedElement.getName
55      */

56     public String JavaDoc getName() {
57         return fBase.getName();
58     }
59     
60     /* (non-Javadoc)
61      * see ITypedElement.getImage
62      */

63     public Image getImage() {
64         return fBase.getImage();
65     }
66     
67     /* (non-Javadoc)
68      * see ITypedElement.getType
69      */

70     public String JavaDoc getType() {
71         return fBase.getType();
72     }
73
74     /* (non-Javadoc)
75      * see IModificationDate.getModificationDate
76      */

77     public long getModificationDate() {
78         return fFileState.getModificationTime();
79     }
80     
81     /* (non-Javadoc)
82      * see IStreamContentAccessor.getContents
83      */

84     public InputStream JavaDoc getContents() throws CoreException {
85         return new BufferedInputStream JavaDoc(fFileState.getContents());
86     }
87
88     /* (non-Javadoc)
89      * @see org.eclipse.compare.IEncodedStreamContentAccessor#getCharset()
90      */

91     public String JavaDoc getCharset() throws CoreException {
92         String JavaDoc charset= fFileState.getCharset();
93         if (charset == null) {
94             IResource resource= getResource();
95             if (resource instanceof IEncodedStorage)
96                 charset= ((IEncodedStorage)resource).getCharset();
97         }
98         return charset;
99     }
100
101     /* (non-Javadoc)
102      * @see org.eclipse.compare.internal.IResourceProvider#getResource()
103      */

104     public IResource getResource() {
105         IPath fullPath= fFileState.getFullPath();
106         return ResourcesPlugin.getWorkspace().getRoot().findMember(fullPath);
107     }
108 }
109
110
Popular Tags