package kt.layer;

import java.awt.*;
import javax.swing.*;
import java.util.*;

import ilog.cpl.graphic.views.IlpShelfItemPosition;
import ilog.cpl.graphic.IlpRelativePoint;
import ilog.cpl.graphic.IlpGraphicViewStyleManager;
import ilog.tgo.datasource.IltDefaultDataSource;
import ilog.tgo.model.*;
import ilog.cpl.graphic.IlpPoint;
import ilog.cpl.service.IlpContext;
import ilog.cpl.style.IlpStyleManager;
import ilog.cpl.style.key.IlpFloatKey;
import ilog.cpl.style.IlpStyle;
import ilog.cpl.model.IlpObject;
import ilog.cpl.model.IlpClass;
import ilog.cpl.IlpNetwork;
import ilog.cpl.style.key.IlpBooleanKey;
import ilog.cpl.IlpSystem;
import ilog.cpl.model.IlpClassManager;
import ilog.cpl.style.key.IlpColorKey;
import ilog.views.interactor.IlvUnZoomViewInteractor;
import ilog.cpl.equipment.IlpEquipmentController;
import ilog.cpl.network.action.toolbar.*;
import ilog.cpl.IlpEquipment;
import ilog.cpl.equipment.IlpEquipmentView;
import ilog.cpl.network.IlpNetworkView;
import ilog.views.graphlayout.circular.IlvClusterNumber;
import ilog.views.graphlayout.circular.IlvCircularLayout;
import ilog.views.graphlayout.IlvGraphLayoutException;
import ilog.views.graphlayout.IlvGraphLayout;
import ilog.tgo.graphic.IltCompositeGrapher;
import ilog.views.graphlayout.circular.IlvClusterId;
import ilog.tgo.model.attribute.IltAttribute;
import ilog.views.IlvGraphic;
import ilog.cpl.graphic.views.IlpManagerView;


class Demo extends JPanel
{
	private Vector vLinkModel;

	IltDefaultDataSource dataSource;
	IlpNetwork networkComponent;
	IlvCircularLayout nodeLayout = new IlvCircularLayout();
	IlpNetworkView view = null;
	IlpManagerView managerView = null;
	IltCompositeGrapher grapher = null;

	IltNetworkElement iltNeSystem[] = new IltNetworkElement[8];

	public Demo()
	{
		createNetworkComponent();
		makeDataSource();
		makeLink();

		///¡Ú¡Ú¡Ú///
		//view is IlpNetworkView
		view = getNetwork().getView();
		managerView = getNetwork().getManagerView();

		//grapher is IltCompositeGrapher
		grapher = view.getCompositeGrapher();

		//////
		setNodeLayout(true);
	}

	void createNetworkComponent()
	{
		dataSource = new IltDefaultDataSource();
		networkComponent = new IlpNetwork( );

		networkComponent.setDataSource( dataSource );
		setLayout( new BorderLayout() );
		add( networkComponent, BorderLayout.CENTER );
	}

	void makeDataSource()
	{
		vLinkModel = new Vector();

		// ÇÏ³ª
		iltNeSystem[0] = new IltNetworkElement( "SLSCSCTG1" );
		iltNeSystem[0].setName( "¼­ÃÊ" + "_" + "TrunkGateway1" );
		iltNeSystem[0].setType( IltNetworkElement.Type.IP_Router2 );
		vLinkModel.add(iltNeSystem[0]);

		// µÎ¿ï
		iltNeSystem[1] = new IltNetworkElement( "SLSCSCAG1" );
		iltNeSystem[1].setName( "¼­ÃÊ" + "_" + "Gateway1" );
		iltNeSystem[1].setType( IltNetworkElement.Type.SAN_FiberChannelSwitch );
		vLinkModel.add(iltNeSystem[1]);

		// ¼Â
		iltNeSystem[2] = new IltNetworkElement( "SLSCSCAG2" );
		iltNeSystem[2].setName( "¼­ÃÊ" + "_" + "Gateway2" );
		iltNeSystem[2].setType( IltNetworkElement.Type.SAN_FiberChannelSwitch );
		vLinkModel.add(iltNeSystem[2]);

		// ³Ý
		iltNeSystem[3] = new IltNetworkElement( "SLSCSCSW1" );
		iltNeSystem[3].setName( "¼­ÃÊ" + "_" + "Switch1" );
		iltNeSystem[3].setType( IltNetworkElement.Type.SAN_FiberChannelSwitch );
		vLinkModel.add(iltNeSystem[3]);


		// ÇÏ³ª
		iltNeSystem[4] = new IltNetworkElement( "DJYSBDTG1" );
		iltNeSystem[4].setName( "ºÏ´ëÀü" + "_" + "TrunkGateway1" );
		iltNeSystem[4].setType( IltNetworkElement.Type.IP_Router2 );
		vLinkModel.add(iltNeSystem[4]);

		// µÎ¿ï
		iltNeSystem[5] = new IltNetworkElement( "DJYSBDAG1" );
		iltNeSystem[5].setName( "ºÏ´ëÀü" + "_" + "Gateway1" );
		iltNeSystem[5].setType( IltNetworkElement.Type.SAN_FiberChannelSwitch );
		vLinkModel.add(iltNeSystem[5]);

		// ¼Â
		iltNeSystem[6] = new IltNetworkElement( "DJYSBDAG2" );
		iltNeSystem[6].setName( "ºÏ´ëÀü" + "_" + "Gateway2" );
		iltNeSystem[6].setType( IltNetworkElement.Type.SAN_FiberChannelSwitch );
		vLinkModel.add(iltNeSystem[6]);

		// ³Ý
		iltNeSystem[7] = new IltNetworkElement( "DJYSBDSW1" );
		iltNeSystem[7].setName( "ºÏ´ëÀü" + "_" + "Switch1" );
		iltNeSystem[7].setType( IltNetworkElement.Type.SAN_FiberChannelSwitch );
		vLinkModel.add(iltNeSystem[7]);

		dataSource.addObjects( vLinkModel );
	}

