/* 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.symbols3d;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JPanel;

import pedviz.algorithms.Sugiyama;
import pedviz.algorithms.sugiyama.RandomSplitter;
import pedviz.graph.Graph;
import pedviz.graph.LayoutedGraph;
import pedviz.graph.Node;
import pedviz.loader.RessourceGraphLoader;
import pedviz.view.DefaultEdgeView;
import pedviz.view.DefaultNodeView;
import pedviz.view.GraphView3D;
import pedviz.view.NodeEvent;
import pedviz.view.NodeListener;
import pedviz.view.rules.ColorRule;
import pedviz.view.rules.ShapeRule;
import pedviz.view.symbols3d.SymbolAdopted3d;
import pedviz.view.symbols3d.SymbolDeceased3d;
import pedviz.view.symbols3d.SymbolSexFemale3d;
import pedviz.view.symbols3d.SymbolSexMale3d;
import pedviz.view.symbols3d.SymbolSexUndesignated3d;

import com.sun.j3d.utils.universe.SimpleUniverse;

public class Symbols3D extends JApplet {

	private GraphView3D graphView;

	private Graph graph;

	public void init() {

		graph = new Graph();

		RessourceGraphLoader loader = new RessourceGraphLoader(
				getParameter("datasource"), ",");
		loader.setSettings("PID", "MOM", "DAD");
		loader.load(graph);

		// node look
		DefaultEdgeView e = new DefaultEdgeView();
		e.setWidth(0.005f);

		DefaultNodeView n = new DefaultNodeView();
		n.setColor(Color.white);
		n.addHintAttribute("pid");
		n.addHintAttribute("mom");
		n.addHintAttribute("dad");
		// runs sugiyama
		Sugiyama layout = new Sugiyama(graph, n, e, new RandomSplitter());
		layout.run();
		LayoutedGraph layoutedGraph = layout.getLayoutedGraph();

		graphView = new GraphView3D(SimpleUniverse.getPreferredConfiguration());

		graphView.setBackgroundColor(new Color(0, 0, 0));
		graphView.addRule(new ShapeRule("SEX", "1", new SymbolSexFemale3d()));
		graphView.addRule(new ShapeRule("SEX", "2", new SymbolSexMale3d()));
		graphView.addRule(new ShapeRule("SEX", "-1",
				new SymbolSexUndesignated3d()));
		graphView.addRule(new ShapeRule("dead", "1", new SymbolDeceased3d()));
		graphView.addRule(new ShapeRule("adopted", "1", new SymbolAdopted3d()));

		ColorRule colorRule = new ColorRule("trait1", "yes", Color.red);
		colorRule.addRule("trait2", "yes", Color.cyan);
		graphView.addRule(colorRule);

		graphView.setSelectionEnabled(true);
		graphView.setGraph(layoutedGraph);

		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, 8));
		toolBar2.setBackground(Color.WHITE);

		final JLabel caption1 = new JLabel("Adopted: ");
		final JLabel caption2 = new JLabel("-");
		final JLabel caption3 = new JLabel("Deceased: ");
		final JLabel caption4 = new JLabel("-");
		final JLabel caption5 = new JLabel("Trait1: ");
		final JLabel caption6 = new JLabel("-");
		final JLabel caption7 = new JLabel("Trait2: ");
		final JLabel caption8 = new JLabel("-");

		caption1.setHorizontalAlignment(JLabel.RIGHT);
		caption3.setHorizontalAlignment(JLabel.RIGHT);
		caption5.setHorizontalAlignment(JLabel.RIGHT);
		caption7.setHorizontalAlignment(JLabel.RIGHT);

		toolBar2.add(caption1, BorderLayout.LINE_END);
		toolBar2.add(caption2, BorderLayout.LINE_END);
		toolBar2.add(caption3, BorderLayout.LINE_END);
		toolBar2.add(caption4, BorderLayout.LINE_END);
		toolBar2.add(caption5, BorderLayout.LINE_END);
		toolBar2.add(caption6, BorderLayout.LINE_END);
		toolBar2.add(caption7, BorderLayout.LINE_END);
		toolBar2.add(caption8, BorderLayout.LINE_END);

		getContentPane().add(toolBar, BorderLayout.PAGE_END);

		getContentPane().add(graphView.getComponent());
		graphView.centerGraph();

		graphView.addNodeListener(new NodeListener() {
			public void onNodeEvent(NodeEvent event) {
				switch (event.getType()) {
				case NodeEvent.MOUSE_ENTER:
					caption2.setText(event.getNode().getUserData("adopted")
							.equals("1") ? "yes" : "no");
					caption4.setText(event.getNode().getUserData("dead")
							.equals("1") ? "yes" : "no");
					caption6.setText(event.getNode().getUserData("trait1")
							.toString());
					caption8.setText(event.getNode().getUserData("trait2")
							.toString());
					break;
				case NodeEvent.MOUSE_LEAVE:
					caption2.setText("-");
					caption4.setText("-");
					caption6.setText("-");
					caption8.setText("-");
					break;
				}
			}
		});

	}
}

