2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.mercurial.ui.diff;
44
import org.netbeans.api.diff.StreamSource;
45
import org.netbeans.api.diff.Difference;
46
import org.netbeans.modules.mercurial.Mercurial;
47
import org.netbeans.modules.mercurial.VersionsCache;
48
import org.netbeans.modules.versioning.util.Utils;
53
import org.openide.util.*;
54
import org.openide.util.lookup.Lookups;
55
import org.openide.filesystems.FileObject;
56
import org.openide.filesystems.FileUtil;
57
import org.openide.loaders.DataObject;
58
import org.openide.loaders.DataObjectNotFoundException;
61
* Stream source for diffing CVS managed files.
63
* @author Maros Sandor
65
public class DiffStreamSource extends StreamSource {
67
private final File baseFile;
68
private final String revision;
69
private final String title;
70
private String mimeType;
71
private Boolean start;
74
* Null is a valid value if base file does not exist in this revision.
76
private File remoteFile;
79
* Creates a new StreamSource implementation for Diff engine.
82
* @param revision file revision, may be null if the revision does not exist (ie for new files)
83
* @param title title to use in diff panel
85
public DiffStreamSource(File baseFile, String revision, String title) {
86
this.baseFile = baseFile;
87
this.revision = revision;
92
/** Creates DiffStreamSource for nonexiting files. */
93
public DiffStreamSource(String title) {
100
public String getName() {
101
if (baseFile != null) {
102
return baseFile.getName();
104
return NbBundle.getMessage(DiffStreamSource.class, "LBL_Diff_Anonymous"); // NOI18N
108
public String getTitle() {
112
public synchronized String getMIMEType() {
113
if (baseFile.isDirectory()) {
114
// http://www.rfc-editor.org/rfc/rfc2425.txt
115
return "content/unknown"; // "text/directory"; //HACK no editor for directory MIME type => NPE while constructing EditorKit // NOI18N
120
} catch (IOException e) {
121
return null; // XXX use error manager HACK null potentionally kills DiffViewImpl, NPE while constructing EditorKit
126
public synchronized Reader createReader() throws IOException {
127
if (baseFile.isDirectory()) {
128
// XXX return directory listing?
129
// could be nice te return sorted directory content
130
// such as vim if user "edits" directory // NOI18N
131
return new StringReader(NbBundle.getMessage(DiffStreamSource.class, "LBL_Diff_NoFolderDiff")); // NOI18N
134
if (revision == null || remoteFile == null) return null;
135
if (!mimeType.startsWith("text/")) { // NOI18N
138
return Utils.createReader(remoteFile);
142
public Writer createWriter(Difference[] conflicts) throws IOException {
143
throw new IOException("Operation not supported"); // NOI18N
146
public boolean isEditable() {
147
return Setup.REVISION_CURRENT.equals(revision) && isPrimary();
150
private boolean isPrimary() {
151
FileObject fo = FileUtil.toFileObject(baseFile);
154
DataObject dao = DataObject.find(fo);
155
return fo.equals(dao.getPrimaryFile());
156
} catch (DataObjectNotFoundException e) {
157
// no dataobject, never mind
163
public synchronized Lookup getLookup() {
166
} catch (IOException e) {
167
return Lookups.fixed();
169
if (remoteFile == null || !isPrimary()) return Lookups.fixed();
170
FileObject remoteFo = FileUtil.toFileObject(remoteFile);
171
if (remoteFo == null) return Lookups.fixed();
173
return Lookups.fixed(remoteFo);
179
synchronized void init() throws IOException {
180
if (baseFile.isDirectory()) {
183
if (start == false) return;
185
if (remoteFile != null || revision == null) return;
186
mimeType = Mercurial.getInstance().getMimeType(baseFile);
189
// we cannot move editable documents because that would break Document sharing
190
remoteFile = VersionsCache.getInstance().getFileRevision(baseFile, revision);
192
File tempFolder = Utils.getTempFolder();
193
// To correctly get content of the base file, we need to checkout all files that belong to the same
194
// DataObject. One example is Form files: data loader removes //GEN:BEGIN comments from the java file but ONLY
195
// if it also finds associate .form file in the same directory
196
Set<File> allFiles = Utils.getAllDataObjectFiles(baseFile);
197
for (File file : allFiles) {
198
boolean isBase = file.equals(baseFile);
200
File rf = VersionsCache.getInstance().getFileRevision(file, revision);
205
File newRemoteFile = new File(tempFolder, file.getName());
206
Utils.copyStreamsCloseAll(new FileOutputStream(newRemoteFile), new FileInputStream(rf));
207
newRemoteFile.deleteOnExit();
209
remoteFile = newRemoteFile;
211
} catch (Exception e) {
213
// we cannot check out peer file so the dataobject will not be constructed properly
217
if (!baseFile.exists() && remoteFile != null && remoteFile.exists()) {
218
mimeType = Mercurial.getInstance().getMimeType(remoteFile);
220
} catch (Exception e) {
221
// TODO detect interrupted IO (exception subclass), i.e. user cancel
222
IOException failure = new IOException("Can not load remote file for " + baseFile); // NOI18N
223
failure.initCause(e);