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

(-)loaders/src/org/openide/loaders/DataLoader.java (-11 / +51 lines)
Lines 16-21 Link Here
16
import java.beans.*;
16
import java.beans.*;
17
import java.io.*;
17
import java.io.*;
18
import java.util.*;
18
import java.util.*;
19
import javax.swing.Action;
19
20
20
import org.openide.ErrorManager;
21
import org.openide.ErrorManager;
21
import org.openide.filesystems.*;
22
import org.openide.filesystems.*;
Lines 47-52 Link Here
47
    public static final String PROP_DISPLAY_NAME = "displayName"; // NOI18N
48
    public static final String PROP_DISPLAY_NAME = "displayName"; // NOI18N
48
    /** property name of list of actions */
49
    /** property name of list of actions */
49
    public static final String PROP_ACTIONS = "actions"; // NOI18N
50
    public static final String PROP_ACTIONS = "actions"; // NOI18N
51
    /** property to store Action[] */
52
    private static final Object REAL_ACTIONS = new Object ();
53
    
50
    /** property name of list of default actions */
54
    /** property name of list of default actions */
51
    private static final String PROP_DEF_ACTIONS = "defaultActions"; // NOI18N
55
    private static final String PROP_DEF_ACTIONS = "defaultActions"; // NOI18N
52
    /** representation class, not public property */
56
    /** representation class, not public property */
Lines 123-128 Link Here
123
        return (String)getProperty (PROP_REPRESENTATION_CLASS_NAME);
127
        return (String)getProperty (PROP_REPRESENTATION_CLASS_NAME);
124
    }
128
    }
125
129
130
    /** All actions, even swing ones associated with the loader.
131
     */
132
    final Action[] getSwingActions () {
133
        Action[] actions = (Action[])getProperty (PROP_ACTIONS);
134
        if (actions == null) {
135
            actions = (Action[])getProperty (REAL_ACTIONS);
136
        }
137
        if ( actions == null ) {
138
            actions = (SystemAction[])getProperty (PROP_DEF_ACTIONS);
139
            if ( actions == null ) {
140
                actions = defaultActions();
141
                putProperty (PROP_DEF_ACTIONS, actions, false);
142
            }        
143
        }
144
        return actions;
145
    }
146
    
147
    
126
    /** Get actions.
148
    /** Get actions.
127
     * These actions are used to compose
149
     * These actions are used to compose
128
    * a popup menu for the data object. Also these actions should
150
    * a popup menu for the data object. Also these actions should
Lines 133-147 Link Here
133
    *   actions
155
    *   actions
134
    */
156
    */
135
    public final SystemAction[] getActions () {
157
    public final SystemAction[] getActions () {
136
        SystemAction[] actions = (SystemAction[])getProperty (PROP_ACTIONS);
158
        ArrayList list = new ArrayList ();
137
        if ( actions == null ) {
159
        Action[] swingActions = getSwingActions ();
138
            actions = (SystemAction[])getProperty (PROP_DEF_ACTIONS);
160
        for (int i = 0; i < swingActions.length; i++) {
139
            if ( actions == null ) {
161
            if (swingActions[i] == null || swingActions[i] instanceof SystemAction) {
140
                actions = defaultActions();
162
                list.add (swingActions[i]);
141
                putProperty (PROP_DEF_ACTIONS, actions, false);
163
            }
142
            }        
143
        }
164
        }
144
        return actions;
165
        return (SystemAction[])list.toArray (new SystemAction[0]);
145
    }
166
    }
