PageRenderTime 29ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/libjava/classpath/javax/swing/MenuSelectionManager.java

https://bitbucket.org/pizzafactory/pf-gcc
Java | 440 lines | 220 code | 50 blank | 170 comment | 51 complexity | 793098a4e829e4fbcddd711e183ef955 MD5 | raw file
  1. /* MenuSelectionManager.java --
  2. Copyright (C) 2002, 2004 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.swing;
  32. import java.awt.Component;
  33. import java.awt.Dimension;
  34. import java.awt.Point;
  35. import java.awt.event.KeyEvent;
  36. import java.awt.event.MouseEvent;
  37. import java.util.ArrayList;
  38. import java.util.Vector;
  39. import javax.swing.event.ChangeEvent;
  40. import javax.swing.event.ChangeListener;
  41. import javax.swing.event.EventListenerList;
  42. /**
  43. * This class manages current menu selectection. It provides
  44. * methods to clear and set current selected menu path.
  45. * It also fires StateChange event to its registered
  46. * listeners whenever selected path of the current menu hierarchy
  47. * changes.
  48. *
  49. */
  50. public class MenuSelectionManager
  51. {
  52. /** ChangeEvent fired when selected path changes*/
  53. protected ChangeEvent changeEvent = new ChangeEvent(this);
  54. /** List of listeners for this MenuSelectionManager */
  55. protected EventListenerList listenerList = new EventListenerList();
  56. /** Default manager for the current menu hierarchy*/
  57. private static final MenuSelectionManager manager = new MenuSelectionManager();
  58. /** Path to the currently selected menu */
  59. private Vector selectedPath = new Vector();
  60. /**
  61. * Fires StateChange event to registered listeners
  62. */
  63. protected void fireStateChanged()
  64. {
  65. ChangeListener[] listeners = getChangeListeners();
  66. for (int i = 0; i < listeners.length; i++)
  67. listeners[i].stateChanged(changeEvent);
  68. }
  69. /**
  70. * Adds ChangeListener to this MenuSelectionManager
  71. *
  72. * @param listener ChangeListener to add
  73. */
  74. public void addChangeListener(ChangeListener listener)
  75. {
  76. listenerList.add(ChangeListener.class, listener);
  77. }
  78. /**
  79. * Removes ChangeListener from the list of registered listeners
  80. * for this MenuSelectionManager.
  81. *
  82. * @param listener ChangeListner to remove
  83. */
  84. public void removeChangeListener(ChangeListener listener)
  85. {
  86. listenerList.remove(ChangeListener.class, listener);
  87. }
  88. /**
  89. * Returns list of registered listeners with MenuSelectionManager
  90. *
  91. * @since 1.4
  92. */
  93. public ChangeListener[] getChangeListeners()
  94. {
  95. return (ChangeListener[]) listenerList.getListeners(ChangeListener.class);
  96. }
  97. /**
  98. * Unselects all the menu elements on the selection path
  99. */
  100. public void clearSelectedPath()
  101. {
  102. // Send events from the bottom most item in the menu - hierarchy to the
  103. // top most
  104. for (int i = selectedPath.size() - 1; i >= 0; i--)
  105. ((MenuElement) selectedPath.get(i)).menuSelectionChanged(false);
  106. // clear selected path
  107. selectedPath.clear();
  108. // notify all listeners that the selected path was changed
  109. fireStateChanged();
  110. }
  111. /**
  112. * This method returns menu element on the selected path that contains
  113. * given source point. If no menu element on the selected path contains this
  114. * point, then null is returned.
  115. *
  116. * @param source Component relative to which sourcePoint is given
  117. * @param sourcePoint point for which we want to find menu element that contains it
  118. *
  119. * @return Returns menu element that contains given source point and belongs
  120. * to the currently selected path. Null is return if no such menu element found.
  121. */
  122. public Component componentForPoint(Component source, Point sourcePoint)
  123. {
  124. // Convert sourcePoint to screen coordinates.
  125. Point sourcePointOnScreen = sourcePoint;
  126. if (source.isShowing())
  127. SwingUtilities.convertPointToScreen(sourcePointOnScreen, source);
  128. Point compPointOnScreen;
  129. Component resultComp = null;
  130. // For each menu element on the selected path, express its location
  131. // in terms of screen coordinates and check if there is any
  132. // menu element on the selected path that contains given source point.
  133. for (int i = 0; i < selectedPath.size(); i++)
  134. {
  135. Component comp = ((Component) selectedPath.get(i));
  136. Dimension size = comp.getSize();
  137. // convert location of this menu item to screen coordinates
  138. compPointOnScreen = comp.getLocationOnScreen();
  139. if (compPointOnScreen.x <= sourcePointOnScreen.x
  140. && sourcePointOnScreen.x < compPointOnScreen.x + size.width
  141. && compPointOnScreen.y <= sourcePointOnScreen.y
  142. && sourcePointOnScreen.y < compPointOnScreen.y + size.height)
  143. {
  144. Point p = sourcePointOnScreen;
  145. if (comp.isShowing())
  146. SwingUtilities.convertPointFromScreen(p, comp);
  147. resultComp = SwingUtilities.getDeepestComponentAt(comp, p.x, p.y);
  148. break;
  149. }
  150. }
  151. return resultComp;
  152. }
  153. /**
  154. * Returns shared instance of MenuSelection Manager
  155. *
  156. * @return default Manager
  157. */
  158. public static MenuSelectionManager defaultManager()
  159. {
  160. return manager;
  161. }
  162. /**
  163. * Returns path representing current menu selection
  164. *
  165. * @return Current selection path
  166. */
  167. public MenuElement[] getSelectedPath()
  168. {
  169. MenuElement[] path = new MenuElement[selectedPath.size()];
  170. for (int i = 0; i < path.length; i++)
  171. path[i] = (MenuElement) selectedPath.get(i);
  172. return path;
  173. }
  174. /**
  175. * Returns true if specified component is part of current menu
  176. * heirarchy and false otherwise
  177. *
  178. * @param c Component for which to check
  179. * @return True if specified component is part of current menu
  180. */
  181. public boolean isComponentPartOfCurrentMenu(Component c)
  182. {
  183. MenuElement[] subElements;
  184. boolean ret = false;
  185. for (int i = 0; i < selectedPath.size(); i++)
  186. {
  187. // Check first element.
  188. MenuElement first = (MenuElement) selectedPath.get(i);
  189. if (SwingUtilities.isDescendingFrom(c, first.getComponent()))
  190. {
  191. ret = true;
  192. break;
  193. }
  194. else
  195. {
  196. // Check sub elements.
  197. subElements = first.getSubElements();
  198. for (int j = 0; j < subElements.length; j++)
  199. {
  200. MenuElement me = subElements[j];
  201. if (me != null
  202. && (SwingUtilities.isDescendingFrom(c, me.getComponent())))
  203. {
  204. ret = true;
  205. break;
  206. }
  207. }
  208. }
  209. }
  210. return ret;
  211. }
  212. /**
  213. * Processes key events on behalf of the MenuElements. MenuElement
  214. * instances should always forward their key events to this method and
  215. * get their {@link MenuElement#processKeyEvent(KeyEvent, MenuElement[],
  216. * MenuSelectionManager)} eventually called back.
  217. *
  218. * @param e the key event
  219. */
  220. public void processKeyEvent(KeyEvent e)
  221. {
  222. MenuElement[] selection = (MenuElement[])
  223. selectedPath.toArray(new MenuElement[selectedPath.size()]);
  224. if (selection.length == 0)
  225. return;
  226. MenuElement[] path;
  227. for (int index = selection.length - 1; index >= 0; index--)
  228. {
  229. MenuElement el = selection[index];
  230. // This method's main purpose is to forward key events to the
  231. // relevant menu items, so that they can act in response to their
  232. // mnemonics beeing typed. So we also need to forward the key event
  233. // to all the subelements of the currently selected menu elements
  234. // in the path.
  235. MenuElement[] subEls = el.getSubElements();
  236. path = null;
  237. for (int subIndex = 0; subIndex < subEls.length; subIndex++)
  238. {
  239. MenuElement sub = subEls[subIndex];
  240. // Skip elements that are not showing or not enabled.
  241. if (sub == null || ! sub.getComponent().isShowing()
  242. || ! sub.getComponent().isEnabled())
  243. {
  244. continue;
  245. }
  246. if (path == null)
  247. {
  248. path = new MenuElement[index + 2];
  249. System.arraycopy(selection, 0, path, 0, index + 1);
  250. }
  251. path[index + 1] = sub;
  252. sub.processKeyEvent(e, path, this);
  253. if (e.isConsumed())
  254. break;
  255. }
  256. if (e.isConsumed())
  257. break;
  258. }
  259. // Dispatch to first element in selection if it hasn't been consumed.
  260. if (! e.isConsumed())
  261. {
  262. path = new MenuElement[1];
  263. path[0] = selection[0];
  264. path[0].processKeyEvent(e, path, this);
  265. }
  266. }
  267. /**
  268. * Forwards given mouse event to all of the source subcomponents.
  269. *
  270. * @param event Mouse event
  271. */
  272. public void processMouseEvent(MouseEvent event)
  273. {
  274. Component source = ((Component) event.getSource());
  275. // In the case of drag event, event.getSource() returns component
  276. // where drag event originated. However menu element processing this
  277. // event should be the one over which mouse is currently located,
  278. // which is not necessary the source of the drag event.
  279. Component mouseOverMenuComp;
  280. // find over which menu element the mouse is currently located
  281. if (event.getID() == MouseEvent.MOUSE_DRAGGED
  282. || event.getID() == MouseEvent.MOUSE_RELEASED)
  283. mouseOverMenuComp = componentForPoint(source, event.getPoint());
  284. else
  285. mouseOverMenuComp = source;
  286. // Process this event only if mouse is located over some menu element
  287. if (mouseOverMenuComp != null && (mouseOverMenuComp instanceof MenuElement))
  288. {
  289. MenuElement[] path = getPath(mouseOverMenuComp);
  290. ((MenuElement) mouseOverMenuComp).processMouseEvent(event, path,
  291. manager);
  292. // FIXME: Java specification says that mouse events should be
  293. // forwarded to subcomponents. The code below does it, but
  294. // menu's work fine without it. This code is commented for now.
  295. /*
  296. MenuElement[] subComponents = ((MenuElement) mouseOverMenuComp)
  297. .getSubElements();
  298. for (int i = 0; i < subComponents.length; i++)
  299. {
  300. subComponents[i].processMouseEvent(event, path, manager);
  301. }
  302. */
  303. }
  304. else
  305. {
  306. if (event.getID() == MouseEvent.MOUSE_RELEASED)
  307. clearSelectedPath();
  308. }
  309. }
  310. /**
  311. * Sets menu selection to the specified path
  312. *
  313. * @param path new selection path
  314. */
  315. public void setSelectedPath(MenuElement[] path)
  316. {
  317. if (path == null)
  318. {
  319. clearSelectedPath();
  320. return;
  321. }
  322. int minSize = path.length; // size of the smaller path.
  323. int currentSize = selectedPath.size();
  324. int firstDiff = 0;
  325. // Search first item that is different in the current and new path.
  326. for (int i = 0; i < minSize; i++)
  327. {
  328. if (i < currentSize && (MenuElement) selectedPath.get(i) == path[i])
  329. firstDiff++;
  330. else
  331. break;
  332. }
  333. // Remove items from selection and send notification.
  334. for (int i = currentSize - 1; i >= firstDiff; i--)
  335. {
  336. MenuElement el = (MenuElement) selectedPath.get(i);
  337. selectedPath.remove(i);
  338. el.menuSelectionChanged(false);
  339. }
  340. // Add new items to selection and send notification.
  341. for (int i = firstDiff; i < minSize; i++)
  342. {
  343. if (path[i] != null)
  344. {
  345. selectedPath.add(path[i]);
  346. path[i].menuSelectionChanged(true);
  347. }
  348. }
  349. fireStateChanged();
  350. }
  351. /**
  352. * Returns path to the specified component
  353. *
  354. * @param c component for which to find path for
  355. *
  356. * @return path to the specified component
  357. */
  358. private MenuElement[] getPath(Component c)
  359. {
  360. // FIXME: There is the same method in BasicMenuItemUI. However I
  361. // cannot use it here instead of this method, since I cannot assume that
  362. // all the menu elements on the selected path are JMenuItem or JMenu.
  363. // For now I've just duplicated it here. Please
  364. // fix me or delete me if another better approach will be found, and
  365. // this method will not be necessary.
  366. ArrayList path = new ArrayList();
  367. // if given component is JMenu, we also need to include
  368. // it's popup menu in the path
  369. if (c instanceof JMenu)
  370. path.add(((JMenu) c).getPopupMenu());
  371. while (c instanceof MenuElement)
  372. {
  373. path.add(0, (MenuElement) c);
  374. if (c instanceof JPopupMenu)
  375. c = ((JPopupMenu) c).getInvoker();
  376. else
  377. c = c.getParent();
  378. }
  379. MenuElement[] pathArray = new MenuElement[path.size()];
  380. path.toArray(pathArray);
  381. return pathArray;
  382. }
  383. }