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

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