Index: openide/src/org/openide/nodes/AbstractNode.java =================================================================== RCS file: /cvs/openide/src/org/openide/nodes/AbstractNode.java,v retrieving revision 1.61 diff -u -r1.61 AbstractNode.java --- openide/src/org/openide/nodes/AbstractNode.java 26 Mar 2003 23:54:36 -0000 1.61 +++ openide/src/org/openide/nodes/AbstractNode.java 8 Apr 2003 23:01:24 -0000 @@ -105,6 +105,7 @@ protected SystemAction[] systemActions; /** Preferred action. */ private Action preferredAction; + private SystemAction defaultAction; /** Listener for changes in the sheet and the cookie set. */ private final class SheetAndCookieListener implements PropertyChangeListener, ChangeListener { @@ -445,6 +446,8 @@ public NewType[] getNewTypes () { return NO_NEW_TYPES; } + + private ThreadLocal callingDefaultAction = null; /** Gets preferred action. * By default, null. @@ -454,7 +457,17 @@ * @since 3.29 */ public Action getPreferredAction() { - return preferredAction; + if (preferredAction != null) { + return preferredAction; + } else { + // Perhaps getDefaultAction was overridden? + if (callingDefaultAction == null || Boolean.FALSE.equals(callingDefaultAction.get())) { + return getDefaultAction(); + } else { + // Recursion. + return null; + } + } } /** Sets preferred action. @@ -471,12 +484,28 @@ * @deprecated Use {@link #getPreferredAction} instead. */ public SystemAction getDefaultAction () { - Action a = getPreferredAction(); - if(a instanceof SystemAction) { - return (SystemAction)a; + if (defaultAction != null) { + return defaultAction; + } + if (callingDefaultAction == null) { + callingDefaultAction = new ThreadLocal() { + protected Object initialValue() { + return Boolean.TRUE; + } + }; + } else { + callingDefaultAction.set(Boolean.TRUE); + } + try { + Action a = getPreferredAction(); + if (a instanceof SystemAction) { + return (SystemAction)a; + } else { + return null; + } + } finally { + callingDefaultAction.set(Boolean.FALSE); } - - return null; } /** Set a default action for the node. @@ -484,7 +513,7 @@ * @deprecated Use {@link #setPreferredAction} instead. */ public void setDefaultAction (SystemAction action) { - setPreferredAction(action); + defaultAction = action; } /** Get all actions for the node. Index: openide/test/unit/src/org/openide/nodes/NodeTest.java =================================================================== RCS file: /cvs/openide/test/unit/src/org/openide/nodes/NodeTest.java,v retrieving revision 1.3 diff -u -r1.3 NodeTest.java --- openide/test/unit/src/org/openide/nodes/NodeTest.java 27 Feb 2003 23:41:24 -0000 1.3 +++ openide/test/unit/src/org/openide/nodes/NodeTest.java 8 Apr 2003 23:01:24 -0000 @@ -13,18 +13,23 @@ package org.openide.nodes; +import java.awt.event.ActionEvent; import java.beans.*; import java.util.*; +import javax.swing.AbstractAction; +import javax.swing.Action; import junit.textui.TestRunner; import org.netbeans.junit.NbTestCase; import org.netbeans.junit.NbTestSuite; +import org.openide.actions.OpenAction; +import org.openide.actions.PropertiesAction; import org.openide.nodes.*; import org.openide.util.actions.SystemAction; -/** Checking some of the behaviour of Node. - * @author Jaroslav Tulach +/** Checking some of the behaviour of Node (and AbstractNode). + * @author Jaroslav Tulach, Jesse Glick */ public class NodeTest extends NbTestCase { @@ -38,7 +43,7 @@ public void testGetActions () throws Exception { final SystemAction[] arr1 = { - SystemAction.get (org.openide.actions.PropertiesAction.class) + SystemAction.get (PropertiesAction.class) }; final SystemAction[] arr2 = { }; @@ -57,6 +62,82 @@ assertEquals ("getActions(false) properly delegates to getActions()", arr1, an.getActions (false)); assertEquals ("getActions(true) properly delegates to getContextActions()", arr2, an.getActions (true)); + } + + public void testPreferredAction() throws Exception { + final SystemAction a1 = SystemAction.get(PropertiesAction.class); + final Action a2 = new AbstractAction() { + public void actionPerformed(ActionEvent ev) {} + }; + final SystemAction a3 = SystemAction.get(OpenAction.class); + final Action a4 = new AbstractAction() { + public void actionPerformed(ActionEvent ev) {} + }; + // Old code: + Node n1 = new AbstractNode(Children.LEAF) { + { + setDefaultAction(a1); + } + }; + Node n2 = new AbstractNode(Children.LEAF) { + public SystemAction getDefaultAction() { + return a1; + } + }; + // New code: + Node n3 = new AbstractNode(Children.LEAF) { + { + setPreferredAction(a1); + } + }; + Node n4 = new AbstractNode(Children.LEAF) { + public Action getPreferredAction() { + return a1; + } + }; + Node n5 = new AbstractNode(Children.LEAF) { + { + setPreferredAction(a2); + } + }; + Node n6 = new AbstractNode(Children.LEAF) { + public Action getPreferredAction() { + return a2; + } + }; + // Wacko code: + Node n7 = new AbstractNode(Children.LEAF) { + { + setDefaultAction(a1); + } + public SystemAction getDefaultAction() { + return a3; + } + }; + Node n8 = new AbstractNode(Children.LEAF) { + { + setPreferredAction(a2); + } + public Action getPreferredAction() { + return a4; + } + }; + assertEquals(a1, n1.getDefaultAction()); + assertEquals(a1, n1.getPreferredAction()); + assertEquals(a1, n2.getDefaultAction()); + assertEquals(a1, n2.getPreferredAction()); + assertEquals(a1, n3.getDefaultAction()); + assertEquals(a1, n3.getPreferredAction()); + assertEquals(a1, n4.getDefaultAction()); + assertEquals(a1, n4.getPreferredAction()); + assertEquals(null, n5.getDefaultAction()); + assertEquals(a2, n5.getPreferredAction()); + assertEquals(null, n6.getDefaultAction()); + assertEquals(a2, n6.getPreferredAction()); + assertEquals(a3, n7.getDefaultAction()); + assertEquals(a3, n7.getPreferredAction()); + assertEquals(null, n8.getDefaultAction()); + assertEquals(a4, n8.getPreferredAction()); } }