EMMA Coverage Report (generated Sat Oct 08 11:41:37 CEST 2011)
[all classes][net.sf.jomic.ui]

COVERAGE SUMMARY FOR SOURCE FILE [BooleanSettingCheckBox.java]

nameclass, %method, %block, %line, %
BooleanSettingCheckBox.java100% (1/1)29%  (2/7)24%  (68/280)28%  (14.3/51)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BooleanSettingCheckBox100% (1/1)29%  (2/7)24%  (68/280)28%  (14.3/51)
actionPerformed (ActionEvent): void 0%   (0/1)0%   (0/41)0%   (0/7)
dispose (): void 0%   (0/1)0%   (0/10)0%   (0/3)
itemStateChanged (ItemEvent): void 0%   (0/1)0%   (0/58)0%   (0/10)
propertyChange (PropertyChangeEvent): void 0%   (0/1)0%   (0/66)0%   (0/12)
toggleSettingsValue (): void 0%   (0/1)0%   (0/17)0%   (0/3)
BooleanSettingCheckBox (String, String, String): void 100% (1/1)77%  (56/73)90%  (13.5/15)
<static initializer> 100% (1/1)80%  (12/15)80%  (0.8/1)

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/>.
16package net.sf.jomic.ui;
17 
18import java.awt.event.ActionEvent;
19import java.awt.event.ActionListener;
20import java.awt.event.ItemEvent;
21import java.awt.event.ItemListener;
22import java.beans.PropertyChangeEvent;
23import java.beans.PropertyChangeListener;
24 
25import javax.swing.JCheckBox;
26 
27import net.sf.jomic.common.JomicTools;
28import net.sf.jomic.common.Settings;
29 
30import org.apache.commons.logging.Log;
31import 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 */
40public 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}

[all classes][net.sf.jomic.ui]
EMMA 2.0.4217 (C) Vladimir Roubtsov