KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > EditorSupport


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.openide.text;
21
22
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.io.*;
25 import javax.swing.JEditorPane JavaDoc;
26 import javax.swing.event.ChangeListener JavaDoc;
27 import javax.swing.text.*;
28 import org.openide.awt.UndoRedo;
29 import org.openide.cookies.*;
30 import org.openide.filesystems.*;
31 import org.openide.loaders.*;
32 import org.openide.nodes.*;
33 import org.openide.text.CloneableEditorSupport.Pane;
34 import org.openide.util.Exceptions;
35 import org.openide.util.Task;
36 import org.openide.util.actions.SystemAction;
37 import org.openide.windows.*;
38
39 /** Support for associating an editor and a Swing {@link Document} to a data object.
40  * Can be assigned as a cookie to any editable data object.
41  * Then this data object will be capable of being opened in an editor, and there will be ways of retrieving and acting upon the Swing document which is editing it.
42 *
43 * @author Jaroslav Tulach
44 * @deprecated Use DataEditorSupport instead
45 */

46 @Deprecated JavaDoc
47 public class EditorSupport extends OpenSupport
48 implements EditorCookie.Observable, OpenCookie, CloseCookie, PrintCookie {
49     /** Common name for editor mode.
50      * @deprecated Use {@link org.openide.text.CloneableEditorSupport#EDITOR_MODE} instead.
51      */

52     @Deprecated JavaDoc
53     public static final String JavaDoc EDITOR_MODE = CloneableEditorSupport.EDITOR_MODE;
54
55     /** @deprecated no longer used */
56     @Deprecated JavaDoc
57     protected String JavaDoc modifiedAppendix = " *"; // NOI18N
58

59     /** The flag saying if we should listen to the document modifications */
60     private boolean listenToModifs = true;
61     
62     /** delegating support */
63     private Del del;
64
65     /** Support an existing loader entry. The file is taken from the
66     * entry and is updated if the entry is moved or renamed.
67     * @param entry entry to create instance from
68     */

69     public EditorSupport(MultiDataObject.Entry entry) {
70         super(entry, new DelEnv(entry.getDataObject()));
71
72         del = new Del (
73             entry.getDataObject (),
74             (DelEnv)env,
75             allEditors
76         );
77     }
78     
79     /** Message to display when an object is being opened.
80     * @return the message or null if nothing should be displayed
81     */

82     protected String JavaDoc messageOpening () {
83         return del.superMessageOpening ();
84     }
85     
86     /** Message to display when an object has been opened.
87     * @return the message or null if nothing should be displayed
88     */

89     protected String JavaDoc messageOpened () {
90         return del.superMessageOpened ();
91     }
92
93     /** Constructs message that should be displayed when the data object
94     * is modified and is being closed.
95     *
96     * @return text to show to the user
97     */

98     protected String JavaDoc messageSave () {
99         return del.superMessageSave ();
100     }
101     
102     /** Constructs message that should be used to name the editor component.
103     *
104     * @return name of the editor
105     */

106     protected String JavaDoc messageName () {
107         return del.superMessageName ();
108     }
109     
110     /** Text to use as tooltip for component.
111     *
112     * @return text to show to the user
113     */

114     protected String JavaDoc messageToolTip () {
115         return del.superMessageToolTip ();
116     }
117     
118     
119     /** Updates titles of all editors.
120     */

121     protected void updateTitles () {
122         del.superUpdateTitles ();
123     }
124     
125
126     /* A method to create a new component. Overridden in subclasses.
127     * @return the {@link Editor} for this support
128     */

129     protected CloneableTopComponent createCloneableTopComponent () {
130         // initializes the document if not initialized
131
prepareDocument ();
132
133         DataObject obj = findDataObject ();
134         Editor editor = new Editor (obj);
135         return editor;
136     }
137
138     /** Create an undo/redo manager.
139     * This manager is then attached to the document, and listens to
140     * all changes made in it.
141     * <P>
142     * The default implementation simply uses <code>UndoRedo.Manager</code>.
143     *
144     * @return the undo/redo manager
145     */

146     protected UndoRedo.Manager createUndoRedoManager () {
147         return del.superUndoRedoManager ();
148     }
149
150     /** Passes the actual opening to internal delegate support.
151      * Overrides superclass method. */

152     public void open() {
153         del.open();
154     }
155
156     // editor cookie .......................................................................
157

158     /** Closes all opened editors (if the user agrees) and
159     * flushes content of the document to the file.
160     *
161     * @return <code>false</code> if the operation is cancelled
162     */

163     public boolean close () {
164         return del.close ();
165     }
166     
167     /** Closes the editor, asks if necessary.
168      * @param ask true if we should ask the user
169      * @return true if succesfully closed
170      */

171     protected boolean close (boolean ask) {
172         return del.superClose (ask);
173     }
174
175     /** Load the document into memory. This is done
176     * in different thread. A task for the thread is returned
177     * so anyone may test whether the loading has been finished
178     * or is still in process.
179     *
180     * @return task for control over loading
181     */

182     public synchronized Task prepareDocument () {
183         return del.prepareDocument ();
184     }
185
186     /** Get the document associated with this cookie.
187     * It is an instance of Swing's {@link StyledDocument} but it should
188     * also understand the NetBeans {@link org.openide.text.NbDocument#GUARDED} to
189     * prevent certain lines from being edited by the user.
190     * <P>
191     * If the document is not loaded the method blocks until
192     * it is.
193     *
194     * @return the styled document for this cookie that
195     * understands the guarded attribute
196     * @exception IOException if the document could not be loaded
197     */

198     public StyledDocument openDocument () throws IOException {
199         return del.openDocument ();
200     }
201
202     /** Get the document. This method may be called before the document initialization
203      * (<code>prepareTask</code>)
204      * has been completed, in such a case the document must not be modified.
205      * @return document or <code>null</code> if it is not yet loaded
206      */

207     public StyledDocument getDocument () {
208         return del.getDocument ();
209     }
210
211     /** Test whether the document is in memory, or whether loading is still in progress.
212     * @return <code>true</code> if document is loaded
213     */

214     public boolean isDocumentLoaded() {
215         return del.isDocumentLoaded ();
216     }
217
218     /** Save the document in this thread.
219     * Create 'orig' document for the case that the save would fail.
220     * @exception IOException on I/O error
221     */

222     public void saveDocument () throws IOException {
223         del.superSaveDocument ();
224     }
225
226     /**
227      * Actually write file data to an output stream from an editor kit's document.
228      * Called during a file save by {@link #saveDocument}.
229      * <p>The default implementation just calls {@link EditorKit#write(OutputStream, Document, int, int) EditorKit.write(...)}.
230      * Subclasses could override this to provide support for persistent guard blocks, for example.
231      * @param doc the document to write from
232      * @param kit the associated editor kit
233      * @param stream the open stream to write to
234      * @throws IOException if there was a problem writing the file
235      * @throws BadLocationException should not normally be thrown
236      * @see #loadFromStreamToKit
237      */

238     protected void saveFromKitToStream (StyledDocument doc, EditorKit kit, OutputStream stream) throws IOException, BadLocationException {
239         del.superSaveFromKitToStream (doc, kit, stream);
240     }
241
242     /** Test whether the document is modified.
243     * @return <code>true</code> if the document is in memory and is modified;
244     * otherwise <code>false</code>
245     */

246     public boolean isModified () {
247         return del.isModified ();
248     }
249
250     /** Finds data object the entry belongs to.
251     * @return data object or null
252     */

253     protected MultiDataObject findDataObject () {
254         return entry.getDataObject ();
255     }
256
257     /** Create a position reference for the given offset.
258     * The position moves as the document is modified and
259     * reacts to closing and opening of the document.
260     *
261     * @param offset the offset to create position at
262     * @param bias the Position.Bias for new creating position.
263     * @return position reference for that offset
264     */

265     public final PositionRef createPositionRef (int offset, Position.Bias bias) {
266         return del.createPositionRef (offset, bias);
267     }
268
269     /** Get the line set for all paragraphs in the document.
270     * @return positions of all paragraphs on last save
271     */

272     public Line.Set getLineSet () {
273         return del.getLineSet ();
274     }
275
276     // other public methods ................................................................
277

278     /**
279     * Set the MIME type for the document.
280     * @param s the new MIME type
281     */

282     public void setMIMEType (String JavaDoc s) {
283         del.setMIMEType (s);
284     }
285
286     /** @deprecated has no effect
287     */

288     @Deprecated JavaDoc
289     public void setActions (SystemAction[] actions) {
290     }
291
292     /** Creates editor kit for this source.
293     * @return editor kit
294     */

295     protected EditorKit createEditorKit () {
296         return del.superCreateEditorKit ();
297     }
298
299     /** Utility method which enables or disables listening to modifications
300     * on asociated document.
301     * <P>
302     * Could be useful if we have to modify document, but do not want the
303     * Save and Save All actions to be enabled/disabled automatically.
304     * Initially modifications are listened to.
305     * @param listenToModifs whether to listen to modifications
306     */

307     public void setModificationListening (final boolean listenToModifs) {
308         this.listenToModifs = listenToModifs;
309     }
310
311     /** Adds a listener for status changes. An event is fired
312     * when the document is moved or removed from memory.
313     * @param l new listener
314     */

315     public void addChangeListener (ChangeListener JavaDoc l) {
316         del.addChangeListener (l);
317     }
318
319     /** Removes a listener for status changes.
320      * @param l listener to remove
321     */

322     public void removeChangeListener (ChangeListener JavaDoc l) {
323         del.removeChangeListener (l);
324     }
325
326     public final void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
327         del.addPropertyChangeListener (l);
328     }
329     
330     public final void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
331         del.removePropertyChangeListener (l);
332     }
333
334     /** The implementation of @see org.openide.cookies.PrintCookie#print() method. */
335     public void print() {
336         del.print ();
337     }
338
339     /**
340      * Actually read file data into an editor kit's document from an input stream.
341      * Called during a file load by {@link #prepareDocument}.
342      * <p>The default implementation just calls {@link EditorKit#read(InputStream, Document, int) EditorKit.read(...)}.
343      * Subclasses could override this to provide support for persistent guard blocks, for example.
344      * @param doc the document to read into
345      * @param stream the open stream to read from
346      * @param kit the associated editor kit
347      * @throws IOException if there was a problem reading the file
348      * @throws BadLocationException should not normally be thrown
349      * @see #saveFromKitToStream
350      */

