package ch.ffhs.pvanv.cd1.view;
import java.awt.BorderLayout;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.event.TreeModelEvent;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import ch.ffhs.pvanv.cd1.control.SammlungController;
import ch.ffhs.pvanv.cd1.model.CdModel;
import ch.ffhs.pvanv.cd1.model.SammlungListener;
import ch.ffhs.pvanv.cd1.model.SammlungModel;
import ch.ffhs.pvanv.cd1.model.SammlungModelEvent;
public class CdSammlung extends JFrame implements SammlungListener {
HeadPanel headPanel;
JTree content;
SammlungController scon;
protected DefaultTreeModel treeModel;
protected DefaultMutableTreeNode top = null;
public CdSammlung(SammlungController scon) {
super("CD Sammlung");
this.top = new DefaultMutableTreeNode("CD Collection");
this.scon=scon;
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
headPanel=new HeadPanel(this);
String[] genres = NeueCdView.getGenres();
for (int i=0; i<genres.length; i++)
{
String genre = genres[i];
DefaultMutableTreeNode child = new DefaultMutableTreeNode(genre);
this.top.add(child);
}
content = new JTree(top);
content.setEditable(false);
setLayout(new BorderLayout());
add(headPanel,BorderLayout.NORTH);
add(content,BorderLayout.CENTER);
setSize(600,700);
setVisible(true);
}
public JTree getContent() {
return content;
}
public SammlungController getSammlungController() {
return scon;
}
@Override
public void sammlungChanged(SammlungModelEvent evt) {
System.out.println("sammlung changes");
SammlungModel model = ((SammlungModel)evt.getSource());
model.getCdList();
Collection<CdModel> cds = (Collection<CdModel>)((SammlungModel)evt.getSource()).getCdList();
for(int i=0; i<this.top.getChildCount(); i++)
{
Iterator<CdModel> it = cds.iterator();
while(it.hasNext())
{
CdModel cd = (CdModel)it.next();
if((""+this.top.getChildAt(i)).equalsIgnoreCase(cd.getGenre()))
{
((DefaultMutableTreeNode)this.top.getChildAt(i)).
add(new DefaultMutableTreeNode(cd.getKuenstler()));
}
}
}
repaint();
}
public static void main(String[] args) {
SammlungModel model=new SammlungModel();
SammlungController controller=new SammlungController(model);
CdSammlung mainView=new CdSammlung(controller);
model.addSammlungListener(mainView);
CdModel cd1=new CdModel(1995,"Michael Jackson","Bad","Pop","USA");
CdModel cd2=new CdModel(1996,"Joe Cocker","Blafuzz","Pop","USA");
CdModel cd3=new CdModel(1997,"Ronin","Strange stuff","Jazz","USA");
CdModel cd4=new CdModel(1998,"Richard Wagner","Siegfried","Klassik","Germany");
model.addCd(cd1);
model.addCd(cd2);
model.addCd(cd3);
model.addCd(cd4);
}
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent, Object child)
{
return addObject(parent, child, false);
}
public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,Object child,boolean shouldBeVisible)
{
DefaultMutableTreeNode childNode =
new DefaultMutableTreeNode(child);
if (parent == null) {
parent = this.top;
}
treeModel.insertNodeInto(childNode, parent, parent.getChildCount());
return childNode;
}
@Override
public void treeNodesChanged(TreeModelEvent e) {
DefaultMutableTreeNode node;
node = (DefaultMutableTreeNode)(e.getTreePath().getLastPathComponent());
int index = e.getChildIndices()[0];
node = (DefaultMutableTreeNode)(node.getChildAt(index));
System.out.println("The user has finished editing the node.");
System.out.println("New value: " + node.getUserObject());
}
@Override
public void treeNodesInserted(TreeModelEvent e) {
System.out.println("a tree node was inserted");
}
@Override
public void treeNodesRemoved(TreeModelEvent e) {
System.out.println("a node was removed");
}
@Override
public void treeStructureChanged(TreeModelEvent e) {
System.out.println("the structure was changed");
}
}