KickJava   Java API By Example, From Geeks To Geeks.

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


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

42 final class SimpleES extends DataEditorSupport
43 implements OpenCookie, EditCookie, EditorCookie.Observable, PrintCookie, CloseCookie {
44     /** SaveCookie for this support instance. The cookie is adding/removing
45      * data object's cookie set depending on if modification flag was set/unset. */

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

59     SimpleES (DataObject obj, MultiDataObject.Entry entry, CookieSet set) {
60         super(obj, new Environment(obj, entry));
61         this.set = set;
62     }
63     
64     /**
65      * Overrides superclass method. Adds adding of save cookie if the document has been marked modified.
66      * @return true if the environment accepted being marked as modified
67      * or false if it has refused and the document should remain unmodified
68      */

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

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

140         public CloneableOpenSupport findCloneableOpenSupport() {
141             return (SimpleES)getDataObject().getCookie(SimpleES.class);
142         }
143     } // End of nested Environment class.
144

145 }
146
Popular Tags