EMMA Coverage Report (generated Sun Apr 20 22:38:01 CEST 2008)
[all classes][net.sf.jomic.ui]

COVERAGE SUMMARY FOR SOURCE FILE [BooleanSettingMenuItem.java]

nameclass, %method, %block, %line, %
BooleanSettingMenuItem.java100% (1/1)86%  (6/7)68%  (202/299)76%  (43.9/58)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BooleanSettingMenuItem100% (1/1)86%  (6/7)68%  (202/299)76%  (43.9/58)
dispose (): void 0%   (0/1)0%   (0/10)0%   (0/3)
itemStateChanged (ItemEvent): void 100% (1/1)40%  (27/68)54%  (6/11)
BooleanSettingMenuItem (): void 100% (1/1)75%  (15/20)95%  (4.8/5)
BooleanSettingMenuItem (String, String, String, ActionListener): void 100% (1/1)77%  (53/69)87%  (13.1/15)
propertyChange (PropertyChangeEvent): void 100% (1/1)79%  (52/66)76%  (9.1/12)
<static initializer> 100% (1/1)80%  (12/15)80%  (0.8/1)
BooleanSettingMenuItem (String, String): void 100% (1/1)84%  (43/51)92%  (10.1/11)

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.Action;
26import javax.swing.JCheckBoxMenuItem;
27 
28import net.sf.jomic.common.JomicTools;
29import net.sf.jomic.common.Settings;
30import net.sf.wraplog.Logger;
31 
32/**
33 *  JCheckBoxMenuItem to change a setting of type <code>boolean</code>, and keep in sync with it. It
34 *  attaches a PropertyChangeListener that updates the item state according to the current value of
35 *  the property. Likewise, if the menu item is selected, it sends an ActionEvent with a specified
36 *  command to a specified ActionListener indicating that the property needs to be updated. It does
37 *  not however update the property itself - this has to be done by (the centralized) delegate
38 *  ActionListener.
39 *
40 * @see       net.sf.jomic.common.Settings
41 * @author    Thomas Aglassinger
42 */
43public class BooleanSettingMenuItem extends JCheckBoxMenuItem implements ItemListener, PropertyChangeListener
44{
45    private String command;
46    private ActionListener delegate;
47    private JomicTools jomicTools;
48    private Logger logger;
49    private String propertyName;
50    private Settings settings;
51 
52    /**
53     * @param  newLabel         Description of the parameter
54     * @param  newPropertyName  Description of the parameter
55     */
56    public BooleanSettingMenuItem(String newLabel, String newPropertyName) {
57        this();
58        assert newLabel != null;
59        assert newPropertyName != null;
60        propertyName = newPropertyName;
61 
62        Action toggleAction = new ToggleBooleanSettingAction(newLabel, propertyName);
63 
64        setAction(toggleAction);
65 
66        boolean defaultState = settings.getBooleanProperty(propertyName);
67 
68        setState(defaultState);
69        setActionCommand(command);
70        settings.addPropertyChangeListener(propertyName, this);
71    }
72 
73    /**
74     *  Create a new BooleanSettingMenuItem.
75     *
76     * @param  newLabel         the label to use for the menu item (already localized)
77     * @param  newPropertyName  the name of the propery in Settings the menu item should synchronize
78     *      with
79     * @param  newCommand       the action command the menu item should send to toggle the state of
80     *      the property
81     * @param  newDelegate      the target ActionListener where to send newCommand to
82     */
83    public BooleanSettingMenuItem(
84            String newLabel, String newPropertyName, String newCommand, ActionListener newDelegate) {
85        this();
86        assert newLabel != null;
87        assert newPropertyName != null;
88        assert newCommand != null;
89        assert newDelegate != null;
90 
91        setText(newLabel);
92        propertyName = newPropertyName;
93        command = newCommand;
94        delegate = newDelegate;
95 
96        boolean defaultState = settings.getBooleanProperty(propertyName);
97 
98        setState(defaultState);
99        setActionCommand(command);
100        settings.addPropertyChangeListener(propertyName, this);
101        addItemListener(this);
102    }
103 
104    private BooleanSettingMenuItem() {
105        super();
106        jomicTools = JomicTools.instance();
107        settings = Settings.instance();
108        logger = Logger.getLogger(BooleanSettingMenuItem.class);
109    }
110 
111    public void dispose() {
112        removeItemListener(this);
113        settings.removePropertyChangeListener(propertyName, this);
114    }
115 
116    public void itemStateChanged(ItemEvent event) {
117        try {
118            assert event != null;
119            assert event.getSource() == this;
120            boolean menuItemSelected = event.getStateChange() == ItemEvent.SELECTED;
121            boolean currentValue = settings.getBooleanProperty(propertyName);
122 
123            if (menuItemSelected != currentValue) {
124                ActionEvent actionEvent = new ActionEvent(this, 0, command);
125 
126                delegate.actionPerformed(actionEvent);
127            }
128        } catch (Throwable error) {
129            jomicTools.showError(null, "errors.cannotToggleProperty", new String[]{propertyName, command}, error);
130        }
131    }
132 
133    public void propertyChange(PropertyChangeEvent event) {
134        try {
135            assert event != null;
136            assert event.getSource() == settings;
137            boolean currentState = getState();
138            boolean newState = Boolean.valueOf((String) event.getNewValue()).booleanValue();
139 
140            if (logger.isInfoEnabled()) {
141                logger.info("property " + propertyName + " changed from " + currentState + " to " + newState);
142            }
143            if (currentState != newState) {
144                setState(newState);
145            }
146        } catch (Throwable error) {
147            jomicTools.showError(event, error);
148        }
149    }
150}

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