KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > diff > builtin > visualizer > TextDiffEditorSupport


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.diff.builtin.visualizer;
21
22 import java.awt.event.*;
23 import java.beans.*;
24 import java.io.*;
25 import java.util.*;
26
27 import javax.swing.text.*;
28
29 import org.openide.actions.*;
30 import org.openide.cookies.EditorCookie;
31 import org.openide.cookies.OpenCookie;
32 import org.openide.cookies.CloseCookie;
33 import org.openide.cookies.PrintCookie;
34 import org.openide.filesystems.*;
35 import org.openide.loaders.*;
36 import org.openide.text.CloneableEditor;
37 import org.openide.text.CloneableEditorSupport;
38 import org.openide.windows.*;
39 //import org.openide.util.Task;
40
//import org.openide.util.TaskListener;
41
import org.openide.util.HelpCtx;
42 import org.openide.util.NbBundle;
43
44 /** Support for associating an editor and a Swing {@link Document} to a revision object.
45  * This is a modification of org.openide.text.DataEditorSupport
46  *
47  * @author Jaroslav Tulach, Martin Entlicher
48  */

49 public class TextDiffEditorSupport extends CloneableEditorSupport implements EditorCookie.Observable, OpenCookie, PrintCookie, CloseCookie {
50
51     /** The difference */
52     //private final DiffsListWithOpenSupport diff;
53
private final TextDiffVisualizer.TextDiffInfo diff;
54     
55     /*
56     public static final class DiffsListWithOpenSupport extends Object {
57         
58         private Difference[] diffs;
59         private final String name;
60         private final String tooltip;
61         private CloneableOpenSupport openSupport;
62         private boolean contextMode = false;
63         private int contextNumLines;
64         private Reader r1, r2;
65         
66         public DiffsListWithOpenSupport(Difference[] diffs, String name, String tooltip) {
67             this.diffs = diffs;
68             this.name = name;
69             this.tooltip = tooltip;
70         }
71         
72         /*
73         public void setOpenSupport(CloneableOpenSupport openSupport) {
74             this.openSupport = openSupport;
75         }
76          *
77         public Difference[] getDiffs() {
78             return diffs;
79         }
80         
81         public CloneableOpenSupport getOpenSupport() {
82             if (openSupport == null) {
83                 openSupport = new TextDiffEditorSupport(this);
84             }
85             return openSupport;
86         }
87         
88         public String getName() {
89             return name;
90         }
91         
92         public String getTooltip() {
93             return tooltip;
94         }
95         
96         /** Setter for property contextMode.
97          * @param contextMode New value of property contextMode.
98          *
99         public void setContextMode(boolean contextMode, int contextNumLines) {
100             this.contextMode = contextMode;
101             this.contextNumLines = contextNumLines;
102         }
103         
104         /** Getter for property contextMode.
105          * @return Value of property contextMode.
106          *
107         public boolean isContextMode() {
108             return contextMode;
109         }
110         
111         public int getContextNumLines() {
112             return contextNumLines;
113         }
114         
115         public void setReaders(Reader r1, Reader r2) {
116             this.r1 = r1;
117             this.r2 = r2;
118         }
119         
120         public Reader getFirstReader() {
121             return r1;
122         }
123         
124         public Reader getSecondReader() {
125             return r2;
126         }
127         
128     }
129      */

130             
131     
132     /** Editor support for a given data object. The file is taken from the
133      * data object and is updated if the object moves or renames itself.
134      * @param obj object to work with
135      * @param env environment to pass to
136      */

137     TextDiffEditorSupport(TextDiffVisualizer.TextDiffInfo diff) {//DiffsListWithOpenSupport diff) {
138
super (new TextDiffEditorSupport.Env(diff));
139         this.diff = diff;
140     }
141     
142     /** Getter of the file object that this support is associated with.
143      * @return file object passed in constructor
144      */

145     public final FileObject getFileObject () {
146         return null;
147     }
148
149     /** Message to display when an object is being opened.
150      * @return the message or null if nothing should be displayed
151      */

152     protected String JavaDoc messageOpening () {
153         return NbBundle.getMessage (TextDiffEditorSupport.class , "CTL_ObjectOpen", // NOI18N
154
diff.getName()
155         );
156     }
157     
158
159     /** Message to display when an object has been opened.
160     * @return the message or null if nothing should be displayed
161     */

162     protected String JavaDoc messageOpened () {
163         return NbBundle.getMessage (TextDiffEditorSupport.class, "CTL_ObjectOpened", // NOI18N
164
diff.getName()
165         );
166     }
167
168     /** Constructs message that should be displayed when the data object
169     * is modified and is being closed.
170     *
171     * @return text to show to the user
172     */

173     protected String JavaDoc messageSave () {
174         return "";/*NbBundle.getMessage (
175             DataEditorSupport.class,
176             "MSG_SaveFile", // NOI18N
177             obj.getName()
178         );*/

179     }
180     
181     /** Constructs message that should be used to name the editor component.
182     *
183     * @return name of the editor
184     */

185     protected String JavaDoc messageName () {
186         return diff.getName();
187     }
188     
189     /** Text to use as tooltip for component.
190      *
191      * @return text to show to the user
192      */

193     protected String JavaDoc messageToolTip () {
194         // update tooltip
195
return diff.getTitle();
196     }
197     
198     /** Annotates the editor with icon from the data object and also sets
199      * appropriate selected node.
200      * This implementation also listen to display name and icon chamges of the
201      * node and keeps editor top component up-to-date. If you override this
202      * method and not call super, please note that you will have to keep things
203      * synchronized yourself.
204      *
205      * @param editor the editor that has been created and should be annotated
206      */

207     protected void initializeCloneableEditor (CloneableEditor editor) {
208         editor.setIcon(org.openide.util.Utilities.loadImage("org/netbeans/modules/diff/diffSettingsIcon.gif", true));
209         //ourNode.getIcon (java.beans.BeanInfo.ICON_COLOR_16x16));
210
//nodeL = new DataNodeListener(editor);
211
//ourNode.addNodeListener(WeakListener.node(nodeL, ourNode));
212
}
213     
214     protected CloneableEditor createCloneableEditor() {
215         return new DiffCloneableEditor(this);
216     }
217     
218     /** Let's the super method create the document and also annotates it
219     * with Title and StreamDescription properities.
220     *
221     * @param kit kit to user to create the document
222     * @return the document annotated by the properties
223     */

224     protected StyledDocument createStyledDocument (EditorKit kit) {
225         StyledDocument doc = super.createStyledDocument (kit);
226             
227         // set document name property
228
doc.putProperty(javax.swing.text.Document.TitleProperty,
229             diff.getName()
230         );
231         /* set dataobject to stream desc property
232         doc.putProperty(javax.swing.text.Document.StreamDescriptionProperty,
233             obj
234         );
235          */

236         return doc;
237     }
238     
239     CloneableTopComponent createCloneableTopComponentForMe() {
240         return createCloneableTopComponent();
241     }
242     
243     /** Getter for data object associated with this
244     * data object.
245     *
246     final DataObject getDataObjectHack () {
247         return obj;
248     }
249      */

250     
251     /** Environment that connects the data object and the CloneableEditorSupport.
252     */

253     public static class Env extends Object JavaDoc implements CloneableOpenSupport.Env, CloneableEditorSupport.Env, java.io.Serializable JavaDoc
254                                                       /*PropertyChangeListener, VetoableChangeListener*/ {
255         /** generated Serialized Version UID */
256         static final long serialVersionUID = -2945098431098324441L;
257
258         /** The difference. */
259         private transient TextDiffVisualizer.TextDiffInfo diff;
260         
261         /** Constructor.
262         * @param obj this support should be associated with
263         */

264         public Env (TextDiffVisualizer.TextDiffInfo diff) {//DiffsListWithOpenSupport diff) {
265
this.diff = diff;
266         }
267         
268         /** Locks the file.
269         * @return the lock on the file getFile ()
270         * @exception IOException if the file cannot be locked
271         */

272         //protected abstract FileLock takeLock () throws IOException;
273

274         /** Obtains the input stream.
275         * @exception IOException if an I/O error occures
276         */

277         public InputStream inputStream() throws IOException {
278             if (diff.isContextMode()) {
279                 return TextDiffVisualizer.differenceToContextDiffText(diff);
280             } else {
281                 return TextDiffVisualizer.differenceToLineDiffText(diff.getDifferences());
282             }
283         }
284         
285         /** Obtains the output stream.
286         * @exception IOException if an I/O error occures
287         */

288         public OutputStream outputStream() throws IOException {
289             throw new IOException("No output to a file diff supported.");
290             //return getFileImpl ().getOutputStream (fileLock);
291
}
292         
293         /** Mime type of the document.
294         * @return the mime type to use for the document
295         */

296         public String JavaDoc getMimeType() {
297             return "text/plain"; // NOI18N
298
}
299         
300         /** First of all tries to lock the primary file and
301         * if it succeeds it marks the data object modified.
302         *
303         * @exception IOException if the environment cannot be marked modified
304         * (for example when the file is readonly), when such exception
305         * is the support should discard all previous changes
306         */

307         public void markModified() throws java.io.IOException JavaDoc {
308             throw new IOException("The file revision can not be modified.");
309             /*
310             if (fileLock == null || !fileLock.isValid()) {
311                 fileLock = takeLock ();
312             }
313
314             this.getDataObject ().setModified (true);
315              */

316         }
317         
318         /** Reverse method that can be called to make the environment
319         * unmodified.
320         */

321         public void unmarkModified() {
322             //throw new IOException("The file revision can not be unmodified.");
323
}
324         
325         /** Called from the EnvListener
326         * @param expected is the change expected
327         * @param time of the change
328         *
329         final void fileChanged (boolean expected, long time) {
330             if (expected) {
331                 // newValue = null means do not ask user whether to reload
332                 firePropertyChange (PROP_TIME, null, null);
333             } else {
334                 firePropertyChange (PROP_TIME, null, new Date (time));
335             }
336         }
337          */

338         
339         public void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc propertyChangeListener) {
340         }
341         
342         public boolean isModified() {
343             return false;
344         }
345         
346         public java.util.Date JavaDoc getTime() {
347             return new java.util.Date JavaDoc(System.currentTimeMillis());
348         }
349         
350         public void removeVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc vetoableChangeListener) {
351         }
352         
353         public boolean isValid() {
354             return true;
355         }
356         
357         public void addVetoableChangeListener(java.beans.VetoableChangeListener JavaDoc vetoableChangeListener) {
358         }
359         
360         public void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc propertyChangeListener) {
361         }
362         
363         public CloneableOpenSupport findCloneableOpenSupport() {
364             //return (CloneableOpenSupport) list.getNodeDelegate(revisionItem, null).getCookie(CloneableOpenSupport.class);
365
return diff.getOpenSupport();
366         }
367         
368     } // end of Env
369