351     protected void loadFromStreamToKit (StyledDocument doc, InputStream stream, EditorKit kit) throws IOException, BadLocationException {
352         del.superLoadFromStreamToKit (doc, stream, kit);
353     }
354     
355     /** Reload the document in response to external modification.
356     * @see #reloadDocumentTask
357     */

358     protected void reloadDocument() {
359         reloadDocumentTask ().waitFinished ();
360     }
361
362     /** Starts reloading of document. Could not be named reloadDocument,
363      * because of backward compatibility.
364      *
365      * @return task one can listen on when reloading the document
366      */

367     protected Task reloadDocumentTask () {
368         return del.superReloadDocument ();
369     }
370
371     /** Forcibly create one editor component. Then set the caret
372     * to the given position.
373     * @param pos where to place the caret
374     * @return always non-<code>null</code> editor
375     */

376     protected Editor openAt(PositionRef pos) {
377         CloneableEditorSupport.Pane p = del.openAt (pos, -1);
378         if (p instanceof Editor) {
379             return (Editor)p;
380         }
381         java.awt.Component JavaDoc c = p.getEditorPane();
382         for (;;) {
383             if (c instanceof Editor) {
384                 return (Editor)c;
385             }
386             c = c.getParent();
387         }
388     }
389
390     /** Should test whether all data is saved, and if not, prompt the user
391     * to save.
392     *
393     * @return <code>true</code> if everything can be closed
394     */

