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

« back to all changes in this revision

Viewing changes to substance/src/main/java/org/pushingpixels/substance/internal/utils/SubstanceDropDownButton.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.utils;
 
31
 
 
32
import java.awt.*;
 
33
import java.awt.image.BufferedImage;
 
34
import java.util.Map;
 
35
 
 
36
import javax.swing.*;
 
37
import javax.swing.border.Border;
 
38
 
 
39
import org.pushingpixels.lafwidget.LafWidgetUtilities;
 
40
import org.pushingpixels.lafwidget.animation.AnimationConfigurationManager;
 
41
import org.pushingpixels.lafwidget.animation.AnimationFacet;
 
42
import org.pushingpixels.substance.api.*;
 
43
import org.pushingpixels.substance.internal.animation.StateTransitionTracker;
 
44
import org.pushingpixels.substance.internal.animation.TransitionAwareUI;
 
45
 
 
46
/**
 
47
 * Drop down button in <b>Substance</b> look and feel.
 
48
 * 
 
49
 * @author Kirill Grouchnikov
 
50
 */
 
51
public final class SubstanceDropDownButton extends JButton implements
 
52
                SubstanceInternalArrowButton {
 
53
        static {
 
54
                AnimationConfigurationManager.getInstance().disallowAnimations(
 
55
                                AnimationFacet.GHOSTING_BUTTON_PRESS,
 
56
                                SubstanceDropDownButton.class);
 
57
                AnimationConfigurationManager.getInstance().disallowAnimations(
 
58
                                AnimationFacet.GHOSTING_ICON_ROLLOVER,
 
59
                                SubstanceDropDownButton.class);
 
60
        }
 
61
 
 
62
        /**
 
63
         * Simple constructor.
 
64
         * 
 
65
         * @param parent
 
66
         *            The parent component.
 
67
         */
 
68
        public SubstanceDropDownButton(JComponent parent) {
 
69
                super("");
 
70
                this.setModel(new DefaultButtonModel() {
 
71
                        @Override
 
72
                        public void setArmed(boolean armed) {
 
73
                                super.setArmed(this.isPressed() || armed);
 
74
                        }
 
75
                });
 
76
                this.setEnabled(parent.isEnabled());
 
77
                this.setFocusable(false);
 
78
                this.setRequestFocusEnabled(parent.isEnabled());
 
79
 
 
80
                int fontSize = SubstanceSizeUtils.getComponentFontSize(parent);
 
81
                int tbInset = SubstanceSizeUtils.getAdjustedSize(fontSize, 1, 2, 1,
 
82
                                false);
 
83
                int lrInset = 0;
 
84
                this.setMargin(new Insets(tbInset, lrInset, tbInset, tbInset));
 
85
 
 
86
                this.setBorderPainted(false);
 
87
                this.putClientProperty(SubstanceLookAndFeel.FLAT_PROPERTY, Boolean.TRUE);
 
88
                this.setOpaque(false);
 
89
        }
 
90
 
 
91
        @Override
 
92
        public void setBorder(Border border) {
 
93
        }
 
94
 
 
95
        @Override
 
96
        protected void paintBorder(Graphics g) {
 
97
                if (SubstanceCoreUtilities.isButtonNeverPainted(this)) {
 
98
                        return;
 
99
                }
 
100
 
 
101
                TransitionAwareUI transitionAwareUI = (TransitionAwareUI) this.getUI();
 
102
                StateTransitionTracker stateTransitionTracker = transitionAwareUI
 
103
                                .getTransitionTracker();
 
104
                StateTransitionTracker.ModelStateInfo modelStateInfo = stateTransitionTracker
 
105
                                .getModelStateInfo();
 
106
                Map<ComponentState, StateTransitionTracker.StateContributionInfo> activeStates = modelStateInfo
 
107
                                .getStateContributionMap();
 
108
 
 
109
                ComponentState currState = modelStateInfo.getCurrModelState();
 
110
                float extraAlpha = stateTransitionTracker.getActiveStrength();
 
111
 
 
112
                if (currState == ComponentState.DISABLED_UNSELECTED)
 
113
                        extraAlpha = 0.0f;
 
114
 
 
115
                if (extraAlpha == 0.0f)
 
116
                        return;
 
117
 
 
118
                int componentFontSize = SubstanceSizeUtils.getComponentFontSize(this);
 
119
                int borderDelta = (int) Math.floor(1.5 * SubstanceSizeUtils
 
120
                                .getBorderStrokeWidth(componentFontSize));
 
121
                float radius = Math.max(0, 2.0f
 
122
                                * SubstanceSizeUtils
 
123
                                                .getClassicButtonCornerRadius(componentFontSize)
 
124
                                - borderDelta);
 
125
 
 
126
                int width = getWidth();
 
127
                int height = getHeight();
 
128
 
 
129
                int offsetX = this.getX();
 
130
                int offsetY = this.getY();
 
131
                JComponent parent = (JComponent) this.getParent();
 
132
                SubstanceColorScheme baseBorderScheme = SubstanceColorSchemeUtilities
 
133
                                .getColorScheme(this, ColorSchemeAssociationKind.BORDER,
 
134
                                                currState);
 
135
 
 
136
                BufferedImage offscreen = SubstanceCoreUtilities.getBlankImage(width,
 
137
                                height);
 
138
                Graphics2D g2offscreen = offscreen.createGraphics();
 
139
 
 
140
                SubstanceImageCreator.paintTextComponentBorder(this, g2offscreen, 0, 0,
 
141
                                width, height, radius, baseBorderScheme);
 
142
                g2offscreen.translate(-offsetX, -offsetY);
 
143
                SubstanceImageCreator.paintTextComponentBorder(parent, g2offscreen, 0,
 
144
                                0, parent.getWidth(), parent.getHeight(), radius,
 
145
                                baseBorderScheme);
 
146
                g2offscreen.translate(offsetX, offsetY);
 
147
 
 
148
                for (Map.Entry<ComponentState, StateTransitionTracker.StateContributionInfo> activeEntry : activeStates
 
149
                                .entrySet()) {
 
150
                        ComponentState activeState = activeEntry.getKey();
 
151
                        if (activeState == currState)
 
152
                                continue;
 
153
 
 
154
                        float contribution = activeEntry.getValue().getContribution();
 
155
                        if (contribution == 0.0f)
 
156
                                continue;
 
157
 
 
158
                        g2offscreen.setComposite(AlphaComposite.SrcOver
 
159
                                        .derive(contribution));
 
160
                        SubstanceColorScheme borderScheme = SubstanceColorSchemeUtilities
 
161
                                        .getColorScheme(this, ColorSchemeAssociationKind.BORDER,
 
162
                                                        activeState);
 
163
 
 
164
                        SubstanceImageCreator.paintTextComponentBorder(this, g2offscreen,
 
165
                                        0, 0, width, height, radius, borderScheme);
 
166
                        g2offscreen.translate(-offsetX, -offsetY);
 
167
                        SubstanceImageCreator.paintTextComponentBorder(parent, g2offscreen,
 
168
                                        0, 0, parent.getWidth(), parent.getHeight(), radius,
 
169
                                        borderScheme);
 
170
                        g2offscreen.translate(offsetX, offsetY);
 
171
                }
 
172
                g2offscreen.dispose();
 
173
 
 
174
                Graphics2D g2d = (Graphics2D) g.create();
 
175
                g2d.setComposite(LafWidgetUtilities.getAlphaComposite(this, extraAlpha,
 
176
                                g));
 
177
 
 
178
                g2d.drawImage(offscreen, 0, 0, null);
 
179
                g2d.dispose();
 
180
        }
 
181
 
 
182
        @Override
 
183
        public void paint(Graphics g) {
 
184
                Graphics2D g2d = (Graphics2D) g.create();
 
185
 
 
186
                int componentFontSize = SubstanceSizeUtils.getComponentFontSize(this);
 
187
                int width = getWidth();
 
188
                int height = getHeight();
 
189
                int clipDelta = (int) SubstanceSizeUtils
 
190
                                .getBorderStrokeWidth(componentFontSize);
 
191
 
 
192
                if (this.getComponentOrientation().isLeftToRight()) {
 
193
                        g2d.clipRect(clipDelta, 0, width - clipDelta, height);
 
194
                } else {
 
195
                        g2d.clipRect(0, 0, width - clipDelta, height);
 
196
                }
 
197
                super.paint(g2d);
 
198
                g2d.dispose();
 
199
        }
 
200
}