/* Copyright © 2007 by Christian Fuchsberger and Lukas Forer info@pedvizapi.org.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License <http://www.pedvizapi.org/gpl.txt>
 * for more details. 
 */

package applets.highlighting3d;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JApplet;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import pedviz.algorithms.Highlighter;
import pedviz.algorithms.Sugiyama;
import pedviz.algorithms.sugiyama.RandomSplitter;
import pedviz.graph.Graph;
import pedviz.graph.Node;
import pedviz.loader.RessourceGraphLoader;
import pedviz.view.DefaultEdgeView;
import pedviz.view.DefaultNodeView;
import pedviz.view.GraphView;
import pedviz.view.GraphView3D;
import pedviz.view.LODHighlighter;
import pedviz.view.NodeEvent;
import pedviz.view.NodeListener;
import pedviz.view.rules.PositionZRule;
import pedviz.view.rules.ShapeRule;
import pedviz.view.symbols3d.SymbolSexFemale3d;
import pedviz.view.symbols3d.SymbolSexMale3d;
import pedviz.view.symbols3d.SymbolSexUndesignated3d;

import com.sun.j3d.utils.universe.SimpleUniverse;

public class LinesOfDescents extends JApplet {
	private GraphView graphView;

	private Graph graph;

	private int mode = Highlighter.SUCCESSORS;

	private HashMap<String, Integer> data;

	public void init() {
		data = new HashMap<String, Integer>();
		data.put("maternal", Highlighter.MATERNAL);
		data.put("paternal", Highlighter.PATERNAL);
		data.put("maternal and paternal", Highlighter.MATERNAL_AND_PATERNAL);
		data.put("all ancestors", Highlighter.ANCESTORS);
		data.put("all successors", Highlighter.SUCCESSORS);
		data.put("all ancestors and successors",
				Highlighter.SUCCESSORS_AND_ANCESTORS);

		graph = new Graph();

		RessourceGraphLoader loader = new RessourceGraphLoader(
				getParameter("datasource"), ",");

		loader.setSettings("PID", "MOM", "DAD");
		loader.load(graph);

		DefaultEdgeView e = new DefaultEdgeView();
		e.setWidth(0.00005f);
		e.setHighlightedColor(Color.green);

		DefaultNodeView n = new DefaultNodeView();
		n.setHighlightedColor(Color.green);

		Sugiyama sugiyama = new Sugiyama(graph, n, e, new RandomSplitter());
		sugiyama.getRubberBands().setVerticalSpacing(100f);
		sugiyama.getRubberBands().setDepth(250f);
		sugiyama.run();

		graphView = new GraphView3D(SimpleUniverse.getPreferredConfiguration(),
				sugiyama.getLayoutedGraph());
		graphView.setSelectionEnabled(true);

		graphView.addRule(new ShapeRule("sex", "2", new SymbolSexFemale3d()));
		graphView.addRule(new ShapeRule("sex", "1", new SymbolSexMale3d()));
		graphView.addRule(new ShapeRule("sex", "-1",
				new SymbolSexUndesignated3d()));
		graphView.addRule(new PositionZRule(sugiyama.getRubberBands()
				.getDepth() / 2f, new Color(255, 200, 100)));
		final LODHighlighter lodHighlighter = new LODHighlighter(mode);
		graphView.addNodeListener(lodHighlighter);

		JPanel toolBar = new JPanel();
		toolBar.setLayout(new BorderLayout());
		toolBar.setBackground(Color.WHITE);
		JPanel toolBar2 = new JPanel();
		toolBar.add(toolBar2, BorderLayout.LINE_END);
		toolBar2.setLayout(new GridLayout(1, 2));
		toolBar2.setBackground(Color.WHITE);

		JComboBox dataList = new JComboBox(data.keySet().toArray());
		dataList.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				JComboBox item = (JComboBox) e.getSource();
				mode = data.get(item.getSelectedItem());
				lodHighlighter.setMode(mode);
			}
		});

		JLabel caption = new JLabel("Line of Descents: ");
		caption.setHorizontalAlignment(JLabel.RIGHT);
		toolBar2.add(caption, BorderLayout.LINE_END);
		toolBar2.add(dataList, BorderLayout.LINE_END);
		getContentPane().add(toolBar, BorderLayout.PAGE_END);
		getContentPane().add(graphView.getComponent());
		graphView.centerGraph();

	}
}