395     protected boolean canClose () {
396         return del.superCanClose ();
397     }
398
399     /* List of all JEditorPane's opened by this editor support.
400     * The first item in the array should represent the component
401     * that is currently selected or has been selected lastly.
402     *
403     * @return array of panes or null if no pane is opened.
404     * In no case empty array is returned.
405     */

406     public JEditorPane JavaDoc[] getOpenedPanes () {
407         return del.getOpenedPanes ();
408     }
409
410     /** Notification method called when the document become unmodified.
411     * Called after save or after reload of document.
412     */

413     protected void notifyUnmodified () {
414         EditorSupport.this.modifySaveCookie (false);
415         del.superNotifyUnmodified ();
416     }
417
418     /** Overrides the super method to add a save cookie if the
419     * document has been marked modified.
420     *
421     * @return true if the environment accepted being marked as modified
422     * or false if it refused it and the document should still be unmodified
423     */

424     protected boolean notifyModified () {
425         if (del.superNotifyModified ()) {
426             EditorSupport.this.modifySaveCookie (true);
427             return true;
428         } else {
429             return false;
430         }
431     }
432
433
434     /** Called when the document is closed and released from memory.
435     */

436     protected void notifyClosed() {
437         del.superNotifyClosed ();
438     }
439
440     
441     /** Utility method to extract EditorSupport from Del instance.
442      * @param ces cloneable editor support
443      * @return EditorSupport
444      * @exception ClassCastException if the variables do not match
445      */

