/* 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.fisheye;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.util.ArrayList;

import javax.swing.JApplet;

import pedviz.algorithms.HierarchieUpDown;
import pedviz.algorithms.Highlighter;
import pedviz.algorithms.RubberBands;
import pedviz.algorithms.SameParents;
import pedviz.algorithms.sugiyama.SugiyamaLayout;
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.GraphView2D;
import pedviz.view.NodeView;
import pedviz.view.effects.FisheyeEffect;
import pedviz.view.rules.HintRule;
import pedviz.view.rules.ShapeRule;
import pedviz.view.symbols.SymbolSexFemale;
import pedviz.view.symbols.SymbolSexMale;

public class Fisheye extends JApplet {
	private GraphView2D graphView;

	private Graph graph;

	private LayoutedGraph layoutedGraph;

	public void init() {

		graph = new Graph();

		RessourceGraphLoader loader = new RessourceGraphLoader(
				getParameter("datasource"), ",");

		loader.setSettings("PID", "MOM", "DAD");
		loader.load(graph);

		SameParents sameParents = new SameParents(graph);
		sameParents.run();

		graph.buildHierarchie(new HierarchieUpDown());

		DefaultEdgeView e = new DefaultEdgeView();
		e.setColor(new Color(50, 50, 50));
		e.setHighlightedColor(new Color(255, 0, 0));
		e.setHighlightedWidth(2);
		e.setColorForLongLines(new Color(50, 50, 50));
		e.setConnectChildren(true);
		e.setGapBottom(5);

		DefaultNodeView n = new DefaultNodeView();

		// runs sugiyama
		SugiyamaLayout layout = new SugiyamaLayout(graph, n, e);
		layout.run();
		layoutedGraph = layout.getLayoutGraph();

		RubberBands rubberBands = new RubberBands(layoutedGraph);
		rubberBands.setVerticalSpacing(180);
		rubberBands.setHorizontalSpacing(20);
		rubberBands.run();

		// for 2d:
		graphView = new GraphView2D();

		graphView.addRule(new ShapeRule("sex", "2", new SymbolSexFemale()));
		graphView.addRule(new ShapeRule("sex", "1", new SymbolSexMale()));

		graphView.setSelectionEnabled(true);

		FisheyeEffect effect = new FisheyeEffect(3, 3, 2);
		effect.setMinSize(4f);
		effect.setAutoUpdateOnMove(false);
		effect.setAutoUpdateOnDrag(true);
		effect.setSpeed(3);
		graphView.setEffect(effect);

		graphView.setZoomEnabled(false);
		graphView.setMovingEnabled(false);

		graphView.setGraph(layoutedGraph);
		add(graphView.getComponent(), BorderLayout.CENTER);
	}
}

