~ubuntu-branches/debian/stretch/insubstantial/stretch

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/internal/ui/SubstanceRadioButtonMenuItemUI.java

  • Committer: Package Import Robot
  • Author(s): Felix Natter
  • Date: 2016-01-18 20:58:45 UTC
  • Revision ID: package-import@ubuntu.com-20160118205845-crbmrkda61qsi5qa
Tags: upstream-7.3+dfsg2
ImportĀ upstreamĀ versionĀ 7.3+dfsg2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2005-2010 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 Substance 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.internal.ui;
 
31
 
 
32
import java.awt.*;
 
33
import java.awt.event.MouseEvent;
 
34
import java.beans.PropertyChangeEvent;
 
35
import java.beans.PropertyChangeListener;
 
36
 
 
37
import javax.swing.*;
 
38
import javax.swing.plaf.ComponentUI;
 
39
import javax.swing.plaf.UIResource;
 
40
import javax.swing.plaf.basic.BasicRadioButtonMenuItemUI;
 
41
 
 
42
import org.pushingpixels.substance.internal.animation.StateTransitionTracker;
 
43
import org.pushingpixels.substance.internal.animation.TransitionAwareUI;
 
44
import org.pushingpixels.substance.internal.utils.*;
 
45
import org.pushingpixels.substance.internal.utils.icon.RadioButtonMenuItemIcon;
 
46
import org.pushingpixels.substance.internal.utils.menu.MenuUtilities;
 
47
import org.pushingpixels.substance.internal.utils.menu.SubstanceMenu;
 
48
import org.pushingpixels.substance.internal.utils.menu.MenuUtilities.MenuPropertyListener;
 
49
 
 
50
/**
 
51
 * UI for radio button menu items in <b>Substance</b> look and feel.
 
52
 * 
 
53
 * @author Kirill Grouchnikov
 
54
 */
 
55
public class SubstanceRadioButtonMenuItemUI extends BasicRadioButtonMenuItemUI
 
