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

(-)openide/src/org/openide/nodes/AbstractNode.java (-7 / +36 lines)
Lines 105-110 Link Here
105
    protected SystemAction[] systemActions;
105
    protected SystemAction[] systemActions;
106
    /** Preferred action. */
106
    /** Preferred action. */
107
    private Action preferredAction;
107
    private Action preferredAction;
108
    private SystemAction defaultAction;
108
109
109
    /** Listener for changes in the sheet and the cookie set. */
110
    /** Listener for changes in the sheet and the cookie set. */
110
    private final class SheetAndCookieListener implements PropertyChangeListener, ChangeListener {
111
    private final class SheetAndCookieListener implements PropertyChangeListener, ChangeListener {
Lines 445-450 Link Here
445
    public NewType[] getNewTypes () {
446
    public NewType[] getNewTypes () {
446
        return NO_NEW_TYPES;
447
        return NO_NEW_TYPES;
447
    }
448
    }
449
    
450
    private ThreadLocal callingDefaultAction = null;
448
451
449
    /** Gets preferred action.
452
    /** Gets preferred action.
450
     * By default, null.
453
     * By default, null.
Lines 454-460 Link Here
454
     * @since 3.29
457
     * @since 3.29
455
     */
458
     */
456
    public Action getPreferredAction() {
459
    public Action getPreferredAction() {
457
        return preferredAction;
460
        if (preferredAction != null) {
461
            return preferredAction;
462
        } else {
463
            // Perhaps getDefaultAction was overridden?
464
            if (callingDefaultAction == null || Boolean.FALSE.equals(callingDefaultAction.get())) {
465
                return getDefaultAction();
466
            } else {
467
                // Recursion.
468
                return null;
469
            }
470
        }
458
    }
471
    }
459
    
472
    
460
    /** Sets preferred action.
473
    /** Sets preferred action.
Lines 471-482 Link Here
471
     * @deprecated Use {@link #getPreferredAction} instead.
484
     * @deprecated Use {@link #getPreferredAction} instead.
472
    */
485
    */
473
    public SystemAction getDefaultAction () {
486
    public SystemAction getDefaultAction () {
474
        Action a = getPreferredAction();
487
        if (defaultAction != null) {
475
        if(a instanceof SystemAction) {
488
            return defaultAction;
476
            return (SystemAction)a;
489
        }
490
        if (callingDefaultAction == null) {
491
            callingDefaultAction = new ThreadLocal() {
492
                protected Object initialValue() {
493
                    return Boolean.TRUE;
494
                }
495
            };
496
        } else {
497
            callingDefaultAction.set(Boolean.TRUE);
498
        }
499
        try {
500
            Action a = getPreferredAction();
501
            if (a instanceof SystemAction) {
502
                return (SystemAction)a;
503
            } else {
504
                return null;
505
            }
506
        } finally {
507
            callingDefaultAction.set(Boolean.FALSE);
477
        }
508
        }
478
        
479
        return null;
480
    }
509
    }
481
510
482
    /** Set a default action for the node.
511
    /** Set a default action for the node.
Lines 484-490 Link Here
484
     * @deprecated Use {@link #setPreferredAction} instead.
513
     * @deprecated Use {@link #setPreferredAction} instead.
485
    */
514
    */
486
    public void setDefaultAction (SystemAction action) {
515
    public void setDefaultAction (SystemAction action) {
487
        setPreferredAction(action);
516
        defaultAction = action;
488
    }
517
    }
489
    
518
    
490
    /** Get all actions for the node.
519
    /** Get all actions for the node.
(-)openide/test/unit/src/org/openide/nodes/NodeTest.java (-3 / +84 lines)
Lines 13-30 Link Here
13
13
14
package org.openide.nodes;
14
package org.openide.nodes;
15
15
16
import java.awt.event.ActionEvent;
16
import java.beans.*;
17
import java.beans.*;
17
import java.util.*;
18
import java.util.*;
19
import javax.swing.AbstractAction;
20
import javax.swing.Action;
18
21
19
import junit.textui.TestRunner;
22
import junit.textui.TestRunner;
20
import org.netbeans.junit.NbTestCase;
23
import org.netbeans.junit.NbTestCase;
21
import org.netbeans.junit.NbTestSuite;
24
import org.netbeans.junit.NbTestSuite;
25
import org.openide.actions.OpenAction;
26
import org.openide.actions.PropertiesAction;
22
27
23
import org.openide.nodes.*;
28
import org.openide.nodes.*;
24
import org.openide.util.actions.SystemAction;
29
import org.openide.util.actions.SystemAction;
25
30
26
/** Checking some of the behaviour of Node.
31
/** Checking some of the behaviour of Node (and AbstractNode).
27
 * @author Jaroslav Tulach
32
 * @author Jaroslav Tulach, Jesse Glick
28
 */
33
 */
29
public class NodeTest extends NbTestCase {
34
public class NodeTest extends NbTestCase {
30
    
35
    
Lines 38-44 Link Here
38
43
39
    public void testGetActions () throws Exception {
44
    public void testGetActions () throws Exception {
40
        final SystemAction[] arr1 = {
45
        final SystemAction[] arr1 = {
41
            SystemAction.get (org.openide.actions.PropertiesAction.class)
46
            SystemAction.get (PropertiesAction.class)
42
        };
47
        };
43
        final SystemAction[] arr2 = {
48
        final SystemAction[] arr2 = {
44
        };
49
        };
Lines 57-62 Link Here
57
        assertEquals ("getActions(false) properly delegates to getActions()", arr1, an.getActions (false));
62
        assertEquals ("getActions(false) properly delegates to getActions()", arr1, an.getActions (false));
58
        assertEquals ("getActions(true) properly delegates to getContextActions()", arr2, an.getActions (true));
63
        assertEquals ("getActions(true) properly delegates to getContextActions()", arr2, an.getActions (true));
59
        
64
        
65
    }
66
    
67
    public void testPreferredAction() throws Exception {
68
        final SystemAction a1 = SystemAction.get(PropertiesAction.class);
69
        final Action a2 = new AbstractAction() {
70
            public void actionPerformed(ActionEvent ev) {}
71
        };
72
        final SystemAction a3 = SystemAction.get(OpenAction.class);
73
        final Action a4 = new AbstractAction() {
74
            public void actionPerformed(ActionEvent ev) {}
75
        };
76
        // Old code:
77
        Node n1 = new AbstractNode(Children.LEAF) {
78
            {
79
                setDefaultAction(a1);
80
            }
81
        };
82
        Node n2 = new AbstractNode(Children.LEAF) {
83
            public SystemAction getDefaultAction() {
84
                return a1;
85
            }
86
        };
87
        // New code:
88
        Node n3 = new AbstractNode(Children.LEAF) {
89
            {
90
                setPreferredAction(a1);
91
            }
92
        };
93
        Node n4 = new AbstractNode(Children.LEAF) {
94
            public Action getPreferredAction() {
95
                return a1;
96
            }
97
        };
98
        Node n5 = new AbstractNode(Children.LEAF) {
99
            {
100
                setPreferredAction(a2);
101
            }
102
        };
103
        Node n6 = new AbstractNode(Children.LEAF) {
104
            public Action getPreferredAction() {
105
                return a2;
106
            }
107
        };
108
        // Wacko code:
109
        Node n7 = new AbstractNode(Children.LEAF) {
110
            {
111
                setDefaultAction(a1);
112
            }
113
            public SystemAction getDefaultAction() {
114
                return a3;
115
            }
116
        };
117
        Node n8 = new AbstractNode(Children.LEAF) {
118
            {
119
                setPreferredAction(a2);
120
            }
121
            public Action getPreferredAction() {
122
                return a4;
123
            }
124
        };
125
        assertEquals(a1, n1.getDefaultAction());
126
        assertEquals(a1, n1.getPreferredAction());
127
        assertEquals(a1, n2.getDefaultAction());
128
        assertEquals(a1, n2.getPreferredAction());
129
        assertEquals(a1, n3.getDefaultAction());
130
        assertEquals(a1, n3.getPreferredAction());
131
        assertEquals(a1, n4.getDefaultAction());
132
        assertEquals(a1, n4.getPreferredAction());
133
        assertEquals(null, n5.getDefaultAction());
134
        assertEquals(a2, n5.getPreferredAction());
135
        assertEquals(null, n6.getDefaultAction());
136
        assertEquals(a2, n6.getPreferredAction());
137
        assertEquals(a3, n7.getDefaultAction());
138
        assertEquals(a3, n7.getPreferredAction());
139
        assertEquals(null, n8.getDefaultAction());
140
        assertEquals(a4, n8.getPreferredAction());
60
    }
141
    }
61
142
62
}
143
}

Return to bug 32744