Debian Codegraph @

substance-flamingo/src/main/java/org/pushingpixels/substance/flamingo/common/ui/SubstanceCommandMenuButtonUI.java

raw
1 /*
2 * Copyright (c) 2005-2010 Flamingo / Substance Kirill Grouchnikov. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * o Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 *
10 * o Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * o Neither the name of Flamingo Kirill Grouchnikov nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 package org.pushingpixels.substance.flamingo.common.ui;
31
32 import java.awt.*;
33 import java.awt.event.*;
34
35 import javax.swing.JComponent;
36 import javax.swing.plaf.ComponentUI;
37
38 import org.pushingpixels.flamingo.api.common.JCommandMenuButton;
39 import org.pushingpixels.flamingo.api.common.RolloverActionListener;
40 import org.pushingpixels.flamingo.internal.utils.KeyTipRenderingUtilities;
41
42 /**
43 * UI for {@link JCommandMenuButton} components in <b>Substance</b> look and
44 * feel.
45 *
46 * @author Kirill Grouchnikov
47 */
48 public class SubstanceCommandMenuButtonUI extends SubstanceCommandButtonUI {
49 /**
50 * Rollover menu mouse listener.
51 */
52 protected MouseListener rolloverMenuMouseListener;
53
54 public static ComponentUI createUI(JComponent c) {
55 return new SubstanceCommandMenuButtonUI((JCommandMenuButton)c);
56 }
57
58 public SubstanceCommandMenuButtonUI(JCommandMenuButton button) {
59 super(button);
60 }
61
62 /*
63 * (non-Javadoc)
64 *
65 * @see org.jvnet.flamingo.common.ui.BasicCommandButtonUI#installListeners()
66 */
67 @Override
68 protected void installListeners() {
69 super.installListeners();
70
71 this.rolloverMenuMouseListener = new MouseAdapter() {
72 @Override
73 public void mouseEntered(MouseEvent e) {
74 if (commandButton.isEnabled()) {
75 int modifiers = 0;
76 AWTEvent currentEvent = EventQueue.getCurrentEvent();
77 if (currentEvent instanceof InputEvent) {
78 modifiers = ((InputEvent) currentEvent).getModifiers();
79 } else if (currentEvent instanceof ActionEvent) {
80 modifiers = ((ActionEvent) currentEvent).getModifiers();
81 }
82 fireRolloverActionPerformed(new ActionEvent(this,
83 ActionEvent.ACTION_PERFORMED, commandButton
84 .getActionModel().getActionCommand(),
85 EventQueue.getMostRecentEventTime(), modifiers));
86
87 processPopupAction();
88 }
89 }
90 };
91 this.commandButton.addMouseListener(this.rolloverMenuMouseListener);
92 }
93
94 /*
95 * (non-Javadoc)
96 *
97 * @see
98 * org.jvnet.flamingo.common.ui.BasicCommandButtonUI#uninstallListeners()
99 */
100 @Override
101 protected void uninstallListeners() {
102 this.commandButton.removeMouseListener(this.rolloverMenuMouseListener);
103 this.rolloverMenuMouseListener = null;
104
105 super.uninstallListeners();
106 }
107
108 /**
109 * Fires the rollover action on all registered handlers.
110 *
111 * @param e
112 * Event object.
113 */
114 protected void fireRolloverActionPerformed(ActionEvent e) {
115 // Guaranteed to return a non-null array
116 RolloverActionListener[] listeners = commandButton
117 .getListeners(RolloverActionListener.class);
118 // Process the listeners last to first, notifying
119 // those that are interested in this event
120 for (int i = listeners.length - 1; i >= 0; i--) {
121 (listeners[i]).actionPerformed(e);
122 }
123 }
124
125 @Override
126 public void update(Graphics g, JComponent c) {
127 super.update(g, c);
128
129 JCommandMenuButton menuButton = (JCommandMenuButton) c;
130 KeyTipRenderingUtilities.renderMenuButtonKeyTips(g, menuButton,
131 layoutManager);
132 }
133 }