| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.ui; |
| 17 | |
| 18 | import java.awt.Color; |
| 19 | import java.awt.event.ActionEvent; |
| 20 | import java.awt.event.ActionListener; |
| 21 | import java.awt.image.BufferedImage; |
| 22 | |
| 23 | import javax.swing.ImageIcon; |
| 24 | import javax.swing.JButton; |
| 25 | import javax.swing.JColorChooser; |
| 26 | |
| 27 | import net.sf.jomic.tools.ImageTools; |
| 28 | |
| 29 | import org.apache.commons.logging.Log; |
| 30 | import org.apache.commons.logging.LogFactory; |
| 31 | |
| 32 | /** |
| 33 | * Button to show a color, and open a JColorChooser to change it once clicked. |
| 34 | * |
| 35 | * @author Thomas Aglassinger |
| 36 | */ |
| 37 | public class ColorButton extends JButton implements ActionListener |
| 38 | { |
| 39 | private static final int COLOR_BOX_SIZE = 32; |
| 40 | private static final String COMMAND_CHOOSE = "choose"; |
| 41 | private static Log logger = LogFactory.getLog(ColorButton.class); |
| 42 | private Color color; |
| 43 | private ImageTools imageTools; |
| 44 | |
| 45 | public ColorButton(Color newColor) { |
| 46 | imageTools = ImageTools.instance(); |
| 47 | setActionCommand(COMMAND_CHOOSE); |
| 48 | addActionListener(this); |
| 49 | setColor(newColor); |
| 50 | } |
| 51 | |
| 52 | public void setColor(Color newColor) { |
| 53 | BufferedImage image = imageTools.createColorBox(COLOR_BOX_SIZE, COLOR_BOX_SIZE, newColor); |
| 54 | |
| 55 | setIcon(new ImageIcon(image)); |
| 56 | color = newColor; |
| 57 | } |
| 58 | |
| 59 | public void actionPerformed(ActionEvent event) { |
| 60 | assert event != null; |
| 61 | String command = event.getActionCommand(); |
| 62 | |
| 63 | if (command.equals(COMMAND_CHOOSE)) { |
| 64 | Color newColor = JColorChooser.showDialog(this, "Choose border color", color); |
| 65 | |
| 66 | if (newColor != null) { |
| 67 | setColor(newColor); |
| 68 | } |
| 69 | } else { |
| 70 | logger.warn("unknown command: " + command); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | public void dispose() { |
| 75 | removeActionListener(this); |
| 76 | } |
| 77 | } |