KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > diff > DiffStreamSource


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.subversion.ui.diff;
21
22 import org.netbeans.api.diff.StreamSource;
23 import org.netbeans.api.diff.Difference;
24 import org.netbeans.modules.diff.EncodedReaderFactory;
25 import org.netbeans.modules.subversion.*;
26 import org.netbeans.modules.versioning.util.Utils;
27
28 import java.io.*;
29 import java.util.*;
30
31 import org.openide.util.*;
32 import org.openide.util.lookup.Lookups;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.loaders.DataObject;
36 import org.openide.loaders.DataObjectNotFoundException;
37
38 /**
39  * Stream source for diffing CVS managed files.
40  *
41  * @author Maros Sandor
42  */

43 public class DiffStreamSource extends StreamSource {
44
45     private final File baseFile;
46     private final String JavaDoc revision;
47     private final String JavaDoc title;
48     private String JavaDoc mimeType;
49
50     /**
51      * Null is a valid value if base file does not exist in this revision.
52      */

53     private File remoteFile;
54
55     /**
56      * Creates a new StreamSource implementation for Diff engine.
57      *
58      * @param baseFile
59      * @param revision file revision, may be null if the revision does not exist (ie for new files)
60      * @param title title to use in diff panel
61      */

62     public DiffStreamSource(File baseFile, String JavaDoc revision, String JavaDoc title) {
63         this.baseFile = baseFile;
64         this.revision = revision;
65         this.title = title;
66     }
67
68     /** Creates DiffStreamSource for nonexiting files. */
69     public DiffStreamSource(String JavaDoc title) {
70         this.baseFile = null;
71         this.revision = null;
72         this.title = title;
73     }
74
75     public String JavaDoc getName() {
76         if (baseFile != null) {
77             return baseFile.getName();
78         } else {
79             return NbBundle.getMessage(DiffStreamSource.class, "LBL_Diff_Anonymous"); // NOI18N
80
}
81     }
82
83     public String JavaDoc getTitle() {
84         return title;
85     }
86
87     public synchronized String JavaDoc getMIMEType() {
88         if (baseFile.isDirectory()) {
89             // http://www.rfc-editor.org/rfc/rfc2425.txt
90
return "content/unknown"; // "text/directory"; //HACK no editor for directory MIME type => NPE while constructing EditorKit // NOI18N
91
}
92
93         try {
94             init();
95         } catch (IOException e) {
96             return null; // XXX use error manager HACK null potentionally kills DiffViewImpl, NPE while constructing EditorKit
97
}
98         return mimeType;
99     }
100
101     public synchronized Reader createReader() throws IOException {
102         if (baseFile.isDirectory()) {
103             // XXX return directory listing?
104
// could be nice te return sorted directory content
105
// such as vim if user "edits" directory
106
return new StringReader(NbBundle.getMessage(DiffStreamSource.class, "LBL_Diff_NoFolderDiff")); // NOI18N
107
}
108         init();
109         if (revision == null || remoteFile == null) return null;
110         if (!mimeType.startsWith("text/")) {
111             return new StringReader(NbBundle.getMessage(DiffStreamSource.class, "BK5001", getTitle())); // NOI18N
112
} else {
113             // XXX diff implementation dependency, we need Encoding API or rewrite to binary diff
114
return EncodedReaderFactory.getDefault().getReader(remoteFile, mimeType);
115         }
116     }
117
118     public Writer createWriter(Difference[] conflicts) throws IOException {
119         throw new IOException("Operation not supported"); // NOI18N
120
}
121
122     public boolean isEditable() {
123         return Setup.REVISION_CURRENT.equals(revision) && isPrimary();
124     }
125
126     private boolean isPrimary() {
127         FileObject fo = FileUtil.toFileObject(baseFile);
128         if (fo != null) {
129             try {
130                 DataObject dao = DataObject.find(fo);
131                 return fo.equals(dao.getPrimaryFile());
132             } catch (DataObjectNotFoundException e) {
133                 // no dataobject, never mind
134
}
135         }
136         return true;
137     }
138
139     public synchronized Lookup getLookup() {
140         try {
141             init();
142         } catch (IOException e) {
143             return Lookups.fixed();
144         }
145         if (remoteFile == null || !isPrimary()) return Lookups.fixed();
146         FileObject remoteFo = FileUtil.toFileObject(remoteFile);
147         if (remoteFo == null) return Lookups.fixed();
148
149         return Lookups.fixed(remoteFo);
150     }
151     
152     /**
153      * Loads data over network.
154      */

155     synchronized void init() throws IOException {
156         if (baseFile.isDirectory()) {
157             return;
158         }
159         if (remoteFile != null || revision == null) return;
160         mimeType = Subversion.getInstance().getMimeType(baseFile);
161         try {
162             if (isEditable()) {
163                 // we cannot move editable documents because that would break Document sharing
164
remoteFile = VersionsCache.getInstance().getFileRevision(baseFile, revision);
165             } else {
166                 File tempFolder = Utils.getTempFolder();
167                 // To correctly get content of the base file, we need to checkout all files that belong to the same
168
// DataObject. One example is Form files: data loader removes //GEN:BEGIN comments from the java file but ONLY
169
// if it also finds associate .form file in the same directory
170
Set<File> allFiles = Utils.getAllDataObjectFiles(baseFile);
171                 for (File file : allFiles) {
172                     boolean isBase = file.equals(baseFile);
173                     try {
174                         File rf = VersionsCache.getInstance().getFileRevision(file, revision);
175                         File newRemoteFile = new File(tempFolder, file.getName());
176                         Utils.copyStreamsCloseAll(new FileOutputStream(newRemoteFile), new FileInputStream(rf));
177                         newRemoteFile.deleteOnExit();
178                         if (isBase) {
179                             remoteFile = newRemoteFile;
180                         }
181                     } catch (Exception JavaDoc e) {
182                         if (isBase) throw e;
183                         // we cannot check out peer file so the dataobject will not be constructed properly
184
}
185                 }
186             }
187             if (!baseFile.exists() && remoteFile != null && remoteFile.exists()) {
188                 mimeType = Subversion.getInstance().getMimeType(remoteFile);
189             }
190         } catch (Exception JavaDoc e) {
191             // TODO detect interrupted IO (exception subclass), i.e. user cancel
192
IOException failure = new IOException("Can not load remote file for " + baseFile); // NOI18N
193
failure.initCause(e);
194             throw failure;
195         }
196     }
197 }
198
Popular Tags