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 | import java.beans.PropertyChangeEvent; |
23 | import java.beans.PropertyChangeListener; |
24 | |
25 | import javax.swing.ImageIcon; |
26 | import javax.swing.JColorChooser; |
27 | import javax.swing.JDialog; |
28 | import javax.swing.JMenuItem; |
29 | import javax.swing.colorchooser.ColorSelectionModel; |
30 | import javax.swing.event.ChangeEvent; |
31 | import javax.swing.event.ChangeListener; |
32 | |
33 | import net.sf.jomic.common.JomicTools; |
34 | import net.sf.jomic.common.Settings; |
35 | import net.sf.jomic.tools.ImageTools; |
36 | import net.sf.jomic.tools.StringTools; |
37 | |
38 | import org.apache.commons.logging.Log; |
39 | import org.apache.commons.logging.LogFactory; |
40 | |
41 | /** |
42 | * JMenuItem to change a setting of type <code>Color</code>. The current color is shown as icon |
43 | * next to the menu label. When selecting the item, a JColorChooser allows to change the value. |
44 | * |
45 | * @see net.sf.jomic.common.Settings |
46 | * @see javax.swing.JColorChooser |
47 | * @author Thomas Aglassinger |
48 | */ |
49 | public class ColorSettingMenuItem extends JMenuItem implements ActionListener, ChangeListener, PropertyChangeListener |
50 | { |
51 | private static final int COLOR_ICON_INSET = 3; |
52 | private static final int MIN_COLOR_ICON_SIZE = 4; |
53 | private static Log logger = LogFactory.getLog(ColorSettingMenuItem.class); |
54 | private CancelActionListener cancelListener; |
55 | private JColorChooser chooser; |
56 | private ColorSelectionModel colorSelectionModel; |
57 | private JDialog dialog; |
58 | private ImageTools imageTools; |
59 | private JomicTools jomicTools; |
60 | private String label; |
61 | private String propertyName; |
62 | private Settings settings; |
63 | |
64 | /** |
65 | * Create a new ColorSettingMenuItem labeled <code>newLabel</code> with its value obtained from |
66 | * property <code>newPropertyName</code>. |
67 | * |
68 | * @see Settings |
69 | * @param newLabel label for the menu item |
70 | * @param newPropertyName name of the property that stores the value of the color |
71 | */ |
72 | public ColorSettingMenuItem(String newLabel, String newPropertyName) { |
73 | super(newLabel); |
74 | assert newLabel != null; |
75 | assert newPropertyName != null; |
76 | imageTools = ImageTools.instance(); |
77 | jomicTools = JomicTools.instance(); |
78 | settings = Settings.instance(); |
79 | label = newLabel; |
80 | propertyName = newPropertyName; |
81 | // initialize color icon |
82 | updateColorIcon(); |
83 | chooser = new JColorChooser(getColor()); |
84 | colorSelectionModel = chooser.getSelectionModel(); |
85 | colorSelectionModel.addChangeListener(this); |
86 | cancelListener = new CancelActionListener(); |
87 | dialog = JColorChooser.createDialog(null, label, false, chooser, null, cancelListener); |
88 | addActionListener(this); |
89 | settings.addPropertyChangeListener(propertyName, this); |
90 | } |
91 | |
92 | /** |
93 | * Set color property to <code>newColor</code>. |
94 | */ |
95 | public void setColor(Color newColor) { |
96 | settings.setColorProperty(propertyName, newColor); |
97 | } |
98 | |
99 | public Color getColor() { |
100 | return settings.getColorProperty(propertyName); |
101 | } |
102 | |
103 | /** |
104 | * Open JColorChooser to let user adjust the color. |
105 | * |
106 | * @see JColorChooser |
107 | */ |
108 | public void actionPerformed(ActionEvent event) { |
109 | try { |
110 | assert event != null; |
111 | Object source = event.getSource(); |
112 | |
113 | assert source == this; |
114 | |
115 | if (!dialog.isVisible()) { |
116 | cancelListener.setOldColor(getColor()); |
117 | } |
118 | dialog.setVisible(true); |
119 | } catch (Throwable error) { |
120 | jomicTools.showError(null, "errors.cannotShowDialogToChangeBackgroundColor", error); |
121 | } |
122 | } |
123 | |
124 | public void dispose() { |
125 | settings.removePropertyChangeListener(propertyName, this); |
126 | removeActionListener(this); |
127 | dialog.dispose(); |
128 | } |
129 | |
130 | public void propertyChange(PropertyChangeEvent event) { |
131 | try { |
132 | assert event != null; |
133 | assert event.getSource() == settings; |
134 | String oldColorText = (String) event.getOldValue(); |
135 | String newColorText = (String) event.getNewValue(); |
136 | |
137 | if (logger.isInfoEnabled()) { |
138 | logger.info("property " + propertyName + " changed from " + oldColorText + " to " + newColorText); |
139 | } |
140 | if ((newColorText != null) && !newColorText.equals(oldColorText)) { |
141 | updateColorIcon(); |
142 | } |
143 | } catch (Throwable error) { |
144 | jomicTools.showError(event, error); |
145 | } |
146 | } |
147 | |
148 | /** |
149 | * Update color property on change in JColorChooser. |
150 | */ |
151 | public void stateChanged(ChangeEvent event) { |
152 | assert event != null; |
153 | Color newColor = chooser.getColor(); |
154 | |
155 | setColor(newColor); |
156 | } |
157 | |
158 | private void updateColorIcon() { |
159 | int iconSize = Math.max(MIN_COLOR_ICON_SIZE, getPreferredSize().height - 2 * COLOR_ICON_INSET); |
160 | Color newColor = settings.getColorProperty(propertyName); |
161 | BufferedImage image = imageTools.createColorBox(iconSize, iconSize, newColor); |
162 | |
163 | if (logger.isInfoEnabled()) { |
164 | logger.info("set color to " + StringTools.instance().colorString(newColor)); |
165 | } |
166 | |
167 | setIcon(new ImageIcon(image)); |
168 | } |
169 | |
170 | /** |
171 | * Action listener called when JColorChooser is cancelled. |
172 | * |
173 | * @author Thomas Aglassinger |
174 | */ |
175 | private final class CancelActionListener implements ActionListener |
176 | { |
177 | private Color oldColor; |
178 | |
179 | private CancelActionListener() { |
180 | super(); |
181 | } |
182 | |
183 | /** |
184 | * Set the color to revert to when JColorChooser gets cancelled. |
185 | */ |
186 | private void setOldColor(Color newOldColor) { |
187 | assert newOldColor != null; |
188 | oldColor = newOldColor; |
189 | } |
190 | |
191 | /** |
192 | * Restore background color to value before JColorChooser was opened, and enable this |
193 | * JMenuItem again. |
194 | * |
195 | * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) |
196 | */ |
197 | public void actionPerformed(ActionEvent event) { |
198 | setColor(oldColor); |
199 | } |
200 | } |
201 | } |