2
* Copyright (c) 2005-2010 Flamingo Kirill Grouchnikov. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
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.
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.
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.
30
package org.pushingpixels.flamingo.internal.ui.common.popup;
35
import javax.swing.plaf.ComponentUI;
37
import org.pushingpixels.flamingo.internal.utils.FlamingoUtilities;
40
* Basic UI for color selector panel {@link JColorSelectorPanel}.
42
* @author Kirill Grouchnikov
44
public class BasicColorSelectorPanelUI extends ColorSelectorPanelUI {
45
protected JColorSelectorPanel colorSelectorPanel;
47
protected JLabel captionLabel;
49
protected JPanel colorSelectorContainer;
54
* @see javax.swing.plaf.ComponentUI#createUI(javax.swing.JComponent)
56
public static ComponentUI createUI(JComponent c) {
57
return new BasicColorSelectorPanelUI();
63
* @see javax.swing.plaf.ComponentUI#installUI(javax.swing.JComponent)
66
public void installUI(JComponent c) {
67
this.colorSelectorPanel = (JColorSelectorPanel) c;
77
* @see javax.swing.plaf.ComponentUI#uninstallUI(javax.swing.JComponent)
80
public void uninstallUI(JComponent c) {
82
uninstallComponents();
87
this.colorSelectorPanel = null;
91
* Installs listeners on the associated color selector panel.
93
protected void installListeners() {
97
* Uninstalls listeners from the associated color selector panel.
99
protected void uninstallListeners() {
103
* Installs defaults on the associated color selector panel.
105
protected void installDefaults() {
109
* Uninstalls defaults from the associated color selector panel.
111
protected void uninstallDefaults() {
115
* Installs subcomponents on the associated color selector panel.
117
protected void installComponents() {
118
this.captionLabel = new JLabel(this.colorSelectorPanel.getCaption());
119
this.captionLabel.setFont(this.captionLabel.getFont().deriveFont(
121
this.colorSelectorContainer = this.colorSelectorPanel
122
.getColorSelectionContainer();
124
this.colorSelectorPanel.add(this.captionLabel);
125
if (this.colorSelectorContainer != null) {
126
this.colorSelectorPanel.add(this.colorSelectorContainer);
129
this.colorSelectorPanel.setLayout(new PanelLayout());
133
* Uninstalls subcomponents from the associated color selector panel.
135
protected void uninstallComponents() {
136
this.colorSelectorPanel.remove(this.captionLabel);
137
if (this.colorSelectorContainer != null) {
138
this.colorSelectorPanel.remove(this.colorSelectorContainer);
143
public void paint(Graphics g, JComponent c) {
144
Color bg = this.colorSelectorPanel.getBackground();
146
int w = c.getWidth();
147
int h = c.getHeight();
148
g.fillRect(0, 0, w, h);
150
Rectangle captionBackground = this.captionLabel.getBounds();
151
this.paintCaptionBackground(g, 0, 0, w, captionBackground.height + 2
154
if (this.colorSelectorPanel.isLastPanel()) {
155
paintBottomDivider(g, 0, 0, w, h);
159
protected void paintBottomDivider(Graphics g, int x, int y, int width,
161
g.setColor(FlamingoUtilities.getBorderColor());
162
g.drawLine(x, y + height - 1, x + width - 1, y + height - 1);
165
protected void paintCaptionBackground(Graphics g, int x, int y, int width,
167
FlamingoUtilities.renderSurface(g, this.colorSelectorPanel,
168
new Rectangle(x, y, width, height), false, true, true);
172
* Returns the layout gap for button panel components.
174
* @return The layout gap for button panel components.
176
protected int getLayoutGap() {
180
protected class PanelLayout implements LayoutManager {
182
public void addLayoutComponent(String name, Component comp) {
186
public void removeLayoutComponent(Component comp) {
190
public Dimension minimumLayoutSize(Container parent) {
191
return new Dimension(20, 20);
195
public Dimension preferredLayoutSize(Container parent) {
196
int layoutGap = getLayoutGap();
198
Dimension labelPrefSize = captionLabel.getPreferredSize();
199
Dimension contPrefSize = colorSelectorContainer.getPreferredSize();
201
return new Dimension(Math.max(labelPrefSize.width,
202
contPrefSize.width), 2 * layoutGap + labelPrefSize.height
203
+ contPrefSize.height
204
+ (colorSelectorPanel.isLastPanel() ? 1 : 0));
208
public void layoutContainer(Container parent) {
209
int layoutGap = getLayoutGap();
211
Dimension labelPrefSize = captionLabel.getPreferredSize();
212
int labelWidth = labelPrefSize.width;
213
int labelHeight = labelPrefSize.height;
215
if (captionLabel.getComponentOrientation().isLeftToRight()) {
216
captionLabel.setBounds(layoutGap, y, labelWidth, labelHeight);
218
captionLabel.setBounds(parent.getWidth() - layoutGap
219
- labelWidth, y, labelWidth, labelHeight);
221
y += labelHeight + layoutGap;
223
colorSelectorContainer.setBounds(0, y, parent.getWidth(), parent
225
- y - (colorSelectorPanel.isLastPanel() ? 1 : 0));