package Paket;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.AbstractListModel;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.ListModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class TabListenerDemo {
public static JRadioButton radioFruehstueck;
public static JRadioButton radioMittag;
public static JRadioButton radioAbendbrot;
public static JRadioButton radioZwischenmahlzeit;
public static void main(String[] args)
{
JFrame frame = new JFrame("Tab Listener");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton newEntry = new JButton("Neuer Eintrag");
JListAddItemAction addAction = new JListAddItemAction();
JButton removeEntry = new JButton("Eintrag entfernen");
JListRemoveItemAction removeAction = new JListRemoveItemAction();
newEntry.addActionListener(addAction);
removeEntry.addActionListener(removeAction);
JTabbedPane tab = new JTabbedPane();
ListModelSelector lms = new ListModelSelector();
lms.addAction(addAction);
lms.addAction(removeAction);
tab.addChangeListener(lms);
Factory f = new Factory();
createPanel("Mo", null, "Tab-Montag", tab, f);
addAction.setJList(f.getFruehstueck(), "Fruehstueck");
addAction.setJList(f.getMittag(), "Mittag");
addAction.setJList(f.getAbendbrot(), "Abendbrot");
addAction.setJList(f.getZwischenmahlzeit(), "Zwischenmahlzeit");
//removeAction.setJList(f.getFruehstueck());
lms.addJList(f.getFruehstueck(),1);
lms.addJList(f.getMittag(),2);
lms.addJList(f.getAbendbrot(),3);
lms.addJList(f.getZwischenmahlzeit(),4);
createPanel("Di", null, "Tab-Dienstag", tab, f);
addAction.setJList(f.getFruehstueck(), "Fruehstueck");
addAction.setJList(f.getMittag(), "Mittag");
addAction.setJList(f.getAbendbrot(), "Abendbrot");
addAction.setJList(f.getZwischenmahlzeit(), "Zwischenmahlzeit");
lms.addJList(f.getFruehstueck(),1);
lms.addJList(f.getMittag(),2);
lms.addJList(f.getAbendbrot(),3);
lms.addJList(f.getZwischenmahlzeit(),4);
createPanel("Mi", null, "Tab-Mittwoch", tab, f);
addAction.setJList(f.getFruehstueck(), "Fruehstueck");
addAction.setJList(f.getMittag(), "Mittag");
addAction.setJList(f.getAbendbrot(), "Abendbrot");
addAction.setJList(f.getZwischenmahlzeit(), "Zwischenmahlzeit");
lms.addJList(f.getFruehstueck(),1);
lms.addJList(f.getMittag(),2);
lms.addJList(f.getAbendbrot(),3);
lms.addJList(f.getZwischenmahlzeit(),4);
createPanel("Do", null, "Tab-Donnerstag", tab, f);
addAction.setJList(f.getFruehstueck(), "Fruehstueck");
addAction.setJList(f.getMittag(), "Mittag");
addAction.setJList(f.getAbendbrot(), "Abendbrot");
addAction.setJList(f.getZwischenmahlzeit(), "Zwischenmahlzeit");
lms.addJList(f.getFruehstueck(),1);
lms.addJList(f.getMittag(),2);
lms.addJList(f.getAbendbrot(),3);
lms.addJList(f.getZwischenmahlzeit(),4);
JPanel buttons = new JPanel();
buttons.add(newEntry);
buttons.add(removeEntry);
/* start radio */
radioFruehstueck = new JRadioButton ("Frühstück", true);
radioMittag = new JRadioButton ("Mittag", false);
radioAbendbrot = new JRadioButton ("Abendbrot", false);
radioZwischenmahlzeit = new JRadioButton ("Zwischenmahlzeit", false);
JPanel radios = new JPanel();
ButtonGroup bgMahlzeiten = new ButtonGroup();
bgMahlzeiten.add(radioFruehstueck);
bgMahlzeiten.add(radioMittag);
bgMahlzeiten.add(radioAbendbrot);
bgMahlzeiten.add(radioZwischenmahlzeit);
radios.add(radioFruehstueck);
radios.add(radioMittag);
radios.add(radioAbendbrot);
radios.add(radioZwischenmahlzeit);
/* ende radio */
frame.getContentPane().add(tab);
frame.getContentPane().add(buttons, BorderLayout.SOUTH);
frame.getContentPane().add(radios, BorderLayout.EAST);
frame.pack();
frame.setSize(1000, 360);
frame.setVisible(true);
}
public static void createPanel(String tabText, Icon icon, String tip, JTabbedPane tab, Factory factory)
{
JPanel panel = new JPanel();
panel.add(new JScrollPane(factory.createJList(1)));
panel.add(new JScrollPane(factory.createJList(2)));
panel.add(new JScrollPane(factory.createJList(3)));
panel.add(new JScrollPane(factory.createJList(4)));
factory.getFruehstueck().addListSelectionListener(new TabListener(tabText));
factory.getMittag().addListSelectionListener(new TabListener(tabText));
factory.getAbendbrot().addListSelectionListener(new TabListener(tabText));
factory.getZwischenmahlzeit().addListSelectionListener(new TabListener(tabText));
tab.addTab(tabText, icon, panel, tip);
}
static class ListModelSelector implements ChangeListener
{
private ArrayList<JList> lists;
private ArrayList<JList> listsFruehstueck, listsMittag, listsAbendbrot, listsZwischenmahlzeit;
private ArrayList<JListAction> actions;
public ListModelSelector()
{
this.actions = new ArrayList<JListAction>();
this.lists = new ArrayList<JList>();
this.listsFruehstueck = new ArrayList<JList>();
this.listsMittag = new ArrayList<JList>();
this.listsAbendbrot = new ArrayList<JList>();
this.listsZwischenmahlzeit = new ArrayList<JList>();
}
public void stateChanged(ChangeEvent ce){
if(!(ce.getSource() instanceof JTabbedPane)) return;
JTabbedPane tab = (JTabbedPane)ce.getSource();
int index = tab.getSelectedIndex();
if(index > -1 && index < lists.size())
{
for(JListAction a: actions)
{
a.setJList(listsFruehstueck.get(index), "Fruehstueck");
a.setJList(listsMittag.get(index), "Mittag");
a.setJList(listsAbendbrot.get(index), "Abendbrot");
a.setJList(listsZwischenmahlzeit.get(index), "Zwischenmahlzeit");
}
}
}
public void addJList(JList list, int art)
{
System.out.println("art"+art);
if(art==1)
{
this.listsFruehstueck.add(list);
}else if (art==2) {
this.listsMittag.add(list);
}else if (art==3) {
this.listsAbendbrot.add(list);
}else if (art==4) {
this.listsZwischenmahlzeit.add(list);
}
this.lists.add(list);
}
public void addAction(JListAction action)
{
this.actions.add(action);
System.out.println("123");
}
}
static class TabListener implements ListSelectionListener
{
private String text;
public TabListener(String text)
{
this.text = text;
}
public void valueChanged(ListSelectionEvent e)
{
if(e.getValueIsAdjusting()) return;
Object item = "nothing";
if(e.getSource() instanceof JList)
{
JList jl = (JList)e.getSource();
item = jl.getSelectedValue();
}
System.out.println("LIstSelectionEvent for : " + text + ", selection: " + item);
}
}
static abstract class JListAction implements ActionListener
{
public JList list, listFruehstueck, listMittag, listAbendbrot, listZwischenmahlzeit;
public void setJList(JList jl, String Name)
{
if(Name=="Fruehstueck")
{
this.listFruehstueck = jl;
}else if (Name=="Mittag") {
this.listMittag = jl;
}else if (Name=="Abendbrot") {
this.listAbendbrot = jl;
}else if (Name=="Zwischenmahlzeit") {
this.listZwischenmahlzeit = jl;
}
}
protected JList getJList(int Art)
{
if(Art==1)
{
return this.listFruehstueck;
}else if (Art==2) {
return this.listMittag;
}else if (Art==3) {
return this.listAbendbrot;
}else {
return this.listZwischenmahlzeit;
}
//return this.list;
}
}
static class JListAddItemAction extends JListAction
{
private ListModel model;
public void actionPerformed(ActionEvent e)
{
if(getJList(0) == null)
{
return;
}
String result = JOptionPane.showInputDialog("Neuer Eintrag");
System.out.println("New entry: " + result);
model = getJList(4).getModel();
if(radioFruehstueck.isSelected())
{
model = getJList(1).getModel();
System.out.println("Fruehstueck");
}
if(radioMittag.isSelected())
{
model = getJList(2).getModel();
System.out.println("Mittag");
}
if(radioAbendbrot.isSelected())
{
model = getJList(3).getModel();
System.out.println("Abendbrot");
}
if(radioZwischenmahlzeit.isSelected())
{
model = getJList(4).getModel();
System.out.println("Zwischenmahlzeit");
}
if(model instanceof DefaultListModel)
{
((DefaultListModel)model).addElement(result);
}
}
}
static class JListRemoveItemAction extends JListAction
{
public void actionPerformed(ActionEvent e)
{
if(getJList(2) == null)
{
return;
}
ListModel model = getJList(2).getModel();
if(model instanceof DefaultListModel)
{
((DefaultListModel)model).removeElement(getJList(2).getSelectedValue());
}
}
}
static class Factory
{
private JList lastList, listFruehstueck, listMittag, listAbendbrot, listZwischenmahlzeit;
public JList getLastList()
{
return lastList;
}
public JList getFruehstueck()
{
return listFruehstueck;
}
public JList getMittag()
{
return listMittag;
}
public JList getAbendbrot()
{
return listAbendbrot;
}
public JList getZwischenmahlzeit()
{
return listZwischenmahlzeit;
}
public JList createJList(int art)
{
JList result = new JList();
if(art==1)
{
String[] entries = new String[]{"Frühstück", "Snack2222", "Nudeln mit Tomatenso\u00DFe", "Fr\u00FChst\u00FCck Total"};
DefaultListModel model = new DefaultListModel();
for(String s: entries)
{
model.addElement(s);
}
result.setModel(model);
listFruehstueck = result;
}else if (art==2)
{
String[] entries = new String[]{"Mittag", "Snack23333", "Nudeln mit Tomatenso\u00DFe", "Fr\u00FChst\u00FCck Total"};
DefaultListModel model = new DefaultListModel();
for(String s: entries)
{
model.addElement(s);
}
result.setModel(model);
listMittag = result;
}else if (art==3)
{
String[] entries = new String[]{"Abendbrot", "Snack244444", "Nudeln mit Tomatenso\u00DFe", "Fr\u00FChst\u00FCck Total"};
DefaultListModel model = new DefaultListModel();
for(String s: entries)
{
model.addElement(s);
}
result.setModel(model);
listAbendbrot = result;
}else if (art==4)
{
String[] entries = new String[]{"Zwischenmahlzeit", "Snack25555", "Nudeln mit Tomatenso\u00DFe", "Fr\u00FChst\u00FCck Total"};
DefaultListModel model = new DefaultListModel();
for(String s: entries)
{
model.addElement(s);
}
result.setModel(model);
listZwischenmahlzeit = result;
}
return result;
}
}
}