~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/DoubleArrowResizableIcon.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.icon.ResizableIcon;
 
39
 
 
40
/**
 
41
 * Helper implementation of {@link ResizableIcon} that draws a double arrow.
 
42
 * 
 
43
 * @author Kirill Grouchnikov
 
44
 */
 
45
public class DoubleArrowResizableIcon implements ResizableIcon {
 
46
        /**
 
47
         * Initial dimension.
 
48
         */
 
49
        private Dimension initialDim;
 
50
 
 
51
        /**
 
52
         * The width of the rendered image.
 
53
         */
 
54
        protected int width;
 
55
 
 
56
        /**
 
57
         * The height of the rendered image.
 
58
         */
 
59
        protected int height;
 
60
 
 
61
        /**
 
62
         * Arrow direction. One of {@link SwingConstants#SOUTH},
 
63
         * {@link SwingConstants#NORTH}, {@link SwingConstants#EAST} or
 
64
         * {@link SwingConstants#WEST}.
 
65
         */
 
66
        protected int direction;
 
67
 
 
68
        /**
 
69
         * Creates a new double arrow resizable icon.
 
70
         * 
 
71
         * @param initialDim
 
72
         *            Initial icon dimension.
 
73
         * @param direction
 
74
         *            Arrow direction. Currently only {@link SwingConstants#SOUTH}
 
75
         *            is supported.
 
76
         */
 
77
        public DoubleArrowResizableIcon(Dimension initialDim, int direction) {
 
78
                this.initialDim = initialDim;
 
79
                this.width = initialDim.width;
 
80
                this.height = initialDim.height;
 
81
                this.direction = direction;
 
82
        }
 
83
 
 
84
        /**
 
85
         * Creates a new double arrow resizable icon.
 
86
         * 
 
87
         * @param initialDim
 
88
         *            Initial icon dimension.
 
89
         * @param direction
 
90
         *            Arrow direction. Currently only {@link SwingConstants#SOUTH}
 
91
         *            is supported.
 
92
         */
 
93
        public DoubleArrowResizableIcon(int initialDim, int direction) {
 
94
                this(new Dimension(initialDim, initialDim), direction);
 
95
        }
 
96
 
 
97
        public void revertToOriginalDimension() {
 
98
                this.width = initialDim.width;
 
99
                this.height = initialDim.height;
 
100
        }
 
101
 
 
102
        @Override
 
103
    public void setDimension(Dimension newDimension) {
 
104
                this.width = newDimension.width;
 
105
                this.height = newDimension.height;
 
106
        }
 
107
 
 
108
        @Override
 
109
    public int getIconHeight() {
 
110
                return this.height;
 
111
        }
 
112
 
 
113
        @Override
 
114
    public int getIconWidth() {
 
115
                return this.width;
 
116
        }
 
117
 
 
118
        protected boolean toPaintEnabled(Component c) {
 
119
                return c.isEnabled();
 
120
        }
 
121
 
 
122
        @Override
 
123
    public void paintIcon(Component c, Graphics g, int x, int y) {
 
124
                Graphics2D graphics = (Graphics2D) g.create();
 
125
                graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
 
126
                                RenderingHints.VALUE_ANTIALIAS_ON);
 
127
 
 
128
                Color arrowColor = this.toPaintEnabled(c) ? Color.black : Color.gray;
 
129
                graphics.setColor(arrowColor);
 
130
                Stroke stroke = new BasicStroke(this.width / 8.0f,
 
131
                                BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
 
132
 
 
133
                graphics.setStroke(stroke);
 
134
                graphics.translate(x, y);
 
135
                GeneralPath gp = new GeneralPath();
 
136
                int arrowHeight = height / 2;
 
137
                int arrowWidth = width / 2;
 
138
                int deltaH = (height + 2) / 3;
 
139
                int deltaW = (width + 2) / 3;
 
140
                switch (direction) {
 
141
                case SwingUtilities.NORTH:
 
142
                        gp.moveTo(0, height - 1);
 
143
                        gp.lineTo((float) 0.5 * (width - 1), height - 1 - arrowHeight);
 
144
                        gp.lineTo(width - 1, height - 1);
 
145
 
 
146
                        gp.moveTo(0, height - 1 - deltaH);
 
147
                        gp.lineTo((float) 0.5 * (width - 1), height - 1 - arrowHeight
 
148
                                        - deltaH);
 
149
                        gp.lineTo(width - 1, height - 1 - deltaH);
 
150
 
 
151
                        break;
 
152
 
 
153
                case SwingUtilities.SOUTH:
 
154
                        gp.moveTo(0, 0);
 
155
                        gp.lineTo((float) 0.5 * (width - 1), arrowHeight);
 
156
                        gp.lineTo(width - 1, 0);
 
157
 
 
158
                        gp.moveTo(0, deltaH);
 
159
                        gp.lineTo((float) 0.5 * (width - 1), arrowHeight + deltaH);
 
160
                        gp.lineTo(width - 1, deltaH);
 
161
 
 
162
                        break;
 
163
 
 
164
                case SwingUtilities.EAST:
 
165
                        gp.moveTo(0, 0);
 
166
                        gp.lineTo(arrowWidth, (float) 0.5 * (height - 1));
 
167
                        gp.lineTo(0, height - 1);
 
168
 
 
169
                        gp.moveTo(deltaW, 0);
 
170
                        gp.lineTo(arrowWidth + deltaW, (float) 0.5 * (height - 1));
 
171
                        gp.lineTo(deltaW, height - 1);
 
172
 
 
173
                        break;
 
174
 
 
175
                case SwingUtilities.WEST:
 
176
                        gp.moveTo(width - 1, 0);
 
177
                        gp.lineTo(width - 1 - arrowWidth, (float) 0.5 * (height - 1));
 
178
                        gp.lineTo(width - 1, height - 1);
 
179
 
 
180
                        gp.moveTo(width - 1 - deltaW, 0);
 
181
                        gp.lineTo(width - 1 - arrowWidth - deltaW, (float) 0.5
 
182
                                        * (height - 1));
 
183
                        gp.lineTo(width - 1 - deltaW, height - 1);
 
184
 
 
185
                        break;
 
186
 
 
187
                }
 
188
                graphics.draw(gp);
 
189
                graphics.dispose();
 
190
        }
 
191
}