package guicomponents;
import helper.Helper;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import dao.RCommand;
import dao.RObjectViewportTable;
import datastructures.ItemSet;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Enumeration;
public class OneSetItemSelectionPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private JList rightList;
private JList leftList;
public DefaultListModel rightListModel;
private DefaultListModel leftListModel;
private JButton toRight;
private JButton toLeft;
private JLabel fstLineLftLabel;
private JLabel fstLineRgtLabel;
private JScrollPane leftScrP;
private JScrollPane rightScrP;
DataFileSelectionPanel dfsp;
public OneSetItemSelectionPanel(DataFileSelectionPanel dfsp) {
this.dfsp = dfsp;
fstLineLftLabel = new JLabel(" Testable Items");
fstLineRgtLabel = new JLabel(" Selected Items");
leftListModel = new DefaultListModel();
rightListModel = new DefaultListModel();
leftList = new JList(leftListModel);
rightList = new JList(rightListModel);
rightList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
leftScrP = new JScrollPane(leftList);
rightScrP = new JScrollPane(rightList);
toRight = new JButton(">>");
toLeft = new JButton("<<");
toRight.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
btntoRightClicked();
}
});
toLeft.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
btntoLeftClicked();
}
});
GridBagLayout gbl = new GridBagLayout();
this.setLayout(gbl);
// Z Z Z Z F
// e e e e ü
// l l l l l
// l l l l l
// s z w h v
// p e e o e
// a i i e r
// l l t h h
// t e e e a
// e l
// t
// e
// n
// g g g g f i i a w w
// r r r r i p p n e e
// i i i i l a a c i i
// d d d d l d d h g g
// x y w h x y o h h
// i e r t t
// d i x y
// t g
// h h
// t
addComponent(this, gbl,fstLineLftLabel,0, 0, 1, 1, GridBagConstraints.HORIZONTAL, 0, 0, GridBagConstraints.WEST, 0, 0);
addComponent(this, gbl,fstLineRgtLabel,2, 0, 1, 1, GridBagConstraints.HORIZONTAL, 0, 0, GridBagConstraints.WEST, 0, 0);
addComponent(this, gbl,leftScrP, 0, 1, 1, 2, GridBagConstraints.HORIZONTAL, 0, 0, GridBagConstraints.WEST, 0, 0);
addComponent(this, gbl,toRight, 1, 1, 1, 1, GridBagConstraints.NONE, 0, 0, GridBagConstraints.SOUTH, 0, 0);
addComponent(this, gbl,toLeft, 1, 2, 1, 1, GridBagConstraints.NONE, 0, 0, GridBagConstraints.SOUTH, 0, 0);
addComponent(this, gbl,rightScrP, 2, 1, 2, 2, GridBagConstraints.HORIZONTAL, 0, 0, GridBagConstraints.WEST, 0, 0);
}
private void addComponent(Container cont,
GridBagLayout gbl,
Component c,
int gridx, int gridy,
int gridwidth, int gridheight,
int fill,
int ipadx, int ipady,
/* Insets insets, */
int anchor,
double weightx, double weighty) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = gridx;
gbc.gridy = gridy; // Spalte/Reihe mit ganz links = 0 und ganz oben = 0
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight; // Anzahl Spalten/Reihen, die Komponente nutzt - DEFAULT = 1;
gbc.fill = fill; // entscheidet howto resize the component, wenn sie vergrößert wird = NONE , HORIZONTAL, VERTICAL, BOTH
gbc.ipadx = ipadx;
gbc.ipady = ipady; // interne Füllung (howmuch to add to the minimum weight/height of the component
// gbc.insets = insets; // externe Füllung (howmuch is the minimum space between component and the edges of its display area)
gbc.anchor = anchor; // used if component kleiner als seine display area (verschiedene Konstanten)
gbc.weightx = weightx;
gbc.weighty = weighty; // determines howto distribute space among columns and rows
gbl.setConstraints(c, gbc);
cont.add(c);
}
private void btntoRightClicked() {
// Systemcharacteristics,
if (leftList.getSelectedValues().length < 2) {
Helper.ok("Select 2 or more items in left list.");
return;
}
ItemSet itemSel = new ItemSet(dfsp.getSelectedDatafile());
for (int i = 0; i < leftList.getSelectedValues().length; i++) {
itemSel.put(i+1, (String) leftList.getSelectedValues()[i]);
}
rightListModel.addElement(itemSel);
leftList.clearSelection();
}
private void btntoLeftClicked() {
if (rightList.getSelectedValues().length > 0) {
rightListModel.remove(rightList.getSelectedIndex());
}
}
public void setDataFile(RObjectViewportTable table) {
leftListModel.removeAllElements();
System.out.println("Clocount for " + table.getAttachedRObject().getRVarName() + "... " + RCommand.getColCount(table.getAttachedRObject().getRVarName()));
for (String colname : RCommand.getColNames(
table.getAttachedRObject().getRVarName()))
leftListModel.addElement(colname);
}
public Enumeration<ItemSet> getItemSetEnum() {
return (Enumeration<ItemSet>)rightListModel.elements();
}
}