~piastucki/bzr-eclipse/history-view-enhancements

« back to all changes in this revision

Viewing changes to org.vcs.bazaar.eclipse.core/src/org/vcs/bazaar/eclipse/core/subscriber/BzrResourceVariant.java

  • Committer: Guillermo Gonzalez
  • Date: 2007-03-14 12:15:50 UTC
  • Revision ID: guillo.gonzo@gmail.com-20070314121550-tltk3b6f3zblf0dh
Initial commit with the new code layout.
Now starts the real work

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2004-2006 by Leif Frenzel - see http://leiffrenzel.de
 
2
package org.vcs.bazaar.eclipse.core.subscriber;
 
3
 
 
4
import java.io.BufferedInputStream;
 
5
import java.io.File;
 
6
import java.io.FileInputStream;
 
7
import java.io.FileNotFoundException;
 
8
import java.io.IOException;
 
9
import java.io.InputStream;
 
10
import java.util.Date;
 
11
 
 
12
import org.eclipse.core.runtime.IProgressMonitor;
 
13
import org.eclipse.core.runtime.IStatus;
 
14
import org.eclipse.core.runtime.Status;
 
15
import org.eclipse.team.core.TeamException;
 
16
import org.eclipse.team.core.variants.CachedResourceVariant;
 
17
import org.vcs.bazaar.eclipse.BazaarEclipsePlugin;
 
18
 
 
19
/** <p>A resource variant for a local Darcs repository is the corresponding
 
20
  * resource in the pristine tree.</p> 
 
21
  * 
 
22
  * @author Leif Frenzel
 
23
  */
 
24
public class BzrResourceVariant extends CachedResourceVariant {
 
25
 
 
26
  private File ioFile;
 
27
  private byte[] bytes;
 
28
 
 
29
  public BzrResourceVariant( final File file ) {
 
30
    this.ioFile = file;
 
31
  }
 
32
 
 
33
  public BzrResourceVariant( final File file, final byte[] bytes ) {
 
34
    this.ioFile = file;
 
35
    this.bytes = bytes;
 
36
  }
 
37
 
 
38
  public BzrResourceVariant[] members() {
 
39
    BzrResourceVariant[] result = new BzrResourceVariant[ 0 ];  
 
40
    if( isContainer() ) {
 
41
      File[] members = ioFile.listFiles();
 
42
      result = new BzrResourceVariant[ members.length ];
 
43
      for( int i = 0; i < members.length; i++ ) {
 
44
        result[ i ] = new BzrResourceVariant( members[ i ] );
 
45
      }
 
46
    }
 
47
    return result;
 
48
  }
 
49
 
 
50
  public InputStream getContents() throws TeamException {
 
51
    try {
 
52
      return new BufferedInputStream( new FileInputStream( ioFile ) );
 
53
    } catch( FileNotFoundException fnfex ) {
 
54
      String msg = "Couldn't fetch contents for " + getFilePath(); //$NON-NLS-1$
 
55
      throw new TeamException( msg, fnfex );
 
56
    }
 
57
  }
 
58
 
 
59
  
 
60
  // interface methods of CachedResourceVariant
 
61
  /////////////////////////////////////////////
 
62
  
 
63
  @Override
 
64
  protected void fetchContents( final IProgressMonitor monitor ) 
 
65
                                                          throws TeamException {
 
66
    setContents( getContents(), monitor );
 
67
  }
 
68
 
 
69
  @Override
 
70
  protected String getCachePath() {
 
71
    // append the timestamp to the file path to give each variant a unique path
 
72
    return getFilePath() + " " + ioFile.lastModified(); //$NON-NLS-1$
 
73
  }
 
74
 
 
75
  @Override
 
76
  protected String getCacheId() {
 
77
    return BazaarEclipsePlugin.getPluginId();
 
78
  }
 
79
 
 
80
  public String getName() {
 
81
    return ioFile.getName();
 
82
  }
 
83
 
 
84
  public boolean isContainer() {
 
85
    return ioFile.isDirectory();
 
86
  }
 
87
 
 
88
  public String getContentIdentifier() {
 
89
    // Use the modification timestamp as the content identifier
 
90
    return new Date( ioFile.lastModified() ).toString();
 
91
  }
 
92
 
 
93
  public byte[] asBytes() {
 
94
    if( bytes == null ) {
 
95
      // For simplicity, convert the timestamp to its string representation.
 
96
      // A more optimal storage format would be the 8 bytes that make up the
 
97
      // long.
 
98
      bytes = Long.toString( ioFile.lastModified() ).getBytes();
 
99
    }
 
100
    return bytes;
 
101
  }
 
102
  
 
103
 
 
104
  // helping methods
 
105
  //////////////////
 
106
  
 
107
  private String getFilePath() {
 
108
    String result;
 
109
    try {
 
110
      result = ioFile.getCanonicalPath();
 
111
    } catch( final IOException ioex ) {
 
112
      // Failed for some reason. Try the absolute path.
 
113
      String pluginId = BazaarEclipsePlugin.getPluginId();
 
114
      String msg =   "Failed to obtain canonical path for "  //$NON-NLS-1$
 
115
                   + ioFile.getAbsolutePath();
 
116
      IStatus status = new Status( IStatus.ERROR, pluginId, 0, msg, ioex );
 
117
      BazaarEclipsePlugin.getDefault().getLog().log( status );
 
118
      result = ioFile.getAbsolutePath();
 
119
    }
 
120
    return result;
 
121
  }
 
122
}