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 import java.net.MalformedURLException;
025 import java.net.URL;
026
027 import javax.swing.JButton;
028 import javax.swing.JFileChooser;
029 import javax.swing.JLabel;
030 import javax.swing.JTextField;
031
032 import org.dllearner.core.Component;
033 import org.dllearner.core.options.URLConfigOption;
034 import org.dllearner.gui.Config;
035 import org.dllearner.gui.ExampleFileChooser;
036 import org.dllearner.kb.OWLFile;
037
038 /**
039 * Widget panel for URLs.
040 *
041 * @author Jens Lehmann
042 *
043 */
044 public class WidgetPanelURL extends AbstractWidgetPanel<URL> implements ActionListener {
045
046 private static final long serialVersionUID = -2169739820989891226L;
047
048 private JButton setButton;
049 private JButton chooseLocalButton;
050
051 private URL value;
052 private JTextField stringField;
053
054 /**
055 * Provides a widget for URL options.
056 *
057 * @param config
058 * Central config handler.
059 * @param component
060 * The component of this option.
061 * @param configOption
062 * The option to configure.
063 */
064 public WidgetPanelURL(Config config, Component component, URLConfigOption configOption) {
065 super(config, component, configOption);
066 }
067
068 @Override
069 public void actionPerformed(ActionEvent e) {
070 if (e.getSource() == chooseLocalButton) {
071 JFileChooser fc;
072 if (component instanceof OWLFile) {
073 fc = new ExampleFileChooser("owl");
074 } else {
075 fc = new ExampleFileChooser("kb");
076 }
077
078 int returnVal = fc.showOpenDialog(this);
079 if (returnVal == JFileChooser.APPROVE_OPTION) {
080 try {
081 // get file URI, add it into text field and fire a
082 // value changed event
083 value = fc.getSelectedFile().toURI().toURL();
084 stringField.setText(value.toString());
085 fireValueChanged(value);
086 } catch (MalformedURLException e1) {
087 // should never happen, because an actual file was selected
088 e1.printStackTrace();
089 }
090 }
091 } else if (e.getSource() == setButton) {
092 String stringValue = stringField.getText();
093 try {
094 value = new URL(stringValue);
095 } catch (MalformedURLException e1) {
096 // TODO add error handling
097 e1.printStackTrace();
098 }
099 fireValueChanged(value);
100 }
101 }
102
103 @Override
104 public void buildWidgetPanel() {
105 add(getLabel());
106
107 // get current value of this option for the given component
108 value = config.getConfigOptionValue(component, configOption);
109
110 // text field for strings
111 stringField = new JTextField(35);
112 if (value != null) {
113 stringField.setText(value.toString());
114 }
115 stringField.setToolTipText(configOption.getAllowedValuesDescription());
116
117 // set button (value is only updated when this button is pressed =>
118 // would better without set)
119 setButton = new JButton("Set");
120 setButton.addActionListener(this);
121
122 add(stringField);
123 add(setButton);
124
125 // if the URL can refer to a file, we add the possibility to
126 // choose a local file
127 if (((URLConfigOption) configOption).refersToFile()) {
128 chooseLocalButton = new JButton("Choose Local File");
129 chooseLocalButton.addActionListener(this);
130 add(new JLabel(" or "));
131 add(chooseLocalButton);
132 }
133
134 }
135
136 }