KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > spi > OriginalContent


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 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 JavaDoc;
29 import java.io.*;
30 import java.beans.PropertyChangeSupport JavaDoc;
31 import java.beans.PropertyChangeListener JavaDoc;
32 import java.util.*;
33
34 /**
35  * @author Maros Sandor
36  */

37 public abstract class OriginalContent {
38         
39     /**
40      * This property changes when the original content for the working file changes.
41      */

42     public static final String JavaDoc PROP_CONTENT_CHANGED = "contentChanged";
43     
44     protected final File workingCopy;
45
46     protected final PropertyChangeSupport JavaDoc support = new PropertyChangeSupport JavaDoc(this);
47     
48     protected OriginalContent(File workingCopy) {
49         this.workingCopy = FileUtil.normalizeFile(workingCopy);
50     }
51
52     /**
53      * Gets the original content of the working copy. This method is typically only called after the OriginalContent
54      * object is created and once for every property change event.
55      *
56      * @return Reader original content of the working copy or null if the original content is not available
57      */

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             // no dataobject, never mind
76
}
77
78         try {
79             getOriginalFiles(tempFolder, filesToCheckout);
80         } catch (Exception JavaDoc e) {
81             // let providers raise errors when they feel appropriate
82
return null;
83         }
84         return createReader(new File(tempFolder, workingCopy.getName()));
85     }
86     
87     /**
88      * Used only by the getText() method
89      *
90      * @param files set of current files
91      * @param destination folder where to put original versions of supplied files
92      * @throws Exception in case any error occurs
93      */

94     protected abstract void getOriginalFiles(File destination, Set<File> files) throws Exception JavaDoc;
95
96     public final File getWorkingCopy() {
97         return workingCopy;
98     }
99
100     /**
101      * Ensures proper behavior for hashing.
102      */

103     public final boolean equals(Object JavaDoc o) {
104         return (o instanceof OriginalContent) && workingCopy.equals(((OriginalContent) o).workingCopy);
105     }
106
107     /**
108      * Ensures proper behavior for hashing.
109      */

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 JavaDoc doc = ec.openDocument();
120             return new StringReader(doc.getText(0, doc.getLength()));
121         } catch (Exception JavaDoc e) {
122             // something's wrong, read the file from disk
123
}
124         try {
125             return new FileReader(file);
126         } catch (FileNotFoundException e) {
127             return null;
128         }
129     }
130
131     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
132         support.addPropertyChangeListener(listener);
133     }
134
135     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
136         support.removePropertyChangeListener(listener);
137     }
138 }
139
Popular Tags