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

« back to all changes in this revision

Viewing changes to substance/src/tools/java/tools/jitterbug/JColorComponent.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 tools.jitterbug;
 
31
 
 
32
import java.awt.*;
 
33
import java.awt.event.MouseAdapter;
 
34
import java.awt.event.MouseEvent;
 
35
 
 
36
import javax.swing.*;
 
37
 
 
38
import org.pushingpixels.lafwidget.utils.RenderingUtils;
 
39
 
 
40
public class JColorComponent extends JComponent {
 
41
        private JRadioButton radio;
 
42
 
 
43
        private Color selectedColor;
 
44
 
 
45
        private String name;
 
46
 
 
47
        private ColorVisualizer visualizer;
 
48
 
 
49
        private class ColorVisualizer extends JComponent {
 
50
                boolean isRollover;
 
51
 
 
52
                public ColorVisualizer() {
 
53
                        this.addMouseListener(new MouseAdapter() {
 
54
                                @Override
 
55
                                public void mouseClicked(MouseEvent e) {
 
56
                                        if (!isEnabled())
 
57
                                                return;
 
58
 
 
59
                                        SwingUtilities.invokeLater(new Runnable() {
 
60
                                                @Override
 
61
                                                public void run() {
 
62
                                                        radio.setSelected(true);
 
63
                                                        Color selected = JColorChooser.showDialog(
 
64
                                                                        ColorVisualizer.this, "Color chooser",
 
65
                                                                        selectedColor);
 
66
                                                        if (selected != null) {
 
67
                                                                Color old = selectedColor;
 
68
                                                                selectedColor = selected;
 
69
                                                                JColorComponent.this.firePropertyChange(
 
70
                                                                                "selectedColor", old, selectedColor);
 
71
                                                        }
 
72
                                                }
 
73
                                        });
 
74
                                }
 
75
 
 
76
                                @Override
 
77
                                public void mouseEntered(MouseEvent e) {
 
78
                                        if (!isEnabled())
 
79
                                                return;
 
80
 
 
81
                                        isRollover = true;
 
82
                                        repaint();
 
83
                                }
 
84
 
 
85
                                @Override
 
86
                                public void mouseExited(MouseEvent e) {
 
87
                                        if (!isEnabled())
 
88
                                                return;
 
89
 
 
90
                                        isRollover = false;
 
91
                                        repaint();
 
92
                                }
 
93
                        });
 
94
                        this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
 
95
                        this.setToolTipText("Open color chooser and change the color");
 
96
                        this.isRollover = false;
 
97
                }
 
98
 
 
99
                @Override
 
100
                protected void paintComponent(Graphics g) {
 
101
                        Graphics2D g2d = (Graphics2D) g.create();
 
102
                        RenderingUtils.installDesktopHints(g2d, this);
 
103
                        g2d.setFont(UIManager.getFont("Label.font"));
 
104
 
 
105
                        if (selectedColor != null) {
 
106
                                g2d.setColor(selectedColor);
 
107
                                g2d.fillRect(2, 2, 100, getHeight() - 4);
 
108
                                g2d.setStroke(new BasicStroke(isRollover ? 2.5f : 1.0f));
 
109
                                g2d.setColor(selectedColor.darker());
 
110
                                g2d.drawRect(2, 2, 99, getHeight() - 5);
 
111
 
 
112
                                g2d.setColor(Color.black);
 
113
                                g2d.drawString(getEncodedColor(), 108, (getHeight() + g2d
 
114
                                                .getFontMetrics().getHeight())
 
115
                                                / 2 - g2d.getFontMetrics().getDescent());
 
116
                        } else {
 
117
                                g2d.setColor(isEnabled() ? Color.gray : Color.lightGray);
 
118
                                g2d.drawString("click to choose", 5, (getHeight() + g2d
 
119
                                                .getFontMetrics().getHeight())
 
120
                                                / 2 - g2d.getFontMetrics().getDescent());
 
121
                        }
 
122
 
 
123
                        g2d.dispose();
 
124
                }
 
125
 
 
126
                @Override
 
127
                public Dimension getPreferredSize() {
 
128
                        return new Dimension(200, 25);
 
129
                }
 
130
        }
 
131
 
 
132
        public JColorComponent(String name, Color color) {
 
133
                this.radio = new JRadioButton(name);
 
134
                this.radio.setFocusable(false);
 
135
                this.selectedColor = color;
 
136
                this.visualizer = new ColorVisualizer();
 
137
                this.setLayout(new ColorComponentLayout());
 
138
 
 
139
                this.add(this.radio);
 
140
                this.add(this.visualizer);
 
141
        }
 
142
 
 
143
        private class ColorComponentLayout implements LayoutManager {
 
144
                @Override
 
145
                public void addLayoutComponent(String name, Component comp) {
 
146
                }
 
147
 
 
148
                @Override
 
149
                public void layoutContainer(Container parent) {
 
150
                        JColorComponent cc = (JColorComponent) parent;
 
151
                        int width = cc.getWidth();
 
152
                        int height = cc.getHeight();
 
153
 
 
154
                        ColorVisualizer cv = cc.visualizer;
 
155
                        Dimension cvPref = cv.getPreferredSize();
 
156
                        cv.setBounds(width - cvPref.width, 0, cvPref.width, height);
 
157
                        cc.radio.setBounds(0, 0, width - cvPref.width, height);
 
158
                }
 
159
 
 
160
                @Override
 
161
                public Dimension minimumLayoutSize(Container parent) {
 
162
                        return preferredLayoutSize(parent);
 
163
                }
 
164
 
 
165
                @Override
 
166
                public Dimension preferredLayoutSize(Container parent) {
 
167
                        JColorComponent cc = (JColorComponent) parent;
 
168
                        ColorVisualizer cv = cc.visualizer;
 
169
                        Dimension cvPref = cv.getPreferredSize();
 
170
                        return new Dimension(100 + cvPref.width, cvPref.height);
 
171
                }
 
172
 
 
173
                @Override
 
174
                public void removeLayoutComponent(Component comp) {
 
175
                }
 
176
        }
 
177
 
 
178
        public String getEncodedColor() {
 
179
                return "#" + encodeColorComponent(selectedColor.getRed())
 
180
                                + encodeColorComponent(selectedColor.getGreen())
 
181
                                + encodeColorComponent(selectedColor.getBlue());
 
182
        }
 
183
 
 
184
        private static String encodeColorComponent(int colorComp) {
 
185
                String hex = "0123456789ABCDEF";
 
186
                return "" + hex.charAt(colorComp / 16) + hex.charAt(colorComp % 16);
 
187
        }
 
188
 
 
189
        public JRadioButton getRadio() {
 
190
                return radio;
 
191
        }
 
192
 
 
193
        public void setColor(Color color, boolean firePropertyChange) {
 
194
                Color old = this.selectedColor;
 
195
                this.selectedColor = color;
 
196
                this.repaint();
 
197
                if (firePropertyChange) {
 
198
                        this.firePropertyChange("selectedColor", old, selectedColor);
 
199
                }
 
200
        }
 
201
 
 
202
        public Color getColor() {
 
203
                return this.selectedColor;
 
204
        }
 
205
 
 
206
        public boolean isDefined() {
 
207
                return (this.selectedColor != null);
 
208
        }
 
209
}