370     public static class DiffCloneableEditor extends CloneableEditor {
371         
372         DiffCloneableEditor(CloneableEditorSupport support) {
373             super(support);
374         }
375         
376         /**
377          * When I'm added to some other component I suppose, that I'll be displayed.
378          * In this case this method call componentShowing(). It must be assured,
379          * that the initialization is done.
380          */

381         public void addNotify() {
382             componentShowing();
383             super.addNotify();
384         }
385         
386         private boolean componentShowingCalled = false;
387         /**
388          * The componentShowing() method is used in CloneableEditor to make
389          * some initializations. It calls super method only once, since
390          * it can be called multiple times when called from addNotify() as well.
391          */

392         protected void componentShowing() {
393             if (!componentShowingCalled) {
394                 super.componentShowing();
395                 componentShowingCalled = true;
396             }
397         }
398
399         public HelpCtx getHelpCtx() {
400             return new HelpCtx(TextDiffEditorSupport.class);
401         }
402     }
403     
404     /** Listener on file object that notifies the Env object
405     * that a file has been modified.
406     *
407     private static final class EnvListener extends FileChangeAdapter {
408         /** Reference (Env) *
409         private Reference env;
410         
411         /** @param env environement to use
412         *
413         public EnvListener (Env env) {
414             this.env = new java.lang.ref.WeakReference (env);
415         }
416
417         /** Fired when a file is changed.
418         * @param fe the event describing context where action has taken place
419         *
420         public void fileChanged(FileEvent fe) {
421             Env env = (Env)this.env.get ();
422             if (env == null || env.getFileImpl () != fe.getFile ()) {
423                 // the Env change its file and we are not used
424                 // listener anymore => remove itself from the list of listeners
425                 fe.getFile ().removeFileChangeListener (this);
426                 return;
427             }
428
429             env.fileChanged (fe.isExpected (), fe.getTime ());
430         }
431                 
432     }
433     
434     /** Listener on node representing asociated data object, listens to the
435      * property changes of the node and updates state properly
436      *
437     private final class DataNodeListener extends NodeAdapter {
438         /** Asociated editor *
439         CloneableEditor editor;
440         
441         DataNodeListener (CloneableEditor editor) {
442             this.editor = editor;
443         }
444         
445         public void propertyChange (java.beans.PropertyChangeEvent ev) {
446             
447             if (Node.PROP_DISPLAY_NAME.equals(ev.getPropertyName())) {
448                 updateTitles();
449             }
450             if (Node.PROP_ICON.equals(ev.getPropertyName())) {
451                 editor.setIcon(
452                     getDataObject().getNodeDelegate().getIcon (java.beans.BeanInfo.ICON_COLOR_16x16)
453                 );
454             }
455         }
456         
457     } // end of DataNodeListener
458      */

459     
460 }
461
Popular Tags