[gui][PATCH] More fixes to menu related components

Olga Rodimina rodimina@redhat.com
Tue Jul 6 14:33:00 GMT 2004


Hi,

I made few more fixes to menu related components. Some of the fixes were
necessary to make gcj compile with jikes. 

I'll be committing this patch to java-gui-branch.



Olga.
-------------- next part --------------
? .snprj
? libjava.proj
? package-list
? patch
? patch2
? resources
Index: ChangeLog
===================================================================
RCS file: /cvs/gcc/gcc/libjava/ChangeLog,v
retrieving revision 1.2660.2.242
diff -c -p -u -r1.2660.2.242 ChangeLog
--- ChangeLog	2 Jul 2004 19:08:47 -0000	1.2660.2.242
+++ ChangeLog	6 Jul 2004 14:28:38 -0000
@@ -1,3 +1,25 @@
+2004-07-05  Olga Rodimina  <rodimina@redhat.com>
+
+	* javax/swing/JMenu.java:
+	(setSelected): Display popup menu only if this menu 
+	is showing on the screen.
+	* javax/swing/JPopupMenu.java:
+	(processMouseEvent): Added comment.
+	(processKeyEvent): Likewise.
+	* javax/swing/MenuSelectionManager.java:
+	(clearSelectedPath): Only fireStateChanged() after
+	selected path was changed, not before.
+	(setSelectedPath): Likewise.
+	* javax/swing/plaf/basic/BasicMenuItemUI.java:
+	(paintMenuItem): Corrected position of menu item's
+	icon.
+	* javax/swing/plaf/basic/BasicPopupMenuUI.java:
+	(installUI): Correct setDefaultLightWeightPopupEnabled call.
+	(popupMenuCanceled): Reimplemented.
+	(popupMenuWillBecomeVisible): Select first menu item by default 
+	when displaying free floating popup menus.
+	(TopWindowListener): Reimplemented.
+
 2004-07-02  Olga Rodimina  <rodimina@redhat.com>
 
 	* java/awt/Window.java: Reverted changes from my 
Index: javax/swing/JMenu.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/JMenu.java,v
retrieving revision 1.3.8.9
diff -c -p -u -r1.3.8.9 JMenu.java
--- javax/swing/JMenu.java	30 Jun 2004 16:33:07 -0000	1.3.8.9
+++ javax/swing/JMenu.java	6 Jul 2004 14:28:39 -0000
@@ -339,23 +339,28 @@ public class JMenu extends JMenuItem imp
       {
 	super.setArmed(true);
 	super.setSelected(true);
-	fireMenuSelected();
 
-	int x = 0;
-	int y = 0;
-	if (menuLocation == null)
+	if (this.isShowing())
 	  {
-	    // 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();
+	    fireMenuSelected();
+
+	    int x = 0;
+	    int y = 0;
 
-	    getPopupMenu().show(this, x, y);
+	    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
-	  getPopupMenu().show(this, menuLocation.x, menuLocation.y);
       }
 
     else
Index: javax/swing/JPopupMenu.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/JPopupMenu.java,v
retrieving revision 1.3.8.11
diff -c -p -u -r1.3.8.11 JPopupMenu.java
--- javax/swing/JPopupMenu.java	30 Jun 2004 16:33:07 -0000	1.3.8.11
+++ javax/swing/JPopupMenu.java	6 Jul 2004 14:28:39 -0000
@@ -130,7 +130,6 @@ public class JPopupMenu extends JCompone
 
     lightWeightPopupEnabled = DefaultLightWeightPopupEnabled;
     selectionModel = new DefaultSingleSelectionModel();
-
     super.setVisible(false);
   }
 
