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.IntegerConfigOption;
032    import org.dllearner.gui.Config;
033    
034    /**
035     * Panel for option Integer, defined in
036     * org.dllearner.core.config.IntegerConfigOption.
037     * 
038     * @author Tilo Hielscher
039     * @author Jens Lehmann
040     * 
041     */
042    public class WidgetPanelInteger extends AbstractWidgetPanel<Integer> implements ActionListener {
043    
044            private static final long serialVersionUID = -1802111225835164644L;
045    
046            private JButton setButton; // = new JButton("Set");
047            private JLabel problemLabel ; //= new JLabel();
048    
049            private Integer value;
050            private JTextField integerField; // = new JTextField(3);
051    
052            /**
053             * Provides a widget for integer options.
054             * @param config Central config handler.
055             * @param component The component of this option.
056             * @param configOption The option to configure.
057             */
058            public WidgetPanelInteger(Config config, Component component, IntegerConfigOption configOption) {
059                    super(config, component, configOption);
060            }
061    
062            @Override
063            public void actionPerformed(ActionEvent e) {
064                    if (e.getSource() == setButton) {
065    
066                            // TODO need better way for integer parsing than throwing an
067                            // exception
068                            try {
069                                    value = Integer.valueOf(integerField.getText());
070                                    fireValueChanged(value);
071                                    problemLabel.setText("");
072                            } catch(NumberFormatException e1) {
073                                    problemLabel.setText("Please enter a valid integer 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                    integerField = new JTextField(3);               
088                    if (value == null) {
089                            value = 0;
090                    } else {
091                            integerField.setText(value.toString());
092    //                      setEntry();
093                    }
094                    
095                    integerField.setText(value.toString());
096                    integerField.setToolTipText(configOption.getAllowedValuesDescription());
097                    setButton.addActionListener(this);
098                    add(integerField);
099                    add(setButton); 
100                    add(problemLabel);
101            }
102    }