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.event.ActionEvent;
023    import java.awt.event.ActionListener;
024    
025    import javax.swing.JButton;
026    import javax.swing.JComboBox;
027    import javax.swing.JTextField;
028    
029    import org.dllearner.core.Component;
030    import org.dllearner.core.options.StringConfigOption;
031    import org.dllearner.gui.Config;
032    
033    /**
034     * Panel for option String, defined in
035     * {@link org.dllearner.core.options.StringConfigOption}.
036     * 
037     * @author Jens Lehmann
038     * @author Tilo Hielscher
039     * 
040     */
041    public class WidgetPanelString extends AbstractWidgetPanel<String> implements ActionListener {
042    
043            private static final long serialVersionUID = -2169739820989891226L;
044    
045            private JButton setButton;
046    
047            private String value;
048            private JTextField stringField;
049            private JComboBox comboBox;
050    
051            /**
052             * Provides a widget for string options.
053             * @param config Central config handler.
054             * @param component The component of this option.
055             * @param configOption The option to configure.
056             */
057            public WidgetPanelString(Config config, Component component, StringConfigOption configOption) {
058                    super(config, component, configOption);
059            }
060    
061            @Override
062            public void actionPerformed(ActionEvent e) {
063                    if (e.getSource() == setButton) {
064                            // fire value changed event
065                            value = stringField.getText();
066                            fireValueChanged(value);
067                    } else if(e.getSource() == comboBox) {
068                            value = (String) comboBox.getSelectedItem();
069                            fireValueChanged(value);
070                    }
071            }
072    
073            @Override
074            public void buildWidgetPanel() {
075                    add(getLabel());
076    
077                    // get current value of this option for the given component
078                    value = config.getConfigOptionValue(component, configOption);
079                    // default values can be null, so we interpret this as empty string
080                    if (value == null) {
081                            value = "";
082                    }
083    
084                    StringConfigOption option = (StringConfigOption) configOption; 
085                    if(option.getAllowedValues().size() == 0) {
086                    
087                            // text field for strings
088                            stringField = new JTextField(35);
089                            stringField.setText(value);
090                            stringField.setToolTipText(configOption.getAllowedValuesDescription());
091            
092                            // set button (value is only updated when this button is pressed =>
093                            // would better without set)
094                            setButton = new JButton("Set");
095                            setButton.addActionListener(this);
096            
097                            add(stringField);
098                            add(setButton);
099                    
100                    // if there is a fixed set of strings available as options, we
101                    // only offer those
102                    } else {
103                            comboBox = new JComboBox(option.getAllowedValues().toArray());
104                            comboBox.setSelectedItem(option.getDefaultValue());
105                            comboBox.addActionListener(this);
106                            add(comboBox);
107                    }
108            }
109    
110    }