KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > loaders > DefaultES


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
21 package org.openide.loaders;
22
23
24 import java.io.IOException JavaDoc;
25
26 import org.openide.cookies.CloseCookie;
27 import org.openide.cookies.EditCookie;
28 import org.openide.cookies.EditorCookie;
29 import org.openide.cookies.OpenCookie;
30 import org.openide.cookies.PrintCookie;
31 import org.openide.cookies.SaveCookie;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileLock;
34 import org.openide.loaders.DataObject;
35 import org.openide.loaders.MultiDataObject;
36 import org.openide.nodes.CookieSet;
37 import org.openide.nodes.Node.Cookie;
38 import org.openide.text.DataEditorSupport;
39 import org.openide.windows.CloneableOpenSupport;
40
41
42 /**
43  * Basic editor support.
44  *
45  * @author Jaroslav Tulach
46  */

47 final class DefaultES extends DataEditorSupport
48 implements OpenCookie, EditCookie, EditorCookie.Observable, PrintCookie, CloseCookie {
49     /** SaveCookie for this support instance. The cookie is adding/removing
50      * data object's cookie set depending on if modification flag was set/unset. */

51     private final SaveCookie saveCookie = new SaveCookie() {
52         /** Implements <code>SaveCookie</code> interface. */
53         public void save() throws IOException JavaDoc {
54             DefaultES.this.saveDocument();
55         }
56     };
57     
58     private CookieSet set;
59     
60     /** Constructor.
61      * @param obj data object to work on
62      * @param set set to add/remove save cookie from
63      */

64     DefaultES (DataObject obj, MultiDataObject.Entry entry, CookieSet set) {
65         super(obj, new Environment(obj, entry));
66         this.set = set;
67         setMIMEType("text/plain"); // NOI18N
68
}
69     
70     /**
71      * Overrides superclass method. Adds adding of save cookie if the document has been marked modified.
72      * @return true if the environment accepted being marked as modified
73      * or false if it has refused and the document should remain unmodified
74      */

75     protected boolean notifyModified () {
76         if (!super.notifyModified())
77             return false;
78
79         addSaveCookie();
80
81         return true;
82     }
83
84     /** Overrides superclass method. Adds removing of save cookie. */
85     protected void notifyUnmodified () {
86         super.notifyUnmodified();
87
88         removeSaveCookie();
89     }
90
91     /** Helper method. Adds save cookie to the data object. */
92     private void addSaveCookie() {
93         DataObject obj = getDataObject();
94
95         // Adds save cookie to the data object.
96
if(obj.getCookie(SaveCookie.class) == null) {
97             set.add(saveCookie);
98             obj.setModified(true);
99         }
100     }
101
102     /** Helper method. Removes save cookie from the data object. */
103     private void removeSaveCookie() {
104         DataObject obj = getDataObject();
105         
106         // Remove save cookie from the data object.
107
Cookie cookie = obj.getCookie(SaveCookie.class);
108
109         if(cookie != null && cookie.equals(saveCookie)) {
110             set.remove(saveCookie);
111             obj.setModified(false);
112         }
113     }
114
115     
116     /** Nested class. Environment for this support. Extends
117      * <code>DataEditorSupport.Env</code> abstract class.
118      */

119     
120     private static class Environment extends DataEditorSupport.Env {
121         private static final long serialVersionUID = 5451434321155443431L;
122         
123         private MultiDataObject.Entry entry;
124         
125         /** Constructor. */
126         public Environment(DataObject obj, MultiDataObject.Entry entry) {
127             super(obj);
128             this.entry = entry;
129         }
130
131         
132         /** Implements abstract superclass method. */
133         protected FileObject getFile() {
134             return entry.getFile();
135         }
136
137         /** Implements abstract superclass method.*/
138         protected FileLock takeLock() throws IOException JavaDoc {
139             return entry.takeLock();
140         }
141
142         /**
143          * Overrides superclass method.
144          * @return text editor support (instance of enclosing class)
145          */

146         public CloneableOpenSupport findCloneableOpenSupport() {
147             DataObject obj = getDataObject ();
148             DefaultES ret;
149             if (obj instanceof DefaultDataObject) {
150                 ret = (DefaultES)((DefaultDataObject)obj).getCookie (DefaultES.class, true);
151             } else {
152                 ret = (DefaultES)getDataObject().getCookie(DefaultES.class);
153             }
154             
155             // this is necessary as for large files, this methods sets flag that
156
// prevents UserQuestionException
157
super.findCloneableOpenSupport ();
158
159             return ret;
160         }
161     } // End of nested Environment class.
162

163 }
164
Popular Tags