1 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 43 public class DiffStreamSource extends StreamSource { 44 45 private final File baseFile; 46 private final String revision; 47 private final String title; 48 private String mimeType; 49 50 53 private File remoteFile; 54 55 62 public DiffStreamSource(File baseFile, String revision, String title) { 63 this.baseFile = baseFile; 64 this.revision = revision; 65 this.title = title; 66 } 67 68 69 public DiffStreamSource(String title) { 70 this.baseFile = null; 71 this.revision = null; 72 this.title = title; 73 } 74 75 public String getName() { 76 if (baseFile != null) { 77 return baseFile.getName(); 78 } else { 79 return NbBundle.getMessage(DiffStreamSource.class, "LBL_Diff_Anonymous"); } 81 } 82 83 public String getTitle() { 84 return title; 85 } 86 87 public synchronized String getMIMEType() { 88 if (baseFile.isDirectory()) { 89 return "content/unknown"; } 92 93 try { 94 init(); 95 } catch (IOException e) { 96 return null; } 98 return mimeType; 99 } 100 101 public synchronized Reader createReader() throws IOException { 102 if (baseFile.isDirectory()) { 103 return new StringReader(NbBundle.getMessage(DiffStreamSource.class, "LBL_Diff_NoFolderDiff")); } 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())); } else { 113 return EncodedReaderFactory.getDefault().getReader(remoteFile, mimeType); 115 } 116 } 117 118 public Writer createWriter(Difference[] conflicts) throws IOException { 119 throw new IOException("Operation not supported"); } 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 } 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 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 remoteFile = VersionsCache.getInstance().getFileRevision(baseFile, revision); 165 } else { 166 File tempFolder = Utils.getTempFolder(); 167 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 e) { 182 if (isBase) throw e; 183 } 185 } 186 } 187 if (!baseFile.exists() && remoteFile != null && remoteFile.exists()) { 188 mimeType = Subversion.getInstance().getMimeType(remoteFile); 189 } 190 } catch (Exception e) { 191 IOException failure = new IOException("Can not load remote file for " + baseFile); failure.initCause(e); 194 throw failure; 195 } 196 } 197 } 198 | Popular Tags |