1. /*
  2. * @(#)MenuSelectionManager.java 1.23 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing;
  11. import java.awt.*;
  12. import java.util.*;
  13. import java.awt.event.*;
  14. import javax.swing.event.*;
  15. /**
  16. * A MenuSelectionManager owns the selection in menu hierarchy.
  17. *
  18. * @version 1.23 02/02/00
  19. * @author Arnaud Weber
  20. */
  21. public class MenuSelectionManager {
  22. private static final MenuSelectionManager instance =
  23. new MenuSelectionManager();
  24. private Vector selection = new Vector();
  25. /* diagnostic aids -- should be false for production builds. */
  26. private static final boolean TRACE = false; // trace creates and disposes
  27. private static final boolean VERBOSE = false; // show reuse hits/misses
  28. private static final boolean DEBUG = false; // show bad params, misc.
  29. /**
  30. * Returns the default menu selection manager.
  31. *
  32. * @return a MenuSelectionManager object
  33. */
  34. public static MenuSelectionManager defaultManager() {
  35. return instance;
  36. }
  37. /**
  38. * Only one ChangeEvent is needed per button model instance since the
  39. * event's only state is the source property. The source of events
  40. * generated is always "this".
  41. */
  42. protected transient ChangeEvent changeEvent = null;
  43. protected EventListenerList listenerList = new EventListenerList();
  44. /**
  45. * Change the selection in the menu hierarchy.
  46. *
  47. * @param path an array of MenuElement objects specifying the selected path
  48. */
  49. public void setSelectedPath(MenuElement[] path) {
  50. int i,c;
  51. int currentSelectionCount = selection.size();
  52. int firstDifference = 0;
  53. if(path == null) {
  54. path = new MenuElement[0];
  55. }
  56. if (DEBUG) {
  57. System.out.print("Previous: "); printMenuElementArray(getSelectedPath());
  58. System.out.print("New: "); printMenuElementArray(path);
  59. }
  60. for(i=0,c=path.length;i<c;i++) {
  61. if(i < currentSelectionCount && (MenuElement)selection.elementAt(i) == path[i])
  62. firstDifference++;
  63. else
  64. break;
  65. }
  66. for(i=currentSelectionCount - 1 ; i >= firstDifference ; i--) {
  67. ((MenuElement)selection.elementAt(i)).menuSelectionChanged(false);
  68. selection.removeElementAt(i);
  69. }
  70. for(i = firstDifference, c = path.length ; i < c ; i++) {
  71. if (path[i] != null) {
  72. path[i].menuSelectionChanged(true);
  73. selection.addElement(path[i]);
  74. }
  75. }
  76. fireStateChanged();
  77. }
  78. /**
  79. * Returns the path to the currently selected menu item
  80. *
  81. * @return an array of MenuElement objects representing the selected path
  82. */
  83. public MenuElement[] getSelectedPath() {
  84. MenuElement res[] = new MenuElement[selection.size()];
  85. int i,c;
  86. for(i=0,c=selection.size();i<c;i++)
  87. res[i] = (MenuElement) selection.elementAt(i);
  88. return res;
  89. }
  90. /**
  91. * Tell the menu selection to close and unselect all the menu components. Call this method
  92. * when a choice has been made
  93. */
  94. public void clearSelectedPath() {
  95. setSelectedPath(null);
  96. }
  97. /**
  98. * Adds a ChangeListener to the button.
  99. *
  100. * @param l the listener to add
  101. */
  102. public void addChangeListener(ChangeListener l) {
  103. listenerList.add(ChangeListener.class, l);
  104. }
  105. /**
  106. * Removes a ChangeListener from the button.
  107. *
  108. * @param l the listener to remove
  109. */
  110. public void removeChangeListener(ChangeListener l) {
  111. listenerList.remove(ChangeListener.class, l);
  112. }
  113. /*
  114. * Notify all listeners that have registered interest for
  115. * notification on this event type. The event instance
  116. * is lazily created using the parameters passed into
  117. * the fire method.
  118. *
  119. * @see EventListenerList
  120. */
  121. protected void fireStateChanged() {
  122. // Guaranteed to return a non-null array
  123. Object[] listeners = listenerList.getListenerList();
  124. // Process the listeners last to first, notifying
  125. // those that are interested in this event
  126. for (int i = listeners.length-2; i>=0; i-=2) {
  127. if (listeners[i]==ChangeListener.class) {
  128. // Lazily create the event:
  129. if (changeEvent == null)
  130. changeEvent = new ChangeEvent(this);
  131. ((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
  132. }
  133. }
  134. }
  135. /**
  136. * When a MenuElement receives an event from a MouseListener, it should never process the event
  137. * directly. Instead all MenuElements should call this method with the event.
  138. *
  139. * @param event a MouseEvent object
  140. */
  141. public void processMouseEvent(MouseEvent event) {
  142. int screenX,screenY;
  143. Point p;
  144. int i,c,j,d;
  145. Component mc;
  146. Rectangle r2;
  147. int cWidth,cHeight;
  148. MenuElement menuElement;
  149. MenuElement subElements[];
  150. MenuElement path[];
  151. Vector tmp;
  152. int selectionSize;
  153. p = event.getPoint();
  154. Component source = (Component)event.getSource();
  155. if (!source.isShowing()) {
  156. // This can happen if a mouseReleased removes the
  157. // containing component -- bug 4146684
  158. return;
  159. }
  160. int type = event.getID();
  161. int modifiers = event.getModifiers();
  162. // 4188027: drag enter/exit added in JDK 1.1.7A, JDK1.2
  163. if ((type==MouseEvent.MOUSE_ENTERED||
  164. type==MouseEvent.MOUSE_EXITED)
  165. && ((modifiers & (InputEvent.BUTTON1_MASK |
  166. InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK)) !=0 )) {
  167. return;
  168. }
  169. SwingUtilities.convertPointToScreen(p,source);
  170. screenX = p.x;
  171. screenY = p.y;
  172. tmp = (Vector)selection.clone();
  173. selectionSize = tmp.size();
  174. boolean success = false;
  175. for (i=selectionSize - 1;i >= 0 && success == false; i--) {
  176. menuElement = (MenuElement) tmp.elementAt(i);
  177. subElements = menuElement.getSubElements();
  178. path = null;
  179. for (j = 0, d = subElements.length;j < d && success == false; j++) {
  180. if (subElements[j] == null)
  181. continue;
  182. mc = subElements[j].getComponent();
  183. if(!mc.isShowing())
  184. continue;
  185. if(mc instanceof JComponent) {
  186. cWidth = ((JComponent)mc).getWidth();
  187. cHeight = ((JComponent)mc).getHeight();
  188. } else {
  189. r2 = mc.getBounds();
  190. cWidth = r2.width;
  191. cHeight = r2.height;
  192. }
  193. p.x = screenX;
  194. p.y = screenY;
  195. SwingUtilities.convertPointFromScreen(p,mc);
  196. /** Send the event to visible menu element if menu element currently in
  197. * the selected path or contains the event location
  198. */
  199. if(
  200. (p.x >= 0 && p.x < cWidth && p.y >= 0 && p.y < cHeight)) {
  201. int k;
  202. if(path == null) {
  203. path = new MenuElement[i+2];
  204. for(k=0;k<=i;k++)
  205. path[k] = (MenuElement)tmp.elementAt(k);
  206. }
  207. path[i+1] = subElements[j];
  208. MenuElement currentSelection[] = getSelectedPath();
  209. // Enter/exit detection -- needs tuning...
  210. if (currentSelection[currentSelection.length-1] !=
  211. path[i+1] &&
  212. currentSelection[currentSelection.length-2] !=
  213. path[i+1]) {
  214. Component oldMC = currentSelection[currentSelection.length-1].getComponent();
  215. MouseEvent exitEvent = new MouseEvent(oldMC, MouseEvent.MOUSE_EXITED,
  216. event.getWhen(),
  217. event.getModifiers(), p.x, p.y,
  218. event.getClickCount(),
  219. event.isPopupTrigger());
  220. currentSelection[currentSelection.length-1].
  221. processMouseEvent(exitEvent, path, this);
  222. MouseEvent enterEvent = new MouseEvent(mc,
  223. MouseEvent.MOUSE_ENTERED,
  224. event.getWhen(),
  225. event.getModifiers(), p.x, p.y,
  226. event.getClickCount(),
  227. event.isPopupTrigger());
  228. subElements[j].processMouseEvent(enterEvent, path, this);
  229. }
  230. MouseEvent mouseEvent = new MouseEvent(mc, event.getID(),event. getWhen(),
  231. event.getModifiers(), p.x, p.y,
  232. event.getClickCount(),
  233. event.isPopupTrigger());
  234. subElements[j].processMouseEvent(mouseEvent, path, this);
  235. success = true;
  236. event.consume();
  237. }
  238. }
  239. }
  240. }
  241. private void printMenuElementArray(MenuElement path[]) {
  242. printMenuElementArray(path, false);
  243. }
  244. private void printMenuElementArray(MenuElement path[], boolean dumpStack) {
  245. System.out.println("Path is(");
  246. int i, j;
  247. for(i=0,j=path.length; i<j ;i++){
  248. for (int k=0; k<=i; k++)
  249. System.out.print(" ");
  250. MenuElement me = (MenuElement) path[i];
  251. if(me instanceof JMenuItem) {
  252. System.out.println(((JMenuItem)me).getText() + ", ");
  253. } else if (me instanceof JMenuBar) {
  254. System.out.println("JMenuBar, ");
  255. } else if(me instanceof JPopupMenu) {
  256. System.out.println("JPopupMenu, ");
  257. } else if (me == null) {
  258. System.out.println("NULL , ");
  259. } else {
  260. System.out.println("" + me + ", ");
  261. }
  262. }
  263. System.out.println(")");
  264. if (dumpStack == true)
  265. Thread.dumpStack();
  266. }
  267. /**
  268. * Returns the component in the currently selected path
  269. * which contains sourcePoint.
  270. *
  271. * @param source The component in whose coordinate space sourcePoint
  272. * is given
  273. * @param sourcePoint The point which is being tested
  274. * @return The component in the currently selected path which
  275. * contains sourcePoint (relative to the source component's
  276. * coordinate space. If sourcePoint is not inside a component
  277. * on the currently selected path, null is returned.
  278. */
  279. public Component componentForPoint(Component source, Point sourcePoint) {
  280. int screenX,screenY;
  281. Point p = sourcePoint;
  282. int i,c,j,d;
  283. Component mc;
  284. Rectangle r2;
  285. int cWidth,cHeight;
  286. MenuElement menuElement;
  287. MenuElement subElements[];
  288. Vector tmp;
  289. int selectionSize;
  290. SwingUtilities.convertPointToScreen(p,source);
  291. screenX = p.x;
  292. screenY = p.y;
  293. tmp = (Vector)selection.clone();
  294. selectionSize = tmp.size();
  295. for(i=selectionSize - 1 ; i >= 0 ; i--) {
  296. menuElement = (MenuElement) tmp.elementAt(i);
  297. subElements = menuElement.getSubElements();
  298. for(j = 0, d = subElements.length ; j < d ; j++) {
  299. if (subElements[j] == null)
  300. continue;
  301. mc = subElements[j].getComponent();
  302. if(!mc.isShowing())
  303. continue;
  304. if(mc instanceof JComponent) {
  305. cWidth = ((JComponent)mc).getWidth();
  306. cHeight = ((JComponent)mc).getHeight();
  307. } else {
  308. r2 = mc.getBounds();
  309. cWidth = r2.width;
  310. cHeight = r2.height;
  311. }
  312. p.x = screenX;
  313. p.y = screenY;
  314. SwingUtilities.convertPointFromScreen(p,mc);
  315. /** Return the deepest component on the selection
  316. * path in whose bounds the event's point occurs
  317. */
  318. if (p.x >= 0 && p.x < cWidth && p.y >= 0 && p.y < cHeight) {
  319. return mc;
  320. }
  321. }
  322. }
  323. return null;
  324. }
  325. /**
  326. * When a MenuElement receives an event from a KeyListener, it should never process the event
  327. * directly. Instead all MenuElements should call this method with the event.
  328. *
  329. * @param e a KeyEvent object
  330. */
  331. public void processKeyEvent(KeyEvent e) {
  332. Vector tmp;
  333. int selectionSize;
  334. int i,j,d;
  335. MenuElement menuElement;
  336. MenuElement subElements[];
  337. MenuElement path[];
  338. Component mc;
  339. if (DEBUG) {
  340. System.out.println("in MenuSelectionManager.processKeyEvent");
  341. }
  342. tmp = (Vector)selection.clone();
  343. selectionSize = tmp.size();
  344. for(i=selectionSize - 1 ; i >= 0 ; i--) {
  345. menuElement = (MenuElement) tmp.elementAt(i);
  346. subElements = menuElement.getSubElements();
  347. path = null;
  348. for(j = 0, d = subElements.length ; j < d ; j++) {
  349. if (subElements[j] == null)
  350. continue;
  351. mc = subElements[j].getComponent();
  352. if(!mc.isShowing())
  353. continue;
  354. if(path == null) {
  355. int k;
  356. path = new MenuElement[i+2];
  357. for(k=0;k<=i;k++)
  358. path[k] = (MenuElement)tmp.elementAt(k);
  359. }
  360. path[i+1] = subElements[j];
  361. subElements[j].processKeyEvent(e,path,this);
  362. if(e.isConsumed())
  363. return;
  364. }
  365. }
  366. }
  367. /**
  368. * Return true if c is part of the currently used menu
  369. */
  370. public boolean isComponentPartOfCurrentMenu(Component c) {
  371. if(selection.size() > 0) {
  372. MenuElement me = (MenuElement)selection.elementAt(0);
  373. return isComponentPartOfCurrentMenu(me,c);
  374. } else
  375. return false;
  376. }
  377. private boolean isComponentPartOfCurrentMenu(MenuElement root,Component c) {
  378. MenuElement children[];
  379. int i,d;
  380. if (root == null)
  381. return false;
  382. if(root.getComponent() == c)
  383. return true;
  384. else {
  385. children = root.getSubElements();
  386. for(i=0,d=children.length;i<d;i++) {
  387. if(isComponentPartOfCurrentMenu(children[i],c))
  388. return true;
  389. }
  390. }
  391. return false;
  392. }
  393. }