This patch is in reference to bug report 13683, JMenu setSelected
behavior.  Direct calls to setSelected no longer expand the popupMenu,
although mouse events still do.

This also allows direct calls to setSelected to select the menu even
while it is disabled (as in the JDK), however it is not properly painted
yet.  Once I look into that I will close the bug report.

This patch is pending approval.

Patch attached.



2005-07-11  Anthony Balkissoon  <[EMAIL PROTECTED]>

* javax/swing/JMenu.java:
(setSelectedHelper): new method.
(setSelected): Code moved to setSelectedHelper. Calls
setSelectedHelper(selected,true,false) which doesn't expand the popup
menu and works whether the menu is enabled or not.
(menuSelectionChanged): Changed call to setSelected(changed) to 
setSelectedHelper(changed,isEnabled(),true) which does expand the
popup menu, but only if the menu is enabled.

-Tony
Index: javax/swing/JMenu.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JMenu.java,v
retrieving revision 1.18
diff -u -r1.18 JMenu.java
--- javax/swing/JMenu.java	8 Jul 2005 00:23:22 -0000	1.18
+++ javax/swing/JMenu.java	11 Jul 2005 16:59:10 -0000
@@ -332,52 +332,50 @@
   }
 
   /**
-   * Changes this menu selected state if selected is true and false otherwise
-   * This method fires menuEvents to menu's registered listeners.
-   *
-   * @param selected true if the menu should be selected and false otherwise
+   * A helper method to handle setSelected calls from both mouse events and 
+   * direct calls to setSelected.  Direct calls shouldn't expand the popup
+   * menu and should select the JMenu even if it is disabled.  Mouse events
+   * only select the JMenu if it is enabled and should expand the popup menu
+   * associated with this JMenu.
+   * @param selected whether or not the JMenu was selected
+   * @param menuEnabled whether or not selecting the menu is "enabled".  This
+   * is always true for direct calls, and is set to isEnabled() for mouse 
+   * based calls.
+   * @param showMenu whether or not to show the popup menu
    */
-  public void setSelected(boolean selected)
+  private void setSelectedHelper(boolean selected, boolean menuEnabled, boolean showMenu)
   {
     // If menu is selected and enabled, activates the menu and 
     // displays associated popup.	
-    if (selected && isEnabled())
+    if (selected && menuEnabled)
       {
 	super.setArmed(true);
 	super.setSelected(true);
-
-	// FIXME: The reference implementation behaves different here. When
-	// calling setSelected(true) it will *not* open the popup but appear
-	// selected. This is even true when the menu is disabled. Our
-	// implementation will always open the popup (when enabled) and 
-	// will not appear selected when disabled.
-
-	// FIXME: The popup menu should be shown on the screen after certain
-	// number of seconds pass. The 'delay' property of this menu indicates
-	// this amount of seconds. 'delay' property is 0 by default.
+        
 	if (isShowing())
 	  {
 	    fireMenuSelected();
-
+            
 	    int x = 0;
 	    int y = 0;
-
-	    if (menuLocation == null)
-	      {
-		// Calculate correct position of the popup. Note that location of the popup 
-		// passed to show() should be relative to the popup's invoker
-		if (isTopLevelMenu())
-		  y = this.getHeight();
-		else
-		  x = this.getWidth();
-
-		getPopupMenu().show(this, x, y);
-	      }
-	    else
-	      getPopupMenu().show(this, menuLocation.x, menuLocation.y);
+            if (showMenu)
+              if (menuLocation == null)
+                {
+                  // Calculate correct position of the popup. Note that location of the popup 
+                  // passed to show() should be relative to the popup's invoker
+                  if (isTopLevelMenu())
+                    y = this.getHeight();
+                  else
+                    x = this.getWidth();
+                  getPopupMenu().show(this, x, y);
+                }
+              else
+                {
+                  getPopupMenu().show(this, menuLocation.x, menuLocation.y);
+                }
 	  }
       }
-
+    
     else
       {
 	super.setSelected(false);
@@ -385,6 +383,18 @@
 	fireMenuDeselected();
 	popupMenu.setVisible(false);
       }
+
+  }
+
+  /**
+   * Changes this menu selected state if selected is true and false otherwise
+   * This method fires menuEvents to menu's registered listeners.
+   *
+   * @param selected true if the menu should be selected and false otherwise
+   */
+  public void setSelected(boolean selected)
+  {
+    setSelectedHelper(selected, true, false); 
   }
 
   /**
@@ -715,7 +725,7 @@
   {
     // if this menu selection is true, then activate this menu and 
     // display popup associated with this menu
-    setSelected(changed);
+    setSelectedHelper(changed, isEnabled(), true);
   }
 
   /**
_______________________________________________
Classpath-patches mailing list
Classpath-patches@gnu.org
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to