KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > actions > 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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.versioning.system.cvss.ui.actions.diff;
21
22 import org.netbeans.api.diff.StreamSource;
23 import org.netbeans.api.diff.Difference;
24 import org.netbeans.modules.versioning.system.cvss.*;
25 import org.netbeans.modules.versioning.util.Utils;
26 import org.netbeans.modules.diff.EncodedReaderFactory;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileUtil;
29
30 import java.io.*;
31 import java.util.*;
32
33 import org.openide.util.*;
34 import org.openide.util.lookup.Lookups;
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 = "text/plain"; // reasonable default
49

50     /**
51      * Null is a valid value if base file does not exist in this revision.
52      */

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

66     public DiffStreamSource(File baseFile, String JavaDoc revision, String JavaDoc title) {
67         this.baseFile = baseFile;
68         this.revision = revision;
69         this.title = title;
70     }
71
72     public String JavaDoc getName() {
73         return baseFile.getName();
74     }
75
76     public String JavaDoc getTitle() {
77         return title;
78     }
79
80     public synchronized void setGroup(ExecutorGroup group) {
81         this.group = group;
82     }
83
84     public synchronized String JavaDoc getMIMEType() {
85         try {
86             init(null);
87         } catch (IOException e) {
88             return null;
89         }
90         return mimeType;
91     }
92
93     public synchronized Reader createReader() throws IOException {
94         init(group);
95         if (revision == null || remoteFile == null) return null;
96         if (binary) {
97             return new StringReader(NbBundle.getMessage(DiffStreamSource.class, "BK5001", getTitle()));
98         } else {
99             FileObject remoteFo = FileUtil.toFileObject(remoteFile);
100             return EncodedReaderFactory.getDefault().getReader(remoteFo, null);
101         }
102     }
103
104     public Writer createWriter(Difference[] conflicts) throws IOException {
105         throw new IOException("Operation not supported"); // NOI18N
106
}
107
108     public boolean isEditable() {
109         return VersionsCache.REVISION_CURRENT.equals(revision) && isPrimary();
110     }
111
112     private boolean isPrimary() {
113         FileObject fo = FileUtil.toFileObject(baseFile);
114         if (fo != null) {
115             try {
116                 DataObject dao = DataObject.find(fo);
117                 return fo.equals(dao.getPrimaryFile());
118             } catch (DataObjectNotFoundException e) {
119                 // no dataobject, never mind
120
}
121         }
122         return true;
123     }
124
125     public synchronized Lookup getLookup() {
126         try {
127             init(null);
128         } catch (IOException e) {
129             return Lookups.fixed();
130         }
131         if (remoteFile == null || !isPrimary()) return Lookups.fixed();
132         FileObject remoteFo = FileUtil.toFileObject(remoteFile);
133         if (remoteFo == null) return Lookups.fixed();
134
135         return Lookups.fixed(remoteFo);
136     }
137     
138     /**
139      * Loads data over network.
140      *
141      * @param group combines multiple loads or <code>null</code>
142      * Note that this group must not be executed later on.
143      */

144     synchronized void init(ExecutorGroup group) throws IOException {
145         if (initialized) return;
146         initialized = true;
147         if (revision == null) return;
148         binary = !CvsVersioningSystem.getInstance().isText(baseFile);
149         try {
150             if (isEditable()) {
151                 // we cannot move editable documents because that would break Document sharing
152
remoteFile = VersionsCache.getInstance().getRemoteFile(baseFile, revision, group);
153             } else {
154                 File tempFolder = Utils.getTempFolder();
155                 // To correctly get content of the base file, we need to checkout all files that belong to the same
156
// DataObject. One example is Form files: data loader removes //GEN:BEGIN comments from the java file but ONLY
157
// if it also finds associate .form file in the same directory
158
Set<File> allFiles = Utils.getAllDataObjectFiles(baseFile);
159                 for (File file : allFiles) {
160                     boolean isBase = file.equals(baseFile);
161                     try {
162                         File rf = VersionsCache.getInstance().getRemoteFile(file, revision, group);
163                         File newRemoteFile = new File(tempFolder, file.getName());
164                         Utils.copyStreamsCloseAll(new FileOutputStream(newRemoteFile), new FileInputStream(rf));
165                         newRemoteFile.deleteOnExit();
166                         if (isBase) {
167                             remoteFile = newRemoteFile;
168                         }
169                     } catch (Exception JavaDoc e) {
170                         if (isBase) throw e;
171                         // we cannot check out peer file so the dataobject will not be constructed properly
172
}
173                 }
174             }
175             if (!baseFile.exists() && remoteFile != null && remoteFile.exists()) {
176                 binary = !CvsVersioningSystem.getInstance().isText(remoteFile);
177             }
178         } catch (Exception JavaDoc e) {
179             IOException failure = new IOException("Cannot initialize stream source"); // NOI18N
180
failure.initCause(e);
181             throw failure;
182         }
183         FileObject fo = FileUtil.toFileObject(baseFile);
184         if (fo == null && remoteFile != null) {
185             fo = FileUtil.toFileObject(remoteFile);
186         }
187         if (fo != null) {
188             mimeType = fo.getMIMEType();
189         } else if (binary) {
190             mimeType = "application/octet-stream"; // NOI18N
191
} else {
192             mimeType = "text/plain"; // NOI18N
193
}
194     }
195 }
196
Popular Tags