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;
021
022 import javax.swing.*;
023
024 import org.dllearner.gui.widgets.WidgetPanelStringSet;
025
026 import java.awt.event.ActionEvent;
027 import java.awt.event.ActionListener;
028 import java.awt.BorderLayout;
029 import java.awt.Dimension;
030 import java.awt.GridBagConstraints;
031 import java.awt.GridBagLayout;
032 import java.util.HashSet;
033 import java.util.LinkedList;
034 import java.util.Set;
035
036 /**
037 * CheckBoxList constitute a list of CheckBoxes.
038 *
039 * @author Tilo Hielscher
040 */
041 public class CheckBoxList extends JPanel implements ActionListener {
042 private static final long serialVersionUID = -7119007550662195455L;
043 private JPanel checkBoxPanel = new JPanel();
044 private LinkedList<JCheckBox> list = new LinkedList<JCheckBox>();
045 private GridBagLayout gridbag = new GridBagLayout();
046 private GridBagConstraints constraints = new GridBagConstraints();
047 private WidgetPanelStringSet widgetPanel;
048
049 /**
050 * Make a JPanel with GridBagLayout.
051 * @param panel The StringPanel the check box list is added to.
052 * (TODO Actually, there shouldn't be a dependency of a
053 * visual element to its parent.)
054 */
055 public CheckBoxList(WidgetPanelStringSet panel) {
056 this.widgetPanel = panel;
057 checkBoxPanel.setLayout(gridbag);
058
059 JScrollPane scrollPane = new JScrollPane(checkBoxPanel);
060 // scrollPane.setSize(new Dimension(500,100));
061 scrollPane.setPreferredSize(new Dimension(500, 300));
062 scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
063 add(scrollPane, BorderLayout.CENTER);
064
065 constraints.anchor = GridBagConstraints.WEST;
066 }
067
068 /**
069 * Add new entry and make a new JCheckBox.
070 *
071 * @param label
072 * This text will be shown. It is the name of new JCheckBox and
073 * will add to list.
074 */
075 public void add(String label) {
076 JCheckBox chkBox = new JCheckBox(label);
077 list.add(chkBox);
078 chkBox.addActionListener(this);
079 update();
080 }
081
082
083 // Return a set of selected items.
084 private Set<String> getSelections() {
085 Set<String> selectionSet = new HashSet<String>();
086 // selectionSet.clear(); // remove all
087 for (int i = 0; i < list.size(); i++) {
088 if (list.get(i).isSelected()) {
089 selectionSet.add(list.get(i).getText());
090 }
091 }
092 return selectionSet;
093 }
094
095 /**
096 * Select items.
097 *
098 * @param selectionSet
099 * Is a Set of Strings.
100 */
101 public void setSelections(Set<String> selectionSet) {
102 for (int i = 0; i < this.list.size(); i++) {
103 if (selectionSet.contains(list.get(i).getText())) {
104 list.get(i).setSelected(true);
105 } else {
106 this.list.get(i).setSelected(false);
107 }
108 }
109 }
110
111 // update checkbox
112 private void update() {
113 checkBoxPanel.removeAll();
114 for (int i = 0; i < list.size(); i++) {
115 buildConstraints(constraints, 0, i, 1, 1, 100, 100);
116 gridbag.setConstraints(list.get(i), constraints);
117 checkBoxPanel.add(list.get(i), constraints);
118 }
119 }
120
121 private void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx,
122 int wy) {
123 gbc.gridx = gx;
124 gbc.gridy = gy;
125 gbc.gridwidth = gw;
126 gbc.gridheight = gh;
127 gbc.weightx = wx;
128 gbc.weighty = wy;
129 }
130
131 @Override
132 public void actionPerformed(ActionEvent e) {
133 Set<String> value = getSelections();
134 widgetPanel.fireValueChanged(value);
135
136 // widgetPanel.specialSet();
137 }
138 }