summaryrefslogtreecommitdiff
path: root/libjava/javax/swing/plaf/basic/BasicPopupMenuUI.java
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/javax/swing/plaf/basic/BasicPopupMenuUI.java')
-rw-r--r--libjava/javax/swing/plaf/basic/BasicPopupMenuUI.java205
1 files changed, 142 insertions, 63 deletions
diff --git a/libjava/javax/swing/plaf/basic/BasicPopupMenuUI.java b/libjava/javax/swing/plaf/basic/BasicPopupMenuUI.java
index 48612d8769e..f5b3e24c277 100644
--- a/libjava/javax/swing/plaf/basic/BasicPopupMenuUI.java
+++ b/libjava/javax/swing/plaf/basic/BasicPopupMenuUI.java
@@ -40,15 +40,20 @@ package javax.swing.plaf.basic;
import java.awt.AWTKeyStroke;
import java.awt.BasicStroke;
import java.awt.Color;
+import java.awt.Component;
+import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
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;
+import java.awt.event.ComponentEvent;
+import java.awt.event.ComponentListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
@@ -88,14 +93,22 @@ import javax.swing.plaf.PopupMenuUI;
/**
- * DOCUMENT ME!
+ * UI Delegate for JPopupMenu
*/
public class BasicPopupMenuUI extends PopupMenuUI
{
+ /* popupMenu for which this UI delegate is for*/
protected JPopupMenu popupMenu;
+
+ /* MouseInputListener listens to mouse events */
private static transient MouseInputListener mouseInputListener;
+
+ /* PopupMenuListener listens to popup menu events fired by JPopupMenu*/
private transient PopupMenuListener popupMenuListener;
+ /* ComponentListener listening to popupMenu's invoker. */
+ private TopWindowListener topWindowListener;
+
/**
* Creates a new BasicPopupMenuUI object.
*/
@@ -103,14 +116,16 @@ public class BasicPopupMenuUI extends PopupMenuUI
{
popupMenuListener = new PopupMenuHandler();
mouseInputListener = new MouseInputHandler();
+ topWindowListener = new TopWindowListener();
}
/**
- * DOCUMENT ME!
+ * Factory method to create a BasicPopupMenuUI for the given {@link
+ * JComponent}, which should be a {@link JMenuItem}.
*
- * @param x DOCUMENT ME!
+ * @param x The {@link JComponent} a UI is being created for.
*
- * @return DOCUMENT ME!
+ * @return A BasicPopupMenuUI for the {@link JComponent}.
*/
public static ComponentUI createUI(JComponent x)
{
@@ -118,9 +133,11 @@ public class BasicPopupMenuUI extends PopupMenuUI
}
/**
- * DOCUMENT ME!
+ * Installs and initializes all fields for this UI delegate. Any properties
+ * of the UI that need to be initialized and/or set to defaults will be
+ * done now. It will also install any listeners necessary.
*
- * @param c DOCUMENT ME!
+ * @param c The {@link JComponent} that is having this UI installed.
*/
public void installUI(JComponent c)
{
@@ -128,14 +145,15 @@ public class BasicPopupMenuUI extends PopupMenuUI
popupMenu = (JPopupMenu) c;
popupMenu.setLayout(new GridBagLayout());
popupMenu.setBorderPainted(true);
- popupMenu.setDefaultLightWeightPopupEnabled(true);
+ JPopupMenu.setDefaultLightWeightPopupEnabled(true);
installDefaults();
installListeners();
}
/**
- * DOCUMENT ME!
+ * This method installs the defaults that are defined in the Basic look and
+ * feel for this {@link JPopupMenu}.
*/
public void installDefaults()
{
@@ -148,7 +166,7 @@ public class BasicPopupMenuUI extends PopupMenuUI
}
/**
- * DOCUMENT ME!
+ * This method installs the listeners for the {@link JMenuItem}.
*/
protected void installListeners()
{
@@ -158,16 +176,19 @@ public class BasicPopupMenuUI extends PopupMenuUI
}
/**
- * DOCUMENT ME!
+ * This method installs the keyboard actions for this {@link JPopupMenu}.
*/
protected void installKeyboardActions()
{
+ // FIXME: Need to implement
}
/**
- * DOCUMENT ME!
+ * Performs the opposite of installUI. Any properties or resources that need
+ * to be cleaned up will be done now. It will also uninstall any listeners
+ * it has. In addition, any properties of this UI will be nulled.
*
- * @param c DOCUMENT ME!
+ * @param c The {@link JComponent} that is having this UI uninstalled.
*/
public void uninstallUI(JComponent c)
{
@@ -177,7 +198,8 @@ public class BasicPopupMenuUI extends PopupMenuUI
}
/**
- * DOCUMENT ME!
+ * This method uninstalls the defaults and sets any objects created during
+ * install to null
*/
protected void uninstallDefaults()
{
@@ -188,25 +210,29 @@ public class BasicPopupMenuUI extends PopupMenuUI
}
/**
- * DOCUMENT ME!
+ * Unregisters all the listeners that this UI delegate was using.
*/
protected void uninstallListeners()
{
+ popupMenu.removeMouseListener(mouseInputListener);
+ popupMenu.removeMouseMotionListener(mouseInputListener);
+ popupMenu.removePopupMenuListener(popupMenuListener);
}
/**
- * DOCUMENT ME!
+ * Uninstalls any keyboard actions.
*/
protected void uninstallKeyboardActions()
{
+ // FIXME: Need to implement
}
/**
- * DOCUMENT ME!
+ * This method returns the minimum size of the JPopupMenu.
*
- * @param c DOCUMENT ME!
+ * @param c The JComponent to find a size for.
*
- * @return DOCUMENT ME!
+ * @return The minimum size.
*/
public Dimension getMinimumSize(JComponent c)
{
@@ -214,11 +240,11 @@ public class BasicPopupMenuUI extends PopupMenuUI
}
/**
- * DOCUMENT ME!
+ * This method returns the preferred size of the JPopupMenu.
*
- * @param c DOCUMENT ME!
+ * @param c The JComponent to find a size for.
*
- * @return DOCUMENT ME!
+ * @return The preferred size.
*/
public Dimension getPreferredSize(JComponent c)
{
@@ -226,11 +252,11 @@ public class BasicPopupMenuUI extends PopupMenuUI
}
/**
- * DOCUMENT ME!
+ * This method returns the minimum size of the JPopupMenu.
*
- * @param c DOCUMENT ME!
+ * @param c The JComponent to find a size for.
*
- * @return DOCUMENT ME!
+ * @return The minimum size.
*/
public Dimension getMaximumSize(JComponent c)
{
@@ -238,11 +264,13 @@ public class BasicPopupMenuUI extends PopupMenuUI
}
/**
- * DOCUMENT ME!
+ * Return true if given mouse event is a platform popup trigger,
+ * and false otherwise
*
- * @param e DOCUMENT ME!
+ * @param e MouseEvent that is to be checked for popup trigger event
*
- * @return DOCUMENT ME!
+ * @return true if given mouse event is a platform popup trigger,
+ * and false otherwise
*/
public boolean isPopupTrigger(MouseEvent e)
{
@@ -250,102 +278,153 @@ public class BasicPopupMenuUI extends PopupMenuUI
}
/**
- * DOCUMENT ME!
+ * This listener handles PopupMenuEvents fired by JPopupMenu
*/
- protected class PopupMenuHandler implements PopupMenuListener
+ private class PopupMenuHandler implements PopupMenuListener
{
/**
- * DOCUMENT ME!
+ * This method is invoked when JPopupMenu is cancelled.
*
- * @param event DOCUMENT ME!
+ * @param event the PopupMenuEvent
*/
public void popupMenuCanceled(PopupMenuEvent event)
{
+ MenuSelectionManager manager = MenuSelectionManager.defaultManager();
+ manager.clearSelectedPath();
}
/**
- * DOCUMENT ME!
+ * This method is invoked when JPopupMenu becomes invisible
*
- * @param event DOCUMENT ME!
+ * @param event the PopupMenuEvent
*/
public void popupMenuWillBecomeInvisible(PopupMenuEvent event)
{
+ // 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);
}
/**
- * DOCUMENT ME!
+ * This method is invoked when JPopupMenu becomes visible
*
- * @param event DOCUMENT ME!
+ * @param event the PopupMenuEvent
*/
public void popupMenuWillBecomeVisible(PopupMenuEvent event)
{
+ // Adds topWindowListener to top-level window to listener to
+ // ComponentEvents fired by it. We need to cancel this popup menu
+ // if topWindow to which this popup belongs was resized or moved.
+ 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);
+ }
}
}
/**
- * DOCUMENT ME!
+ * ComponentListener that listens to Component Events fired by the
+ * top - level window to which popup menu belongs. If top-level
+ * window was resized, moved or hidded then popup menu will
+ * be hidded and selected path of current menu hierarchy will be set
+ * to null.
+ *
*/
- protected class MouseInputHandler implements MouseInputListener
+ private class TopWindowListener implements ComponentListener
{
/**
- * DOCUMENT ME!
+ * This method is invoked when top-level window is resized.
+ * This method closes current menu hierarchy.
*
- * @param e DOCUMENT ME!
+ * @param e The ComponentEvent
*/
- public void mouseClicked(MouseEvent e)
+ public void componentResized(ComponentEvent e)
{
+ MenuSelectionManager manager = MenuSelectionManager.defaultManager();
+ manager.clearSelectedPath();
}
/**
- * DOCUMENT ME!
+ * This method is invoked when top-level window is moved.
+ * This method closes current menu hierarchy.
*
- * @param e DOCUMENT ME!
+ * @param e The ComponentEvent
*/
- public void mouseDragged(MouseEvent e)
+ public void componentMoved(ComponentEvent e)
{
+ MenuSelectionManager manager = MenuSelectionManager.defaultManager();
+ manager.clearSelectedPath();
}
/**
- * DOCUMENT ME!
+ * This method is invoked when top-level window is shown
+ * This method does nothing by default.
*
- * @param e DOCUMENT ME!
+ * @param e The ComponentEvent
*/
- public void mouseEntered(MouseEvent e)
+ public void componentShown(ComponentEvent e)
{
+ MenuSelectionManager manager = MenuSelectionManager.defaultManager();
+ manager.clearSelectedPath();
}
/**
- * DOCUMENT ME!
+ * This method is invoked when top-level window is hidden
+ * This method closes current menu hierarchy.
*
- * @param e DOCUMENT ME!
+ * @param e The ComponentEvent
*/
+ public void componentHidden(ComponentEvent e)
+ {
+ MenuSelectionManager manager = MenuSelectionManager.defaultManager();
+ manager.clearSelectedPath();
+ }
+ }
+
+ private class MouseInputHandler implements MouseInputListener
+ {
+ public void mouseClicked(MouseEvent e)
+ {
+ }
+
+ public void mouseDragged(MouseEvent e)
+ {
+ }
+
+ public void mouseEntered(MouseEvent e)
+ {
+ }
+
public void mouseExited(MouseEvent e)
{
}
- /**
- * DOCUMENT ME!
- *
- * @param e DOCUMENT ME!
- */
public void mouseMoved(MouseEvent e)
{
}
- /**
- * DOCUMENT ME!
- *
- * @param e DOCUMENT ME!
- */
public void mousePressed(MouseEvent e)
{
}
- /**
- * DOCUMENT ME!
- *
- * @param e DOCUMENT ME!
- */
public void mouseReleased(MouseEvent e)
{
}