446     static EditorSupport extract (CloneableEditorSupport ces) {
447         EditorSupport.Del del = (Del)ces;
448         return del.es ();
449     }
450     
451     
452     /** Modifies the save cookie, if necessary
453     * @param add true if we should add the cookie
454     */

455     final void modifySaveCookie (boolean add) {
456         if (listenToModifs) {
457             if (add) {
458                 ((EntryEnv)env).addSaveCookie ();
459             } else {
460                 ((EntryEnv)env).removeSaveCookie ();
461             }
462         }
463     }
464
465     
466     /** Cloneable top component to hold the editor kit.
467     */

468     public static class Editor extends CloneableEditor {
469         /** data object to work with */
470         protected DataObject obj;
471
472         static final long serialVersionUID =-185739563792410059L;
473         
474         /** For externalization of subclasses only */
475         public Editor () {
476             super();
477         }
478
479         /** Constructor
480         * @param obj data object we belong to. The appropriate editor support is
481         * acquired as the DataObject's EditorSupport.class cookie.
482         */

483         public Editor (DataObject obj) {
484             this(obj, (EditorSupport)obj.getCookie(EditorSupport.class));
485         }
486
487         /** Constructor
488         * @param obj data object we belong to.
489         * @param support editor support to use.
490         */

491         public Editor (DataObject obj, EditorSupport support) {
492             super (support.del);
493             this.obj = obj;
494         }
495         
496         /* Deserialize this top component.
497         * @param in the stream to deserialize from
498         */

499         public void readExternal (ObjectInput in)
500         throws IOException, ClassNotFoundException JavaDoc {
501             super.readExternal(in);
502             
503             Object JavaDoc ces = cloneableEditorSupport ();
504             if (ces instanceof Del) {
505                 obj = ((Del)ces).getDataObjectHack2 ();
506             }
507             
508         }
509
510     } // end of Editor inner class
511

512     /** Special implementation of CloneableEditorSupport that is used
513     * by all methods of EditorSupport to delegate on it.
514     */

