KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > history > LocalFileRevision


1 /*******************************************************************************
2  * Copyright (c) 2006, 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.core.history;
12
13 import java.net.URI JavaDoc;
14
15 import org.eclipse.core.filesystem.URIUtil;
16 import org.eclipse.core.resources.*;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.team.core.history.IFileRevision;
21 import org.eclipse.team.core.history.ITag;
22 import org.eclipse.team.core.history.provider.FileRevision;
23 import org.eclipse.team.internal.core.Messages;
24
25 /**
26  * A LocalFileRevision is used for wrapping local files in order to display
27  * them in the History View. As such, this class can be used to wrap either
28  * a local file's IFileState or an IFile.
29  */

30 public class LocalFileRevision extends FileRevision {
31     /*
32      * Either one or the other of these fields is intended
33      * to be used.
34      */

35     //Used for wrapping local file history items
36
private IFileState state;
37
38     //Used for displaying the "real" current file
39
private IFile file;
40     //Used for displaying which base revision
41
private IFileRevision baseRevision;
42
43     /*
44      * Used for wrapping an IFileState.
45      */

46     public LocalFileRevision(IFileState state) {
47         this.state = state;
48         this.file = null;
49         this.baseRevision = null;
50     }
51
52     /*
53      * Used for wrapping an IFile. This is generally used to represent the local
54      * current version of the file being displayed in the history. Make sure to
55      * also pass in the base revision associated with this current version.
56      *
57      * @see #setBaseRevision(IFileRevision)
58      */

59     public LocalFileRevision(IFile file) {
60         this.file = file;
61         this.baseRevision = null;
62         this.state = null;
63     }
64
65     /* (non-Javadoc)
66      * @see org.eclipse.team.internal.core.FileRevision#getContentIdentifier()
67      */

68     public String JavaDoc getContentIdentifier() {
69         if (file != null)
70             return baseRevision == null ? NLS.bind(Messages.LocalFileRevision_currentVersion, "") : NLS.bind(Messages.LocalFileRevision_currentVersion, baseRevision.getContentIdentifier()); //$NON-NLS-1$
71
return ""; //$NON-NLS-1$
72
}
73
74     /* (non-Javadoc)
75      * @see org.eclipse.team.internal.core.FileRevision#getAuthor()
76      */

77     public String JavaDoc getAuthor() {
78         return ""; //$NON-NLS-1$
79
}
80
81     /* (non-Javadoc)
82      * @see org.eclipse.team.internal.core.FileRevision#getComment()
83      */

84     public String JavaDoc getComment() {
85         if (file != null)
86             return Messages.LocalFileRevision_currentVersionTag;
87         return null;
88     }
89
90     /* (non-Javadoc)
91      * @see org.eclipse.team.internal.core.FileRevision#getTags()
92      */

93     public ITag[] getTags() {
94         return new ITag[0];
95     }
96
97     /* (non-Javadoc)
98      * @see org.eclipse.team.core.history.IFileState#getStorage(org.eclipse.core.runtime.IProgressMonitor)
99      */

100     public IStorage getStorage(IProgressMonitor monitor) throws CoreException {
101         if (file != null) {
102             return file;
103         }
104         return state;
105     }
106
107     /* (non-Javadoc)
108      * @see org.eclipse.team.core.history.IFileState#getName()
109      */

110     public String JavaDoc getName() {
111         if (file != null) {
112             return file.getName();
113         }
114
115         return state.getName();
116     }
117
118     /* (non-Javadoc)
119      * @see org.eclipse.team.core.variants.FileState#getTimestamp()
120      */

121     public long getTimestamp() {
122         if (file != null) {
123             return file.getLocalTimeStamp();
124         }
125
126         return state.getModificationTime();
127     }
128
129     /* (non-Javadoc)
130      * @see org.eclipse.team.core.history.provider.FileRevision#exists()
131      * A LocalFileRevision generally should exist, but if it doesn't, this
132      * method should tell the truth.
133      */

134     public boolean exists() {
135         if (file != null) {
136             return file.exists();
137         }
138         
139         return state.exists();
140     }
141
142     /*
143      * Sets the base revision. Can be used to associate a base revision
144      * with an IFile.
145      */

146     public void setBaseRevision(IFileRevision baseRevision) {
147         this.baseRevision = baseRevision;
148     }
149     
150     public boolean isPropertyMissing() {
151         return true;
152     }
153
154     
155     public IFileRevision withAllProperties(IProgressMonitor monitor) {
156         return this;
157     }
158
159     public boolean isPredecessorOf(IFileRevision revision) {
160         long compareRevisionTime = revision.getTimestamp();
161         return (this.getTimestamp() < compareRevisionTime);
162     }
163
164     public boolean isDescendentOf(IFileRevision revision) {
165         long compareRevisionTime = revision.getTimestamp();
166         return (this.getTimestamp() > compareRevisionTime);
167     }
168     
169     public URI JavaDoc getURI() {
170         if (file != null)
171             return file.getLocationURI();
172         
173         return URIUtil.toURI(state.getFullPath());
174     }
175
176     public IFile getFile() {
177         return file;
178     }
179
180     public IFileState getState() {
181         return state;
182     }
183     
184     public boolean isCurrentState() {
185         return file != null;
186     }
187     
188     public boolean equals(Object JavaDoc obj) {
189         if (obj == this)
190             return true;
191         if (obj instanceof LocalFileRevision) {
192             LocalFileRevision other = (LocalFileRevision) obj;
193             if (file != null && other.file != null)
194                 return file.equals(other.file);
195             if (state != null && other.state != null)
196                 return statesEqual(state, other.state);
197         }
198         return false;
199     }
200
201     private boolean statesEqual(IFileState s1, IFileState s2) {
202         return (s1.getFullPath().equals(s2.getFullPath()) && s1.getModificationTime() == s2.getModificationTime());
203     }
204     
205     public int hashCode() {
206         if (file != null)
207             return file.hashCode();
208         if (state != null)
209             return (int)state.getModificationTime();
210         return super.hashCode();
211     }
212 }
213
Popular Tags