146
167
147
    /** Get default actions.
168
    /** Get default actions.
Lines 174-179 Link Here
174
    public final void setActions (SystemAction[] actions) {
195
    public final void setActions (SystemAction[] actions) {
175
        putProperty (PROP_ACTIONS, actions, true);
196
        putProperty (PROP_ACTIONS, actions, true);
176
    }
197
    }
198
    
199
    public final void setActions (javax.swing.Action[] actions) {
200
        putProperty (REAL_ACTIONS, actions);
201
        firePropertyChange (PROP_ACTIONS, null, getActions ());
202
    }
177
203
178
    /** Get the current display name of this loader.
204
    /** Get the current display name of this loader.
179
    * @return display name
205
    * @return display name
Lines 289-295 Link Here
289
    public void writeExternal (ObjectOutput oo) throws IOException {
315
    public void writeExternal (ObjectOutput oo) throws IOException {
290
        oo.writeObject( new Integer(LOADER_VERSION) );
316
        oo.writeObject( new Integer(LOADER_VERSION) );
291
        
317
        
292
        SystemAction[] arr = (SystemAction[])getProperty (PROP_ACTIONS);
318
        Action[] arr = (Action[])getProperty (PROP_ACTIONS);
319
        if (arr == null) {
320
            arr = (Action[])getProperty (REAL_ACTIONS);
321
        }
322
        
293
        if (arr == null) {
323
        if (arr == null) {
294
            oo.writeObject (null);
324
            oo.writeObject (null);
295
        } else {
325
        } else {
Lines 299-305 Link Here
299
                if (arr[i] == null) {
329
                if (arr[i] == null) {
300
                    names.add (null);
330
                    names.add (null);
301
                } else {
331
                } else {
302
                    names.add (arr[i].getClass ().getName ());
332
                    if (arr[i] instanceof SystemAction) {
333
                        names.add (arr[i].getClass ().getName ());
334
                    } else {
335
                        // the action itself
336
                        names.add (arr[i]);
337
                    }
303
                }
338
                }
304
            }
339
            }
305
            oo.writeObject (names.toArray ());
340
            oo.writeObject (names.toArray ());
Lines 348-353 Link Here
348
                    continue;
383
                    continue;
349
                }
384
                }
350
                
385
                
386
                if (arr[i] instanceof Action) {
387
                    ll.add (arr[i]);
388
                    continue;
389
                }
390
                
351
                try {
391
                try {
352
                    Class c = Class.forName (
392
                    Class c = Class.forName (
353
                        Utilities.translate((String)arr[i]),
393
                        Utilities.translate((String)arr[i]),
Lines 373-379 Link Here
373
            }
413
            }
374
            if (main == null && !isdefault) {
414
            if (main == null && !isdefault) {
375
                // Whole action list was successfully read.
415
                // Whole action list was successfully read.
376
                setActions ((SystemAction[])ll.toArray(new SystemAction[ll.size()]));
416
                setActions ((Action[])ll.toArray(new Action[ll.size()]));
377
            } // Else do not try to override the default action list if it is incomplete anyway.
417
            } // Else do not try to override the default action list if it is incomplete anyway.
378
        }
418
        }
379
        
419
        
(-)loaders/src/org/openide/loaders/DataNode.java (+17 lines)
Lines 302-307 Link Here
302
    * @see DataLoader#getActions
302
    * @see DataLoader#getActions
303
    * @return array of actions or <code>null</code>
303
    * @return array of actions or <code>null</code>
304
    */
304
    */
305
    public Action[] getActions (boolean context) {
306
        if (systemActions == null) {
307
            systemActions = createActions ();
308
        }
309
310
        if (systemActions != null) {
311
            return systemActions;
312
        }
313
314
        return obj.getLoader ().getSwingActions ();
315
    }
316
317
    /** Get actions for this data object.
318
    * @deprecated Use getActions(boolean)
319
    * @return array of actions or <code>null</code>
320
    */
