Source for javax.swing.MenuSelectionManager

   1: /* MenuSelectionManager.java --
   2:    Copyright (C) 2002, 2004 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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.
  20: 
  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.
  25: 
  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. */
  37: 
  38: 
  39: package javax.swing;
  40: 
  41: import java.awt.Component;
  42: import java.awt.Dimension;
  43: import java.awt.Point;
  44: import java.awt.event.KeyEvent;
  45: import java.awt.event.MouseEvent;
  46: import java.util.ArrayList;
  47: import java.util.Vector;
  48: 
  49: import javax.swing.event.ChangeEvent;
  50: import javax.swing.event.ChangeListener;
  51: import javax.swing.event.EventListenerList;
  52: 
  53: /**
  54:  * This class manages current menu selectection. It provides
  55:  * methods to clear and set current selected menu path.
  56:  * It also fires StateChange event to its registered
  57:  * listeners whenever selected path of the current menu hierarchy
  58:  * changes.
  59:  *
  60:  */
  61: public class MenuSelectionManager
  62: {
  63:   /** ChangeEvent fired when selected path changes*/
  64:   protected ChangeEvent changeEvent = new ChangeEvent(this);
  65: 
  66:   /** List of listeners for this MenuSelectionManager */
  67:   protected EventListenerList listenerList = new EventListenerList();
  68: 
  69:   /** Default manager for the current menu hierarchy*/
  70:   private static final MenuSelectionManager manager = new MenuSelectionManager();
  71: 
  72:   /** Path to the currently selected menu */
  73:   private Vector selectedPath = new Vector();
  74: 
  75:   /**
  76:    * Fires StateChange event to registered listeners
  77:    */
  78:   protected void fireStateChanged()
  79:   {
  80:     ChangeListener[] listeners = getChangeListeners();
  81: 
  82:     for (int i = 0; i < listeners.length; i++)
  83:       listeners[i].stateChanged(changeEvent);
  84:   }
  85: 
  86:   /**
  87:    * Adds ChangeListener to this MenuSelectionManager
  88:    *
  89:    * @param listener ChangeListener to add
  90:    */
  91:   public void addChangeListener(ChangeListener listener)
  92:   {
  93:     listenerList.add(ChangeListener.class, listener);
  94:   }
  95: 
  96:   /**
  97:    * Removes ChangeListener from the list of registered listeners
  98:    * for this MenuSelectionManager.
  99:    *
 100:    * @param listener ChangeListner to remove
 101:    */
 102:   public void removeChangeListener(ChangeListener listener)
 103:   {
 104:     listenerList.remove(ChangeListener.class, listener);
 105:   }
 106: 
 107:   /**
 108:    * Returns list of registered listeners with MenuSelectionManager
 109:    *
 110:    * @since 1.4
 111:    */
 112:   public ChangeListener[] getChangeListeners()
 113:   {
 114:     return (ChangeListener[]) listenerList.getListeners(ChangeListener.class);
 115:   }
 116: 
 117:   /**
 118:    * Unselects all the menu elements on the selection path
 119:    */
 120:   public void clearSelectedPath()
 121:   {
 122:     // Send events from the bottom most item in the menu - hierarchy to the
 123:     // top most
 124:     for (int i = selectedPath.size() - 1; i >= 0; i--)
 125:       ((MenuElement) selectedPath.get(i)).menuSelectionChanged(false);
 126: 
 127:     // clear selected path
 128:     selectedPath.clear();
 129: 
 130:     // notify all listeners that the selected path was changed    
 131:     fireStateChanged();
 132:   }
 133: 
 134:   /**
 135:    * This method returns menu element on the selected path that contains
 136:    * given source point. If no menu element on the selected path contains this
 137:    * point, then null is returned.
 138:    *
 139:    * @param source Component relative to which sourcePoint is given
 140:    * @param sourcePoint point for which we want to find menu element that contains it
 141:    *
 142:    * @return Returns menu element that contains given source point and belongs
 143:    * to the currently selected path. Null is return if no such menu element found.
 144:    */
 145:   public Component componentForPoint(Component source, Point sourcePoint)
 146:   {
 147:     // Convert sourcePoint to screen coordinates.
 148:     Point sourcePointOnScreen = sourcePoint;
 149:     SwingUtilities.convertPointToScreen(sourcePointOnScreen, source);
 150: 
 151:     Point compPointOnScreen;
 152:     Component resultComp = null;
 153: 
 154:     // For each menu element on the selected path, express its location 
 155:     // in terms of screen coordinates and check if there is any 
 156:     // menu element on the selected path that contains given source point.
 157:     for (int i = 0; i < selectedPath.size(); i++)
 158:       {
 159:     Component comp = ((Component) selectedPath.get(i));
 160:     Dimension size = comp.getSize();
 161: 
 162:     // convert location of this menu item to screen coordinates
 163:     compPointOnScreen = comp.getLocationOnScreen();
 164: 
 165:     if (compPointOnScreen.x <= sourcePointOnScreen.x
 166:         && sourcePointOnScreen.x < compPointOnScreen.x + size.width
 167:         && compPointOnScreen.y <= sourcePointOnScreen.y
 168:         && sourcePointOnScreen.y < compPointOnScreen.y + size.height)
 169:       {
 170:         Point p = sourcePointOnScreen;
 171:         SwingUtilities.convertPointFromScreen(p, comp);
 172:         resultComp = SwingUtilities.getDeepestComponentAt(comp, p.x, p.y);
 173:         break;
 174:       }
 175:       }
 176:     return resultComp;
 177:   }
 178: 
 179:   /**
 180:    * Returns shared instance of MenuSelection Manager
 181:    *
 182:    * @return default Manager
 183:    */
 184:   public static MenuSelectionManager defaultManager()
 185:   {
 186:     return manager;
 187:   }
 188: 
 189:   /**
 190:    * Returns path representing current menu selection
 191:    *
 192:    * @return Current selection path
 193:    */
 194:   public MenuElement[] getSelectedPath()
 195:   {
 196:     MenuElement[] path = new MenuElement[selectedPath.size()];
 197: 
 198:     for (int i = 0; i < path.length; i++)
 199:       path[i] = (MenuElement) selectedPath.get(i);
 200: 
 201:     return path;
 202:   }
 203: 
 204:   /**
 205:    * Returns true if specified component is part of current menu
 206:    * heirarchy and false otherwise
 207:    *
 208:    * @param c Component for which to check
 209:    * @return True if specified component is part of current menu
 210:    */
 211:   public boolean isComponentPartOfCurrentMenu(Component c)
 212:   {
 213:     MenuElement[] subElements;
 214:       for (int i = 0; i < selectedPath.size(); i++)
 215:       {
 216:          subElements = ((MenuElement) selectedPath.get(i)).getSubElements();
 217:          for (int j = 0; j < subElements.length; j++)
 218:          {
 219:             MenuElement me = subElements[j]; 
 220:             if (me != null && (me.getComponent()).equals(c))
 221:                return true;
 222:          }
 223:       }
 224: 
 225:       return false;
 226:   }
 227: 
 228:   /**
 229:    * DOCUMENT ME!
 230:    *
 231:    * @param e DOCUMENT ME!
 232:    */
 233:   public void processKeyEvent(KeyEvent e)
 234:   {
 235:     throw new UnsupportedOperationException("not implemented");
 236:   }
 237: 
 238:   /**
 239:    * Forwards given mouse event to all of the source subcomponents.
 240:    *
 241:    * @param event Mouse event
 242:    */
 243:   public void processMouseEvent(MouseEvent event)
 244:   {
 245:     Component source = ((Component) event.getSource());
 246: 
 247:     // In the case of drag event, event.getSource() returns component
 248:     // where drag event originated. However menu element processing this 
 249:     // event should be the one over which mouse is currently located, 
 250:     // which is not necessary the source of the drag event.     
 251:     Component mouseOverMenuComp;
 252: 
 253:     // find over which menu element the mouse is currently located
 254:     if (event.getID() == MouseEvent.MOUSE_DRAGGED
 255:         || event.getID() == MouseEvent.MOUSE_RELEASED)
 256:       mouseOverMenuComp = componentForPoint(source, event.getPoint());
 257:     else
 258:       mouseOverMenuComp = source;
 259: 
 260:     // Process this event only if mouse is located over some menu element
 261:     if (mouseOverMenuComp != null && (mouseOverMenuComp instanceof MenuElement))
 262:       {
 263:     MenuElement[] path = getPath(mouseOverMenuComp);
 264:     ((MenuElement) mouseOverMenuComp).processMouseEvent(event, path,
 265:                                                         manager);
 266: 
 267:     // FIXME: Java specification says that mouse events should be
 268:     // forwarded to subcomponents. The code below does it, but
 269:     // menu's work fine without it. This code is commented for now.      
 270: 
 271:     /*
 272:     MenuElement[] subComponents = ((MenuElement) mouseOverMenuComp)
 273:                                   .getSubElements();
 274: 
 275:     for (int i = 0; i < subComponents.length; i++)
 276:      {
 277:           subComponents[i].processMouseEvent(event, path, manager);
 278:      }
 279:     */
 280:       }
 281:     else
 282:       {
 283:     if (event.getID() == MouseEvent.MOUSE_RELEASED)
 284:       clearSelectedPath();
 285:       }
 286:   }
 287: 
 288:   /**
 289:    * Sets menu selection to the specified path
 290:    *
 291:    * @param path new selection path
 292:    */
 293:   public void setSelectedPath(MenuElement[] path)
 294:   {
 295:     if (path == null)
 296:       {
 297:     clearSelectedPath();
 298:     return;
 299:       }
 300: 
 301:     int i;
 302:     int minSize = path.length; // size of the smaller path. 
 303: 
 304:     if (path.length > selectedPath.size())
 305:       {
 306:     minSize = selectedPath.size();
 307: 
 308:     // if new selected path contains more elements then current
 309:     // selection then first add all elements at 
 310:     // the indexes > selectedPath.size 
 311:     for (i = selectedPath.size(); i < path.length; i++)
 312:       {
 313:         selectedPath.add(path[i]);
 314:         path[i].menuSelectionChanged(true);
 315:       }
 316:       }
 317: 
 318:     else if (path.length < selectedPath.size())
 319:       {
 320:     // if new selected path contains less elements then current 
 321:     // selection then first remove all elements from the selection
 322:     // at the indexes > path.length
 323:     for (i = selectedPath.size() - 1; i >= path.length; i--)
 324:       {
 325:         ((MenuElement) selectedPath.get(i)).menuSelectionChanged(false);
 326:         selectedPath.remove(i);
 327:       }
 328: 
 329:     minSize = path.length;
 330:       }
 331: 
 332:     // Now compare elements in new and current selection path at the 
 333:     // same location and adjust selection until 
 334:     // same menu elements will be encountered at the
 335:     // same index in both current and new selection path.
 336:     MenuElement oldSelectedItem;
 337: 
 338:     for (i = minSize - 1; i >= 0; i--)
 339:       {
 340:     oldSelectedItem = (MenuElement) selectedPath.get(i);
 341: 
 342:     if (path[i].equals(oldSelectedItem))
 343:       break;
 344: 
 345:     oldSelectedItem.menuSelectionChanged(false);
 346:     path[i].menuSelectionChanged(true);
 347:     selectedPath.setElementAt(path[i], i);
 348:       }
 349: 
 350:     fireStateChanged();
 351:   }
 352: 
 353:   /**
 354:    * Returns path to the specified component
 355:    *
 356:    * @param c component for which to find path for
 357:    *
 358:    * @return path to the specified component
 359:    */
 360:   private MenuElement[] getPath(Component c)
 361:   {
 362:     // FIXME: There is the same method in BasicMenuItemUI. However I
 363:     // cannot use it here instead of this method, since I cannot assume that 
 364:     // all the menu elements on the selected path are JMenuItem or JMenu.
 365:     // For now I've just duplicated it here. Please 
 366:     // fix me or delete me if another better approach will be found, and 
 367:     // this method will not be necessary.
 368:     ArrayList path = new ArrayList();
 369: 
 370:     // if given component is JMenu, we also need to include 
 371:     // it's popup menu in the path 
 372:     if (c instanceof JMenu)
 373:       path.add(((JMenu) c).getPopupMenu());
 374:     while (c instanceof MenuElement)
 375:       {
 376:     path.add(0, (MenuElement) c);
 377: 
 378:     if (c instanceof JPopupMenu)
 379:       c = ((JPopupMenu) c).getInvoker();
 380:     else
 381:       c = c.getParent();
 382:       }
 383: 
 384:     MenuElement[] pathArray = new MenuElement[path.size()];
 385:     path.toArray(pathArray);
 386:     return pathArray;
 387:   }
 388: }