1 19 package org.netbeans.modules.versioning.spi; 20 21 import org.openide.filesystems.FileUtil; 22 import org.openide.filesystems.FileObject; 23 import org.openide.loaders.DataObject; 24 import org.openide.loaders.DataObjectNotFoundException; 25 import org.openide.cookies.EditorCookie; 26 import org.netbeans.modules.versioning.util.Utils; 27 28 import javax.swing.text.Document ; 29 import java.io.*; 30 import java.beans.PropertyChangeSupport ; 31 import java.beans.PropertyChangeListener ; 32 import java.util.*; 33 34 37 public abstract class OriginalContent { 38 39 42 public static final String PROP_CONTENT_CHANGED = "contentChanged"; 43 44 protected final File workingCopy; 45 46 protected final PropertyChangeSupport support = new PropertyChangeSupport (this); 47 48 protected OriginalContent(File workingCopy) { 49 this.workingCopy = FileUtil.normalizeFile(workingCopy); 50 } 51 52 58 public Reader getText() { 59 FileObject fo = FileUtil.toFileObject(workingCopy); 60 if (fo == null) return null; 61 62 File tempFolder = Utils.getTempFolder(); 63 64 Set<File> filesToCheckout = new HashSet<File>(2); 65 filesToCheckout.add(workingCopy); 66 DataObject dao = null; 67 try { 68 dao = DataObject.find(fo); 69 Set<FileObject> fileObjects = dao.files(); 70 for (FileObject fileObject : fileObjects) { 71 File file = FileUtil.toFile(fileObject); 72 filesToCheckout.add(file); 73 } 74 } catch (DataObjectNotFoundException e) { 75 } 77 78 try { 79 getOriginalFiles(tempFolder, filesToCheckout); 80 } catch (Exception e) { 81 return null; 83 } 84 return createReader(new File(tempFolder, workingCopy.getName())); 85 } 86 87 94 protected abstract void getOriginalFiles(File destination, Set<File> files) throws Exception ; 95 96 public final File getWorkingCopy() { 97 return workingCopy; 98 } 99 100 103 public final boolean equals(Object o) { 104 return (o instanceof OriginalContent) && workingCopy.equals(((OriginalContent) o).workingCopy); 105 } 106 107 110 public final int hashCode() { 111 return workingCopy.hashCode(); 112 } 113 114 private Reader createReader(File file) { 115 try { 116 FileObject fo = FileUtil.toFileObject(FileUtil.normalizeFile(file)); 117 DataObject dao = DataObject.find(fo); 118 EditorCookie ec = dao.getCookie(EditorCookie.class); 119 Document doc = ec.openDocument(); 120 return new StringReader(doc.getText(0, doc.getLength())); 121 } catch (Exception e) { 122 } 124 try { 125 return new FileReader(file); 126 } catch (FileNotFoundException e) { 127 return null; 128 } 129 } 130 131 public void addPropertyChangeListener(PropertyChangeListener listener) { 132 support.addPropertyChangeListener(listener); 133 } 134 135 public void removePropertyChangeListener(PropertyChangeListener listener) { 136 support.removePropertyChangeListener(listener); 137 } 138 } 139 | Popular Tags |