/* * Copyright (c) 2008, 2009, 2010, 2011 Denis Tulskiy * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * version 3 along with this work. If not, see <http://www.gnu.org/licenses/>. */ package com.tulskiy.musique.gui.components; import com.tulskiy.musique.images.Images; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; /** * Author: Denis Tulskiy * Date: 12/23/10 */ public class ColorChooser extends JPanel { private Color color; private JPanel panel = new JPanel(); public ColorChooser() { this(null); } public ColorChooser(Color color) { super(new BorderLayout()); add(panel, BorderLayout.CENTER); JButton clear = new JButton(Images.loadIcon("clear.png")); clear.setMargin(new Insets(1, 1, 1, 1)); clear.setPreferredSize(new Dimension(25, -1)); clear.setFocusable(false); setPreferredSize(new Dimension(100, -1)); add(clear, BorderLayout.LINE_END); clear.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { setColor(null); } }); setColor(color); setPreferredSize(new Dimension(10, 25)); panel.setBorder(BorderFactory.createEtchedBorder()); final JComponent comp = this; addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { Color c = JColorChooser.showDialog(comp, "Choose Color", getColor()); if (c != null) setColor(c); } }); } public Color getColor() { return color; } public void setColor(Color color) { this.color = color; panel.setBackground(color); } }