KickJava   Java API By Example, From Geeks To Geeks.

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


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.core.history;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IFileState;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.team.core.TeamException;
20 import org.eclipse.team.core.history.IFileRevision;
21 import org.eclipse.team.core.history.provider.FileHistory;
22 import org.eclipse.team.internal.core.Messages;
23
24 public class LocalFileHistory extends FileHistory {
25
26     protected IFile file;
27     //used to hold all revisions (changes based on filtering)
28
protected IFileRevision[] revisions;
29     private final boolean includeCurrent;
30
31     /*
32      * Creates a new CVSFile history that will fetch remote revisions by default.
33      */

34     public LocalFileHistory(IFile file, boolean includeCurrent) {
35         this.file = file;
36         this.includeCurrent = includeCurrent;
37     }
38     
39     /* (non-Javadoc)
40      * @see org.eclipse.team.core.history.IFileHistory#getContributors(org.eclipse.team.core.history.IFileRevision)
41      */

42     public IFileRevision[] getContributors(IFileRevision revision) {
43
44         IFileRevision[] revisions = getFileRevisions();
45
46         //the predecessor is the file with a timestamp that is the largest timestamp
47
//from the set of all timestamps smaller than the root file's timestamp
48
IFileRevision fileRevision = null;
49         for (int i = 0; i < revisions.length; i++) {
50             if (((LocalFileRevision) revisions[i]).isPredecessorOf(revision)) {
51                 //no revision has been set as of yet
52
if (fileRevision == null)
53                     fileRevision = revisions[i];
54                 //this revision is a predecessor - now check to see if it comes
55
//after the current predecessor, if it does make it the current predecessor
56
if (fileRevision != null && revisions[i].getTimestamp() > fileRevision.getTimestamp()) {
57                     fileRevision = revisions[i];
58                 }
59             }
60         }
61         if (fileRevision == null)
62             return new IFileRevision[0];
63         return new IFileRevision[] {fileRevision};
64     }
65
66     /* (non-Javadoc)
67      * @see org.eclipse.team.core.history.IFileHistory#getFileRevision(java.lang.String)
68      */

69     public IFileRevision getFileRevision(String JavaDoc id) {
70         if (revisions != null) {
71             for (int i = 0; i < revisions.length; i++) {
72                 IFileRevision revision = revisions[i];
73                 if (revision.getContentIdentifier().equals(id)) {
74                     return revision;
75                 }
76             }
77         }
78         return null;
79     }
80
81     /* (non-Javadoc)
82      * @see org.eclipse.team.core.history.IFileHistory#getFileRevisions()
83      */

84     public IFileRevision[] getFileRevisions() {
85         if (revisions == null)
86             return new IFileRevision[0];
87         return revisions;
88     }
89
90     /* (non-Javadoc)
91      * @see org.eclipse.team.core.history.IFileHistory#getTargets(org.eclipse.team.core.history.IFileRevision)
92      */

93     public IFileRevision[] getTargets(IFileRevision revision) {
94         IFileRevision[] revisions = getFileRevisions();
95         ArrayList JavaDoc directDescendents = new ArrayList JavaDoc();
96
97         for (int i = 0; i < revisions.length; i++) {
98             if (((LocalFileRevision) revisions[i]).isDescendentOf(revision)) {
99                 directDescendents.add(revisions[i]);
100             }
101         }
102         return (IFileRevision[]) directDescendents.toArray(new IFileRevision[directDescendents.size()]);
103     }
104
105     /**
106      * Refreshes the revisions for this local file.
107      *
108      * @param monitor a progress monitor
109      * @throws TeamException
110      */

111     public void refresh(IProgressMonitor monitor) throws TeamException {
112         monitor.beginTask(Messages.LocalFileHistory_RefreshLocalHistory/*, file.getProjectRelativePath().toString())*/, 300);
113         try {
114             // Include the file's current state if and only if the file exists.
115
LocalFileRevision currentRevision =
116                 (includeRevisionForFile() ? new LocalFileRevision(file) : null);
117             IFileState[] fileStates = file.getHistory(monitor);
118             int numRevisions = fileStates.length + (currentRevision != null ? 1 : 0);
119             revisions = new LocalFileRevision[numRevisions];
120             for (int i = 0; i < fileStates.length; i++) {
121                 revisions[i] = new LocalFileRevision(fileStates[i]);
122             }
123             if (currentRevision != null)
124                 revisions[fileStates.length] = currentRevision;
125         } catch (CoreException e) {
126             throw TeamException.asTeamException(e);
127         } finally {
128             monitor.done();
129         }
130     }
131
132     private boolean includeRevisionForFile() {
133         return file.exists() && includeCurrent;
134     }
135
136 }
137
Popular Tags