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

« back to all changes in this revision

Viewing changes to flamingo/src/main/java/org/pushingpixels/flamingo/internal/utils/ArrowResizableIcon.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 Flamingo 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.flamingo.internal.utils;
 
31
 
 
32
import java.awt.*;
 
33
import java.awt.geom.GeneralPath;
 
34
 
 
35
import javax.swing.SwingConstants;
 
36
import javax.swing.SwingUtilities;
 
37
 
 
38
import org.pushingpixels.flamingo.api.common.JCommandButton;
 
39
import org.pushingpixels.flamingo.api.common.icon.ResizableIcon;
 
40
 
 
41
/**
 
42
 * Helper implementation of {@link ResizableIcon} that draws an arrow.
 
43
 * 
 
44
 * @author Kirill Grouchnikov
 
45
 */
 
46
public class ArrowResizableIcon implements ResizableIcon {
 
47
        /**
 
48
         * Initial dimension.
 
49
         */
 
50
        private Dimension initialDim;
 
51
 
 
52
        /**
 
53
         * The current icon width.
 
54
         */
 
55
        protected int width;
 
56
 
 
57
        /**
 
58
         * The current icon height.
 
59
         */
 
60
        protected int height;
 
61
 
 
62
        /**
 
63
         * Arrow direction. One of {@link SwingConstants#SOUTH},
 
64
         * {@link SwingConstants#NORTH}, {@link SwingConstants#EAST} or
 
65
         * {@link SwingConstants#WEST}.
 
66
         */
 
67
        protected int direction;
 
68
 
 
69
        /**
 
70
         * Creates a new arrow resizable icon.
 
71
         * 
 
72
         * @param initialDim
 
73
         *            Initial icon dimension.
 
74
         * @param direction
 
75
         *            Arrow direction. Must be one of {@link SwingConstants#SOUTH},
 
76
         *            {@link SwingConstants#NORTH}, {@link SwingConstants#EAST} or
 
77
         *            {@link SwingConstants#WEST}.
 
78
         */
 
79
        public ArrowResizableIcon(Dimension initialDim, int direction) {
 
80
                this.initialDim = initialDim;
 
81
                this.width = initialDim.width;
 
82
                this.height = initialDim.height;
 
83
                this.direction = direction;
 
84
        }
 
85
 
 
86
        /**
 
87
         * Creates a new arrow resizable icon.
 
88
         * 
 
89
         * @param initialDim
 
90
         *            Initial icon dimension.
 
91
         * @param direction
 
92
         *            Arrow direction. Must be one of {@link SwingConstants#SOUTH},
 
93
         *            {@link SwingConstants#NORTH}, {@link SwingConstants#EAST} or
 
94
         *            {@link SwingConstants#WEST}.
 
95
         */
 
96
        public ArrowResizableIcon(int initialDim, int direction) {
 
97
                this(new Dimension(initialDim, initialDim), direction);
 
98
        }
 
99
 
 
100
        public void revertToOriginalDimension() {
 
101
                this.width = initialDim.width;
 
102
                this.height = initialDim.height;
 
103
        }
 
104
 
 
105
        @Override
 
106
    public void setDimension(Dimension newDimension) {
 
107
                this.width = newDimension.width;
 
108
                this.height = newDimension.height;
 
109
        }
 
110
 
 
111
        @Override
 
112
    public int getIconHeight() {
 
113
                return this.height;
 
114
        }
 
115
 
 
116
        @Override
 
117
    public int getIconWidth() {
 
118
                return this.width;
 
119
        }
 
120
 
 
121
        protected boolean toPaintEnabled(Component c) {
 
122
                return c.isEnabled();
 
123
        }
 
124
 
 
125
        @Override
 
126
    public void paintIcon(Component c, Graphics g, int x, int y) {
 
127
                Graphics2D graphics = (Graphics2D) g.create();
 
128
 
 
129
                float strokeWidth = this.width / 7.0f;
 
130
                if (strokeWidth < 1.0f)
 
131
                        strokeWidth = 1.0f;
 
132
                Stroke stroke = new BasicStroke(strokeWidth, BasicStroke.CAP_BUTT,
 
133
                                BasicStroke.JOIN_MITER);
 
134
 
 
135
                graphics.setStroke(stroke);
 
136
 
 
137
                GeneralPath gp = new GeneralPath();
 
138
                switch (direction) {
 
139
                case SwingUtilities.SOUTH:
 
140
                        gp.moveTo(0, 2);
 
141
                        gp.lineTo((float) 0.5 * (width - 1), height - 2);
 
142
                        gp.lineTo(width - 1, 2);
 
143
                        break;
 
144
                case SwingUtilities.NORTH:
 
145
                        gp.moveTo(0, height - 2);
 
146
                        gp.lineTo((float) 0.5 * (width - 1), 2);
 
147
                        gp.lineTo(width - 1, height - 2);
 
148
                        break;
 
149
                case SwingUtilities.EAST:
 
150
                        gp.moveTo(2, 0);
 
151
                        gp.lineTo(width - 2, (float) 0.5 * (height - 1));
 
152
                        gp.lineTo(2, height - 1);
 
153
                        break;
 
154
                case SwingUtilities.WEST:
 
155
                        gp.moveTo(width - 2, 0);
 
156
                        gp.lineTo(2, (float) 0.5 * (height - 1));
 
157
                        gp.lineTo(width - 2, height - 1);
 
158
                        break;
 
159
                }
 
160
 
 
161
                graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
 
162
                                RenderingHints.VALUE_ANTIALIAS_ON);
 
163
                graphics.translate(x, y + 1);
 
164
                Color dropColor = this.toPaintEnabled(c) ? new Color(255, 255, 255, 196)
 
165
                                : new Color(255, 255, 255, 32);
 
166
                graphics.setColor(dropColor);
 
167
                graphics.draw(gp);
 
168
 
 
169
                graphics.translate(0, -1);
 
170
                Color arrowColor = this.toPaintEnabled(c) ? Color.black : Color.gray;
 
171
                graphics.setColor(arrowColor);
 
172
                if (this.width < 9) {
 
173
                        graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
 
174
                                        RenderingHints.VALUE_ANTIALIAS_OFF);
 
175
                        graphics.draw(gp);
 
176
                }
 
177
                graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
 
178
                                RenderingHints.VALUE_ANTIALIAS_ON);
 
179
                graphics.draw(gp);
 
180
                graphics.dispose();
 
181
        }
 
182
 
 
183
        public static class CommandButtonPopupIcon extends ArrowResizableIcon {
 
184
 
 
185
                public CommandButtonPopupIcon(int initialDim, int direction) {
 
186
                        super(initialDim, direction);
 
187
                }
 
188
 
 
189
                @Override
 
190
                protected boolean toPaintEnabled(Component c) {
 
191
                        JCommandButton jcb = (JCommandButton) c;
 
192
                        return jcb.isEnabled() && jcb.getPopupModel().isEnabled();
 
193
                }
 
194
        }
 
195
}