62 |
|
|
63 |
|
|
64 |
/** |
/** |
65 |
|
* <p> |
66 |
* This class represents a menu that can be added to a menu bar or |
* This class represents a menu that can be added to a menu bar or |
67 |
* to some other menu. When JMenu is selected it displays JPopupMenu |
* can be a submenu in some other menu. When JMenu is selected it |
68 |
* containing its menu items. |
* displays JPopupMenu containing its menu items. |
69 |
|
* </p> |
70 |
|
* |
71 |
|
* <p> |
72 |
|
* JMenu's fires MenuEvents when this menu's selection changes. If this menu |
73 |
|
* is selected, then fireMenuSelectedEvent() is invoked. In case when menu is |
74 |
|
* deselected or cancelled, then fireMenuDeselectedEvent() or |
75 |
|
* fireMenuCancelledEvent() is invoked, respectivelly. |
76 |
|
* </p> |
77 |
|
* |
78 |
*/ |
*/ |
79 |
public class JMenu extends JMenuItem implements Accessible, MenuElement |
public class JMenu extends JMenuItem implements Accessible, MenuElement |
80 |
{ |
{ |
86 |
/** A Popup menu associated with this menu, which pops up when menu is selected */ |
/** A Popup menu associated with this menu, which pops up when menu is selected */ |
87 |
private JPopupMenu popupMenu = new JPopupMenu(); |
private JPopupMenu popupMenu = new JPopupMenu(); |
88 |
|
|
89 |
/** MenuChangeListener that listens to change events occuring in menu's model */ |
/** Whenever menu is selected or deselected the MenuEvent is fired to |
90 |
private ChangeListener menuChangeListener; |
menu's registered listeners. */ |
|
|
|
|
/** MenuEvent */ |
|
91 |
private MenuEvent menuEvent = new MenuEvent(this); |
private MenuEvent menuEvent = new MenuEvent(this); |
92 |
|
|
93 |
/*Amount of time, in milliseconds, that should pass before popupMenu |
/*Amount of time, in milliseconds, that should pass before popupMenu |
97 |
/* PopupListener */ |
/* PopupListener */ |
98 |
protected WinListener popupListener; |
protected WinListener popupListener; |
99 |
|
|
100 |
/** Location at which popup menu associated with this menu will be displayed*/ |
/** Location at which popup menu associated with this menu will be |
101 |
|
displayed */ |
102 |
private Point menuLocation; |
private Point menuLocation; |
103 |
|
|
104 |
/** |
/** |
107 |
public JMenu() |
public JMenu() |
108 |
{ |
{ |
109 |
super(); |
super(); |
|
menuChangeListener = createMenuChangeListener(); |
|
|
getModel().addChangeListener(menuChangeListener); |
|
110 |
} |
} |
111 |
|
|
112 |
/** |
/** |
117 |
public JMenu(String text) |
public JMenu(String text) |
118 |
{ |
{ |
119 |
super(text); |
super(text); |
|
menuChangeListener = createMenuChangeListener(); |
|
|
getModel().addChangeListener(menuChangeListener); |
|
120 |
} |
} |
121 |
|
|
122 |
/** |
/** |
128 |
public JMenu(Action action) |
public JMenu(Action action) |
129 |
{ |
{ |
130 |
super(action); |
super(action); |
131 |
menuChangeListener = createMenuChangeListener(); |
createActionChangeListener(this); |
|
getModel().addChangeListener(menuChangeListener); |
|
132 |
} |
} |
133 |
|
|
134 |
/** |
/** |
339 |
|
|
340 |
/** |
/** |
341 |
* Changes this menu selected state if selected is true and false otherwise |
* Changes this menu selected state if selected is true and false otherwise |
342 |
* This method fires menuEvents to model's registered listeners. |
* This method fires menuEvents to menu's registered listeners. |
343 |
* |
* |
344 |
* @param selected true if the menu should be selected and false otherwise |
* @param selected true if the menu should be selected and false otherwise |
345 |
*/ |
*/ |
346 |
public void setSelected(boolean selected) |
public void setSelected(boolean selected) |
347 |
{ |
{ |
348 |
|
// if this menu selection is true, then activate this menu and |
349 |
|
// display popup associated with this menu |
350 |
|
if (selected) |
351 |
|
{ |
352 |
super.setArmed(true); |
super.setArmed(true); |
353 |
|
super.setSelected(true); |
354 |
|
|
355 |
|
// FIXME: The popup menu should be shown on the screen after certain |
356 |
|
// number of seconds pass. The 'delay' property of this menu indicates |
357 |
|
// this amount of seconds. 'delay' property is 0 by default. |
358 |
|
if (this.isShowing()) |
359 |
|
{ |
360 |
fireMenuSelected(); |
fireMenuSelected(); |
361 |
|
|
362 |
|
int x = 0; |
363 |
|
int y = 0; |
364 |
|
|
365 |
|
if (menuLocation == null) |
366 |
|
{ |
367 |
|
// Calculate correct position of the popup. Note that location of the popup |
368 |
|
// passed to show() should be relative to the popup's invoker |
369 |
|
if (isTopLevelMenu()) |
370 |
|
y = this.getHeight(); |
371 |
|
else |
372 |
|
x = this.getWidth(); |
373 |
|
|
374 |
|
getPopupMenu().show(this, x, y); |
375 |
|
} |
376 |
|
else |
377 |
|
getPopupMenu().show(this, menuLocation.x, menuLocation.y); |
378 |
|
} |
379 |
|
} |
380 |
|
|
381 |
|
else |
382 |
|
{ |
383 |
|
super.setSelected(false); |
384 |
|
super.setArmed(false); |
385 |
|
fireMenuDeselected(); |
386 |
|
popupMenu.setVisible(false); |
387 |
|
} |
388 |
} |
} |
389 |
|
|
390 |
/** |
/** |
683 |
} |
} |
684 |
|
|
685 |
/** |
/** |
|
* Creates MenuChangeListener to listen to change events occuring |
|
|
* in the model |
|
|
* |
|
|
* @return ChangeListener |
|
|
*/ |
|
|
private ChangeListener createMenuChangeListener() |
|
|
{ |
|
|
return new MenuChangeListener(); |
|
|
} |
|
|
|
|
|
/** |
|
686 |
* Creates WinListener that listens to the menu;s popup menu. |
* Creates WinListener that listens to the menu;s popup menu. |
687 |
* |
* |
688 |
* @param popup JPopupMenu to listen to |
* @param popup JPopupMenu to listen to |
706 |
{ |
{ |
707 |
// if this menu selection is true, then activate this menu and |
// if this menu selection is true, then activate this menu and |
708 |
// display popup associated with this menu |
// display popup associated with this menu |
709 |
if (changed) |
setSelected(changed); |
|
{ |
|
|
setArmed(true); |
|
|
fireMenuSelected(); |
|
|
|
|
|
int x = 0; |
|
|
int y = 0; |
|
|
if (menuLocation == null) |
|
|
{ |
|
|
// Calculate correct position of the popup. Note that location of the popup |
|
|
// passed to show() should be relative to the popup's invoker |
|
|
if (isTopLevelMenu()) |
|
|
y = this.getHeight(); |
|
|
else |
|
|
x = this.getWidth(); |
|
|
|
|
|
getPopupMenu().show(this, x, y); |
|
|
} |
|
|
else |
|
|
getPopupMenu().show(this, menuLocation.x, menuLocation.y); |
|
|
} |
|
|
|
|
|
else |
|
|
{ |
|
|
fireMenuDeselected(); |
|
|
popupMenu.setVisible(false); |
|
|
setArmed(false); |
|
|
} |
|
710 |
} |
} |
711 |
|
|
712 |
/** |
/** |
864 |
} |
} |
865 |
} |
} |
866 |
|
|
|
/** This class listens to ChangeEvent fired by menu's model*/ |
|
|
protected class MenuChangeListener implements ChangeListener |
|
|
{ |
|
|
/** This method is invoked when there is change in menu's model property */ |
|
|
public void stateChanged(ChangeEvent e) |
|
|
{ |
|
|
revalidate(); |
|
|
repaint(); |
|
|
} |
|
|
} |
|
|
|
|
867 |
/** |
/** |
868 |
* This class listens to PropertyChangeEvents occuring in menu's action |
* This class listens to PropertyChangeEvents occuring in menu's action |
869 |
*/ |
*/ |