This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 17081
Collapse All | Expand All

(-)openide/loaders/manifest.mf (-1 / +1 lines)
Lines 1-5 Link Here
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.openide.loaders
2
OpenIDE-Module: org.openide.loaders
3
OpenIDE-Module-Specification-Version: 5.1
3
OpenIDE-Module-Specification-Version: 5.2
4
OpenIDE-Module-Localizing-Bundle: org/openide/loaders/Bundle.properties
4
OpenIDE-Module-Localizing-Bundle: org/openide/loaders/Bundle.properties
5
5
(-)openide/loaders/api/apichanges.xml (+20 lines)
Lines 78-83 Link Here
78
78
79
  <changes>
79
  <changes>
80
     <change>
80
     <change>
81
      <summary>A factory method to simplify creation of <code>DataEditorSupport</code></summary>
82
        <version major="5" minor="2"/>
83
        <date day="9" month="3" year="2005"/>
84
        <author login="jtulach"/>
85
        <compatibility addition="yes" binary="compatible" semantic="compatible" />
86
        <description>
87
        <p>
88
            Many people subclass DataEditorSupport to provide editing ability for data
89
            objects. The great majority of them wish to provide an editor cookie 
90
            and with standard semantics for open, modify, save, undo,
91
            close, reload, etc. By using the 
92
            <code>DataEditorSupport</code> <code>create</code> factory 
93
            method this task is simplified to two lines of code.
94
        </p>
95
        </description>
96
        <class package="org.openide.text" name="DataEditorSupport" />
97
        <issue number="17081"/>
98
    </change>
99
    
100
     <change>
81
      <summary>Added <code>DataLoaderPool.getDefault()</code></summary>
101
      <summary>Added <code>DataLoaderPool.getDefault()</code></summary>
82
        <version major="5" minor="1"/>
102
        <version major="5" minor="1"/>
83
        <date day="7" month="2" year="2005"/>
103
        <date day="7" month="2" year="2005"/>
(-)openide/loaders/src/org/openide/text/DataEditorSupport.java (+23 lines)
Lines 57-62 Link Here
57
        this.obj = obj;
57
        this.obj = obj;
58
    }
58
    }
59
    
59
    
60
    /** Factory method to create simple CloneableEditorSupport for a given
61
     * entry of a given DataObject. The common use inside DataObject looks like
62
     * this:
63
     * <pre>
64
     *  Node.Cookie cookie = (Node.Cookie)DataEditorSupport.create(this, getPrimaryEntry(), getCookieSet ());
65
     *  getCookieSet ().add (cookie);
66
     * </pre>
67
     *
68
     * @param obj the data object
69
     * @param entry the entry to read and write from
70
     * @param set cookie set to add remove additional cookies (currently only {@link org.openide.cookies.SaveCookie})
71
     * @return a subclass of DataEditorSupport that implements at least
72
     *   {@link org.openide.cookies.OpenCookie}, 
73
     *   {@link org.openide.cookies.EditCookie}, 
74
     *   {@link org.openide.cookies.EditorCookie.Observable}, 
75
     *   {@link org.openide.cookies.PrintCookie}, 
76
     *   {@link org.openide.cookies.CloseCookie}
77
     * @since 5.2
78
     */
79
    public static CloneableEditorSupport create (DataObject obj, MultiDataObject.Entry entry, org.openide.nodes.CookieSet set) {
80
        return new SimpleES (obj, entry, set);
81
    }
82
    
60
    /** Getter of the data object that this support is associated with.
83
    /** Getter of the data object that this support is associated with.
61
    * @return data object passed in constructor
84
    * @return data object passed in constructor
62
    */
85
    */