305
    public SystemAction[] getActions () {
321
    public SystemAction[] getActions () {
306
        if (systemActions == null) {
322
        if (systemActions == null) {
307
            systemActions = createActions ();
323
            systemActions = createActions ();
Lines 314-319 Link Here
314
        return obj.getLoader ().getActions ();
330
        return obj.getLoader ().getActions ();
315
    }
331
    }
316
332
333
    
317
    /** Get default action. In the current implementation the 
334
    /** Get default action. In the current implementation the 
318
    *<code>null</code> is returned in case the underlying data 
335
    *<code>null</code> is returned in case the underlying data 
319
    * object is a template. The templates should not have any default 
336
    * object is a template. The templates should not have any default 
(-)test/unit/src/org/openide/loaders/DataNodeTest.java (-4 / +113 lines)
Lines 13-18 Link Here
13
13
14
package org.openide.loaders;
14
package org.openide.loaders;
15
15
16
import java.io.*;
16
import junit.textui.TestRunner;
17
import junit.textui.TestRunner;
17
import org.openide.filesystems.FileSystem;
18
import org.openide.filesystems.FileSystem;
18
import java.util.Enumeration;
19
import java.util.Enumeration;
Lines 25-30 Link Here
25
 * @author Jesse Glick
26
 * @author Jesse Glick
26
 */
27
 */
27
public class DataNodeTest extends NbTestCase {
28
public class DataNodeTest extends NbTestCase {
29
    private DataFolder top;
30
    private DataObject obj;
31
    private org.openide.util.actions.SystemAction[] originalActions;
28
    
32
    
29
    public DataNodeTest(String name) {
33
    public DataNodeTest(String name) {
30
        super(name);
34
        super(name);
Lines 36-47 Link Here
36
        System.exit(0);
40
        System.exit(0);
37
    }
41
    }
38
    
42
    
39
    /*
40
    protected void setUp() throws Exception {
43
    protected void setUp() throws Exception {
44
        FileSystem sfs = Repository.getDefault().getDefaultFileSystem();
45
        top = DataFolder.findFolder(sfs.getRoot ());
46
        org.openide.filesystems.FileObject fo = org.openide.filesystems.FileUtil.createData (
47
            sfs.getRoot (), "data.ext"
48
        );
49
        obj = DataObject.find (fo);
50
        
51
        originalActions = top.getLoader ().getActions ();
41
    }
52
    }
53
    
42
    protected void tearDown() throws Exception {
54
    protected void tearDown() throws Exception {
55
        top.getLoader ().setActions (originalActions);
43
    }
56
    }
44
     */
45
    
57
    
46
    /** Test that for all examples to be found in the system file system,
58
    /** Test that for all examples to be found in the system file system,
47
     * the node delegate has the same object as a cookie from DataObject.class.
59
     * the node delegate has the same object as a cookie from DataObject.class.
Lines 55-62 Link Here
55
        // outside a running IDE.
67
        // outside a running IDE.
56
//        TopManager tm = TopManager.getDefault();
68
//        TopManager tm = TopManager.getDefault();
57
        // Now scan SFS for all DO's and check the assertion.
69
        // Now scan SFS for all DO's and check the assertion.
58
        FileSystem sfs = Repository.getDefault().getDefaultFileSystem();
59
        DataFolder top = DataFolder.findFolder(sfs.getRoot());
60
        Enumeration e = top.children(true);
70
        Enumeration e = top.children(true);
61
        while (e.hasMoreElements()) {
71
        while (e.hasMoreElements()) {
62
            DataObject o = (DataObject)e.nextElement();
72
            DataObject o = (DataObject)e.nextElement();
Lines 66-69 Link Here
66
        }
76
        }
67
    }
77
    }
68
    
78
    
79
    public void testDataNodeReturnsLoadersActions () throws Exception {
80
        javax.swing.Action[] arr = {
81
            new MyAction ("Name"),
82
            new MyAction ("Two"),
83
            null, // separator
84
            new MyAction ("Last")
85
        };
86
        obj.getLoader ().setActions (arr);
87
        
88
        javax.swing.Action[] nodeActions = obj.getNodeDelegate ().getActions (false);
89
        
90
        assertEquals ("Same actions",
91
            java.util.Arrays.asList (arr),
92
            java.util.Arrays.asList (nodeActions)
93
        );
94
        
95
        nodeActions = obj.getNodeDelegate ().getActions (true);
96
        
97
        assertEquals ("Same actions even of context ones",
98
            java.util.Arrays.asList (arr),
99
            java.util.Arrays.asList (nodeActions)
100
        );
101
        
102
        assertEquals ("There is just separator for system actions", 
103
            java.util.Collections.singletonList (null),
104
            java.util.Arrays.asList (obj.getNodeDelegate ().getActions ())
105
        );
106
        
107
        assertEquals ("Preferred is the first", 
108
            arr[0], obj.getNodeDelegate ().getPreferredAction ()
109
        );
110
    }
111
    
112
    public void testNonSerializableActionsAreRemovedAfterRebootSerializableStays () throws Exception {
113
        javax.swing.Action[] arr = {
114
            new MyAction ("NotSerial"),
115
            new SerAction ("Serial"),
116
            null, // separator
117
            org.openide.actions.OpenAction.get (org.openide.actions.OpenAction.class)
118
        };
119
        obj.getLoader ().setActions (arr);
120
        
121
        org.openide.util.io.NbMarshalledObject mar = new org.openide.util.io.NbMarshalledObject (obj.getLoader ());
122
        
123
        Object read = mar.get ();
124
        
125
        assertSame ("Well SharedClassObject means the instance is still same", read, obj.getLoader ());
126
        
127
        javax.swing.Action[] newArr = ((DataLoader)read).getSwingActions ();
128
        
129
        assertEquals ("Only serial, null, open are present", 3, newArr.length);
130
        assertNull ("Separator is ok", newArr[1]);
131
        assertEquals ("Serial", arr[1], newArr[0]);
132
        assertEquals ("OpenAction", arr[2], newArr[1]);
133
    }
134
    
135
    private static class MyAction extends javax.swing.AbstractAction {
136
        String name;
137
        
138
        public MyAction (String name) {
139
            this.name = name;
140
        }
141
        
142
143
        public void actionPerformed (java.awt.event.ActionEvent e) {
144
        }
145
        
146
        public boolean equals (Object o) {
147
            if (o instanceof MyAction) {
148
                MyAction m = (MyAction)o;
149
                return m.name.equals (name);
150
            }
151
            return false;
152
        }
153
        
154
        public int hashCode () {
155
            return 7 + name.hashCode ();
156
        }
157
        
158
        public String toString () {
159
            return "MyAction " + name;
160
        }
161
    }
162
    
163
    private static final class SerAction extends MyAction implements java.io.Serializable {
164
        static final long serialVersionUID = 4398571748139L;
165
        
166
        public SerAction (String name) {
167
            super (name);
168
        }
169
        
170
        private void readObject (ObjectInputStream ois) throws IOException, ClassNotFoundException {
171
            name = (String)ois.readObject ();
172
        }
173
        
174
        private void writeObject (ObjectOutputStream oos) throws IOException {
175
            oos.writeObject (name);
176
        }
177
    }
69
}
178
}

Return to bug 45137