	void makeLink()
	{
		String sLinkKey = "SLSCSCTG1" + "-" + "DJYSBDTG1";

		IltNetworkElement iltNeFrom = (IltNetworkElement)dataSource.getObject( "SLSCSCTG1" );
		IltNetworkElement iltNeTo   = (IltNetworkElement)dataSource.getObject( "DJYSBDTG1" );

		if ( iltNeFrom == null || iltNeTo == null ) return;

		IltLink link = new IltLink( sLinkKey );

		link.setFrom(iltNeFrom);
		link.setTo(iltNeTo);
		
		vLinkModel.add(link);
		dataSource.addObjects( vLinkModel );
	}

	private IlpNetwork getNetwork()
	{
		return networkComponent;
	}

	///¡Ú¡Ú¡Ú///
	void setCluster()
	{
		int[] cluster = new int[] { 0, 1 };

		int count = 0;
		// ¼­ÃÊ
		IlvClusterId clusterId0 = new IlvClusterNumber(cluster[0]);
		// ºÏ´ëÀü
		IlvClusterId clusterId1 = new IlvClusterNumber(cluster[1]);
		
		String anyOffice = "";

		//IltAttribute nodeTypeAttr = IltNetworkElement.TypeAttribute;

		Enumeration enum = grapher.getNodes();
//System.out.println( "before while" );
		while (enum.hasMoreElements())
		{
			//System.out.println( "in while" );
			
			IltObject object = (IltObject)enum.nextElement();

			if (object.getName() == null)
				continue;

			//¼öÁ¤ÇÑ Áö¿ªº° Å¬·¯½ºÅÍ¸µ
			anyOffice = (String)object.getIdentifier();

//System.out.println("ÀåºñÄÚµå=" + anyOffice );

			IlvGraphic g = object.getLayoutProxy(grapher);
			if (g != null)
			{
				if ( anyOffice.length() > 0 && anyOffice.startsWith( "SLSCSC" ) )
				{
					if (count == 0)
						nodeLayout.setRootClusterId(clusterId0);
					nodeLayout.setClusterId(g, clusterId0, count);
				}
				else if ( anyOffice.length() > 0 && anyOffice.startsWith( "DJYSBD" ) )
				{
					if (count == 0)
						nodeLayout.setRootClusterId(clusterId1);
					nodeLayout.setClusterId(g, clusterId1, count);
				}
				count++;
			}


			/* ¹ÞÀº ¼Ò½º´Â...
			if (object.hasAttributeValue(nodeTypeAttr))
			{

				if (object.getAttributeValue(nodeTypeAttr) != ilog.cpl.model.IlpAttributeValueHolder.VALUE_NOT_SET)
				{
					nodeType = object.getAttributeValue(nodeTypeAttr).toString();
				}
			}


			IlvGraphic g = object.getLayoutProxy(grapher);
			if (g != null)
			{
				if (nodeType.length() > 0 && nodeType.substring(0,3).equals("ACE"))
				{
					if (count == 0)
						nodeLayout.setRootClusterId(clusterId0);
					nodeLayout.setClusterId(g, clusterId0, count);
				}
				else if (nodeType.length() > 0 && nodeType.substring(0,3).equals("MSS"))
				{
					if (count == 0)
						nodeLayout.setRootClusterId(clusterId2);
					nodeLayout.setClusterId(g, clusterId2, count);
				}
				else
				{
					// CANS, AC120, etc
					if (count == 0)
						nodeLayout.setRootClusterId(clusterId2);
					nodeLayout.setClusterId(g, clusterId1, count);
				}
				count++;
			}
			*/
		}
	}

	public void setNodeLayout(boolean flag)
	{
		//final IlpNetworkView view = getNetwork().getView();
		//final IltCompositeGrapher grapher = view.getCompositeGrapher();

		//if(view != null) // no use layout
		//	return;

		System.out.println("Circular Layout starting .......");
		if (nodeLayout == null)
			nodeLayout = new IlvCircularLayout();
		nodeLayout.setLinkStyle(IlvCircularLayout.NO_RESHAPE_STYLE);
		//nodeLayout.setLinkStyle(IlvCircularLayout.STRAIGHT_LINE_STYLE);
		
		nodeLayout.setLevelOffset(50);
		
		if (flag)
			nodeLayout.setOffset(30);
		else
			nodeLayout.setOffset(50);

		nodeLayout.attach(grapher.getGraphModel());
		nodeLayout.setCoordinatesMode(IlvGraphLayout.VIEW_COORDINATES);
		
		//nodeLayout.setLayoutRegion( new ilog.views.IlvRect(80, 80, 300, 300) );
		nodeLayout.setLayoutRegion( managerView, new ilog.views.IlvRect(20, 20, 300, 300) );

		///////////////¡Ú¡Ú¡Ú///
		setCluster();

		try
		{
			nodeLayout.performLayout(true, false, true);
		}
		catch (IlvGraphLayoutException e)
		{
			e.printStackTrace();
		}
	}
	

	public static void main( String args[] )
	{
		JFrame f = new JFrame();
		f.getContentPane().add( "Center", new Demo() );
		f.setSize(400,400);
		f.setVisible(true);
	}
}
