001    /**
002     * Copyright (C) 2007-2008, Jens Lehmann
003     *
004     * This file is part of DL-Learner.
005     * 
006     * DL-Learner is free software; you can redistribute it and/or modify
007     * it under the terms of the GNU General Public License as published by
008     * the Free Software Foundation; either version 3 of the License, or
009     * (at your option) any later version.
010     *
011     * DL-Learner is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014     * GNU General Public License for more details.
015     *
016     * You should have received a copy of the GNU General Public License
017     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018     *
019     */
020    package org.dllearner.gui.widgets;
021    
022    import java.awt.Color;
023    import java.awt.event.ActionEvent;
024    import java.awt.event.ActionListener;
025    
026    import javax.swing.JButton;
027    import javax.swing.JLabel;
028    import javax.swing.JTextField;
029    
030    import org.dllearner.core.Component;
031    import org.dllearner.core.options.DoubleConfigOption;
032    import org.dllearner.gui.Config;
033    
034    /**
035     * Panel for option Double, defined in
036     * {@link org.dllearner.core.options.DoubleConfigOption}.
037     * 
038     * @author Tilo Hielscher
039     * 
040     */
041    public class WidgetPanelDouble extends AbstractWidgetPanel<Double> implements ActionListener {
042    
043            private static final long serialVersionUID = 5238903690721116289L;
044    
045            private JButton setButton = new JButton("Set");
046            private JLabel problemLabel;
047    
048    //      private Class<? extends Component> componentOption;
049    
050            private Double value;
051            private JTextField doubleField = new JTextField(5);
052    
053            /**
054             * Provides a widget for double options.
055             * @param config Central config handler.
056             * @param component The component of this option.
057             * @param configOption The option to configure.
058             */
059            public WidgetPanelDouble(Config config, Component component, DoubleConfigOption configOption) {
060                    super(config, component, configOption);
061            }
062    
063            @Override
064            public void actionPerformed(ActionEvent e) {
065                    if (e.getSource() == setButton) {
066                            // TODO need better way for double parsing than throwing an
067                            // exception
068                            try {
069                                    value = Double.valueOf(doubleField.getText());
070                                    fireValueChanged(value);
071                                    problemLabel.setText("");
072                            } catch(NumberFormatException e1) {
073                                    problemLabel.setText("Please enter a valid double value.");
074                            }                       
075                    }
076            }
077    
078            @Override
079            public void buildWidgetPanel() {
080                    add(getLabel());
081                    problemLabel = new JLabel();
082                    problemLabel.setForeground(Color.RED);          
083    
084                    value = config.getConfigOptionValue(component, configOption);
085                    
086                    setButton = new JButton("Set");
087                    doubleField = new JTextField(5);
088                    if (value == null) {
089                            value = 0.0;
090                    } else {
091                            doubleField.setText(value.toString());
092    //                      setEntry();
093                    }               
094                    
095                    doubleField.setText(value.toString());
096                    doubleField.setToolTipText(configOption.getAllowedValuesDescription());
097                    setButton.addActionListener(this);
098                    add(doubleField);
099                    add(setButton);         
100                    
101            }
102    }