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.HashSet;
029 import java.util.Iterator;
030 import java.util.LinkedList;
031 import java.util.Set;
032
033 import javax.swing.DefaultListModel;
034 import javax.swing.JButton;
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.StringSetConfigOption;
042 import org.dllearner.core.owl.Individual;
043 import org.dllearner.core.owl.NamedClass;
044 import org.dllearner.core.owl.ObjectProperty;
045 import org.dllearner.gui.CheckBoxList;
046 import org.dllearner.gui.Config;
047
048 /**
049 * Panel for option StringSet, defined in
050 * org.dllearner.core.config.StringSetConfigOption.
051 *
052 * There are 2 layouts defined. First for normal option and a second for special
053 * options. Second layout shows a list of JCheckBox's.
054 *
055 * @author Tilo Hielscher
056 * @author Jens Lehmann
057 *
058 */
059 public class WidgetPanelStringSet extends AbstractWidgetPanel<Set<String>> implements ActionListener {
060
061 private static final long serialVersionUID = 7832726987046601916L;
062
063 private GridBagLayout gridbag = new GridBagLayout();
064 private GridBagConstraints constraints = new GridBagConstraints();
065
066 private JPanel widgetPanel; // = new JPanel();
067 private JButton addButton; // = new JButton("add");
068 private JButton removeButton; // = new JButton("remove");
069 private JButton clearButton; // = new JButton("clear");
070 private JTextField stringField; // = new JTextField(30);
071
072 private Set<String> value = new HashSet<String>();
073 private JList stringList; // = new JList();
074 private DefaultListModel listModel; // = new DefaultListModel();
075
076 private CheckBoxList cBL; // = new CheckBoxList(this);
077
078 /**
079 * Provides a widget for string set options.
080 * @param config Central config handler.
081 * @param component The component of this option.
082 * @param configOption The option to configure.
083 */
084 public WidgetPanelStringSet(Config config, Component component, StringSetConfigOption configOption) {
085 super(config, component, configOption);
086 }
087
088 @Override
089 public void actionPerformed(ActionEvent e) {
090 if (!isSpecial()) {
091 // NORMAL LAYOUT
092 Set<String> exampleSet = new HashSet<String>();
093 // add to list
094 if (e.getSource() == addButton && !listModel.contains(stringField.getText())) {
095 System.out.println("add event");
096 listModel.addElement(stringField.getText());
097 }
098 // remove selection
099 if (e.getSource() == removeButton) {
100 int[] selectedIndices = stringList.getSelectedIndices();
101 int count = 0;
102 // remove i.e. 2 and 4: after delete 2: 4 is now 3
103 for (int i : selectedIndices) {
104 listModel.remove(i - count++);
105 }
106 }
107 // clear list
108 if (e.getSource() == clearButton) {
109 listModel.clear();
110 }
111 // update
112 // stringList.setModel(listModel);
113 for (int i = 0; i < listModel.size(); i++) {
114 if (!listModel.get(i).toString().equalsIgnoreCase("")) {
115 exampleSet.add(listModel.get(i).toString());
116 }
117 }
118 // set entry
119 value = exampleSet;
120 // setEntry();
121 fireValueChanged(value);
122 }
123 }
124
125 private void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx,
126 int wy) {
127 gbc.gridx = gx;
128 gbc.gridy = gy;
129 gbc.gridwidth = gw;
130 gbc.gridheight = gh;
131 gbc.weightx = wx;
132 gbc.weighty = wy;
133 }
134
135 /**
136 * Special layout returns true if 2nd layout should used.
137 */
138 private boolean isSpecial() {
139 return configOption.getName().equalsIgnoreCase("positiveExamples")
140 || configOption.getName().equalsIgnoreCase("negativeExamples")
141 || configOption.getName().equalsIgnoreCase("allowedConcepts")
142 || configOption.getName().equalsIgnoreCase("ignoredConcepts")
143 || configOption.getName().equalsIgnoreCase("allowedRoles")
144 || configOption.getName().equalsIgnoreCase("ignoredRoles");
145
146 }
147
148 @Override
149 public void buildWidgetPanel() {
150 gridbag = new GridBagLayout();
151 widgetPanel = new JPanel();
152 widgetPanel.setLayout(gridbag);
153 add(widgetPanel, BorderLayout.CENTER);
154 add(getLabel());
155
156 value = config.getConfigOptionValue(component, configOption);
157
158 listModel = new DefaultListModel();
159 // fill list
160 if (value != null) {
161 // setEntry();
162 for (Iterator<String> iterator = value.iterator(); iterator.hasNext();) {
163 String item = iterator.next();
164 listModel.addElement(item);
165 }
166 }
167
168 constraints = new GridBagConstraints();
169 cBL = new CheckBoxList(this);
170 stringList = new JList();
171 stringField = new JTextField(30);
172 addButton = new JButton("add");
173 removeButton = new JButton("remove");
174 clearButton = new JButton("clear");
175
176 if (!isSpecial()) {
177 // NORMAL LAYOUT
178 // stringField
179 buildConstraints(constraints, 0, 1, 1, 1, 100, 100);
180 gridbag.setConstraints(stringField, constraints);
181 widgetPanel.add(stringField, constraints);
182 // addButton
183 buildConstraints(constraints, 1, 1, 1, 1, 100, 100);
184 gridbag.setConstraints(addButton, constraints);
185 widgetPanel.add(addButton, constraints);
186 // list
187 stringList.setModel(listModel);
188 stringList.setLayoutOrientation(JList.VERTICAL);
189 stringList.setVisibleRowCount(-1);
190 JScrollPane stringListScroller = new JScrollPane(stringList);
191 stringListScroller.setPreferredSize(new Dimension(380, 100));
192 buildConstraints(constraints, 0, 2, 1, 2, 100, 100);
193 gridbag.setConstraints(stringListScroller, constraints);
194 widgetPanel.add(stringListScroller, constraints);
195 // removeButton
196 buildConstraints(constraints, 1, 2, 1, 1, 100, 100);
197 gridbag.setConstraints(removeButton, constraints);
198 widgetPanel.add(removeButton, constraints);
199 // clearButton
200 buildConstraints(constraints, 1, 3, 1, 1, 100, 100);
201 gridbag.setConstraints(clearButton, constraints);
202 widgetPanel.add(clearButton, constraints);
203 } else {
204 // System.out.println("SPECIAL OPTION " + configOption.getName());
205
206 // SPECIAL LAYOUT
207 // ComboBoxList
208 buildConstraints(constraints, 0, 1, 1, 1, 100, 100);
209 gridbag.setConstraints(cBL, constraints);
210 widgetPanel.add(cBL, constraints);
211 // DEFINE LIST
212 // positiveExamples or negativeExamples
213 if (configOption.getName().equalsIgnoreCase("positiveExamples")
214 || configOption.getName().equalsIgnoreCase("negativeExamples")) {
215 // fill lists
216 Set<Individual> individualsSet = config.getReasoner()
217 .getIndividuals();
218 if(individualsSet != null) {
219 LinkedList<Individual> individuals = new LinkedList<Individual>(
220 individualsSet);
221 // int i = 0;
222 for (Individual ind : individuals) {
223 cBL.add(ind.getName());
224 // i++;
225 // do not display more than 200 examples (freezes GUI)
226 // if(i == 200) {
227 // break;
228 // }
229 }
230 }
231 }
232 // allowedConcepts or ignoredConcepts
233 if (configOption.getName().equalsIgnoreCase("allowedConcepts")
234 || configOption.getName().equalsIgnoreCase("ignoredConcepts")) {
235 // fill lists
236 Set<NamedClass> atomicsSet = config.getReasoner()
237 .getNamedClasses();
238 if(atomicsSet != null) {
239 LinkedList<NamedClass> atomicConcepts = new LinkedList<NamedClass>(
240 atomicsSet);
241 for (NamedClass ind : atomicConcepts) {
242 cBL.add(ind.getName());
243 }
244 }
245 }
246 // allowedRoles or ignoredRoles
247 if (configOption.getName().equalsIgnoreCase("allowedRoles")
248 || configOption.getName().equalsIgnoreCase("ignoredRoles")) {
249 // fill lists
250 Set<ObjectProperty> atomicsSet = config.getReasoner()
251 .getObjectProperties();
252 if(atomicsSet != null) {
253 LinkedList<ObjectProperty> atomicRoles = new LinkedList<ObjectProperty>(
254 atomicsSet);
255 for (ObjectProperty ind : atomicRoles) {
256 cBL.add(ind.getName());
257 }
258 }
259 }
260 // set selections
261 if (value != null) {
262 cBL.setSelections(value);
263 }
264 }
265
266 stringList.setModel(listModel);
267
268 // ActionListeners
269 addButton.addActionListener(this);
270 removeButton.addActionListener(this);
271 clearButton.addActionListener(this);
272
273 }
274
275 }