Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / swing / plaf / basic / BasicMenuUI.java
blobf8936be5b66544f30a8cb79272117775a6236e3b
1 /* BasicMenuUI.java
2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.plaf.basic;
41 import gnu.classpath.NotImplementedException;
43 import java.awt.Component;
44 import java.awt.Dimension;
45 import java.awt.event.MouseEvent;
46 import java.beans.PropertyChangeListener;
48 import javax.swing.JComponent;
49 import javax.swing.JMenu;
50 import javax.swing.JMenuBar;
51 import javax.swing.JPopupMenu;
52 import javax.swing.LookAndFeel;
53 import javax.swing.MenuSelectionManager;
54 import javax.swing.UIDefaults;
55 import javax.swing.UIManager;
56 import javax.swing.event.ChangeEvent;
57 import javax.swing.event.ChangeListener;
58 import javax.swing.event.MenuDragMouseEvent;
59 import javax.swing.event.MenuDragMouseListener;
60 import javax.swing.event.MenuEvent;
61 import javax.swing.event.MenuKeyEvent;
62 import javax.swing.event.MenuKeyListener;
63 import javax.swing.event.MenuListener;
64 import javax.swing.event.MouseInputListener;
65 import javax.swing.plaf.ComponentUI;
67 /**
68 * UI Delegate for JMenu
70 public class BasicMenuUI extends BasicMenuItemUI
72 protected ChangeListener changeListener;
74 /* MenuListener listens to MenuEvents fired by JMenu */
75 protected MenuListener menuListener;
77 /* PropertyChangeListner that listens to propertyChangeEvents occuring in JMenu*/
78 protected PropertyChangeListener propertyChangeListener;
80 /**
81 * Creates a new BasicMenuUI object.
83 public BasicMenuUI()
85 mouseInputListener = createMouseInputListener((JMenu) menuItem);
86 menuListener = createMenuListener((JMenu) menuItem);
87 propertyChangeListener = createPropertyChangeListener((JMenu) menuItem);
90 /**
91 * This method creates a new ChangeListener.
93 * @return A new ChangeListener.
95 protected ChangeListener createChangeListener(JComponent c)
97 return new ChangeHandler((JMenu) c, this);
101 * This method creates new MenuDragMouseListener to listen to mouse dragged events
102 * occuring in the Menu
104 * @param c the menu to listen to
106 * @return The MenuDrageMouseListener
108 protected MenuDragMouseListener createMenuDragMouseListener(JComponent c)
110 return new MenuDragMouseHandler();
114 * This method creates new MenuDragKeyListener to listen to key events
116 * @param c the menu to listen to
118 * @return The MenuKeyListener
120 protected MenuKeyListener createMenuKeyListener(JComponent c)
122 return new MenuKeyHandler();
126 * This method creates new MenuListener to listen to menu events
127 * occuring in the Menu
129 * @param c the menu to listen to
131 * @return The MenuListener
133 protected MenuListener createMenuListener(JComponent c)
135 return new MenuHandler();
139 * This method creates new MouseInputListener to listen to mouse input events
140 * occuring in the Menu
142 * @param c the menu to listen to
144 * @return The MouseInputListener
146 protected MouseInputListener createMouseInputListener(JComponent c)
148 return new MouseInputHandler();
152 * This method creates newPropertyChangeListener to listen to property changes
153 * occuring in the Menu
155 * @param c the menu to listen to
157 * @return The PropertyChangeListener
159 protected PropertyChangeListener createPropertyChangeListener(JComponent c)
161 return new PropertyChangeHandler();
165 * This method creates a new BasicMenuUI.
167 * @param c The JComponent to create a UI for.
169 * @return A new BasicMenuUI.
171 public static ComponentUI createUI(JComponent c)
173 return new BasicMenuUI();
177 * Get the component's maximum size.
179 * @param c The JComponent for which to get maximum size
181 * @return The maximum size of the component
183 public Dimension getMaximumSize(JComponent c)
185 return c.getPreferredSize();
189 * Returns the prefix for entries in the {@link UIDefaults} table.
191 * @return "Menu"
193 protected String getPropertyPrefix()
195 return "Menu";
199 * Initializes any default properties that this UI has from the defaults for
200 * the Basic look and feel.
202 protected void installDefaults()
204 LookAndFeel.installBorder(menuItem, "Menu.border");
205 LookAndFeel.installColorsAndFont(menuItem, "Menu.background",
206 "Menu.foreground", "Menu.font");
207 menuItem.setMargin(UIManager.getInsets("Menu.margin"));
208 acceleratorFont = UIManager.getFont("Menu.acceleratorFont");
209 acceleratorForeground = UIManager.getColor("Menu.acceleratorForeground");
210 acceleratorSelectionForeground = UIManager.getColor("Menu.acceleratorSelectionForeground");
211 selectionBackground = UIManager.getColor("Menu.selectionBackground");
212 selectionForeground = UIManager.getColor("Menu.selectionForeground");
213 arrowIcon = UIManager.getIcon("Menu.arrowIcon");
214 oldBorderPainted = UIManager.getBoolean("Menu.borderPainted");
218 * Installs any keyboard actions. The list of keys that need to be bound are
219 * listed in Basic look and feel's defaults.
222 protected void installKeyboardActions()
223 throws NotImplementedException
225 // FIXME: Need to implement
229 * Creates and registers all the listeners for this UI delegate.
231 protected void installListeners()
233 ((JMenu) menuItem).addMouseListener(mouseInputListener);
234 ((JMenu) menuItem).addMouseMotionListener(mouseInputListener);
235 ((JMenu) menuItem).addMenuListener(menuListener);
236 ((JMenu) menuItem).addMenuDragMouseListener(menuDragMouseListener);
239 protected void setupPostTimer(JMenu menu)
241 // TODO: Implement this properly.
245 * This method uninstalls the defaults and sets any objects created during
246 * install to null
248 protected void uninstallDefaults()
250 menuItem.setBackground(null);
251 menuItem.setBorder(null);
252 menuItem.setFont(null);
253 menuItem.setForeground(null);
254 menuItem.setMargin(null);
255 acceleratorFont = null;
256 acceleratorForeground = null;
257 acceleratorSelectionForeground = null;
258 selectionBackground = null;
259 selectionForeground = null;
260 arrowIcon = null;
264 * Uninstalls any keyboard actions. The list of keys used are listed in
265 * Basic look and feel's defaults.
267 protected void uninstallKeyboardActions()
268 throws NotImplementedException
270 // FIXME: Need to implement
274 * Unregisters all the listeners that this UI delegate was using. In
275 * addition, it will also null any listeners that it was using.
277 protected void uninstallListeners()
279 ((JMenu) menuItem).removeMouseListener(mouseInputListener);
280 ((JMenu) menuItem).removeMenuListener(menuListener);
281 ((JMenu) menuItem).removePropertyChangeListener(propertyChangeListener);
285 * This class is used by menus to handle mouse events occuring in the
286 * menu.
288 protected class MouseInputHandler implements MouseInputListener
290 public void mouseClicked(MouseEvent e)
292 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
293 manager.processMouseEvent(e);
296 public void mouseDragged(MouseEvent e)
298 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
299 manager.processMouseEvent(e);
302 private boolean popupVisible()
304 JMenuBar mb = (JMenuBar) ((JMenu) menuItem).getParent();
305 // check if mb.isSelected because if no menus are selected
306 // we don't have to look through the list for popup menus
307 if (!mb.isSelected())
308 return false;
309 for (int i = 0; i < mb.getMenuCount(); i++)
311 JMenu m = mb.getMenu(i);
312 if (m != null && m.isPopupMenuVisible())
313 return true;
315 return false;
318 public void mouseEntered(MouseEvent e)
320 /* When mouse enters menu item, it should be considered selected
322 if (i) if this menu is a submenu in some other menu
323 (ii) or if this menu is in a menu bar and some other menu in a
324 menu bar was just selected and has its popup menu visible.
325 (If nothing was selected, menu should be pressed before
326 it will be selected)
328 JMenu menu = (JMenu) menuItem;
330 // NOTE: the following if used to require !menu.isArmed but I could find
331 // no reason for this and it was preventing some JDK-compatible behaviour.
332 // Specifically, if a menu is selected but its popup menu not visible,
333 // and then another menu is selected whose popup menu IS visible, when
334 // the mouse is moved over the first menu, its popup menu should become
335 // visible.
337 if (! menu.isTopLevelMenu() || popupVisible())
339 // set new selection and forward this event to MenuSelectionManager
340 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
341 manager.setSelectedPath(getPath());
342 manager.processMouseEvent(e);
346 public void mouseExited(MouseEvent e)
348 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
349 manager.processMouseEvent(e);
352 public void mouseMoved(MouseEvent e)
354 // TODO: What should be done here, if anything?
357 public void mousePressed(MouseEvent e)
359 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
360 JMenu menu = (JMenu) menuItem;
361 manager.processMouseEvent(e);
363 // Menu should be displayed when the menu is pressed only if
364 // it is top-level menu
365 if (menu.isTopLevelMenu())
367 if (menu.getPopupMenu().isVisible())
368 // If menu is visible and menu button was pressed..
369 // then need to cancel the menu
370 manager.clearSelectedPath();
371 else
373 // Display the menu
374 int x = 0;
375 int y = menu.getHeight();
377 manager.setSelectedPath(getPath());
379 JMenuBar mb = (JMenuBar) menu.getParent();
381 // set selectedIndex of the selectionModel of a menuBar
382 mb.getSelectionModel().setSelectedIndex(mb.getComponentIndex(menu));
387 public void mouseReleased(MouseEvent e)
389 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
390 manager.processMouseEvent(e);
395 * This class handles MenuEvents fired by the JMenu
397 private class MenuHandler implements MenuListener
400 * This method is called when menu is cancelled. The menu is cancelled
401 * when its popup menu is closed without selection. It clears selected index
402 * in the selectionModel of the menu parent.
404 * @param e The MenuEvent.
406 public void menuCanceled(MenuEvent e)
408 menuDeselected(e);
412 * This method is called when menu is deselected. It clears selected index
413 * in the selectionModel of the menu parent.
415 * @param e The MenuEvent.
417 public void menuDeselected(MenuEvent e)
419 JMenu menu = (JMenu) menuItem;
420 if (menu.getParent() != null)
422 if (menu.isTopLevelMenu())
423 ((JMenuBar) menu.getParent()).getSelectionModel().clearSelection();
424 else
425 ((JPopupMenu) menu.getParent()).getSelectionModel().clearSelection();
430 * This method is called when menu is selected. It sets selected index
431 * in the selectionModel of the menu parent.
433 * @param e The MenuEvent.
435 public void menuSelected(MenuEvent e)
437 JMenu menu = (JMenu) menuItem;
438 if (menu.isTopLevelMenu())
439 ((JMenuBar) menu.getParent()).setSelected(menu);
440 else
441 ((JPopupMenu) menu.getParent()).setSelected(menu);
446 * Obsolete as of JDK1.4.
448 public class ChangeHandler implements ChangeListener
451 * Not used.
453 public boolean isSelected;
456 * Not used.
458 public JMenu menu;
461 * Not used.
463 public BasicMenuUI ui;
466 * Not used.
468 public Component wasFocused;
471 * Not used.
473 public ChangeHandler(JMenu m, BasicMenuUI ui)
475 // Not used.
479 * Not used.
481 public void stateChanged(ChangeEvent e)
483 // Not used.
488 * This class handles mouse dragged events occuring in the menu.
490 private class MenuDragMouseHandler implements MenuDragMouseListener
493 * This method is invoked when mouse is dragged over the menu item.
495 * @param e The MenuDragMouseEvent
497 public void menuDragMouseDragged(MenuDragMouseEvent e)
499 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
500 manager.setSelectedPath(e.getPath());
504 * This method is invoked when mouse enters the menu item while it is
505 * being dragged.
507 * @param e The MenuDragMouseEvent
509 public void menuDragMouseEntered(MenuDragMouseEvent e)
511 MenuSelectionManager manager = MenuSelectionManager.defaultManager();
512 manager.setSelectedPath(e.getPath());
516 * This method is invoked when mouse exits the menu item while
517 * it is being dragged
519 * @param e The MenuDragMouseEvent
521 public void menuDragMouseExited(MenuDragMouseEvent e)
523 // TODO: What should be done here, if anything?
527 * This method is invoked when mouse was dragged and released
528 * inside the menu item.
530 * @param e The MenuDragMouseEvent
532 public void menuDragMouseReleased(MenuDragMouseEvent e)
534 // TODO: What should be done here, if anything?
539 * This class handles key events occuring when menu item is visible on the
540 * screen.
542 private class MenuKeyHandler implements MenuKeyListener
545 * This method is invoked when key has been pressed
547 * @param e A {@link MenuKeyEvent}.
549 public void menuKeyPressed(MenuKeyEvent e)
551 // TODO: What should be done here, if anything?
555 * This method is invoked when key has been pressed
557 * @param e A {@link MenuKeyEvent}.
559 public void menuKeyReleased(MenuKeyEvent e)
561 // TODO: What should be done here, if anything?
565 * This method is invoked when key has been typed
566 * It handles the mnemonic key for the menu item.
568 * @param e A {@link MenuKeyEvent}.
570 public void menuKeyTyped(MenuKeyEvent e)
572 // TODO: What should be done here, if anything?