@@ -788,6 +787,8 @@ public class JPopupMenu extends JCompone
   public void processMouseEvent(MouseEvent event, MenuElement[] path,
                                 MenuSelectionManager manager)
   {
+    // Empty Implementation. This method is needed for the implementation
+    // of MenuElement interface
   }
 
   /**
@@ -800,6 +801,8 @@ public class JPopupMenu extends JCompone
   public void processKeyEvent(KeyEvent event, MenuElement[] path,
                               MenuSelectionManager manager)
   {
+    // Empty Implementation. This method is needed for the implementation
+    // of MenuElement interface
   }
 
   /**
Index: javax/swing/MenuSelectionManager.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/MenuSelectionManager.java,v
retrieving revision 1.2.18.5
diff -c -p -u -r1.2.18.5 MenuSelectionManager.java
--- javax/swing/MenuSelectionManager.java	17 Jun 2004 18:18:38 -0000	1.2.18.5
+++ javax/swing/MenuSelectionManager.java	6 Jul 2004 14:28:40 -0000
@@ -124,11 +124,12 @@ public class MenuSelectionManager
     for (int i = selectedPath.size() - 1; i >= 0; i--)
       ((MenuElement) selectedPath.get(i)).menuSelectionChanged(false);
 
+    // clear selected path
+    selectedPath.clear();
+    
     // notify all listeners that the selected path was changed    
     fireStateChanged();
 
-    // clear selected path
-    selectedPath.clear();
   }
 
   /**
@@ -235,9 +236,7 @@ public class MenuSelectionManager
       {
 	clearSelectedPath();
 	return;
-      }
-
-    fireStateChanged();
+      }    
 
     int i;
     int minSize = path.length; // size of the smaller path. 
@@ -287,6 +286,8 @@ public class MenuSelectionManager
 	path[i].menuSelectionChanged(true);
 	selectedPath.setElementAt(path[i], i);
       }
+      
+    fireStateChanged();  
   }
 
   /**
Index: javax/swing/plaf/basic/BasicMenuItemUI.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/plaf/basic/BasicMenuItemUI.java,v
retrieving revision 1.1.2.14
diff -c -p -u -r1.1.2.14 BasicMenuItemUI.java
--- javax/swing/plaf/basic/BasicMenuItemUI.java	30 Jun 2004 16:33:08 -0000	1.1.2.14
+++ javax/swing/plaf/basic/BasicMenuItemUI.java	6 Jul 2004 14:28:40 -0000
@@ -558,6 +558,18 @@ public class BasicMenuItemUI extends Men
 	  }
       }
 
+    // paint icon
+    // FIXME: should paint different icon at different button state's.
+    // i.e disabled icon when button is disabled.. etc.
+    Icon i = m.getIcon();
+    if (i != null)
+      {
+	i.paintIcon(c, g, vr.x, vr.y);
+
+	// Adjust view rectangle, s.t text would be drawn after menu item's icon.
+	vr.x += i.getIconWidth() + defaultTextIconGap;
+      }
+
     // paint text and user menu icon if it exists	     
     SwingUtilities.layoutCompoundLabel(c, fm, m.getText(), m.getIcon(),
                                        vertAlign, horAlign, vertTextPos,
@@ -566,19 +578,6 @@ public class BasicMenuItemUI extends Men
 
     paintText(g, m, tr, m.getText());
 
-    // paint icon
-    // FIXME: should paint different icon at different button state's.
-    // i.e disabled icon when button is disabled.. etc.
-
-    
-    Icon i = m.getIcon();
-    if (i != null)
-      {
-         int x = ir.x;
-         int y = ir.y;
-         i.paintIcon(c, g, x, y);
-      }    
-
     // paint accelerator    
     String acceleratorText = "";
 
@@ -989,8 +988,8 @@ public class BasicMenuItemUI extends Men
      */
     public void propertyChange(PropertyChangeEvent evt)
     {
-	menuItem.revalidate();
-	menuItem.repaint();
+      menuItem.revalidate();
+      menuItem.repaint();
     }
   }
 }
