~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to openide/loaders/src/org/openide/loaders/DefaultES.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
 
 
43
package org.openide.loaders;
 
44
 
 
45
 
 
46
import java.io.IOException;
 
47
 
 
48
import org.openide.cookies.CloseCookie;
 
49
import org.openide.cookies.EditCookie;
 
50
import org.openide.cookies.EditorCookie;
 
51
import org.openide.cookies.OpenCookie;
 
52
import org.openide.cookies.PrintCookie;
 
53
import org.openide.cookies.SaveCookie;
 
54
import org.openide.filesystems.FileObject;
 
55
import org.openide.filesystems.FileLock;
 
56
import org.openide.loaders.DataObject;
 
57
import org.openide.loaders.MultiDataObject;
 
58
import org.openide.nodes.CookieSet;
 
59
import org.openide.nodes.Node.Cookie;
 
60
import org.openide.text.DataEditorSupport;
 
61
import org.openide.windows.CloneableOpenSupport;
 
62
 
 
63
 
 
64
/** 
 
65
 * Basic editor support.
 
66
 *
 
67
 * @author Jaroslav Tulach
 
68
 */
 
69
final class DefaultES extends DataEditorSupport 
 
70
implements OpenCookie, EditCookie, EditorCookie.Observable, PrintCookie, CloseCookie, SaveAsCapable {
 
71
    /** SaveCookie for this support instance. The cookie is adding/removing 
 
72
     * data object's cookie set depending on if modification flag was set/unset. */
 
73
    private final SaveCookie saveCookie = new SaveCookie() {
 
74
        /** Implements <code>SaveCookie</code> interface. */
 
75
        public void save() throws IOException {
 
76
            DefaultES.this.saveDocument();
 
77
        }
 
78
    };
 
79
    
 
80
    private CookieSet set;
 
81
    
 
82
    /** Constructor. 
 
83
     * @param obj data object to work on
 
84
     * @param set set to add/remove save cookie from
 
85
     */
 
86
    DefaultES (DataObject obj, MultiDataObject.Entry entry, CookieSet set) {
 
87
        super(obj, new Environment(obj, entry));
 
88
        this.set = set;
 
89
        setMIMEType("text/plain"); // NOI18N
 
90
    }
 
91
    
 
92
    /** 
 
93
     * Overrides superclass method. Adds adding of save cookie if the document has been marked modified.
 
94
     * @return true if the environment accepted being marked as modified
 
95
     *    or false if it has refused and the document should remain unmodified
 
96
     */
 
97
    protected boolean notifyModified () {
 
98
        if (!super.notifyModified()) 
 
99
            return false;
 
100
 
 
101
        addSaveCookie();
 
102
 
 
103
        return true;
 
104
    }
 
105
 
 
106
    /** Overrides superclass method. Adds removing of save cookie. */
 
107
    protected void notifyUnmodified () {
 
108
        super.notifyUnmodified();
 
109
 
 
110
        removeSaveCookie();
 
111
    }
 
112
 
 
113
    /** Helper method. Adds save cookie to the data object. */
 
114
    private void addSaveCookie() {
 
115
        DataObject obj = getDataObject();
 
116
 
 
117
        // Adds save cookie to the data object.
 
118
        if(obj.getCookie(SaveCookie.class) == null) {
 
119
            set.add(saveCookie);
 
120
            obj.setModified(true);
 
121
        }
 
122
    }
 
123
 
 
124
    /** Helper method. Removes save cookie from the data object. */
 
125
    private void removeSaveCookie() {
 
126
        DataObject obj = getDataObject();
 
127
        
 
128
        // Remove save cookie from the data object.
 
129
        Cookie cookie = obj.getCookie(SaveCookie.class);
 
130
 
 
131
        if(cookie != null && cookie.equals(saveCookie)) {
 
132
            set.remove(saveCookie);
 
133
            obj.setModified(false);
 
134
        }
 
135
    }
 
136
 
 
137
    
 
138
    /** Nested class. Environment for this support. Extends
 
139
     * <code>DataEditorSupport.Env</code> abstract class.
 
140
     */
 
141
    
 
142
    private static class Environment extends DataEditorSupport.Env {
 
143
        private static final long serialVersionUID = 5451434321155443431L;
 
144
        
 
145
        private MultiDataObject.Entry entry;
 
146
        
 
147
        /** Constructor. */
 
148
        public Environment(DataObject obj, MultiDataObject.Entry entry) {
 
149
            super(obj);
 
150
            this.entry = entry;
 
151
        }
 
152
 
 
153
        
 
154
        /** Implements abstract superclass method. */
 
155
        protected FileObject getFile() {
 
156
            return entry.getFile();
 
157
        }
 
158
 
 
159
        /** Implements abstract superclass method.*/
 
160
        protected FileLock takeLock() throws IOException {
 
161
            return entry.takeLock();
 
162
        }
 
163
 
 
164
        /** 
 
165
         * Overrides superclass method.
 
166
         * @return text editor support (instance of enclosing class)
 
167
         */
 
168
        public CloneableOpenSupport findCloneableOpenSupport() {
 
169
            DataObject obj = getDataObject ();
 
170
            DefaultES ret;
 
171
            if (obj instanceof DefaultDataObject) {
 
172
                ret = (DefaultES)((DefaultDataObject)obj).getCookie (DefaultES.class, true);
 
173
            } else {
 
174
                ret = (DefaultES)getDataObject().getCookie(DefaultES.class);
 
175
            }
 
176
            
 
177
            // this is necessary as for large files, this methods sets flag that
 
178
            // prevents UserQuestionException
 
179
            super.findCloneableOpenSupport ();
 
180
 
 
181
            return ret;
 
182
        }
 
183
    } // End of nested Environment class.
 
184
 
 
185
}