~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/SubstanceMenuUI.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.*;
 
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.basic.BasicMenuUI;
 
40
 
 
41
import org.pushingpixels.substance.api.SubstanceLookAndFeel;
 
42
import org.pushingpixels.substance.internal.animation.StateTransitionTracker;
 
43
import org.pushingpixels.substance.internal.animation.TransitionAwareUI;
 
44
import org.pushingpixels.substance.internal.utils.SubstanceCoreUtilities;
 
45
import org.pushingpixels.substance.internal.utils.SubstanceSizeUtils;
 
46
import org.pushingpixels.substance.internal.utils.icon.MenuArrowIcon;
 
47
import org.pushingpixels.substance.internal.utils.menu.MenuUtilities;
 
48
import org.pushingpixels.substance.internal.utils.menu.SubstanceMenu;
 
49
import org.pushingpixels.substance.internal.utils.menu.MenuUtilities.MenuPropertyListener;
 
50
 
 
51
/**
 
52
 * UI for menus in <b>Substance</b> look and feel.
 
53
 * 
 
54
 * @author Kirill Grouchnikov
 
55
 */
 
56
public class SubstanceMenuUI extends BasicMenuUI implements SubstanceMenu,
 
57
                TransitionAwareUI {
 
58
        /**
 
59
         * For rollover effects - enhancement 93.
 
60
         */
 
61
        protected MouseListener substanceMouseListener;
 
62
 
 
63
        protected StateTransitionTracker stateTransitionTracker;
 
64
 
 
65
        /**
 
66
         * Listens on all changes to the underlying menu item.
 
67
         */
 
68
        protected MenuPropertyListener substanceMenuPropertyListener;
 
69
 
 
70
        /**
 
71
         * Property change listener. Listens on changes to
 
72
         * {@link AbstractButton#MODEL_CHANGED_PROPERTY} property.
 
73
         */
 
74
        protected PropertyChangeListener substancePropertyListener;
 
75
 
 
76
        /**
 
77
         * For rollover effects - enhancement 93.
 
78
         */
 
79
        protected FocusListener substanceFocusListener;
 
80
 
 
81
        /*
 
82
         * (non-Javadoc)
 
83
         * 
 
84
         * @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
 
85
         */
 
86
        public static ComponentUI createUI(JComponent comp) {
 
87
                SubstanceCoreUtilities.testComponentCreationThreadingViolation(comp);
 
88
                return new SubstanceMenuUI((JMenu) comp);
 
89
        }
 
90
 
 
91
        public SubstanceMenuUI(JMenu menuItem) {
 
92
                this.stateTransitionTracker = new StateTransitionTracker(menuItem,
 
93
                                menuItem.getModel());
 
94
        }
 
95
 
 
96
        /*
 
97
         * (non-Javadoc)
 
98
         * 
 
99
         * @see javax.swing.plaf.basic.BasicMenuUI#installDefaults()
 
100
         */
 
101
        @Override
 
102
        protected void installDefaults() {
 
103
                super.installDefaults();
 
104
                this.menuItem.setRolloverEnabled(true);
 
105
                // this.menuItem.setOpaque(false);
 
106
 
 
107
                this.arrowIcon = new MenuArrowIcon((JMenu) this.menuItem);
 
108
 
 
109
                this.defaultTextIconGap = SubstanceSizeUtils
 
110
                                .getTextIconGap(SubstanceSizeUtils
 
111
                                                .getComponentFontSize(this.menuItem));
 
112
 
 
113
                this.menuItem.putClientProperty(SubstanceLookAndFeel.FLAT_PROPERTY,
 
114
                                Boolean.TRUE);
 
115
 
 
116
                LookAndFeel.installProperty(menuItem, "opaque", Boolean.FALSE);
 
117
        }
 
118
 
 
119
        /*
 
120
         * (non-Javadoc)
 
121
         * 
 
122
         * @see javax.swing.plaf.basic.BasicMenuUI#installListeners()
 
123
         */
 
124
        @Override
 
125
        protected void installListeners() {
 
126
                super.installListeners();
 
127
 
 
128
                // Improving performance on big menus.
 
129
                this.substanceMenuPropertyListener = new MenuPropertyListener(
 
130
                                this.menuItem);
 
131
                this.substanceMenuPropertyListener.install();
 
132
 
 
133
                this.stateTransitionTracker.registerModelListeners();
 
134
 
 
135
                // fix for enhancement 93 - rollover effects on menu items
 
136
                this.substanceMouseListener = new MouseAdapter() {
 
137
                        // fix for defect 93 - no rollover effects on menu
 
138
                        // items that are not in the selected path
 
139
                        private boolean toRepaint() {
 
140
                                MenuElement[] selectedMenuPath = MenuSelectionManager
 
141
                                                .defaultManager().getSelectedPath();
 
142
                                for (MenuElement elem : selectedMenuPath) {
 
143
                                        if (elem == SubstanceMenuUI.this.menuItem) {
 
144
                                                return true;
 
145
                                        }
 
146
                                }
 
147
                                return (selectedMenuPath.length == 0);
 
148
                        }
 
149
 
 
150
                        @Override
 
151
                        public void mouseEntered(MouseEvent e) {
 
152
                                if (this.toRepaint()) {
 
153
                                        stateTransitionTracker.turnOffModelChangeTracking();
 
154
                                        menuItem.getModel().setRollover(true);
 
155
                                        stateTransitionTracker.onModelStateChanged();
 
156
                                        // fix for issue 371 - repaint the menu bar since the
 
157
                                        // menu is marked as flat
 
158
                                        Rectangle bounds = menuItem.getBounds();
 
159
                                        menuItem.getParent().repaint(bounds.x, bounds.y,
 
160
                                                        bounds.width, bounds.height);
 
161
                                }
 
162
                        }
 
163
 
 
164
                        @Override
 
165
                        public void mouseExited(MouseEvent e) {
 
166
                                if (this.toRepaint()) {
 
167
                                        stateTransitionTracker.turnOffModelChangeTracking();
 
168
                                        menuItem.getModel().setRollover(false);
 
169
                                        stateTransitionTracker.onModelStateChanged();
 
170
                                        // fix for issue 371 - repaint the menu bar since the
 
171
                                        // menu is marked as flat
 
172
                                        Rectangle bounds = menuItem.getBounds();
 
173
                                        menuItem.getParent().repaint(bounds.x, bounds.y,
 
174
                                                        bounds.width, bounds.height);
 
175
                                }
 
176
                        }
 
177
                };
 
178
                this.menuItem.addMouseListener(this.substanceMouseListener);
 
179
                this.substanceFocusListener = new FocusAdapter() {
 
180
                        // fix for defect 93 - no rollover effects on menu
 
181
                        // items that are not in the selected path
 
182
                        private boolean toRepaint() {
 
183
                                MenuElement[] selectedMenuPath = MenuSelectionManager
 
184
                                                .defaultManager().getSelectedPath();
 
185
                                for (MenuElement elem : selectedMenuPath) {
 
186
                                        if (elem == SubstanceMenuUI.this.menuItem) {
 
187
                                                return true;
 
188
                                        }
 
189
                                }
 
190
                                return (selectedMenuPath.length == 0);
 
191
                        }
 
192
 
 
193
                        @Override
 
194
                        public void focusLost(FocusEvent e) {
 
195
                                if (toRepaint()) {
 
196
                                        stateTransitionTracker.turnOffModelChangeTracking();
 
197
                                        menuItem.getModel().setRollover(false);
 
198
                                        stateTransitionTracker.onModelStateChanged();
 
199
                                        // fix for issue 371 - repaint the menu bar since the
 
200
                                        // menu is marked as flat
 
201
                                        Rectangle bounds = menuItem.getBounds();
 
202
                                        menuItem.getParent().repaint(bounds.x, bounds.y,
 
203
                                                        bounds.width, bounds.height);
 
204
                                }
 
205
                        }
 
206
                };
 
207
                this.menuItem.addFocusListener(this.substanceFocusListener);
 
208
 
 
209
                this.substancePropertyListener = new PropertyChangeListener() {
 
210
                        @Override
 
211
            public void propertyChange(PropertyChangeEvent evt) {
 
212
                                if (AbstractButton.MODEL_CHANGED_PROPERTY.equals(evt
 
213
                                                .getPropertyName())) {
 
214
                                        stateTransitionTracker.setModel((ButtonModel) evt
 
215
                                                        .getNewValue());
 
216
                                }
 
217
                                if ("font".equals(evt.getPropertyName())) {
 
218
                                        SwingUtilities.invokeLater(new Runnable() {
 
219
                                                @Override
 
220
                        public void run() {
 
221
                                                        if (menuItem != null) {
 
222
                                                                menuItem.updateUI();
 
223
                                                        }
 
224
                                                }
 
225
                                        });
 
226
                                }
 
227
                        }
 
228
                };
 
229
                this.menuItem.addPropertyChangeListener(this.substancePropertyListener);
 
230
        }
 
231
 
 
232
        /*
 
233
         * (non-Javadoc)
 
234
         * 
 
235
         * @see javax.swing.plaf.basic.BasicMenuUI#uninstallListeners()
 
236
         */
 
237
        @Override
 
238
        protected void uninstallListeners() {
 
239
                super.uninstallListeners();
 
240
 
 
241
                // Improving performance on big menus.
 
242
                this.substanceMenuPropertyListener.uninstall();
 
243
                this.substanceMenuPropertyListener = null;
 
244
 
 
245
                // fix for enhancement 93 - rollover effects on menu items
 
246
                this.menuItem.removeMouseListener(this.substanceMouseListener);
 
247
                this.substanceMouseListener = null;
 
248
                this.menuItem.removeFocusListener(this.substanceFocusListener);
 
249
                this.substanceFocusListener = null;
 
250
 
 
251
                this.menuItem
 
252
                                .removePropertyChangeListener(this.substancePropertyListener);
 
253
                this.substancePropertyListener = null;
 
254
 
 
255
                this.stateTransitionTracker.unregisterModelListeners();
 
256
        }
 
257
 
 
258
        /*
 
259
         * (non-Javadoc)
 
260
         * 
 
261
         * @see org.pushingpixels.substance.SubstanceMenu#getAssociatedMenuItem()
 
262
         */
 
263
        @Override
 
264
    public JMenuItem getAssociatedMenuItem() {
 
265
                return this.menuItem;
 
266
        }
 
267
 
 
268
        /*
 
269
         * (non-Javadoc)
 
270
         * 
 
271
         * @see org.pushingpixels.substance.SubstanceMenu#getAcceleratorFont()
 
272
         */
 
273
        @Override
 
274
    public Font getAcceleratorFont() {
 
275
                return this.acceleratorFont;
 
276
        }
 
277
 
 
278
        /*
 
279
         * (non-Javadoc)
 
280
         * 
 
281
         * @see org.pushingpixels.substance.SubstanceMenu#getArrowIcon()
 
282
         */
 
283
        @Override
 
284
    public Icon getArrowIcon() {
 
285
                return this.arrowIcon;
 
286
        }
 
287
 
 
288
        /*
 
289
         * (non-Javadoc)
 
290
         * 
 
291
         * @see org.pushingpixels.substance.SubstanceMenu#getCheckIcon()
 
292
         */
 
293
        @Override
 
294
    public Icon getCheckIcon() {
 
295
                return null;
 
296
        }
 
297
 
 
298
        /*
 
299
         * (non-Javadoc)
 
300
         * 
 
301
         * @see org.pushingpixels.substance.SubstanceMenu#getDefaultTextIconGap()
 
302
         */
 
303
        @Override
 
304
    public int getDefaultTextIconGap() {
 
305
                return this.defaultTextIconGap;
 
306
        }
 
307
 
 
308
        /*
 
309
         * (non-Javadoc)
 
310
         * 
 
311
         * @see
 
312
         * javax.swing.plaf.basic.BasicMenuItemUI#getPreferredMenuItemSize(javax
 
313
         * .swing.JComponent, javax.swing.Icon, javax.swing.Icon, int)
 
314
         */
 
315
        @Override
 
316
        protected Dimension getPreferredMenuItemSize(JComponent c, Icon checkIcon,
 
317
                        Icon arrowIcon, int defaultTextIconGap) {
 
318
                Dimension superDim = super.getPreferredMenuItemSize(c, checkIcon,
 
319
                                arrowIcon, defaultTextIconGap);
 
320
 
 
321
                if (MenuUtilities.getPopupLayoutMetrics(menuItem, false) != null) {
 
322
                        return new Dimension(MenuUtilities.getPreferredWidth(menuItem),
 
323
                                        superDim.height);
 
324
                }
 
325
 
 
326
                return superDim;
 
327
        }
 
328
 
 
329
        /*
 
330
         * (non-Javadoc)
 
331
         * 
 
332
         * @see
 
333
         * javax.swing.plaf.basic.BasicMenuItemUI#paintMenuItem(java.awt.Graphics,
 
334
         * javax.swing.JComponent, javax.swing.Icon, javax.swing.Icon,
 
335
         * java.awt.Color, java.awt.Color, int)
 
336
         */
 
337
        @Override
 
338
        protected void paintMenuItem(Graphics g, JComponent c, Icon checkIcon,
 
339
                        Icon arrowIcon, Color background, Color foreground,
 
340
                        int defaultTextIconGap) {
 
341
                MenuUtilities.paintMenuItem(g, menuItem, checkIcon, arrowIcon,
 
342
                                defaultTextIconGap);
 
343
        }
 
344
 
 
345
        @Override
 
346
        public StateTransitionTracker getTransitionTracker() {
 
347
                return this.stateTransitionTracker;
 
348
        }
 
349
 
 
350
        @Override
 
351
        public boolean isInside(MouseEvent me) {
 
352
                return true;
 
353
        }
 
354
}