Index: javax/swing/plaf/basic/BasicPopupMenuUI.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/plaf/basic/BasicPopupMenuUI.java,v
retrieving revision 1.1.2.3
diff -c -p -u -r1.1.2.3 BasicPopupMenuUI.java
--- javax/swing/plaf/basic/BasicPopupMenuUI.java	30 Jun 2004 16:33:08 -0000	1.1.2.3
+++ javax/swing/plaf/basic/BasicPopupMenuUI.java	6 Jul 2004 14:28:40 -0000
@@ -48,6 +48,7 @@ import java.awt.FontMetrics;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import java.awt.GridBagLayout;
+import java.awt.GridLayout;
 import java.awt.Insets;
 import java.awt.Rectangle;
 import java.awt.Stroke;
@@ -144,7 +145,7 @@ public class BasicPopupMenuUI extends Po
     popupMenu = (JPopupMenu) c;
     popupMenu.setLayout(new GridBagLayout());
     popupMenu.setBorderPainted(true);
-    popupMenu.setDefaultLightWeightPopupEnabled(true);
+    JPopupMenu.setDefaultLightWeightPopupEnabled(true);
 
     installDefaults();
     installListeners();
@@ -284,11 +285,7 @@ public class BasicPopupMenuUI extends Po
     public void popupMenuCanceled(PopupMenuEvent event)
     {
       MenuSelectionManager manager = MenuSelectionManager.defaultManager();
-
-      if (manager.getSelectedPath().length != 0)
-	manager.clearSelectedPath();
-      else
-	popupMenu.setVisible(false);
+      manager.clearSelectedPath();
     }
 
     /**
@@ -301,6 +298,7 @@ public class BasicPopupMenuUI extends Po
       // remove listener that listens to component events fired 
       // by the top - level window that this popup belongs to
       Component invoker = popupMenu.getInvoker();
+
       Container rootContainer = (Container) SwingUtilities.getRoot(invoker);
       rootContainer.removeComponentListener(topWindowListener);
     }
@@ -317,6 +315,22 @@ public class BasicPopupMenuUI extends Po
       Component invoker = popupMenu.getInvoker();
       Container rootContainer = (Container) SwingUtilities.getRoot(invoker);
       rootContainer.addComponentListener(topWindowListener);
+
+      // if this popup menu is a free floating popup menu,
+      // then by default its first element should be always selected when
+      // this popup menu becomes visible. 
+      MenuSelectionManager manager = MenuSelectionManager.defaultManager();
+
+      if (manager.getSelectedPath().length == 0)
+        {
+	  // Set selected path to point to the first item in the popup menu
+	  MenuElement[] path = new MenuElement[2];
+	  path[0] = popupMenu;
+	  Component[] comps = popupMenu.getComponents();
+	  if (comps.length != 0 && comps[0] instanceof MenuElement)
+	    path[1] = (MenuElement) comps[0];
+	  manager.setSelectedPath(path);
+        }
     }
   }
 
@@ -338,7 +352,8 @@ public class BasicPopupMenuUI extends Po
      */
     public void componentResized(ComponentEvent e)
     {
-      popupMenu.firePopupMenuCanceled();
+      MenuSelectionManager manager = MenuSelectionManager.defaultManager();
+      manager.clearSelectedPath();
     }
 
     /**
@@ -349,7 +364,8 @@ public class BasicPopupMenuUI extends Po
      */
     public void componentMoved(ComponentEvent e)
     {
-      popupMenu.firePopupMenuCanceled();
+      MenuSelectionManager manager = MenuSelectionManager.defaultManager();
+      manager.clearSelectedPath();
     }
 
     /**
@@ -360,7 +376,8 @@ public class BasicPopupMenuUI extends Po
      */
     public void componentShown(ComponentEvent e)
     {
-      popupMenu.firePopupMenuCanceled();
+      MenuSelectionManager manager = MenuSelectionManager.defaultManager();
+      manager.clearSelectedPath();
     }
 
     /**
@@ -371,7 +388,8 @@ public class BasicPopupMenuUI extends Po
      */
     public void componentHidden(ComponentEvent e)
     {
-      popupMenu.firePopupMenuCanceled();
+      MenuSelectionManager manager = MenuSelectionManager.defaultManager();
+      manager.clearSelectedPath();
     }
   }
 


More information about the Java-patches mailing list