515     private final class Del extends DataEditorSupport implements EditorCookie.Observable {
516         
517         /** Listener on node changes. */
518         private NodeListener nodeL;
519         
520         
521         /** Constrcutor. Takes environemnt and ref object to replace
522         * the default one
523         */

524         public Del (
525             DataObject obj,
526             CloneableEditorSupport.Env env,
527             CloneableTopComponent.Ref ref
528         ) {
529             super (obj, env);
530             this.allEditors = ref;
531         }
532         
533         /** Getter */
534         public final EditorSupport es () {
535             return EditorSupport.this;
536         }
537         
538         protected void notifyUnmodified () {
539             EditorSupport.this.notifyUnmodified ();
540         }
541
542         protected boolean notifyModified () {
543             return EditorSupport.this.notifyModified ();
544         }
545
546         protected void notifyClosed() {
547             EditorSupport.this.notifyClosed ();
548         }
549
550         final void superNotifyUnmodified () {
551             super.notifyUnmodified ();
552         }
553
554         final boolean superNotifyModified () {
555             return super.notifyModified ();
556         }
557         final void superNotifyClosed() {
558             // #28256(#27645) Unregisters lisntening on node
559
// when all components closed.
560
nodeL = null;
561             
562             super.notifyClosed ();
563         }
564
565         protected CloneableEditor createCloneableEditor() {
566             if (true) throw new IllegalStateException JavaDoc ("Do not call!");
567             CloneableTopComponent ctc = createCloneableTopComponent();
568             if(ctc instanceof Editor) {
569                 return (CloneableEditor)ctc;
570             } else {
571                 return new Editor(getDataObject());
572             }
573         }
574         
575         
576         protected Pane JavaDoc createPane () {
577             CloneableTopComponent ctc = createCloneableTopComponent();
578             if(ctc instanceof Editor) {
579                 return (CloneableEditor)ctc;
580             } else {
581                 Pane JavaDoc pan = (Pane JavaDoc)ctc.getClientProperty("CloneableEditorSupport.Pane");
582                 if (pan != null) {
583                     return pan;
584                 }
585                 if (ctc instanceof Pane JavaDoc) {
586                     return (Pane JavaDoc)ctc;
587                 }
588                 return new Editor(getDataObject());
589             }
590             
591         }
592         
593         //
594
// Messages
595
//
596
protected String JavaDoc messageToolTip() {
597             return EditorSupport.this.messageToolTip ();
598         }
599         
600         protected String JavaDoc messageName() {
601             return EditorSupport.this.messageName ();
602         }
603         
604         protected String JavaDoc messageOpening() {
605             return EditorSupport.this.messageOpening ();
606         }
607         
608         protected String JavaDoc messageOpened() {
609             return EditorSupport.this.messageOpened ();
610         }
611         
612         protected String JavaDoc messageSave() {
613             return EditorSupport.this.messageSave ();
614         }
615         
616         protected void updateTitles () {
617             EditorSupport.this.updateTitles ();
618         }
619
620
621         final String JavaDoc superMessageToolTip() {
622             return super.messageToolTip ();
623         }
624
625         final String JavaDoc superMessageName() {
626             return super.messageName ();
627         }
628
629         final String JavaDoc superMessageOpening() {
630             return super.messageOpening ();
631         }
632
633         final String JavaDoc superMessageOpened() {
634             return super.messageOpened ();
635         }
636
637         final String JavaDoc superMessageSave() {
638             return super.messageSave ();
639         }
640         
641         final void superUpdateTitles () {
642             super.updateTitles ();
643         }
644         
645         
646         //
647
// close
648
//
649

650         protected boolean close(boolean ask) {
651             return EditorSupport.this.close (ask);
652         }
653         
654         protected boolean superClose(boolean ask) {
655             return super.close (ask);
656         }
657         
658         /** Overrides superclass method. Delegates the creation
659          * of component to enclosing class and adds initializing of
660          * editor component. */

661         protected CloneableTopComponent createCloneableTopComponent() {
662             CloneableTopComponent ctc = EditorSupport.this.createCloneableTopComponent ();
663             if(ctc instanceof CloneableEditor) {
664                 initializeCloneableEditor((CloneableEditor)ctc);
665             }
666             return ctc;
667         }
668         
669         /** Overrides superclass method. Initializes editor component. */
670         protected void initializeCloneableEditor (CloneableEditor editor) {
671             DataObject obj = getDataObject();
672             if(obj.isValid()) {
673                 org.openide.nodes.Node ourNode = obj.getNodeDelegate();
674                 editor.setActivatedNodes(new org.openide.nodes.Node[] {ourNode});
675                 editor.setIcon(ourNode.getIcon (java.beans.BeanInfo.ICON_COLOR_16x16));
676                 NodeListener nl = new DataNodeListener(editor);
677                 ourNode.addNodeListener(org.openide.nodes.NodeOp.weakNodeListener (nl, ourNode));
678                 nodeL = nl;
679             }
680         }
681         
682         
683         //
684
// Stream manipulation
685
//
686
final void superLoadFromStreamToKit(
687             StyledDocument doc,InputStream stream,EditorKit kit
688         ) throws IOException, BadLocationException {
689             super.loadFromStreamToKit (doc, stream, kit);
690         }
691         
692         protected void loadFromStreamToKit(
693             StyledDocument doc,InputStream stream,EditorKit kit
694         ) throws IOException, BadLocationException {
695             EditorSupport.this.loadFromStreamToKit (doc, stream, kit);
696         }
697         
698         protected void superSaveFromKitToStream(
699             StyledDocument doc,EditorKit kit,OutputStream stream
700         ) throws IOException, BadLocationException {
701             super.saveFromKitToStream (doc, kit, stream);
702         }
703         protected void saveFromKitToStream(
704             StyledDocument doc,EditorKit kit,OutputStream stream
705         ) throws IOException, BadLocationException {
706             EditorSupport.this.saveFromKitToStream (doc, kit, stream);
707         }
708
709         protected Task reloadDocument () {
710             return EditorSupport.this.reloadDocumentTask ();
711         }
712
713         final Task superReloadDocument () {
714             return super.reloadDocument ();
715         }
716         
717         public void saveDocument() throws IOException {
718             EditorSupport.this.saveDocument();
719         }
720         
721         final void superSaveDocument() throws IOException {
722             super.saveDocument();
723         }
724         final UndoRedo.Manager superUndoRedoManager() {
725             return super.createUndoRedoManager ();
726         }
727         
728         //
729
// Undo manager
730
//
731
protected UndoRedo.Manager createUndoRedoManager() {
732             return EditorSupport.this.createUndoRedoManager ();
733         }
734         
735         //
736
// Editor kit
737
//
738
EditorKit superCreateEditorKit() {
739             return super.createEditorKit ();
740         }
741         protected EditorKit createEditorKit() {
742             return EditorSupport.this.createEditorKit ();
743         }
744         
745 /*
746         protected StyledDocument createStyledDocument(EditorKit kit) {
747             return EditorSupport.this.createStyledDocument (kit);
748         }
749 */

750         //
751
// canClose
752
//
753
final boolean superCanClose() {
754             return super.canClose ();
755         }
756         protected boolean canClose() {
757             return EditorSupport.this.canClose ();
758         }
759
760
761         /** Class which supports listening on node delegate. */
762         private final class DataNodeListener extends NodeAdapter {
763             /** Asociated editor */
764             private final CloneableEditor editor;
765
766             DataNodeListener (CloneableEditor editor) {
767                 this.editor = editor;
768             }
769
770             public void propertyChange (java.beans.PropertyChangeEvent JavaDoc ev) {
771                 if (Node.PROP_DISPLAY_NAME.equals(ev.getPropertyName())) {
772                     updateTitles();
773                 }
774                 if (Node.PROP_ICON.equals(ev.getPropertyName())) {
775                     final DataObject obj = getDataObject();
776                     if (obj.isValid()) {
777                         org.openide.util.Mutex.EVENT.writeAccess(new Runnable JavaDoc() {
778                             public void run() {
779                         editor.setIcon(obj.getNodeDelegate().getIcon (
780                                 java.beans.BeanInfo.ICON_COLOR_16x16));
781                             }
782                         });
783                     }
784                 }
785             }
786         } // End of DataNodeListener class.
787

788     }
789     
790     
791     /** Implementation of the default Environment for EditorSupport
792     */

