KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > forte > node > XMLCEditorSupport


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  * Lisa Reese
21  *
22  */

23
24 package org.enhydra.kelp.forte.node;
25
26 import java.io.IOException JavaDoc;
27
28 import org.openide.cookies.EditCookie;
29 import org.openide.cookies.EditorCookie;
30 import org.openide.cookies.OpenCookie;
31 import org.openide.cookies.PrintCookie;
32 import org.openide.cookies.SaveCookie;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileLock;
35 import org.openide.nodes.Node.Cookie;
36 import org.openide.text.DataEditorSupport;
37 import org.openide.windows.CloneableOpenSupport;
38 import org.openide.loaders.DataObject;
39 import org.openide.loaders.MultiDataObject;
40 //import org.openide.filesystems.FileSystem.Environment;
41

42 /**
43  *
44  * @author rees0234
45  * @version
46  */

47 public class XMLCEditorSupport extends DataEditorSupport implements OpenCookie, EditCookie, EditorCookie, PrintCookie {
48
49     /** Creates new XMLCEditorSupport */
50     public XMLCEditorSupport(DataObject obj) {
51         super(obj, new Environment(obj));
52
53         setMIMEType("text/xml"); // NOI18N
54
}
55     /** SaveCookie for this support instance. The cookie is adding/removing
56      * data object's cookie set depending on if modification flag was set/unset. */

57     private final SaveCookie saveCookie = new SaveCookie() {
58         /** Implements <code>SaveCookie</code> interface. */
59         public void save() throws IOException JavaDoc {
60             XMLCEditorSupport.this.saveDocument();
61             XMLCEditorSupport.this.getDataObject().setModified(false);
62         }
63     };
64
65     /**
66      * Overrides superclass method. Adds adding of save cookie if the document has been marked modified.
67      * @return true if the environment accepted being marked as modified
68      * or false if it has refused and the document should remain unmodified
69      */

70     protected boolean notifyModified () {
71         if (!super.notifyModified())
72             return false;
73
74         addSaveCookie();
75
76         return true;
77     }
78
79     /** Overrides superclass method. Adds removing of save cookie. */
80     protected void notifyUnmodified () {
81         super.notifyUnmodified();
82
83         removeSaveCookie();
84     }
85
86     /** Helper method. Adds save cookie to the data object. */
87     private void addSaveCookie() {
88         XMLCMultiDataObject obj = (XMLCMultiDataObject)getDataObject();
89
90         // Adds save cookie to the data object.
91
if(obj.getCookie(SaveCookie.class) == null) {
92             obj.getXMLCCookieSet().add(saveCookie);
93             obj.setModified(true);
94         }
95     }
96
97     /** Helper method. Removes save cookie from the data object. */
98     private void removeSaveCookie() {
99         XMLCMultiDataObject obj = (XMLCMultiDataObject)getDataObject();
100
101         // Remove save cookie from the data object.
102
Cookie cookie = obj.getCookie(SaveCookie.class);
103
104         if(cookie != null && cookie.equals(saveCookie)) {
105             obj.getXMLCCookieSet().remove(saveCookie);
106             obj.setModified(false);
107         }
108     }
109
110
111     /** Nested class. Environment for this support. Extends <code>DataEditorSupport.Env</code> abstract class. */
112     private static class Environment extends DataEditorSupport.Env {
113
114         /** Constructor. */
115         public Environment(DataObject obj) {
116             super(obj);
117         }
118
119
120         /** Implements abstract superclass method. */
121         protected FileObject getFile() {
122             return getDataObject().getPrimaryFile();
123         }
124
125         /** Implements abstract superclass method.*/
126         protected FileLock takeLock() throws IOException JavaDoc {
127             return ((XMLCMultiDataObject)getDataObject()).getPrimaryEntry().takeLock();
128         }
129
130         /**
131          * Overrides superclass method.
132          * @return xmlc editor support (instance of enclosing class)
133          */

134         public CloneableOpenSupport findCloneableOpenSupport() {
135             return (XMLCEditorSupport)((XMLCMultiDataObject)getDataObject()).getXMLCCookieSet().getCookie(XMLCEditorSupport.class);
136         }
137     } // End of nested Environment class.
138

139 }
140
141
Popular Tags