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