(-)openide/loaders/src/org/openide/text/SimpleES.java (+145 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 * 
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 * 
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
15
package org.openide.text;
16
17
18
import java.io.IOException;
19
20
import org.openide.cookies.CloseCookie;
21
import org.openide.cookies.EditCookie;
22
import org.openide.cookies.EditorCookie;
23
import org.openide.cookies.OpenCookie;
24
import org.openide.cookies.PrintCookie;
25
import org.openide.cookies.SaveCookie;
26
import org.openide.filesystems.FileObject;
27
import org.openide.filesystems.FileLock;
28
import org.openide.loaders.DataObject;
29
import org.openide.loaders.MultiDataObject;
30
import org.openide.nodes.CookieSet;
31
import org.openide.nodes.Node.Cookie;
32
import org.openide.text.DataEditorSupport;
33
import org.openide.windows.CloneableOpenSupport;
34
35
36
/** 
37
 * Basic editor support.
38
 *
39
 * @author Jaroslav Tulach
40
 */
41
final class SimpleES extends DataEditorSupport 
42
implements OpenCookie, EditCookie, EditorCookie.Observable, PrintCookie, CloseCookie {
43
    /** SaveCookie for this support instance. The cookie is adding/removing 
44
     * data object's cookie set depending on if modification flag was set/unset. */
45
    private final SaveCookie saveCookie = new SaveCookie() {
46
        /** Implements <code>SaveCookie</code> interface. */
47
        public void save() throws IOException {
48
            SimpleES.this.saveDocument();
49
        }
50
    };
51
    
52
    private CookieSet set;
53
    
54
    /** Constructor. 
55
     * @param obj data object to work on
56
     * @param set set to add/remove save cookie from
57
     */
58
    SimpleES (DataObject obj, MultiDataObject.Entry entry, CookieSet set) {
59
        super(obj, new Environment(obj, entry));
60
        this.set = set;
61
        setMIMEType("text/plain"); // NOI18N
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 {
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
}
(-)openide/loaders/test/unit/src/org/openide/text/SimpleDESTest.java (+196 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 * 
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 * 
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.openide.text;
15
16
import java.io.PrintStream;
17
import javax.swing.Action;
18
import junit.textui.TestRunner;
19
import org.netbeans.junit.*;
20
import org.openide.DialogDescriptor;
21
import org.openide.cookies.EditorCookie;
22
import org.openide.cookies.OpenCookie;
23
import org.openide.filesystems.*;
24
import org.openide.loaders.DataObject;
25
import org.openide.util.actions.SystemAction;
26
27
/** DefaultDataObject is supposed to have open operation that shows the text
28
 * editor or invokes a dialog with questions.
29
 *
30
 * @author  Jaroslav Tulach
31
 */
32
public final class SimpleDESTest extends NbTestCase {
33
    
34
    private FileSystem lfs;
35
    private DataObject obj;
36
    
37
    /** Creates a new instance of DefaultSettingsContextTest */
38
    public SimpleDESTest(String name) {
39
        super(name);
40
    }
41
    
42
    public static void main(String[] args) {
43
        TestRunner.run(new NbTestSuite(SimpleDESTest.class));
44
        System.exit (0);
45
    }
46
    
47
    protected void setUp() throws java.lang.Exception {
48
        clearWorkDir ();
49
        
50
        System.setProperty("org.openide.util.Lookup", "org.openide.text.SimpleDESTest$Lkp");
51
        super.setUp();
52
        
53
        LocalFileSystem l = new LocalFileSystem ();
54
        l.setRootDirectory (getWorkDir ());
55
        lfs = l;
56
        
57
        FileObject fo = FileUtil.createData (lfs.getRoot (), "AA/" + getName () + ".test");
58
        assertNotNull("file not found", fo);
59
        obj = DataObject.find(fo);
60
        
61
        assertEquals ("The right class", obj.getClass (), SO.class);
62
    }
63
    
64
    protected void tearDown() throws java.lang.Exception {
65
        super.tearDown();
66
    }
67
    
68
    public void testHasEditorCookieForResonableContentOfFiles () throws Exception {
69
        doCookieCheck (true);
70
    }
71
    
72
    private void doCookieCheck (boolean hasEditCookie) throws Exception {
73
        EditorCookie c = tryToOpen (
74
            "Ahoj Jardo," +
75
            "how are you" +
76
            "\t\n\rBye"
77
        );
78
        assertNotNull (c);
79
        
80
        assertEquals (
81
            "Next questions results in the same cookie", 
82
            c, 
83
            obj.getCookie(EditorCookie.class)
84
        );
85
        assertEquals (
86
            "Print cookie is provided",
87
            c,
88
            obj.getCookie(org.openide.cookies.PrintCookie.class)
89
        );
90
        assertEquals (
91
            "CloseCookie as well",
92
            c,
93
            obj.getCookie(org.openide.cookies.CloseCookie.class)
94
        );
95
        
96
        if (hasEditCookie) {
97
            assertEquals (
98
                "EditCookie as well",
99
                c,
100
                obj.getCookie(org.openide.cookies.EditCookie.class)
101
            );
102
        } else {
103
            assertNull (
104
                "No EditCookie",
105
                obj.getCookie(org.openide.cookies.EditCookie.class)
106
            );
107
            
108
        }
109
        
110
        OpenCookie open = (OpenCookie)obj.getCookie (OpenCookie.class);
111
        open.open ();
112
        
113
        javax.swing.text.Document d = c.getDocument();
114
        assertNotNull (d);
115
        
116
        d.insertString(0, "Kuk", null);
117
        
118
        assertNotNull (
119
            "Now there is a save cookie", 
120
            obj.getCookie (org.openide.cookies.SaveCookie.class)
121
        );
122
    }
123
    
124
    public void testItIsPossibleToMaskEditCookie () throws Exception {
125
        doCookieCheck (false);
126
    }
127
    
128
    private EditorCookie tryToOpen (String content) throws Exception {
129
        FileObject fo = obj.getPrimaryFile();
130
        FileLock lock = fo.lock();
131
        PrintStream os = new PrintStream (fo.getOutputStream(lock));
132
        os.print (content);
133
        os.close ();
134
        lock.releaseLock();
135
        
136
        return (EditorCookie)obj.getCookie (EditorCookie.class);
137
    }
138
    
139
    //
140
    // Our fake lookup
141
    //
142
    public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
143
        public Lkp () {
144
            this (new org.openide.util.lookup.InstanceContent ());
145
        }
146
        
147
        private Lkp (org.openide.util.lookup.InstanceContent ic) {
148
            super (ic);
149
            ic.add (new DLP ());
150
        }
151
    }
152
    
153
    private static final class SL extends org.openide.loaders.UniFileLoader {
154
        public SL () {
155
            super (SO.class.getName ());
156
            getExtensions().addExtension("test");
157
        }
158
        protected org.openide.loaders.MultiDataObject createMultiObject(FileObject primaryFile) throws org.openide.loaders.DataObjectExistsException, java.io.IOException {
159
            return new SO (primaryFile);
160
        }
161
    } // end of SL
162
    
163
    private static final class SO extends org.openide.loaders.MultiDataObject implements org.openide.nodes.CookieSet.Factory {
164
        private org.openide.nodes.Node.Cookie cookie = (org.openide.nodes.Node.Cookie)DataEditorSupport.create(this, getPrimaryEntry(), getCookieSet ());
165
        
166
        
167
        public SO (FileObject fo) throws org.openide.loaders.DataObjectExistsException {
168
            super (fo, (SL)SL.getLoader(SL.class));
169
            
170
            if (fo.getNameExt().indexOf ("MaskEdit") == -1) {
171
                getCookieSet ().add (cookie);
172
            } else {
173
                getCookieSet ().add (new Class[] { 
174
                    OpenCookie.class, 
175
                    org.openide.cookies.CloseCookie.class, EditorCookie.class, 
176
                    org.openide.cookies.PrintCookie.class
177
                }, this); 
178
            }
179
        }
180
        
181
        
182
        public org.openide.nodes.Node.Cookie createCookie (Class c) {
183
            return cookie;
184
        }
185
    } // end of SO
186
187
    private static final class DLP extends org.openide.loaders.DataLoaderPool {
188
        protected java.util.Enumeration loaders() {
189
            return java.util.Collections.enumeration(
190
                java.util.Collections.singleton(
191
                    SL.getLoader (SL.class)
192
                )
193
            );
194
        }
195
    } // end of DataLoaderPool
196
}

Return to bug 17081