[gui][PATCH] Implementation of MenuDragMouseEvents
Olga Rodimina
rodimina@redhat.com
Fri Jul 9 19:19:00 GMT 2004
Hi,
Here is the patch that implements MenuDragMouseEvents for Menu Elements.
I'll be committing this patch to java-gui-branch.
Olga.
-------------- next part --------------
? .snprj
? libjava.proj
? package-list
? patch
? pathc
? resources
Index: ChangeLog
===================================================================
RCS file: /cvs/gcc/gcc/libjava/ChangeLog,v
retrieving revision 1.2660.2.247
diff -c -p -u -r1.2660.2.247 ChangeLog
--- ChangeLog 7 Jul 2004 19:14:27 -0000 1.2660.2.247
+++ ChangeLog 9 Jul 2004 19:16:49 -0000
@@ -1,3 +1,34 @@
+2004-07-09 Olga Rodimina <rodimina@redhat.com>
+
+ * javax/swing/JMenuItem.java:
+ (processMouseEvent): Reimplemented to deal with
+ mouse drag events.
+ (createMenuDragMouseEvent): New private helper method.
+ Creates MenuDragMouseEvent.
+ * javax/swing/MenuSelectionManager.java
+ (componentForPoint): Implemented.
+ (isComponentPartOfCurrentMenu): Made public.
+ (processMouseEvent): Reimplemented to deal with
+ mouse drag events.
+ (setSelectedPath): Corrected small mistake that caused
+ path to be set incorrectly.
+ (getPath): If given component is JMenu then also add this
+ menu's popup menu to the selected path.
+ (javax/swing/plaf/basic/BasicMenuItemUI.java):
+ (getPath): Ditto.
+ (getPreferredSize): Call getPreferredMenuItemSize().
+ (getPreferredItemSize): Moved code from getPreferredSize to here.
+ (installListeners): Install MouseMotionListeners.
+ (MouseInputHandler): Pass mouse release event to MenuSelectionManager
+ if mouse wasn't released in the bounds of this menu item.
+ (MenuDragMouseHandler): Implemented.
+ * javax/swing/plaf/basic/BasicMenuUI.java:
+ (installListeners): Install MouseMotionListener and
+ MenuDrageMouseListener.
+ (MenuDragMouseHandler): Implemented.
+ * javax/swing/plaf/basic/BasicPopupMenuUI.java:
+ (uninstallListeners): Implemented.
+
2004-07-07 Michael Koch <konqueror@gmx.de>
* javax/swing/JCheckBox.java: Reformated.
Index: javax/swing/JMenuItem.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/JMenuItem.java,v
retrieving revision 1.2.18.10
diff -c -p -u -r1.2.18.10 JMenuItem.java
--- javax/swing/JMenuItem.java 30 Jun 2004 16:33:07 -0000 1.2.18.10
+++ javax/swing/JMenuItem.java 9 Jul 2004 19:16:50 -0000
@@ -171,12 +171,10 @@ public class JMenuItem extends AbstractB
// Initializes properties for this menu item, that are different
// from Abstract button properties.
-
- /* NOTE: According to java specifications paint_border should be set to false,
- since menu item should not have a border. However running few java programs
- it seems that menu items and menues can have a border. Commenting
- out statement below for now. */
-
+ /* NOTE: According to java specifications paint_border should be set to
+ false, since menu item should not have a border. However running few
+ java programs it seems that menu items and menues can have a border.
+ Commenting out statement below for now. */
//paint_border = false;
paint_focus = false;
hori_align = JButton.LEFT;
@@ -314,40 +312,29 @@ public class JMenuItem extends AbstractB
public void processMouseEvent(MouseEvent event, MenuElement[] path,
MenuSelectionManager manager)
{
+ // Fire MenuDragMouseEvents if mouse is being dragged.
+ if ((event.getModifiers() & InputEvent.BUTTON1_MASK) != 0)
+ processMenuDragMouseEvent(createMenuDragMouseEvent(event, path, manager));
+
switch (event.getID())
{
case MouseEvent.MOUSE_CLICKED:
break;
case MouseEvent.MOUSE_ENTERED:
- if (event.getSource() instanceof JMenuItem)
- {
- JMenuItem item = (JMenuItem) event.getSource();
-
- ButtonModel model = item.getModel();
- if (item.isRolloverEnabled())
- model.setRollover(true);
- }
+ if (isRolloverEnabled())
+ model.setRollover(true);
break;
case MouseEvent.MOUSE_EXITED:
- if (event.getSource() instanceof JMenuItem)
- {
- JMenuItem item = (JMenuItem) event.getSource();
- ButtonModel model = item.getModel();
- if (item.isRolloverEnabled())
- model.setRollover(false);
-
- if (! (event.getSource() instanceof JMenu))
- setArmed(false);
- }
+ if (isRolloverEnabled())
+ model.setRollover(false);
+ if (! (path[path.length - 1] instanceof JMenu))
+ setArmed(false);
break;
case MouseEvent.MOUSE_PRESSED:
- if (event.getSource() instanceof JMenuItem)
+ if ((event.getModifiers() & InputEvent.BUTTON1_MASK) != 0)
{
- if ((event.getModifiers() & InputEvent.BUTTON1_MASK) != 0)
- {
- model.setArmed(true);
- model.setPressed(true);
- }
+ model.setArmed(true);
+ model.setPressed(true);
}
break;
case MouseEvent.MOUSE_RELEASED:
@@ -355,17 +342,6 @@ public class JMenuItem extends AbstractB
case MouseEvent.MOUSE_MOVED:
break;
case MouseEvent.MOUSE_DRAGGED:
- MenuDragMouseEvent e = new MenuDragMouseEvent((Component) event
- .getSource(),
- event.getID(),
- event.getWhen(),
- event.getModifiers(),
- event.getX(),
- event.getY(),
- event.getClickCount(),
- event.isPopupTrigger(),
- path, manager);
- processMenuDragMouseEvent(e);
break;
}
}
@@ -376,6 +352,26 @@ public class JMenuItem extends AbstractB
* @param event DOCUMENT ME!
* @param path DOCUMENT ME!
* @param manager DOCUMENT ME!
+ *
+ * @return DOCUMENT ME!
+ */
+ private MenuDragMouseEvent createMenuDragMouseEvent(MouseEvent event,
+ MenuElement[] path,
+ MenuSelectionManager manager)
+ {
+ return new MenuDragMouseEvent((Component) event.getSource(),
+ event.getID(), event.getWhen(),
+ event.getModifiers(), event.getX(),
+ event.getY(), event.getClickCount(),
+ event.isPopupTrigger(), path, manager);
+ }
+
+ /**
+ * DOCUMENT ME!
+ *
+ * @param event DOCUMENT ME!
+ * @param path DOCUMENT ME!
+ * @param manager DOCUMENT ME!
*/
public void processKeyEvent(KeyEvent event, MenuElement[] path,
MenuSelectionManager manager)
@@ -526,19 +522,20 @@ public class JMenuItem extends AbstractB
*/
public void menuSelectionChanged(boolean changed)
{
- if (changed) {
- model.setArmed(true);
-
- if (this.getParent() instanceof JPopupMenu)
- ((JPopupMenu) this.getParent()).setSelected(this);
- }
- else {
-
- model.setArmed(false);
-
- if (this.getParent() instanceof JPopupMenu)
- ((JPopupMenu) this.getParent()).getSelectionModel().clearSelection();
- }
+ if (changed)
+ {
+ model.setArmed(true);
+
+ if (this.getParent() instanceof JPopupMenu)
+ ((JPopupMenu) this.getParent()).setSelected(this);
+ }
+ else
+ {
+ model.setArmed(false);
+
+ if (this.getParent() instanceof JPopupMenu)
+ ((JPopupMenu) this.getParent()).getSelectionModel().clearSelection();
+ }
}
/**
@@ -546,7 +543,7 @@ public class JMenuItem extends AbstractB
*
* @return $MenuElement[]$ Returns array of sub-components for this menu
* item. By default menuItem doesn't have any subcomponents and so
- * empty array is returned instead.
+ * empty array is returned instead.
*/
public MenuElement[] getSubElements()
{
Index: javax/swing/MenuSelectionManager.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/MenuSelectionManager.java,v
retrieving revision 1.2.18.6
diff -c -p -u -r1.2.18.6 MenuSelectionManager.java
--- javax/swing/MenuSelectionManager.java 6 Jul 2004 14:43:14 -0000 1.2.18.6
+++ javax/swing/MenuSelectionManager.java 9 Jul 2004 19:16:50 -0000
@@ -38,6 +38,7 @@ exception statement from your version. *
package javax.swing;
import java.awt.Component;
+import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
@@ -126,23 +127,54 @@ public class MenuSelectionManager
// clear selected path
selectedPath.clear();
-
+
// notify all listeners that the selected path was changed
fireStateChanged();
-
}
/**
- * DOCUMENT ME!
+ * This method returns menu element on the selected path that contains
+ * given source point. If no menu element on the selected path contains this
+ * point, then null is returned.
*
- * @param source DOCUMENT ME!
- * @param sourcePoint DOCUMENT ME!
+ * @param source Component relative to which sourcePoint is given
+ * @param sourcePoint point for which we want to find menu element that contains it
*
- * @return DOCUMENT ME!
+ * @return Returns menu element that contains given source point and belongs
+ * to the currently selected path. Null is return if no such menu element found.
*/
public Component componentForPoint(Component source, Point sourcePoint)
{
- throw new UnsupportedOperationException("not implemented");
+ // Convert sourcePoint to screen coordinates.
+ Point sourcePointOnScreen = sourcePoint;
+ SwingUtilities.convertPointToScreen(sourcePointOnScreen, source);
+
+ Point compPointOnScreen;
+ Component resultComp = null;
+
+ // For each menu element on the selected path, express its location
+ // in terms of screen coordinates and check if there is any
+ // menu element on the selected path that contains given source point.
+ for (int i = 0; i < selectedPath.size(); i++)
+ {
+ Component comp = ((Component) selectedPath.get(i));
+ Dimension size = comp.getSize();
+
+ // convert location of this menu item to screen coordinates
+ compPointOnScreen = comp.getLocationOnScreen();
+
+ if (compPointOnScreen.x <= sourcePointOnScreen.x
+ && sourcePointOnScreen.x < compPointOnScreen.x + size.width
+ && compPointOnScreen.y <= sourcePointOnScreen.y
+ && sourcePointOnScreen.y < compPointOnScreen.y + size.height)
+ {
+ Point p = sourcePointOnScreen;
+ SwingUtilities.convertPointFromScreen(p, comp);
+ resultComp = SwingUtilities.getDeepestComponentAt(comp, p.x, p.y);
+ break;
+ }
+ }
+ return resultComp;
}
/**
@@ -177,7 +209,7 @@ public class MenuSelectionManager
* @param c Component for which to check
* @return True if specified component is part of current menu
*/
- boolean isComponentPartOfCurrentMenu(Component c)
+ public boolean isComponentPartOfCurrentMenu(Component c)
{
MenuElement[] subElements;
for (int i = 0; i < selectedPath.size(); i++)
@@ -210,18 +242,41 @@ public class MenuSelectionManager
*/
public void processMouseEvent(MouseEvent event)
{
- Component c = ((Component) event.getSource());
-
- MenuElement[] path = getPath(c);
- ((MenuElement) c).processMouseEvent(event, path, manager);
+ Component source = ((Component) event.getSource());
- // forward events to subcomponents
- MenuElement[] subComponents = ((MenuElement) c).getSubElements();
+ // In the case of drag event, event.getSource() returns component
+ // where drag event originated. However menu element processing this
+ // event should be the one over which mouse is currently located,
+ // which is not necessary the source of the drag event.
+ Component mouseOverMenuComp;
+
+ // find over which menu element the mouse is currently located
+ if (event.getID() == MouseEvent.MOUSE_DRAGGED
+ || event.getID() == MouseEvent.MOUSE_RELEASED)
+ mouseOverMenuComp = componentForPoint(source, event.getPoint());
+ else
+ mouseOverMenuComp = source;
- for (int i = 0; i < subComponents.length; i++)
+ // Process this event only if mouse is located over some menu element
+ if (mouseOverMenuComp != null)
{
- if (subComponents[i] instanceof JMenuItem)
- subComponents[i].processMouseEvent(event, path, manager);
+ MenuElement[] path = getPath(mouseOverMenuComp);
+ ((MenuElement) mouseOverMenuComp).processMouseEvent(event, path,
+ manager);
+
+ // FIXME: Java specification says that mouse events should be
+ // forwarded to subcomponents. The code below does it, but
+ // menu's work fine without it. This code is commented for now.
+
+ /*
+ MenuElement[] subComponents = ((MenuElement) mouseOverMenuComp)
+ .getSubElements();
+
+ for (int i = 0; i < subComponents.length; i++)
+ {
+ subComponents[i].processMouseEvent(event, path, manager);
+ }
+ */
}
}
@@ -236,13 +291,15 @@ public class MenuSelectionManager
{
clearSelectedPath();
return;
- }
+ }
int i;
int minSize = path.length; // size of the smaller path.
if (path.length > selectedPath.size())
{
+ minSize = selectedPath.size();
+
// if new selected path contains more elements then current
// selection then first add all elements at
// the indexes > selectedPath.size
@@ -251,8 +308,6 @@ public class MenuSelectionManager
selectedPath.add(path[i]);
path[i].menuSelectionChanged(true);
}
-
- minSize = selectedPath.size();
}
else if (path.length < selectedPath.size())
@@ -273,21 +328,21 @@ public class MenuSelectionManager
// same location and adjust selection until
// same menu elements will be encountered at the
// same index in both current and new selection path.
- MenuElement oldSelectedPath;
+ MenuElement oldSelectedItem;
for (i = minSize - 1; i >= 0; i--)
{
- oldSelectedPath = (MenuElement) selectedPath.get(i);
+ oldSelectedItem = (MenuElement) selectedPath.get(i);
- if (path[i].equals(oldSelectedPath))
+ if (path[i].equals(oldSelectedItem))
break;
- oldSelectedPath.menuSelectionChanged(false);
+ oldSelectedItem.menuSelectionChanged(false);
path[i].menuSelectionChanged(true);
selectedPath.setElementAt(path[i], i);
}
-
- fireStateChanged();
+
+ fireStateChanged();
}
/**
@@ -299,7 +354,18 @@ public class MenuSelectionManager
*/
private MenuElement[] getPath(Component c)
{
+ // FIXME: There is the same method in BasicMenuItemUI. However I
+ // cannot use it here instead of this method, since I cannot assume that
+ // all the menu elements on the selected path are JMenuItem or JMenu.
+ // For now I've just duplicated it here. Please
+ // fix me or delete me if another better approach will be found, and
+ // this method will not be necessary.
ArrayList path = new ArrayList();
+
+ // if given component is JMenu, we also need to include
+ // it's popup menu in the path
+ if (c instanceof JMenu)
+ path.add(((JMenu) c).getPopupMenu());
while (c instanceof MenuElement)
{
path.add(0, (MenuElement) c);
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.15
diff -c -p -u -r1.1.2.15 BasicMenuItemUI.java
--- javax/swing/plaf/basic/BasicMenuItemUI.java 6 Jul 2004 14:43:14 -0000 1.1.2.15
+++ javax/swing/plaf/basic/BasicMenuItemUI.java 9 Jul 2004 19:16:50 -0000
@@ -279,6 +279,11 @@ public class BasicMenuItemUI extends Men
public MenuElement[] getPath()
{
ArrayList path = new ArrayList();
+
+ // Path to menu should also include its popup menu.
+ if (menuItem instanceof JMenu)
+ path.add(((JMenu) menuItem).getPopupMenu());
+
Component c = menuItem;
while (c instanceof MenuElement)
{
@@ -309,19 +314,6 @@ public class BasicMenuItemUI extends Men
Icon arrowIcon,
int defaultTextIconGap)
{
- // FIXME: Need to implement.
- return null;
- }
-
- /**
- * Returns preferred size of the given component
- *
- * @param c component for which to return preferred size
- *
- * @return $Dimension$ preferred size for the given component
- */
- public Dimension getPreferredSize(JComponent c)
- {
JMenuItem m = (JMenuItem) c;
Dimension d = BasicGraphicsUtils.getPreferredButtonSize(m,
defaultTextIconGap);
@@ -364,10 +356,18 @@ public class BasicMenuItemUI extends Men
}
/**
- * DOCUMENT ME!
+ * Returns preferred size of the given component
*
- * @return $returnType$ DOCUMENT ME!
+ * @param c component for which to return preferred size
+ *
+ * @return $Dimension$ preferred size for the given component
*/
+ public Dimension getPreferredSize(JComponent c)
+ {
+ return getPreferredMenuItemSize(c, checkIcon, arrowIcon,
+ defaultTextIconGap);
+ }
+
protected String getPropertyPrefix()
{
return null;
@@ -419,6 +419,7 @@ public class BasicMenuItemUI extends Men
protected void installListeners()
{
menuItem.addMouseListener(mouseInputListener);
+ menuItem.addMouseMotionListener(mouseInputListener);
menuItem.addMenuDragMouseListener(menuDragMouseListener);
menuItem.addMenuKeyListener(menuKeyListener);
menuItem.addPropertyChangeListener(propertyChangeListener);
@@ -884,14 +885,17 @@ public class BasicMenuItemUI extends Men
*/
public void mouseReleased(MouseEvent e)
{
- Rectangle size = menuItem.getBounds(); //this.getParent().getSize();
+ Rectangle size = menuItem.getBounds();
+ MenuSelectionManager manager = MenuSelectionManager.defaultManager();
if (e.getX() > 0 && e.getX() < size.width && e.getY() > 0
&& e.getY() < size.height)
{
- MenuSelectionManager manager = MenuSelectionManager.defaultManager();
manager.clearSelectedPath();
- menuItem.doClick(0);
+ menuItem.doClick();
}
+
+ else
+ manager.processMouseEvent(e);
}
}
@@ -907,6 +911,8 @@ public class BasicMenuItemUI extends Men
*/
public void menuDragMouseDragged(MenuDragMouseEvent e)
{
+ MenuSelectionManager manager = MenuSelectionManager.defaultManager();
+ manager.setSelectedPath(e.getPath());
}
/**
@@ -917,6 +923,8 @@ public class BasicMenuItemUI extends Men
*/
public void menuDragMouseEntered(MenuDragMouseEvent e)
{
+ MenuSelectionManager manager = MenuSelectionManager.defaultManager();
+ manager.setSelectedPath(e.getPath());
}
/**
@@ -937,6 +945,13 @@ public class BasicMenuItemUI extends Men
*/
public void menuDragMouseReleased(MenuDragMouseEvent e)
{
+ MenuElement[] path = e.getPath();
+
+ if (path[path.length - 1] instanceof JMenuItem)
+ ((JMenuItem) path[path.length - 1]).doClick();
+
+ MenuSelectionManager manager = MenuSelectionManager.defaultManager();
+ manager.clearSelectedPath();
}
}
Index: javax/swing/plaf/basic/BasicMenuUI.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/javax/swing/plaf/basic/BasicMenuUI.java,v
retrieving revision 1.1.2.8
diff -c -p -u -r1.1.2.8 BasicMenuUI.java
--- javax/swing/plaf/basic/BasicMenuUI.java 22 Jun 2004 17:03:44 -0000 1.1.2.8
+++ javax/swing/plaf/basic/BasicMenuUI.java 9 Jul 2004 19:16:50 -0000
@@ -236,7 +236,9 @@ public class BasicMenuUI extends BasicMe
protected void installListeners()
{
((JMenu) menuItem).addMouseListener(mouseInputListener);
+ ((JMenu) menuItem).addMouseMotionListener(mouseInputListener);
((JMenu) menuItem).addMenuListener(menuListener);
+ ((JMenu) menuItem).addMenuDragMouseListener(menuDragMouseListener);
}
protected void setupPostTimer(JMenu menu)
@@ -452,6 +454,8 @@ public class BasicMenuUI extends BasicMe
*/
public void menuDragMouseDragged(MenuDragMouseEvent e)
{
+ MenuSelectionManager manager = MenuSelectionManager.defaultManager();
+ manager.setSelectedPath(e.getPath());
}
/**
@@ -462,6 +466,8 @@ public class BasicMenuUI extends BasicMe
*/
public void menuDragMouseEntered(MenuDragMouseEvent e)
{
+ MenuSelectionManager manager = MenuSelectionManager.defaultManager();
+ manager.setSelectedPath(e.getPath());
}
/**
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.4
diff -c -p -u -r1.1.2.4 BasicPopupMenuUI.java
--- javax/swing/plaf/basic/BasicPopupMenuUI.java 6 Jul 2004 14:43:14 -0000 1.1.2.4
+++ javax/swing/plaf/basic/BasicPopupMenuUI.java 9 Jul 2004 19:16:50 -0000
@@ -214,6 +214,9 @@ public class BasicPopupMenuUI extends Po
*/
protected void uninstallListeners()
{
+ popupMenu.removeMouseListener(mouseInputListener);
+ popupMenu.removeMouseMotionListener(mouseInputListener);
+ popupMenu.removePopupMenuListener(popupMenuListener);
}
/**
More information about the Java-patches
mailing list