793     private static class EntryEnv extends DataEditorSupport.Env
794     implements SaveCookie {
795         /** generated Serialized Version UID */
796         static final long serialVersionUID = 354528097109874355L;
797
798         /** Constructor.
799         * @param obj this support should be associated with
800         */

801         public EntryEnv (MultiDataObject obj) {
802             super (obj);
803         }
804         
805         /** Getter for file associated with this environment.
806         * @return the file input/output operation should be performed on
807         */

808         protected FileObject getFile () {
809             return getDataObject ().getPrimaryFile ();
810         }
811         
812         /** Locks the file.
813         * @return the lock on the file getFile ()
814         * @exception IOException if the file cannot be locked
815         */

816         protected FileLock takeLock () throws IOException {
817             return ((MultiDataObject)getDataObject ()).getPrimaryEntry ().takeLock ();
818         }
819         
820         /** Gives notification that the DataObject was changed.
821         * @param ev PropertyChangeEvent
822         */

823         public void propertyChange(PropertyChangeEvent JavaDoc ev) {
824             if (DataObject.PROP_PRIMARY_FILE.equals(ev.getPropertyName())) {
825                 changeFile ();
826             }
827             if (DataObject.PROP_NAME.equals(ev.getPropertyName())) {
828                 EditorSupport es = (EditorSupport)getDataObject ().getCookie (
829                     EditorSupport.class
830                 );
831                 if (es != null) {
832                     es.updateTitles ();
833                 }
834             }
835             
836             super.propertyChange (ev);
837         }
838         
839         
840         /** Invoke the save operation.
841         * @throws IOException if the object could not be saved
842         */

843         public void save() throws java.io.IOException JavaDoc {
844             // Do not use findCloneableOpenSupport; it will not work if the
845
// DataObject has both an EditorSupport and an OpenSupport attached
846
// at once.
847
EditorSupport es = (EditorSupport)getDataObject ().getCookie (EditorSupport.class);
848             if (es == null)
849                 throw new IOException ("no EditorSupport found on this data object"); // NOI18N
850
else
851                 es.saveDocument ();
852         }
853 /*
854         void clearSaveCookie() {
855             DataObject dataObj = findDataObject();
856             // remove save cookie (if save was succesfull)
857             dataObj.setModified(false);
858             releaseFileLock();
859         }
860 */

861         /** Adds save cookie to the DO.
862         */

863         final void addSaveCookie() {
864             DataObject dataObj = getDataObject ();
865             // add Save cookie to the data object
866
if (dataObj instanceof MultiDataObject) {
867                 if (dataObj.getCookie(SaveCookie.class) == null) {
868                     getCookieSet((MultiDataObject)dataObj).add(this);
869                 }
870             }
871         }
872         
873         /** Removes save cookie from the DO.
874         */

875         final void removeSaveCookie() {
876             DataObject dataObj = getDataObject ();
877             // add Save cookie to the data object
878
if (dataObj instanceof MultiDataObject) {
879                 if (dataObj.getCookie(SaveCookie.class) == this) {
880                     getCookieSet((MultiDataObject)dataObj).remove(this);
881                 }
882             }
883         }
884
885         // UGLY
886
private static java.lang.reflect.Method JavaDoc getCookieSetMethod = null;
887         private static final org.openide.nodes.CookieSet getCookieSet (MultiDataObject obj) {
888             try {
889                 if (getCookieSetMethod == null) {
890                     getCookieSetMethod = MultiDataObject.class.getDeclaredMethod ("getCookieSet", new Class JavaDoc[] { }); // NOI18N
891
getCookieSetMethod.setAccessible (true);
892                 }
893                 return (org.openide.nodes.CookieSet) getCookieSetMethod.invoke (obj, new Object JavaDoc[] { });
894             } catch (Exception JavaDoc e) {
895                 Exceptions.printStackTrace(e);
896                 return new org.openide.nodes.CookieSet ();
897             }
898         }
899         
900         /** Method that allows environment to find its
901          * cloneable open support.
902         * @return the support or null if the environemnt is not in valid
903         * state and the CloneableOpenSupport cannot be found for associated
904         * data object
905         */

906         public CloneableOpenSupport findCloneableOpenSupport() {
907             CloneableOpenSupport s = super.findCloneableOpenSupport ();
908             if (s != null) {
909                 return s;
910             }
911                 
912             EditorSupport es = (EditorSupport)getDataObject ().getCookie (EditorSupport.class);
913             if (es != null) {
914                 return es.del;
915             } else {
916                 return null;
917             }
918         }
919         
920     } // end of EntryEnv
921

922     /** Environment for delegating object.
923     */

924     private static final class DelEnv extends EntryEnv {
925         /** generated Serialized Version UID */
926         static final long serialVersionUID = 174320972368471234L;
927         
928         public DelEnv (MultiDataObject obj) {
929             super (obj);
930         }
931         
932         /** Finds delegating environment for this editor object.
933         */

934         public CloneableOpenSupport findCloneableOpenSupport() {
935         // Svata: is this really needed ? EditorSupport does not implement CloneableOpenSupport anyway.
936
CloneableOpenSupport o = super.findCloneableOpenSupport ();
937             if (o instanceof EditorSupport) {
938                 EditorSupport es = (EditorSupport)o;
939                 return es.del;
940             }
941             return o;
942         }
943     }
944 }
945
Popular Tags