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

« back to all changes in this revision

Viewing changes to flamingo/src/main/java/org/pushingpixels/flamingo/internal/ui/common/popup/BasicColorSelectorPanelUI.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.ui.common.popup;
 
31
 
 
32
import java.awt.*;
 
33
 
 
34
import javax.swing.*;
 
35
import javax.swing.plaf.ComponentUI;
 
36
 
 
37
import org.pushingpixels.flamingo.internal.utils.FlamingoUtilities;
 
38
 
 
39
/**
 
40
 * Basic UI for color selector panel {@link JColorSelectorPanel}.
 
41
 * 
 
42
 * @author Kirill Grouchnikov
 
43
 */
 
44
public class BasicColorSelectorPanelUI extends ColorSelectorPanelUI {
 
45
        protected JColorSelectorPanel colorSelectorPanel;
 
46
 
 
47
        protected JLabel captionLabel;
 
48
 
 
49
        protected JPanel colorSelectorContainer;
 
50
 
 
51
        /*
 
52
         * (non-Javadoc)
 
53
         * 
 
54
         * @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
 
55
         */
 
56
        public static ComponentUI createUI(JComponent c) {
 
57
                return new BasicColorSelectorPanelUI();
 
58
        }
 
59
 
 
60
        /*
 
61
         * (non-Javadoc)
 
62
         * 
 
63
         * @see javax.swing.plaf.ComponentUI#installUI(javax.swing.JComponent)
 
64
         */
 
65
        @Override
 
66
        public void installUI(JComponent c) {
 
67
                this.colorSelectorPanel = (JColorSelectorPanel) c;
 
68
 
 
69
                installDefaults();
 
70
                installComponents();
 
71
                installListeners();
 
72
        }
 
73
 
 
74
        /*
 
75
         * (non-Javadoc)
 
76
         * 
 
77
         * @see javax.swing.plaf.ComponentUI#uninstallUI(javax.swing.JComponent)
 
78
         */
 
79
        @Override
 
80
        public void uninstallUI(JComponent c) {
 
81
                uninstallListeners();
 
82
                uninstallComponents();
 
83
                uninstallDefaults();
 
84
 
 
85
                c.setLayout(null);
 
86
 
 
87
                this.colorSelectorPanel = null;
 
88
        }
 
89
 
 
90
        /**
 
91
         * Installs listeners on the associated color selector panel.
 
92
         */
 
93
        protected void installListeners() {
 
94
        }
 
95
 
 
96
        /**
 
97
         * Uninstalls listeners from the associated color selector panel.
 
98
         */
 
99
        protected void uninstallListeners() {
 
100
        }
 
101
 
 
102
        /**
 
103
         * Installs defaults on the associated color selector panel.
 
104
         */
 
105
        protected void installDefaults() {
 
106
        }
 
107
 
 
108
        /**
 
109
         * Uninstalls defaults from the associated color selector panel.
 
110
         */
 
111
        protected void uninstallDefaults() {
 
112
        }
 
113
 
 
114
        /**
 
115
         * Installs subcomponents on the associated color selector panel.
 
116
         */
 
117
        protected void installComponents() {
 
118
                this.captionLabel = new JLabel(this.colorSelectorPanel.getCaption());
 
119
                this.captionLabel.setFont(this.captionLabel.getFont().deriveFont(
 
120
                                Font.BOLD));
 
121
                this.colorSelectorContainer = this.colorSelectorPanel
 
122
                                .getColorSelectionContainer();
 
123
 
 
124
                this.colorSelectorPanel.add(this.captionLabel);
 
125
                if (this.colorSelectorContainer != null) {
 
126
                        this.colorSelectorPanel.add(this.colorSelectorContainer);
 
127
                }
 
128
 
 
129
                this.colorSelectorPanel.setLayout(new PanelLayout());
 
130
        }
 
131
 
 
132
        /**
 
133
         * Uninstalls subcomponents from the associated color selector panel.
 
134
         */
 
135
        protected void uninstallComponents() {
 
136
                this.colorSelectorPanel.remove(this.captionLabel);
 
137
                if (this.colorSelectorContainer != null) {
 
138
                        this.colorSelectorPanel.remove(this.colorSelectorContainer);
 
139
                }
 
140
        }
 
141
 
 
142
        @Override
 
143
        public void paint(Graphics g, JComponent c) {
 
144
                Color bg = this.colorSelectorPanel.getBackground();
 
145
                g.setColor(bg);
 
146
                int w = c.getWidth();
 
147
                int h = c.getHeight();
 
148
                g.fillRect(0, 0, w, h);
 
149
 
 
150
                Rectangle captionBackground = this.captionLabel.getBounds();
 
151
                this.paintCaptionBackground(g, 0, 0, w, captionBackground.height + 2
 
152
                                * getLayoutGap());
 
153
 
 
154
                if (this.colorSelectorPanel.isLastPanel()) {
 
155
                        paintBottomDivider(g, 0, 0, w, h);
 
156
                }
 
157
        }
 
158
 
 
159
        protected void paintBottomDivider(Graphics g, int x, int y, int width,
 
160
                        int height) {
 
161
                g.setColor(FlamingoUtilities.getBorderColor());
 
162
                g.drawLine(x, y + height - 1, x + width - 1, y + height - 1);
 
163
        }
 
164
 
 
165
        protected void paintCaptionBackground(Graphics g, int x, int y, int width,
 
166
                        int height) {
 
167
                FlamingoUtilities.renderSurface(g, this.colorSelectorPanel,
 
168
                                new Rectangle(x, y, width, height), false, true, true);
 
169
        }
 
170
 
 
171
        /**
 
172
         * Returns the layout gap for button panel components.
 
173
         * 
 
174
         * @return The layout gap for button panel components.
 
175
         */
 
176
        protected int getLayoutGap() {
 
177
                return 4;
 
178
        }
 
179
 
 
180
        protected class PanelLayout implements LayoutManager {
 
181
                @Override
 
182
                public void addLayoutComponent(String name, Component comp) {
 
183
                }
 
184
 
 
185
                @Override
 
186
                public void removeLayoutComponent(Component comp) {
 
187
                }
 
188
 
 
189
                @Override
 
190
                public Dimension minimumLayoutSize(Container parent) {
 
191
                        return new Dimension(20, 20);
 
192
                }
 
193
 
 
194
                @Override
 
195
                public Dimension preferredLayoutSize(Container parent) {
 
196
                        int layoutGap = getLayoutGap();
 
197
 
 
198
                        Dimension labelPrefSize = captionLabel.getPreferredSize();
 
199
                        Dimension contPrefSize = colorSelectorContainer.getPreferredSize();
 
200
 
 
201
                        return new Dimension(Math.max(labelPrefSize.width,
 
202
                                        contPrefSize.width), 2 * layoutGap + labelPrefSize.height
 
203
                                        + contPrefSize.height
 
204
                                        + (colorSelectorPanel.isLastPanel() ? 1 : 0));
 
205
                }
 
206
 
 
207
                @Override
 
208
                public void layoutContainer(Container parent) {
 
209
                        int layoutGap = getLayoutGap();
 
210
 
 
211
                        Dimension labelPrefSize = captionLabel.getPreferredSize();
 
212
                        int labelWidth = labelPrefSize.width;
 
213
                        int labelHeight = labelPrefSize.height;
 
214
                        int y = layoutGap;
 
215
                        if (captionLabel.getComponentOrientation().isLeftToRight()) {
 
216
                                captionLabel.setBounds(layoutGap, y, labelWidth, labelHeight);
 
217
                        } else {
 
218
                                captionLabel.setBounds(parent.getWidth() - layoutGap
 
219
                                                - labelWidth, y, labelWidth, labelHeight);
 
220
                        }
 
221
                        y += labelHeight + layoutGap;
 
222
 
 
223
                        colorSelectorContainer.setBounds(0, y, parent.getWidth(), parent
 
224
                                        .getHeight()
 
225
                                        - y - (colorSelectorPanel.isLastPanel() ? 1 : 0));
 
226
 
 
227
                }
 
228
        }
 
229
}