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.BorderLayout;
023 import java.awt.Dimension;
024 import java.awt.GridBagConstraints;
025 import java.awt.GridBagLayout;
026 import java.awt.event.ActionEvent;
027 import java.awt.event.ActionListener;
028 import java.util.Iterator;
029 import java.util.LinkedList;
030 import java.util.List;
031
032 import javax.swing.DefaultListModel;
033 import javax.swing.JButton;
034 import javax.swing.JLabel;
035 import javax.swing.JList;
036 import javax.swing.JPanel;
037 import javax.swing.JScrollPane;
038 import javax.swing.JTextField;
039
040 import org.dllearner.core.Component;
041 import org.dllearner.core.options.StringTupleListConfigOption;
042 import org.dllearner.gui.Config;
043 import org.dllearner.utilities.datastructures.StringTuple;
044
045 /**
046 * Panel for option StringTupleList, defined in
047 * org.dllearner.core.config.StringTupleListConfigOption.
048 *
049 * @author Tilo Hielscher
050 */
051 public class WidgetPanelStringTupleList extends AbstractWidgetPanel<List<StringTuple>> implements
052 ActionListener {
053
054 private static final long serialVersionUID = 7832726987046601916L;
055
056 private GridBagLayout gridbag = new GridBagLayout();
057 private GridBagConstraints constraints = new GridBagConstraints();
058
059 private JPanel widgetPanel; // = new JPanel();
060 private JButton addButton; // = new JButton("add");
061 private JButton removeButton; // = new JButton("remove");
062 private JButton clearButton; // = new JButton("clear");
063 private JTextField stringFieldA; // = new JTextField(10);
064 private JTextField stringFieldB; // = new JTextField(10);
065 private List<StringTuple> exampleList; // = new LinkedList<StringTuple>();
066
067 private List<StringTuple> value; // = new LinkedList<StringTuple>();
068 private JList stringList; // = new JList();
069 private DefaultListModel listModel; // = new DefaultListModel();
070
071 private JButton setButton; // = new JButton("set");
072
073 /**
074 * Provides a widget for string tuple list options.
075 * @param config Central config handler.
076 * @param component The component of this option.
077 * @param configOption The option to configure.
078 */
079 public WidgetPanelStringTupleList(Config config, Component component,
080 StringTupleListConfigOption configOption) {
081 super(config, component, configOption);
082 }
083
084 @Override
085 public void actionPerformed(ActionEvent e) {
086 // add to list
087 if (e.getSource() == addButton
088 && !listModel.contains(stringFieldA.getText() + " --> " + stringFieldB.getText())
089 && !stringFieldA.getText().equalsIgnoreCase("")
090 && !stringFieldB.getText().equalsIgnoreCase("")) {
091 listModel.addElement(stringFieldA.getText() + " --> " + stringFieldB.getText());
092 exampleList.add(new StringTuple(stringFieldA.getText(), stringFieldB.getText()));
093 }
094 // remove selection
095 if (e.getSource() == removeButton) {
096 int[] selectedIndices = stringList.getSelectedIndices();
097 int count = 0;
098 // remove i.e. 2 and 4: after delete 2: 4 is now 3
099 for (int i : selectedIndices) {
100 listModel.remove(i - count);
101 exampleList.remove(i - count++);
102 }
103 }
104 // clear list
105 if (e.getSource() == clearButton) {
106 listModel.clear();
107 exampleList.clear();
108 }
109 // set entry
110 value = exampleList;
111 // setEntry();
112 fireValueChanged(value);
113 }
114
115 private void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx,
116 int wy) {
117 gbc.gridx = gx;
118 gbc.gridy = gy;
119 gbc.gridwidth = gw;
120 gbc.gridheight = gh;
121 gbc.weightx = wx;
122 gbc.weighty = wy;
123 }
124
125 @Override
126 public void buildWidgetPanel() {
127
128 gridbag = new GridBagLayout();
129 constraints = new GridBagConstraints();
130
131 widgetPanel = new JPanel();
132 addButton = new JButton("add");
133 removeButton = new JButton("remove");
134 clearButton = new JButton("clear");
135 stringFieldA = new JTextField(10);
136 stringFieldB = new JTextField(10);
137 exampleList = new LinkedList<StringTuple>();
138
139 stringList = new JList();
140 listModel = new DefaultListModel();
141
142 setButton = new JButton("set");
143
144 widgetPanel.setLayout(gridbag);
145 add(widgetPanel, BorderLayout.CENTER);
146 add(getLabel());
147
148 value = config.getConfigOptionValue(component, configOption);
149
150 if (value != null) {
151 // setEntry();
152 exampleList = value;
153 }
154
155 // fill list
156 if (value != null) {
157 for (Iterator<StringTuple> iterator = value.iterator(); iterator.hasNext();) {
158 StringTuple item = iterator.next();
159 listModel.addElement(item);
160 }
161 }
162
163 // stringFieldA
164 buildConstraints(constraints, 0, 1, 1, 1, 100, 100);
165 gridbag.setConstraints(stringFieldA, constraints);
166 widgetPanel.add(stringFieldA, constraints);
167 // arrow
168 JLabel arrowLabel = new JLabel(" --> ");
169 buildConstraints(constraints, 1, 1, 1, 1, 100, 100);
170 constraints.anchor = GridBagConstraints.WEST;
171 gridbag.setConstraints(arrowLabel, constraints);
172 widgetPanel.add(arrowLabel, constraints);
173 // stringFieldB
174 buildConstraints(constraints, 2, 1, 1, 1, 100, 100);
175 gridbag.setConstraints(stringFieldB, constraints);
176 widgetPanel.add(stringFieldB, constraints);
177 // addButton
178 buildConstraints(constraints, 3, 1, 1, 1, 100, 100);
179 gridbag.setConstraints(addButton, constraints);
180 widgetPanel.add(addButton, constraints);
181 // list
182 stringList.setModel(listModel);
183 stringList.setLayoutOrientation(JList.VERTICAL);
184 stringList.setVisibleRowCount(-1);
185 JScrollPane stringListScroller = new JScrollPane(stringList);
186 stringListScroller.setPreferredSize(new Dimension(280, 100));
187 buildConstraints(constraints, 0, 2, 3, 2, 100, 100);
188 gridbag.setConstraints(stringListScroller, constraints);
189 widgetPanel.add(stringListScroller, constraints);
190 // removeButton
191 buildConstraints(constraints, 3, 2, 1, 1, 100, 100);
192 gridbag.setConstraints(removeButton, constraints);
193 widgetPanel.add(removeButton, constraints);
194 // clearButton
195 buildConstraints(constraints, 3, 3, 1, 1, 100, 100);
196 gridbag.setConstraints(clearButton, constraints);
197 widgetPanel.add(clearButton, constraints);
198
199 stringList.setModel(listModel);
200 // ActionListeners
201 addButton.addActionListener(this);
202 removeButton.addActionListener(this);
203 clearButton.addActionListener(this);
204 setButton.addActionListener(this);
205 }
206
207 }