| 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.event.ActionEvent; |
| 19 | import java.awt.event.ActionListener; |
| 20 | import java.awt.event.ItemEvent; |
| 21 | import java.awt.event.ItemListener; |
| 22 | import java.beans.PropertyChangeEvent; |
| 23 | import java.beans.PropertyChangeListener; |
| 24 | |
| 25 | import javax.swing.JCheckBox; |
| 26 | |
| 27 | import net.sf.jomic.common.JomicTools; |
| 28 | import net.sf.jomic.common.Settings; |
| 29 | |
| 30 | import org.apache.commons.logging.Log; |
| 31 | import org.apache.commons.logging.LogFactory; |
| 32 | |
| 33 | /** |
| 34 | * JCheckBox connected to a boolean property in <code>Settings</code>. If the property changes its |
| 35 | * value, so does the check box - and the other way round. |
| 36 | * |
| 37 | * @see net.sf.jomic.common.Settings |
| 38 | * @author Thomas Aglassinger |
| 39 | */ |
| 40 | public class BooleanSettingCheckBox extends JCheckBox implements ActionListener, ItemListener, PropertyChangeListener |
| 41 | { |
| 42 | private String command; |
| 43 | private JomicTools jomicTools; |
| 44 | private Log logger; |
| 45 | private String propertyName; |
| 46 | private Settings settings; |
| 47 | |
| 48 | public BooleanSettingCheckBox(String newLabel, String newPropertyName, String newCommand) { |
| 49 | super(newLabel); |
| 50 | assert newLabel != null; |
| 51 | assert newPropertyName != null; |
| 52 | assert newCommand != null; |
| 53 | |
| 54 | jomicTools = JomicTools.instance(); |
| 55 | settings = Settings.instance(); |
| 56 | logger = LogFactory.getLog(BooleanSettingCheckBox.class); |
| 57 | propertyName = newPropertyName; |
| 58 | command = newCommand; |
| 59 | |
| 60 | boolean defaultState = settings.getBooleanProperty(propertyName); |
| 61 | |
| 62 | setSelected(defaultState); |
| 63 | setActionCommand(command); |
| 64 | settings.addPropertyChangeListener(propertyName, this); |
| 65 | addItemListener(this); |
| 66 | } |
| 67 | |
| 68 | public void actionPerformed(ActionEvent actionEvent) { |
| 69 | try { |
| 70 | assert actionEvent != null; |
| 71 | assert actionEvent.getSource() == this; |
| 72 | toggleSettingsValue(); |
| 73 | } catch (Throwable error) { |
| 74 | jomicTools.showError(null, "errors.cannotToggleProperty", new String[]{propertyName, command}, error); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public void dispose() { |
| 79 | removeItemListener(this); |
| 80 | settings.removePropertyChangeListener(propertyName, this); |
| 81 | } |
| 82 | |
| 83 | public void itemStateChanged(ItemEvent event) { |
| 84 | try { |
| 85 | assert event != null; |
| 86 | assert event.getSource() == this; |
| 87 | boolean menuItemSelected = event.getStateChange() == ItemEvent.SELECTED; |
| 88 | boolean currentValue = settings.getBooleanProperty(propertyName); |
| 89 | |
| 90 | if (menuItemSelected != currentValue) { |
| 91 | toggleSettingsValue(); |
| 92 | } |
| 93 | } catch (Throwable error) { |
| 94 | jomicTools.showError(null, "errors.cannotToggleProperty", new String[]{propertyName, command}, error); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | public void propertyChange(PropertyChangeEvent event) { |
| 99 | try { |
| 100 | assert event != null; |
| 101 | assert event.getSource() == settings; |
| 102 | boolean currentState = isSelected(); |
| 103 | boolean newState = Boolean.valueOf((String) event.getNewValue()).booleanValue(); |
| 104 | |
| 105 | if (currentState != newState) { |
| 106 | if (logger.isInfoEnabled()) { |
| 107 | logger.info("property " + propertyName + " changed from " + currentState + " to " + newState); |
| 108 | } |
| 109 | setSelected(newState); |
| 110 | } |
| 111 | } catch (Throwable error) { |
| 112 | jomicTools.showError(event, error); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | private void toggleSettingsValue() { |
| 117 | boolean newValue = !settings.getBooleanProperty(propertyName); |
| 118 | |
| 119 | settings.setBooleanProperty(propertyName, newValue); |
| 120 | } |
| 121 | } |