Index: openide/loaders/manifest.mf =================================================================== RCS file: /cvs/openide/loaders/manifest.mf,v retrieving revision 1.15.2.2 diff -u -r1.15.2.2 manifest.mf --- openide/loaders/manifest.mf 28 Feb 2005 18:00:00 -0000 1.15.2.2 +++ openide/loaders/manifest.mf 2 Mar 2005 14:45:27 -0000 @@ -1,5 +1,5 @@ Manifest-Version: 1.0 OpenIDE-Module: org.openide.loaders -OpenIDE-Module-Specification-Version: 5.1 +OpenIDE-Module-Specification-Version: 5.2 OpenIDE-Module-Localizing-Bundle: org/openide/loaders/Bundle.properties Index: openide/loaders/api/apichanges.xml =================================================================== RCS file: /cvs/openide/loaders/api/apichanges.xml,v retrieving revision 1.9.16.3 diff -u -r1.9.16.3 apichanges.xml --- openide/loaders/api/apichanges.xml 28 Feb 2005 18:00:01 -0000 1.9.16.3 +++ openide/loaders/api/apichanges.xml 2 Mar 2005 14:45:27 -0000 @@ -78,6 +78,26 @@ + A factory method to simplify creation of DataEditorSupport + + + + + +

+ Many people subclass DataEditorSupport to provide editing ability for data + objects. The great majority of them wish to provide an editor cookie + and with standard semantics for open, modify, save, undo, + close, reload, etc. By using the + DataEditorSupport create factory + method this task is simplified to two lines of code. +