56
                implements SubstanceMenu, TransitionAwareUI {
 
57
        /**
 
58
         * Rollover listener.
 
59
         */
 
60
        protected RolloverMenuItemListener substanceRolloverListener;
 
61
 
 
62
        protected StateTransitionTracker stateTransitionTracker;
 
63
 
 
64
        /**
 
65
         * Property change listener. Listens on changes to
 
66
         * {@link AbstractButton#MODEL_CHANGED_PROPERTY} property.
 
67
         */
 
68
        protected PropertyChangeListener substancePropertyListener;
 
69
 
 
70
        /**
 
71
         * Listens on all changes to the underlying menu item.
 
72
         */
 
73
        protected MenuPropertyListener substanceMenuPropertyListener;
 
74
 
 
75
        /*
 
76
         * (non-Javadoc)
 
77
         * 
 
78
         * @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
 
79
         */
 
80
        public static ComponentUI createUI(JComponent comp) {
 
81
                SubstanceCoreUtilities.testComponentCreationThreadingViolation(comp);
 
82
                JRadioButtonMenuItem item = (JRadioButtonMenuItem) comp;
 
83
                item.setRolloverEnabled(true);
 
84
 
 
85
                return new SubstanceRadioButtonMenuItemUI((JRadioButtonMenuItem) comp);
 
86
        }
 
87
 
 
88
        public SubstanceRadioButtonMenuItemUI(JRadioButtonMenuItem menuItem) {
 
89
                this.stateTransitionTracker = new StateTransitionTracker(menuItem,
 
90
                                menuItem.getModel());
 
91
        }
 
92
 
 
93
        /*
 
94
         * (non-Javadoc)
 
95
         * 
 
96
         * @see javax.swing.plaf.basic.BasicMenuItemUI#installListeners()
 
97
         */
 
98
        @Override
 
99
        protected void installListeners() {
 
100
                super.installListeners();
 
101
 
 
102
                // Improving performance on big menus.
 
103
                this.substanceMenuPropertyListener = new MenuPropertyListener(
 
104
                                this.menuItem);
 
105
                this.substanceMenuPropertyListener.install();
 
106
 
 
107
                // fix for defect 109 - storing reference to rollover listener
 
108
                this.substanceRolloverListener = new RolloverMenuItemListener(
 
109
                                this.menuItem, this.stateTransitionTracker);
 
110
                this.menuItem.addMouseListener(this.substanceRolloverListener);
 
111
 
 
112
                this.stateTransitionTracker.registerModelListeners();
 
113
 
 
114
                this.substancePropertyListener = new PropertyChangeListener() {
 
115
                        @Override
 
116
            public void propertyChange(PropertyChangeEvent evt) {
 
117
                                if (AbstractButton.MODEL_CHANGED_PROPERTY.equals(evt
 
118
                                                .getPropertyName())) {
 
119
                                        stateTransitionTracker.setModel((ButtonModel) evt
 
120
                                                        .getNewValue());
 
121
                                }
 
122
                                if ("font".equals(evt.getPropertyName())) {
 
123
                                        SwingUtilities.invokeLater(new Runnable() {
 
124
                                                @Override
 
125
                        public void run() {
 
126
                                                        if (menuItem != null) {
 
127
                                                                menuItem.updateUI();
 
128
                                                        }
 
129
                                                }
 
130
                                        });
 
131
                                }
 
132
                        }
 
133
                };
 
134
                this.menuItem.addPropertyChangeListener(this.substancePropertyListener);
 
135
        }
 
136
 
 
137
        /*
 
138
         * (non-Javadoc)
 
139
         * 
 
140
         * @see javax.swing.plaf.basic.BasicMenuItemUI#uninstallListeners()
 
141
         */
 
142
        @Override
 
143
        protected void uninstallListeners() {
 
144
                super.uninstallListeners();
 
145
 
 
146
                // Improving performance on big menus.
 
147
                this.substanceMenuPropertyListener.uninstall();
 
148
                this.substanceMenuPropertyListener = null;
 
149
 
 
150
                // fix for defect 109 - unregistering rollover listener
 
151
                this.menuItem.removeMouseListener(this.substanceRolloverListener);
 
152
                this.substanceRolloverListener = null;
 
153
 
 
154
                this.menuItem
 
155
                                .removePropertyChangeListener(this.substancePropertyListener);
 
156
                this.substancePropertyListener = null;
 
157
 
 
158
                this.stateTransitionTracker.unregisterModelListeners();
 
159
        }
 
160
 
 
161
        /*
 
162
         * (non-Javadoc)
 
163
         * 
 
164
         * @see javax.swing.plaf.basic.BasicMenuItemUI#installDefaults()
 
165
         */
 
166
        @Override
 
167
        protected void installDefaults() {
 
168
                super.installDefaults();
 
169
                if (this.checkIcon == null || this.checkIcon instanceof UIResource) {
 
170
                        this.checkIcon = new RadioButtonMenuItemIcon(this.menuItem,
 
171
                                        SubstanceSizeUtils.getMenuCheckMarkSize(SubstanceSizeUtils
 
172
                                                        .getComponentFontSize(this.menuItem)));
 
173
                }
 
174
                this.defaultTextIconGap = SubstanceSizeUtils
 
175
                                .getTextIconGap(SubstanceSizeUtils
 
176
                                                .getComponentFontSize(this.menuItem));
 
177
        }
 
178
 
 
179
        /*
 
180
         * (non-Javadoc)
 
181
         * 
 
182
         * @see org.pushingpixels.substance.SubstanceMenu#getAssociatedMenuItem()
 
183
         */
 
184
        @Override
 
185
    public JMenuItem getAssociatedMenuItem() {
 
186
                return this.menuItem;
 
187
        }
 
188
 
 
189
        /*
 
190
         * (non-Javadoc)
 
191
         * 
 
192
         * @see org.pushingpixels.substance.SubstanceMenu#getAcceleratorFont()
 
193
         */
 
194
        @Override
 
195
    public Font getAcceleratorFont() {
 
196
                return this.acceleratorFont;
 
197
        }
 
198
 
 
199
        /*
 
200
         * (non-Javadoc)
 
201
         * 
 
202
         * @see org.pushingpixels.substance.SubstanceMenu#getArrowIcon()
 
203
         */
 
204
        @Override
 
205
    public Icon getArrowIcon() {
 
206
                return this.arrowIcon;
 
207
        }
 
208
 
 
209
        /*
 
210
         * (non-Javadoc)
 
211
         * 
 
212
         * @see org.pushingpixels.substance.SubstanceMenu#getCheckIcon()
 
213
         */
 
214
        @Override
 
215
    public Icon getCheckIcon() {
 
216
                return this.checkIcon;
 
217
        }
 
218
 
 
219
        /*
 
220
         * (non-Javadoc)
 
221
         * 
 
222
         * @see org.pushingpixels.substance.SubstanceMenu#getDefaultTextIconGap()
 
223
         */
 
224
        @Override
 
225
    public int getDefaultTextIconGap() {
 
226
                return this.defaultTextIconGap;
 
227
        }
 
228
 
 
229
        /*
 
230
         * (non-Javadoc)
 
231
         * 
 
232
         * @see
 
233
         * javax.swing.plaf.basic.BasicMenuItemUI#getPreferredMenuItemSize(javax
 
234
         * .swing.JComponent, javax.swing.Icon, javax.swing.Icon, int)
 
235
         */
 
236
        @Override
 
237
        protected Dimension getPreferredMenuItemSize(JComponent c, Icon checkIcon,
 
238
                        Icon arrowIcon, int defaultTextIconGap) {
 
239
                Dimension superDim = super.getPreferredMenuItemSize(c, checkIcon,
 
240
                                arrowIcon, defaultTextIconGap);
 
241
 
 
242
                return new Dimension(MenuUtilities.getPreferredWidth(menuItem),
 
243
                                superDim.height);
 
244
        }
 
245
 
 
246
        @Override
 
247
        public boolean isInside(MouseEvent me) {
 
248
                return this.menuItem.getBounds().contains(me.getX(), me.getY());
 
249
        }
 
250
 
 
251
        @Override
 
252
        public StateTransitionTracker getTransitionTracker() {
 
253
                return this.stateTransitionTracker;
 
254
        }
 
255
 
 
256
        /*
 
257
         * (non-Javadoc)
 
258
         * 
 
259
         * @see
 
260
         * javax.swing.plaf.basic.BasicMenuItemUI#paintMenuItem(java.awt.Graphics,
 
261
         * javax.swing.JComponent, javax.swing.Icon, javax.swing.Icon,
 
262
         * java.awt.Color, java.awt.Color, int)
 
263
         */
 
264
        @Override
 
265
        protected void paintMenuItem(Graphics g, JComponent c, Icon checkIcon,
 
266
                        Icon arrowIcon, Color background, Color foreground,
 
267
                        int defaultTextIconGap) {
 
268
                MenuUtilities.paintMenuItem(g, menuItem, checkIcon, arrowIcon,
 
269
                                defaultTextIconGap);
 
270
        }
 
271
}