1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.action;
21
22 import java.awt.event.ActionEvent;
23
24 import javax.swing.Action;
25 import javax.swing.JComponent;
26 import javax.swing.JPopupMenu;
27 import javax.swing.event.PopupMenuEvent;
28 import javax.swing.event.PopupMenuListener;
29
30 import org.xnap.action.*;
31 import org.xnap.gui.component.XNapToolBarToggleButton;
32 import org.xnap.gui.util.GUIHelper;
33 import org.xnap.gui.util.IconHelper;
34
35 /***
36 * Provides an action that shows a panel below the button that triggered this
37 * action.
38 */
39 public class PanelAction extends AbstractToggleAction
40 implements ToggleAction
41 {
42
43
44
45
46
47 private JComponent jc;
48 private Object source;
49
50
51
52 public PanelAction(JComponent jc, String iconFilename)
53 {
54 this.jc = jc;
55
56 putValue(Action.SHORT_DESCRIPTION, "Shows a settings panel.");
57 putValue(IconHelper.XNAP_ICON, iconFilename);
58 }
59
60
61
62 public void actionPerformed(ActionEvent event)
63 {
64 source = event.getSource();
65 super.actionPerformed(event);
66 }
67
68 public void toggled(boolean selected)
69 {
70 if (selected && source instanceof JComponent) {
71 JComponent src = (JComponent)source;
72 JPopupMenu jp;
73 if (jc instanceof JPopupMenu) {
74 jp = (JPopupMenu)jc;
75 }
76 else {
77 jp = new JPopupMenu();
78 jp.add(jc);
79 }
80 jp.addPopupMenuListener(new PopupListener());
81 GUIHelper.showPopupMenu(jp, src, 0, src.getHeight(), -src.getHeight());
82 }
83
84 }
85
86
87
88 private class PopupListener implements PopupMenuListener {
89
90 public PopupListener()
91 {
92 }
93
94 public void popupMenuCanceled(PopupMenuEvent e)
95 {
96 }
97
98 public void popupMenuWillBecomeInvisible(PopupMenuEvent e)
99 {
100 if (source instanceof XNapToolBarToggleButton
101 && ((XNapToolBarToggleButton)source).isMouseOver()) {
102
103
104
105 return;
106 }
107 ((JPopupMenu)e.getSource()).removePopupMenuListener(this);
108 PanelAction.this.setSelected(false);
109 }
110
111 public void popupMenuWillBecomeVisible(PopupMenuEvent e)
112 {
113 PanelAction.this.setSelected(true);
114 }
115 }
116
117 }
118