+
+ + +
+ + Added DataLoaderPool.getDefault() Index: openide/loaders/src/org/openide/text/DataEditorSupport.java =================================================================== RCS file: /cvs/openide/loaders/src/org/openide/text/DataEditorSupport.java,v retrieving revision 1.18.6.1 diff -u -r1.18.6.1 DataEditorSupport.java --- openide/loaders/src/org/openide/text/DataEditorSupport.java 9 Nov 2004 15:56:55 -0000 1.18.6.1 +++ openide/loaders/src/org/openide/text/DataEditorSupport.java 2 Mar 2005 14:45:27 -0000 @@ -57,6 +57,29 @@ this.obj = obj; } + /** Factory method to create simple CloneableEditorSupport for a given + * entry of a given DataObject. The common use inside DataObject looks like + * this: + *
+     *  Node.Cookie cookie = (Node.Cookie)DataEditorSupport.create(this, getPrimaryEntry(), getCookieSet ());
+     *  getCookieSet ().add (cookie);
+     * 
+ * + * @param obj the data object + * @param entry the entry to read and write from + * @param set cookie set to add remove additional cookies (currently only {@link org.openide.cookies.SaveCookie}) + * @return a subclass of DataEditorSupport that implements at least + * {@link org.openide.cookies.OpenCookie}, + * {@link org.openide.cookies.EditCookie}, + * {@link org.openide.cookies.EditorCookie.Observable}, + * {@link org.openide.cookies.PrintCookie}, + * {@link org.openide.cookies.CloseCookie} + * @since 5.2 + */ + public static CloneableEditorSupport create (DataObject obj, MultiDataObject.Entry entry, org.openide.nodes.CookieSet set) { + return new SimpleES (obj, entry, set); + } + /** Getter of the data object that this support is associated with. * @return data object passed in constructor */ Index: openide/loaders/src/org/openide/text/SimpleES.java =================================================================== RCS file: openide/loaders/src/org/openide/text/SimpleES.java diff -N openide/loaders/src/org/openide/text/SimpleES.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/loaders/src/org/openide/text/SimpleES.java 2 Mar 2005 14:45:27 -0000 @@ -0,0 +1,145 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun + * Microsystems, Inc. All Rights Reserved. + */ + + +package org.openide.text; + + +import java.io.IOException; + +import org.openide.cookies.CloseCookie; +import org.openide.cookies.EditCookie; +import org.openide.cookies.EditorCookie; +import org.openide.cookies.OpenCookie; +import org.openide.cookies.PrintCookie; +import org.openide.cookies.SaveCookie; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileLock; +import org.openide.loaders.DataObject; +import org.openide.loaders.MultiDataObject; +import org.openide.nodes.CookieSet; +import org.openide.nodes.Node.Cookie; +import org.openide.text.DataEditorSupport; +import org.openide.windows.CloneableOpenSupport; + + +/** + * Basic editor support. + * + * @author Jaroslav Tulach + */ +final class SimpleES extends DataEditorSupport +implements OpenCookie, EditCookie, EditorCookie.Observable, PrintCookie, CloseCookie { + /** SaveCookie for this support instance. The cookie is adding/removing + * data object's cookie set depending on if modification flag was set/unset. */ + private final SaveCookie saveCookie = new SaveCookie() { + /** Implements SaveCookie interface. */ + public void save() throws IOException { + SimpleES.this.saveDocument(); + } + }; + + private CookieSet set; + + /** Constructor. + * @param obj data object to work on + * @param set set to add/remove save cookie from + */ + SimpleES (DataObject obj, MultiDataObject.Entry entry, CookieSet set) { + super(obj, new Environment(obj, entry)); + this.set = set; + setMIMEType("text/plain"); // NOI18N + } + + /** + * Overrides superclass method. Adds adding of save cookie if the document has been marked modified. + * @return true if the environment accepted being marked as modified + * or false if it has refused and the document should remain unmodified + */ + protected boolean notifyModified () { + if (!super.notifyModified()) + return false; + + addSaveCookie(); + + return true; + } + + /** Overrides superclass method. Adds removing of save cookie. */ + protected void notifyUnmodified () { + super.notifyUnmodified(); + + removeSaveCookie(); + } + + /** Helper method. Adds save cookie to the data object. */ + private void addSaveCookie() { + DataObject obj = getDataObject(); + + // Adds save cookie to the data object. + if(obj.getCookie(SaveCookie.class) == null) { + set.add(saveCookie); + obj.setModified(true); + } + } + + /** Helper method. Removes save cookie from the data object. */ + private void removeSaveCookie() { + DataObject obj = getDataObject(); + + // Remove save cookie from the data object. + Cookie cookie = obj.getCookie(SaveCookie.class); + + if(cookie != null && cookie.equals(saveCookie)) { + set.remove(saveCookie); + obj.setModified(false); + } + } + + + /** Nested class. Environment for this support. Extends + * DataEditorSupport.Env abstract class. + */ + + private static class Environment extends DataEditorSupport.Env { + private static final long serialVersionUID = 5451434321155443431L; + + private MultiDataObject.Entry entry; + + /** Constructor. */ + public Environment(DataObject obj, MultiDataObject.Entry entry) { + super(obj); + this.entry = entry; + } + + + /** Implements abstract superclass method. */ + protected FileObject getFile() { + return entry.getFile(); + } + + /** Implements abstract superclass method.*/ + protected FileLock takeLock() throws IOException { + return entry.takeLock(); + } + + /** + * Overrides superclass method. + * @return text editor support (instance of enclosing class) + */ + public CloneableOpenSupport findCloneableOpenSupport() { + return (SimpleES)getDataObject().getCookie(SimpleES.class); + } + } // End of nested Environment class. + +} Index: openide/loaders/test/unit/src/org/openide/text/SimpleDESTest.java =================================================================== RCS file: openide/loaders/test/unit/src/org/openide/text/SimpleDESTest.java diff -N openide/loaders/test/unit/src/org/openide/text/SimpleDESTest.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openide/loaders/test/unit/src/org/openide/text/SimpleDESTest.java 2 Mar 2005 14:45:27 -0000 @@ -0,0 +1,196 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun + * Microsystems, Inc. All Rights Reserved. + */ + +package org.openide.text; + +import java.io.PrintStream; +import javax.swing.Action; +import junit.textui.TestRunner; +import org.netbeans.junit.*; +import org.openide.DialogDescriptor; +import org.openide.cookies.EditorCookie; +import org.openide.cookies.OpenCookie; +import org.openide.filesystems.*; +import org.openide.loaders.DataObject; +import org.openide.util.actions.SystemAction; + +/** DefaultDataObject is supposed to have open operation that shows the text + * editor or invokes a dialog with questions. + * + * @author Jaroslav Tulach + */ +public final class SimpleDESTest extends NbTestCase { + + private FileSystem lfs; + private DataObject obj; + + /** Creates a new instance of DefaultSettingsContextTest */ + public SimpleDESTest(String name) { + super(name); + } + + public static void main(String[] args) { + TestRunner.run(new NbTestSuite(SimpleDESTest.class)); + System.exit (0); + } + + protected void setUp() throws java.lang.Exception { + clearWorkDir (); + + System.setProperty("org.openide.util.Lookup", "org.openide.text.SimpleDESTest$Lkp"); + super.setUp(); + + LocalFileSystem l = new LocalFileSystem (); + l.setRootDirectory (getWorkDir ()); + lfs = l; + + FileObject fo = FileUtil.createData (lfs.getRoot (), "AA/" + getName () + ".test"); + assertNotNull("file not found", fo); + obj = DataObject.find(fo); + + assertEquals ("The right class", obj.getClass (), SO.class); + } + + protected void tearDown() throws java.lang.Exception { + super.tearDown(); + } + + public void testHasEditorCookieForResonableContentOfFiles () throws Exception { + doCookieCheck (true); + } + + private void doCookieCheck (boolean hasEditCookie) throws Exception { + EditorCookie c = tryToOpen ( + "Ahoj Jardo," + + "how are you" + + "\t\n\rBye" + ); + assertNotNull (c); + + assertEquals ( + "Next questions results in the same cookie", + c, + obj.getCookie(EditorCookie.class) + ); + assertEquals ( + "Print cookie is provided", + c, + obj.getCookie(org.openide.cookies.PrintCookie.class) + ); + assertEquals ( + "CloseCookie as well", + c, + obj.getCookie(org.openide.cookies.CloseCookie.class) + ); + + if (hasEditCookie) { + assertEquals ( + "EditCookie as well", + c, + obj.getCookie(org.openide.cookies.EditCookie.class) + ); + } else { + assertNull ( + "No EditCookie", + obj.getCookie(org.openide.cookies.EditCookie.class) + ); + + } + + OpenCookie open = (OpenCookie)obj.getCookie (OpenCookie.class); + open.open (); + + javax.swing.text.Document d = c.getDocument(); + assertNotNull (d); + + d.insertString(0, "Kuk", null); + + assertNotNull ( + "Now there is a save cookie", + obj.getCookie (org.openide.cookies.SaveCookie.class) + ); + } + + public void testItIsPossibleToMaskEditCookie () throws Exception { + doCookieCheck (false); + } + + private EditorCookie tryToOpen (String content) throws Exception { + FileObject fo = obj.getPrimaryFile(); + FileLock lock = fo.lock(); + PrintStream os = new PrintStream (fo.getOutputStream(lock)); + os.print (content); + os.close (); + lock.releaseLock(); + + return (EditorCookie)obj.getCookie (EditorCookie.class); + } + + // + // Our fake lookup + // + public static final class Lkp extends org.openide.util.lookup.AbstractLookup { + public Lkp () { + this (new org.openide.util.lookup.InstanceContent ()); + } + + private Lkp (org.openide.util.lookup.InstanceContent ic) { + super (ic); + ic.add (new DLP ()); + } + } + + private static final class SL extends org.openide.loaders.UniFileLoader { + public SL () { + super (SO.class.getName ()); + getExtensions().addExtension("test"); + } + protected org.openide.loaders.MultiDataObject createMultiObject(FileObject primaryFile) throws org.openide.loaders.DataObjectExistsException, java.io.IOException { + return new SO (primaryFile); + } + } // end of SL + + private static final class SO extends org.openide.loaders.MultiDataObject implements org.openide.nodes.CookieSet.Factory { + private org.openide.nodes.Node.Cookie cookie = (org.openide.nodes.Node.Cookie)DataEditorSupport.create(this, getPrimaryEntry(), getCookieSet ()); + + + public SO (FileObject fo) throws org.openide.loaders.DataObjectExistsException { + super (fo, (SL)SL.getLoader(SL.class)); + + if (fo.getNameExt().indexOf ("MaskEdit") == -1) { + getCookieSet ().add (cookie); + } else { + getCookieSet ().add (new Class[] { + OpenCookie.class, + org.openide.cookies.CloseCookie.class, EditorCookie.class, + org.openide.cookies.PrintCookie.class + }, this); + } + } + + + public org.openide.nodes.Node.Cookie createCookie (Class c) { + return cookie; + } + } // end of SO + + private static final class DLP extends org.openide.loaders.DataLoaderPool { + protected java.util.Enumeration loaders() { + return java.util.Collections.enumeration( + java.util.Collections.singleton( + SL.getLoader (SL.class) + ) + ); + } + } // end of DataLoaderPool +}