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 199416
Collapse All | Expand All

(-)a/apisupport.project/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/NewLoaderIterator.java (-1 / +6 lines)
Lines 169-175 Link Here
169
                if (v == null) {
169
                if (v == null) {
170
                    return false;
170
                    return false;
171
                }
171
                }
172
                return v.compareTo(new SpecificationVersion("1.24")) >= 0; // NOI18N
172
                SpecificationVersion l = getModuleInfo().getDependencyVersion("org.openide.loaders"); // NOI18N
173
                if (l == null) {
174
                    return false;
175
                }
176
                return v.compareTo(new SpecificationVersion("1.24")) >= 0 // NOI18N
177
                  && l.compareTo(new SpecificationVersion("7.26")) >= 0; // NOI18N
173
            } catch (IOException ex) {
178
            } catch (IOException ex) {
174
                return false;
179
                return false;
175
            }
180
            }
(-)a/apisupport.project/src/org/netbeans/modules/apisupport/project/ui/wizard/loader/templateDataObjectMulti.javx (-24 / +4 lines)
Lines 6-50 Link Here
6
package ${PACKAGENAME};
6
package ${PACKAGENAME};
7
7
8
import java.io.IOException;
8
import java.io.IOException;
9
import java.util.concurrent.Callable;
10
import org.netbeans.core.api.multiview.MultiViews;
11
import org.netbeans.core.spi.multiview.MultiViewElement;
9
import org.netbeans.core.spi.multiview.MultiViewElement;
12
import org.netbeans.core.spi.multiview.text.MultiViewEditorElement;
10
import org.netbeans.core.spi.multiview.text.MultiViewEditorElement;
13
import org.openide.filesystems.FileObject;
11
import org.openide.filesystems.FileObject;
14
import org.openide.loaders.DataNode;
15
import org.openide.loaders.DataObjectExistsException;
12
import org.openide.loaders.DataObjectExistsException;
16
import org.openide.loaders.MultiDataObject;
13
import org.openide.loaders.MultiDataObject;
17
import org.openide.loaders.MultiFileLoader;
14
import org.openide.loaders.MultiFileLoader;
18
import org.openide.text.DataEditorSupport;
19
import org.openide.nodes.Children;
20
import org.openide.nodes.CookieSet;
21
import org.openide.nodes.Node;
22
import org.openide.text.CloneableEditorSupport;
23
import org.openide.text.CloneableEditorSupport.Pane;
24
import org.openide.util.Lookup;
15
import org.openide.util.Lookup;
25
import org.openide.windows.TopComponent;
16
import org.openide.windows.TopComponent;
26
17
27
public class ${PREFIX}DataObject extends MultiDataObject implements Callable<Pane> {
18
public class ${PREFIX}DataObject extends MultiDataObject {
28
19
29
    public ${PREFIX}DataObject(FileObject pf, MultiFileLoader loader) throws DataObjectExistsException, IOException {
20
    public ${PREFIX}DataObject(FileObject pf, MultiFileLoader loader) throws DataObjectExistsException, IOException {
30
        super(pf, loader);
21
        super(pf, loader);
31
        CookieSet cookies = getCookieSet();
22
        registerEditor("${MIMETYPE}", true);
32
        cookies.add((Node.Cookie) DataEditorSupport.create(this, getPrimaryEntry(), cookies, this));
33
    }
23
    }
34
24
35
    @Override
25
    @Override
36
    protected Node createNodeDelegate() {
26
    protected int associateLookup() {
37
        return new DataNode(this, Children.LEAF, getLookup());
27
        return 1;
38
    }
39
40
    @Override
41
    public Lookup getLookup() {
42
        return getCookieSet().getLookup();
43
    }
44
45
    @Override
46
    public Pane call() {
47
        return (Pane)MultiViews.createCloneableMultiView("${MIMETYPE}", this);
48
    }
28
    }
49
29
50
    @MultiViewElement.Registration(
30
    @MultiViewElement.Registration(
(-)a/openide.loaders/apichanges.xml (+26 lines)
Lines 109-114 Link Here
109
<!-- ACTUAL CHANGES BEGIN HERE: -->
109
<!-- ACTUAL CHANGES BEGIN HERE: -->
110
110
111
  <changes>
111
  <changes>
112
      <change id="MultiDataObject.associateLookup">
113
          <api name="loaders"/>
114
          <summary>Simpler way to subclass MultiDataObject</summary>
115
          <version major="7" minor="26"/>
116
          <date day="17" month="6" year="2011"/>
117
          <author login="jtulach"/>
118
          <compatibility semantic="compatible" binary="compatible" deletion="no"
119
            addition="yes" modification="yes" source="incompatible">
120
                Added <code>registerEditor</code> method which might clash
121
                with some existing method in subclasses, although it is 
122
                hopefully not very likely.
123
          </compatibility>
124
          <description>
125
              <p>
126
                  New way to subclass <code>MultiDataObject</code>: override
127
                  <a href="@TOP@/org/openide/loaders/MultiDataObject.html#associateLookup()">
128
                  int associateLookup()
129
                  </a> to return <code>1</code>. In constructor call
130
                  <a href="@TOP@/org/openide/loaders/MultiDataObject.html#registerEditor(java.lang.String,%20boolean)">
131
                  registerEditor("your/mimetype", ...)</a> to get default
132
                  editor support. No need to override any other methods to get
133
                  well performing <code>MultiDataObject</code>.
134
              </p>
135
          </description>
136
          <class package="org.openide.loaders" name="MultiDataObject"/>
137
      </change>
112
      <change id="org.openide.loaders.FolderChildren.delayedCreation">
138
      <change id="org.openide.loaders.FolderChildren.delayedCreation">
113
          <api name="loaders"/>
139
          <api name="loaders"/>
114
          <summary>Children for DataFolder are more effective</summary>
140
          <summary>Children for DataFolder are more effective</summary>
(-)a/openide.loaders/manifest.mf (-1 / +1 lines)
Lines 1-6 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: 7.25
3
OpenIDE-Module-Specification-Version: 7.26
4
OpenIDE-Module-Localizing-Bundle: org/openide/loaders/Bundle.properties
4
OpenIDE-Module-Localizing-Bundle: org/openide/loaders/Bundle.properties
5
OpenIDE-Module-Provides: org.netbeans.modules.templates.v1_0
5
OpenIDE-Module-Provides: org.netbeans.modules.templates.v1_0
6
OpenIDE-Module-Layer: org/netbeans/modules/openide/loaders/layer.xml
6
OpenIDE-Module-Layer: org/netbeans/modules/openide/loaders/layer.xml
(-)8a7c045234cc (+147 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2011 Sun Microsystems, Inc.
41
 */
42
package org.openide.loaders;
43
44
import java.io.Serializable;
45
import java.lang.reflect.Method;
46
import java.util.concurrent.Callable;
47
import java.util.logging.Level;
48
import org.netbeans.api.actions.Editable;
49
import org.netbeans.api.actions.Openable;
50
import org.openide.cookies.CloseCookie;
51
import org.openide.cookies.EditorCookie;
52
import org.openide.cookies.LineCookie;
53
import org.openide.cookies.PrintCookie;
54
import org.openide.nodes.CookieSet;
55
import org.openide.nodes.Node.Cookie;
56
import org.openide.text.CloneableEditorSupport;
57
import org.openide.text.CloneableEditorSupport.Pane;
58
import org.openide.text.DataEditorSupport;
59
import org.openide.util.Exceptions;
60
import org.openide.util.Lookup;
61
import org.openide.windows.CloneableOpenSupport;
62
63
/**
64
 *
65
 * @author Jaroslav Tulach <jtulach@netbeans.org>
66
 */
67
final class MultiDOEditor implements Callable<Pane>, CookieSet.Factory {
68
    private CloneableEditorSupport support;
69
    private static final Method factory;
70
    static {
71
        ClassLoader l = Lookup.getDefault().lookup(ClassLoader.class);
72
        if (l == null) {
73
            l = Thread.currentThread().getContextClassLoader();
74
        }
75
        if (l == null) {
76
            l = MultiDOEditor.class.getClassLoader();
77
        }
78
        Method m = null;
79
        try {
80
            Class<?> multiviews = Class.forName("org.netbeans.core.api.multiview.MultiViews", true, l); // NOI18N
81
            m = multiviews.getMethod("createCloneableMultiView", String.class, Serializable.class); // NOI18N
82
        } catch (NoSuchMethodException ex) {
83
            MultiDataObject.LOG.log(Level.WARNING, "Cannot find a method", ex); // NOI18N
84
        } catch (ClassNotFoundException ex) {
85
            MultiDataObject.LOG.info("Not using multiviews for MultiDataObject.registerEditor()"); // NOI18N
86
            MultiDataObject.LOG.log(Level.FINE, "Cannot find a class", ex); // NOI18N
87
        }
88
        factory = m;
89
    }
90
    private final MultiDataObject outer;
91
    private final String mimeType;
92
    private final boolean useMultiview;
93
94
    MultiDOEditor(MultiDataObject outer, String mimeType, boolean useMultiview) {
95
        this.outer = outer;
96
        this.mimeType = mimeType;
97
        this.useMultiview = useMultiview;
98
    }
99
100
    @Override
101
    public Pane call() throws Exception {
102
        if (factory != null) {
103
            return (Pane) factory.invoke(null, mimeType, outer);
104
        }
105
        return null;
106
    }
107
108
    @Override
109
    public <T extends Cookie> T createCookie(Class<T> klass) {
110
        if (
111
            klass.isAssignableFrom(DataEditorSupport.class) || 
112
            DataEditorSupport.class.isAssignableFrom(klass) || 
113
            klass.isAssignableFrom(Openable.class) || 
114
            klass.isAssignableFrom(Editable.class) || 
115
            klass.isAssignableFrom(EditorCookie.Observable.class) || 
116
            klass.isAssignableFrom(PrintCookie.class) || 
117
            klass.isAssignableFrom(CloseCookie.class) || 
118
            klass.isAssignableFrom(LineCookie.class)
119
        ) {
120
            synchronized (this) {
121
                if (support == null) {
122
                    support = DataEditorSupport.create(
123
                        outer, outer.getPrimaryEntry(),
124
                        outer.getCookieSet(), useMultiview ? this : null
125
                    );
126
                }
127
            }
128
            return klass.cast(support);
129
        }
130
        return null;
131
    }
132
133
    public static void registerEditor(MultiDataObject multi, String mime, boolean useMultiview) {
134
        MultiDOEditor ed = new MultiDOEditor(multi, mime, useMultiview);
135
        try {
136
            multi.getCookieSet().add(new Class[]{
137
                Class.forName("org.openide.text.SimpleES"), // NOI18N
138
                SaveAsCapable.class, Openable.class, EditorCookie.Observable.class, 
139
                PrintCookie.class, CloseCookie.class, Editable.class,
140
                DataEditorSupport.class, CloneableEditorSupport.class,
141
                CloneableOpenSupport.class
142
            }, ed);
143
        } catch (ClassNotFoundException ex) {
144
            throw new IllegalStateException(ex);
145
        }
146
    }
147
}
(-)a/openide.loaders/src/org/openide/loaders/MultiDataObject.java (+81 lines)
Lines 49-59 Link Here
49
import java.beans.PropertyVetoException;
49
import java.beans.PropertyVetoException;
50
import java.io.*;
50
import java.io.*;
51
import java.lang.ref.WeakReference;
51
import java.lang.ref.WeakReference;
52
import java.lang.reflect.Method;
52
import java.util.*;
53
import java.util.*;
54
import java.util.concurrent.Callable;
53
import java.util.logging.*;
55
import java.util.logging.*;
54
import javax.swing.event.*;
56
import javax.swing.event.*;
57
import org.netbeans.api.actions.Editable;
58
import org.netbeans.api.actions.Openable;
59
import org.openide.cookies.CloseCookie;
60
import org.openide.cookies.EditorCookie;
61
import org.openide.cookies.LineCookie;
62
import org.openide.cookies.PrintCookie;
55
import org.openide.filesystems.*;
63
import org.openide.filesystems.*;
56
import org.openide.nodes.*;
64
import org.openide.nodes.*;
65
import org.openide.nodes.Node.Cookie;
66
import org.openide.text.CloneableEditor;
67
import org.openide.text.CloneableEditorSupport;
68
import org.openide.text.CloneableEditorSupport.Pane;
69
import org.openide.text.DataEditorSupport;
57
import org.openide.util.*;
70
import org.openide.util.*;
58
71
59
/** Provides support for handling of data objects with multiple files.
72
/** Provides support for handling of data objects with multiple files.
Lines 235-240 Link Here
235
    */
248
    */
236
    @Override
249
    @Override
237
    protected Node createNodeDelegate () {
250
    protected Node createNodeDelegate () {
251
        if (associateLookup() >= 1) {
252
            return new DataNode(this, Children.LEAF, getLookup());
253
        }
238
        DataNode dataNode = (DataNode) super.createNodeDelegate ();
254
        DataNode dataNode = (DataNode) super.createNodeDelegate ();
239
        return dataNode;
255
        return dataNode;
240
    }
256
    }
Lines 922-927 Link Here
922
        return super.getCookie (type);
938
        return super.getCookie (type);
923
    }
939
    }
924
940
941
    @Override
942
    public Lookup getLookup() {
943
        int version = associateLookup();
944
        assert version <= 1;
945
        if (version >= 1) {
946
            return getCookieSet().getLookup();
947
        }
948
        return super.getLookup();
949
    }
950
    
951
    /** Influences behavior of {@link #getLookup()} method. Depending on the
952
     * returned integer, one can get different, better and more modern  content 
953
     * of the {@link Lookup}:
954
     * <ul>
955
     * <li><b>version 0</b> - delegates to <code>getNodeDelegate().getLookup()</code>.</li>
956
     * <li><b>version 1</b> - delegates to <code>getCookieSet().getLookup()</code>
957
     *   and makes sure {@link FileObject}, <code>this</code> and 
958
     *   {@link Node} are in the lookup. The {@link Node} is created lazily
959
     *   by calling {@link #getNodeDelegate()}.
960
     * </li>
961
     * </ul>
962
     * General suggestion is to always return the highest supported version
963
     * when creating new objects and to stick with certain version when backward
964
     * compatibility is requested.
965
     * 
966
     * @return version identifying content of the lookup (currently 0 or 1)
967
     * @since 7.26
968
     */
969
    protected int associateLookup() {
970
        return 0;
971
    }
972
    
973
    /** Utility method to register editor for this {@link DataObject}.
974
     * Call it from constructor with appropriate mimeType. The system will
975
     * make sure that appropriate cookies ({@link Openable}, {@link Editable},
976
     * {@link CloseCookie}, {@link EditorCookie}, {@link SaveAsCapable},
977
     * {@link LineCookie} are registered into {@link #getCookieSet()}.
978
     * <p>
979
     * The selected editor is <a href="@org-netbeans-core-multiview@/org/netbeans/core/api/multiview/MultiViews.html">
980
     * MultiView component</a>, if requested (this requires presence of
981
     * the <a href="@org-netbeans-core-multiview@/overview-summary.html">MultiView API</a>
982
     * in the system. Otherwise it is plain {@link CloneableEditor}.
983
     * 
984
     * @param mimeType mime type to associate with
985
     * @param useMultiview should the used component be multiview?
986
     * @since 7.26
987
     */
988
    protected final void registerEditor(final String mimeType, boolean useMultiview) {
989
        MultiDOEditor.registerEditor(this, mimeType, useMultiview);
990
    }
991
925
    /** Fires cookie change.
992
    /** Fires cookie change.
926
    */
993
    */
927
    final void fireCookieChange () {
994
    final void fireCookieChange () {
Lines 1336-1355 Link Here
1336
        }
1403
        }
1337
    }
1404
    }
1338
1405
1406
    final void updateNodeInCookieSet() {
1407
        if (associateLookup() >= 1) {
1408
            CookieSet set = getCookieSet(false);
1409
            if (set != null) {
1410
                set.assign(Node.class, getNodeDelegate());
1411
            }
1412
        }
1413
    }
1414
1339
    void checkCookieSet(Class<?> c) {
1415
    void checkCookieSet(Class<?> c) {
1340
    }
1416
    }
1341
    
1417
    
1342
    /** Change listener and implementation of before.
1418
    /** Change listener and implementation of before.
1343
     */
1419
     */
1344
    private final class ChangeAndBefore implements ChangeListener, CookieSet.Before {
1420
    private final class ChangeAndBefore implements ChangeListener, CookieSet.Before {
1421
        @Override
1345
        public void stateChanged (ChangeEvent ev) {
1422
        public void stateChanged (ChangeEvent ev) {
1346
            fireCookieChange ();
1423
            fireCookieChange ();
1347
        }
1424
        }
1348
1425
1426
        @Override
1349
        public void beforeLookup(Class<?> clazz) {
1427
        public void beforeLookup(Class<?> clazz) {
1350
            if (clazz.isAssignableFrom(FileObject.class)) {
1428
            if (clazz.isAssignableFrom(FileObject.class)) {
1351
                updateFilesInCookieSet();
1429
                updateFilesInCookieSet();
1352
            }
1430
            }
1431
            if (clazz.isAssignableFrom(Node.class)) {
1432
                updateNodeInCookieSet();
1433
            }
1353
            checkCookieSet(clazz);
1434
            checkCookieSet(clazz);
1354
        }
1435
        }
1355
    }
1436
    }
(-)a/openide.loaders/src/org/openide/text/SimpleES.java (-1 / +4 lines)
Lines 49-54 Link Here
49
import org.openide.cookies.CloseCookie;
49
import org.openide.cookies.CloseCookie;
50
import org.openide.cookies.EditCookie;
50
import org.openide.cookies.EditCookie;
51
import org.openide.cookies.EditorCookie;
51
import org.openide.cookies.EditorCookie;
52
import org.openide.cookies.LineCookie;
52
import org.openide.cookies.OpenCookie;
53
import org.openide.cookies.OpenCookie;
53
import org.openide.cookies.PrintCookie;
54
import org.openide.cookies.PrintCookie;
54
import org.openide.cookies.SaveCookie;
55
import org.openide.cookies.SaveCookie;
Lines 57-62 Link Here
57
import org.openide.loaders.DataObject;
58
import org.openide.loaders.DataObject;
58
import org.openide.loaders.MultiDataObject;
59
import org.openide.loaders.MultiDataObject;
59
import org.openide.loaders.MultiDataObject.Entry;
60
import org.openide.loaders.MultiDataObject.Entry;
61
import org.openide.loaders.SaveAsCapable;
60
import org.openide.nodes.CookieSet;
62
import org.openide.nodes.CookieSet;
61
import org.openide.nodes.Node.Cookie;
63
import org.openide.nodes.Node.Cookie;
62
import org.openide.windows.CloneableOpenSupport;
64
import org.openide.windows.CloneableOpenSupport;
Lines 67-73 Link Here
67
 * @author Jaroslav Tulach
69
 * @author Jaroslav Tulach
68
 */
70
 */
69
final class SimpleES extends DataEditorSupport 
71
final class SimpleES extends DataEditorSupport 
70
implements OpenCookie, EditCookie, EditorCookie.Observable, PrintCookie, CloseCookie {
72
implements OpenCookie, EditCookie, EditorCookie.Observable, 
73
PrintCookie, CloseCookie, SaveAsCapable, LineCookie {
71
    /** SaveCookie for this support instance. The cookie is adding/removing 
74
    /** SaveCookie for this support instance. The cookie is adding/removing 
72
     * data object's cookie set depending on if modification flag was set/unset. */
75
     * data object's cookie set depending on if modification flag was set/unset. */
73
    private final SaveCookie saveCookie = new SaveCookie() {
76
    private final SaveCookie saveCookie = new SaveCookie() {
(-)8a7c045234cc (+216 lines)
Added Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * Contributor(s):
28
 *
29
 * The Original Software is NetBeans. The Initial Developer of the Original
30
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
31
 * Microsystems, Inc. All Rights Reserved.
32
 *
33
 * If you wish your version of this file to be governed by only the CDDL
34
 * or only the GPL Version 2, indicate your decision by adding
35
 * "[Contributor] elects to include this software in this distribution
36
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
37
 * single choice of license, a recipient has the option to distribute
38
 * your version of this file under either the CDDL, the GPL Version 2 or
39
 * to extend the choice of license to its licensees as provided above.
40
 * However, if you add GPL Version 2 code and therefore, elected the GPL
41
 * Version 2 license, then the option applies only if the new code is
42
 * made subject to such option by the copyright holder.
43
 */
44
45
package org.openide.loaders;
46
47
48
import org.openide.filesystems.*;
49
import org.netbeans.junit.*;
50
import java.io.IOException;
51
import java.util.*;
52
import java.util.logging.Level;
53
import org.openide.*;
54
import org.openide.cookies.CloseCookie;
55
import org.openide.cookies.EditCookie;
56
import org.openide.cookies.EditorCookie;
57
import org.openide.cookies.LineCookie;
58
import org.openide.cookies.OpenCookie;
59
import org.openide.cookies.PrintCookie;
60
import org.openide.nodes.Node;
61
import org.openide.util.Enumerations;
62
63
public class MultiDataObjectVersion1Test extends NbTestCase {
64
    FileSystem fs;
65
    DataObject one;
66
    DataFolder from;
67
    DataFolder to;
68
    ErrorManager err;
69
    
70
    
71
    public MultiDataObjectVersion1Test (String name) {
72
        super (name);
73
    }
74
75
    @Override
76
    protected Level logLevel() {
77
        return Level.INFO;
78
    }
79
80
    @Override
81
    protected int timeOut() {
82
        return 45000;
83
    }
84
85
    @Override
86
    public void setUp() throws Exception {
87
        clearWorkDir();
88
        
89
        super.setUp();
90
        
91
        MockServices.setServices(Pool.class);
92
        
93
        err = ErrorManager.getDefault().getInstance("TEST-" + getName());
94
        
95
        LocalFileSystem lfs = new LocalFileSystem();
96
        lfs.setRootDirectory(getWorkDir());
97
        fs = lfs;
98
        FileUtil.createData(fs.getRoot(), "from/x.prima");
99
        FileUtil.createData(fs.getRoot(), "from/x.seconda");
100
        FileUtil.createFolder(fs.getRoot(), "to/");
101
        
102
        one = DataObject.find(fs.findResource("from/x.prima"));
103
        assertEquals(SimpleObject.class, one.getClass());
104
        
105
        from = one.getFolder();
106
        to = DataFolder.findFolder(fs.findResource("to/"));
107
        
108
        assertEquals("Nothing there", 0, to.getPrimaryFile().getChildren().length);
109
    }
110
    
111
112
    public void testWhatIsInTheLookup() throws Exception {
113
        assertTrue(this.one instanceof SimpleObject);
114
        SimpleObject s = (SimpleObject)this.one;
115
116
        assertEquals("DO in lookup", s, s.getLookup().lookup(DataObject.class));
117
        assertEquals("Fo in lookup", s.getPrimaryFile(), s.getLookup().lookup(FileObject.class));
118
        assertEquals("no node created yet", 0, s.cnt);
119
        Node lkpNd = s.getLookup().lookup(Node.class);
120
        assertEquals("now node created", 1, s.cnt);
121
        assertEquals("Node in lookup", s.getNodeDelegate(), lkpNd);
122
    }
123
124
    public void testEditorInLookup() throws Exception {
125
        assertTrue(this.one instanceof SimpleObject);
126
        SimpleObject s = (SimpleObject)this.one;
127
128
        LineCookie line = s.getLookup().lookup(LineCookie.class);
129
        assertNotNull("Line cookie is there", line);
130
        assertEquals("Edit cookie", line, s.getLookup().lookup(EditCookie.class));
131
        assertEquals("Editor cookie", line, s.getLookup().lookup(EditorCookie.class));
132
        assertEquals("Editor objservable cookie", line, s.getLookup().lookup(EditorCookie.Observable.class));
133
        assertEquals("SaveAsCapable", line, s.getLookup().lookup(SaveAsCapable.class));
134
        assertEquals("Open cookie", line, s.getLookup().lookup(OpenCookie.class));
135
        assertEquals("Close cookie", line, s.getLookup().lookup(CloseCookie.class));
136
        assertEquals("Print cookie", line, s.getLookup().lookup(PrintCookie.class));
137
    }
138
139
    
140
    public static final class Pool extends DataLoaderPool {
141
        @Override
142
        protected Enumeration loaders() {
143
            return Enumerations.singleton(SimpleLoader.getLoader(SimpleLoader.class));
144
        }
145
    }
146
    
147
    public static final class SimpleLoader extends MultiFileLoader {
148
        public SimpleLoader() {
149
            super(SimpleObject.class);
150
        }
151
        protected String displayName() {
152
            return "SimpleLoader";
153
        }
154
        @Override
155
        protected FileObject findPrimaryFile(FileObject fo) {
156
            if (!fo.isFolder()) {
157
                // emulate the behaviour of form data object
158
                
159
                /* emulate!? this one is written too well ;-)
160
                FileObject primary = FileUtil.findBrother(fo, "prima");
161
                FileObject secondary = FileUtil.findBrother(fo, "seconda");
162
                
163
                if (primary == null || secondary == null) {
164
                    return null;
165
                }
166
                
167
                if (primary != fo && secondary != fo) {
168
                    return null;
169
                }
170
                 */
171
                
172
                // here is the common code for the worse behaviour
173
                if (fo.hasExt("prima")) {
174
                    return FileUtil.findBrother(fo, "seconda") != null ? fo : null;
175
                }
176
                
177
                if (fo.hasExt("seconda")) {
178
                    return FileUtil.findBrother(fo, "prima");
179
                }
180
            }
181
            return null;
182
        }
183
        protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException {
184
            return new SimpleObject(this, primaryFile);
185
        }
186
        protected MultiDataObject.Entry createPrimaryEntry(MultiDataObject obj, FileObject primaryFile) {
187
            return new FileEntry(obj, primaryFile);
188
        }
189
        protected MultiDataObject.Entry createSecondaryEntry(MultiDataObject obj, FileObject secondaryFile) {
190
            return new FileEntry(obj, secondaryFile);
191
        }
192
    }
193
    
194
    
195
    public static final class SimpleObject extends MultiDataObject {
196
        private int cnt;
197
        public SimpleObject(SimpleLoader l, FileObject fo) throws DataObjectExistsException {
198
            super(fo, l);
199
            registerEditor("mime/type", false);
200
        }
201
202
        @Override
203
        protected int associateLookup() {
204
            return 1;
205
        }
206
207
        @Override
208
        protected Node createNodeDelegate() {
209
            cnt++;
210
            return super.createNodeDelegate();
211
        }
212
        
213
        
214
    }
215
216
}

Return to bug 199416