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 javax.swing.JLabel;
023 import javax.swing.JPanel;
024
025 import org.dllearner.core.Component;
026 import org.dllearner.core.options.ConfigEntry;
027 import org.dllearner.core.options.ConfigOption;
028 import org.dllearner.core.options.InvalidConfigOptionValueException;
029 import org.dllearner.gui.Config;
030
031 /**
032 * Abstract superclass of all widgets. Each widget has an associated component and configuration option,
033 * for which it allows modification by the user.
034 *
035 * @author Jens Lehmann
036 * @param <T> The type of the value, which is configured by this
037 * widget, e.g. String, Integer etc.
038 */
039 public abstract class AbstractWidgetPanel<T> extends JPanel {
040
041 /**
042 * The configuration option configured by this widget.
043 */
044 protected ConfigOption<T> configOption;
045
046 /**
047 * The central configuration handler.
048 */
049 protected Config config;
050
051 /**
052 * The component of the configured option.
053 */
054 protected Component component;
055
056 /**
057 * Constructs a widget.
058 * @param config The status of all components and options (which may be updated by this widget).
059 * @param component The component potentially changed by this widget.
060 * @param optionOption The config option of the specified component, which is potentially changed by this widget.
061 */
062 public AbstractWidgetPanel(Config config, Component component, ConfigOption<T> optionOption) {
063 this.config = config;
064 this.component = component;
065 this.configOption = optionOption;
066
067 if(configOption == null || component == null || config == null) {
068 System.out.println("| " + component + ", " + configOption + ", " + config + " |");
069 throw new Error("Parameters must not be null.");
070 }
071
072 buildWidgetPanel();
073 }
074
075 /**
076 * Convenience method returning a JLabel with the name of
077 * the option and its description as tooltip.
078 * @return The described JLabel.
079 */
080 protected JLabel getLabel() {
081 JLabel nameLabel = new JLabel(configOption.getName());
082 nameLabel.setToolTipText(configOption.getDescription());
083 return nameLabel;
084 }
085
086 /**
087 * Subclasses should call this method if a configuration option
088 * has changed. It redirects the event to the central configuration
089 * handler, which in turn may update the gui (enabling/disabling
090 * tabs, setting status messages etc.)
091 * @param value The new value for the configuration option
092 * configured by this widget.
093 */
094 public void fireValueChanged(T value) {
095 ConfigEntry<T> entry = null;
096 try {
097 entry = new ConfigEntry<T>(configOption, value);
098 } catch (InvalidConfigOptionValueException e) {
099 // TODO display a message on the status bar (where the init
100 // has been before)
101 e.printStackTrace();
102 }
103 // notify config that a value has changed -> it decides what to do
104 config.applyConfigEntry(component, entry);
105 }
106
107 /**
108 * Subclasses should use this method to build the graphical representation of the widgets.
109 */
110 public abstract void buildWidgetPanel();
111
112 }