001    /*
002     * $Id: BooleanOption.java,v 1.7 2012/02/19 17:35:49 davep Exp $
003     *
004     * This file is part of McIDAS-V
005     *
006     * Copyright 2007-2012
007     * Space Science and Engineering Center (SSEC)
008     * University of Wisconsin - Madison
009     * 1225 W. Dayton Street, Madison, WI 53706, USA
010     * https://www.ssec.wisc.edu/mcidas
011     * 
012     * All Rights Reserved
013     * 
014     * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
015     * some McIDAS-V source code is based on IDV and VisAD source code.  
016     * 
017     * McIDAS-V is free software; you can redistribute it and/or modify
018     * it under the terms of the GNU Lesser Public License as published by
019     * the Free Software Foundation; either version 3 of the License, or
020     * (at your option) any later version.
021     * 
022     * McIDAS-V is distributed in the hope that it will be useful,
023     * but WITHOUT ANY WARRANTY; without even the implied warranty of
024     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
025     * GNU Lesser Public License for more details.
026     * 
027     * You should have received a copy of the GNU Lesser Public License
028     * along with this program.  If not, see http://www.gnu.org/licenses.
029     */
030    
031    package edu.wisc.ssec.mcidasv.startupmanager.options;
032    
033    import java.awt.event.ActionEvent;
034    import java.awt.event.ActionListener;
035    
036    import javax.swing.JCheckBox;
037    import javax.swing.JComponent;
038    
039    public class BooleanOption extends AbstractOption {
040        private String value = "0";
041    
042        public BooleanOption(final String id, final String label, 
043            final String defaultValue, 
044            final OptionMaster.OptionPlatform optionPlatform,
045            final OptionMaster.Visibility optionVisibility) 
046        {
047            super(id, label, OptionMaster.Type.BOOLEAN, optionPlatform, optionVisibility);
048            setValue(defaultValue);
049        }
050    
051        public JComponent getComponent() {
052            final JCheckBox cb = new JCheckBox();
053            cb.addActionListener(new ActionListener() {
054                public void actionPerformed(final ActionEvent e) {
055                    setValue(cb.isSelected() ? "1" : "0");
056                }
057            });
058            boolean booleanValue = false;
059            if (value.equals("1"))
060                booleanValue = true;
061            cb.setSelected(booleanValue);
062            if (!onValidPlatform()) {
063                cb.setEnabled(false);
064            }
065            return cb;
066        }
067    
068        public String getValue() {
069            return value;
070        }
071    
072        public void setValue(final String newValue) {
073            if (newValue.equals("0") || newValue.equals("1"))
074                value = newValue;
075        }
076    
077        public String toString() {
078            return String.format("[BooleanOption@%x: optionId=%s, value=%s]", 
079                hashCode(), getOptionId(), getValue());
080        }
081    }