// File: CdView.jaa
// Classes: CdView
// Authors: Fred DiPrizito
// Date 3 March 1997

// Authors:  Modified by Kevin Carlson and Dave Boatwright
// Date 11 March 1997

package uci.graphedit;
import gjt.DialogClient;
import java.awt.*;
import java.util.Vector;
import java.util.Enumeration;
import java.util.Hashtable;
import demeter.Ident;


/* View the current cd  */

public class GcdView extends Frame{
  
  // Components
  Button	dismiss, refresh;
  TextArea	cd_text;
  Panel		button_panel, text_panel;
  
  BorderLayout	borderlayout	= new BorderLayout();
  
  GridLayout	grid		= new GridLayout( 1, 2 );

  Document	document;
	
	
  // Constructor
  GcdView( Document doc ){

    // Name this window and set it up
    super("GCD-File: " + doc.getDocName() );
    setLayout( borderlayout );
    resize( 400, 600);

    //store the current document in the curr obj
    document = doc;
   
    // Create the Buttons
    dismiss = new Button( "Dismiss" );
    refresh = new Button( "Refresh" );
    
    // Create the text area
    cd_text = new TextArea(24, 80);
    cd_text.setEditable(false);

    // convert the document's graph information to a gcd format 
    // that would be saved
    cd_text.setText( this.get_graph( document ) );
    
    // create a button panel 
    button_panel = new Panel();
    button_panel.setLayout( grid );		
    button_panel.add( dismiss );
    button_panel.add( refresh );
    
    // add the text area and the buttons to the window		
    add( "Center",  cd_text );		
    add( "South",  button_panel );
    show();
  }
	
  public boolean action( Event event, Object arg ){
    
    if ( event.target == dismiss ){
      this.hide();
      this.dispose();
      return( true );
    }
    if ( event.target == refresh ){
      cd_text.setText( this.get_graph( document) );
      return( true );
    } 
    else{ 
      return( false );
    }

    
  }

  // function to convert the documents graph information to gcd format
  private String get_graph( Document document ){
    

    if ( document == null ){
      return( "# -- NO DATA --");
    }

    UGraph graph	= new UGraph();
    
    Vector nodes	= document.net().nodes();
    Enumeration elm	= nodes.elements();
    Hashtable hTable	= new Hashtable();
	  
    

    while (elm.hasMoreElements())
      {
	NetNode n=(NetNode)elm.nextElement();
	Point p=n.get_Perspective().position();
	Coordinates c=new Coordinates(new X(new Integer(p.x)),new Y(new Integer(p.y)));
	String name=n.get_Perspective().get_first().get_label();
	UID uid=new UID(new Integer(n.getId()));
	UVertex u;
		  
	if (n.get_Perspective().get_first() instanceof FigAltVert)
	  u=new UAltVertex();
	else 
	  if (n.outSize()==0)
	    u=new UTerm();
	  else
	    u=new UConstVertex();
	u.set_id(uid);
	u.set_vertexname(new UVertexName(new Ident(name)));
	u.set_position(c);
		  
	Vector in=n.inList();
	Enumeration inEdges = in.elements();
	while(inEdges.hasMoreElements())
	  {
	    UID id=new UID(new Integer(((NetArc)inEdges.nextElement()).getId()));
	    u.add_incoming(id);
	  }
	Vector out=n.outList();
	Enumeration outEdges = out.elements();
	while(outEdges.hasMoreElements())
	  {
	    UID id=new UID(new Integer(((NetArc)outEdges.nextElement()).getId()));
	    u.add_outgoing(id);
	  }

	hTable.put(name,u);
	graph.add_uvertex(u);
      }
  
    Enumeration elm1 = nodes.elements();

    while (elm1.hasMoreElements())
      {
	NetNode n=(NetNode)elm1.nextElement();
	String source=n.get_Perspective().get_first().get_label();

	Vector out=n.outList();
	Enumeration outEdges = out.elements();
	while(outEdges.hasMoreElements())
	  {
	    NetArc na=(NetArc)(outEdges.nextElement());
	    ArcPerspective ap=na.get_perspective();
	    Fig f=ap.get_figure();
	    String dest=na.destNode().get_Perspective().get_first().get_label();
	    UID id=new UID(new Integer(na.getId()));
	    UEdge u;
	    if (f instanceof FigConstEdge)
	      {
		u=new UConstEdge();
		((UConstEdge)u).set_edgename(new UEdgeName(new Ident(f.get_label())));
		Cardinality c;
		String card=((FigConstEdge)f).get_cardinality();
		if (card.equals("1"))
		  c=new Cardinality(new Lower(new Integer(1)),null);
		else
		  if (card.equals("0..1"))
		    c=new Cardinality(new Lower(new Integer(0)),
				      new Upper(new String("1")));
		  else
		    if (card.equals("0..*"))
		      c=new Cardinality(new Lower(new Integer(0)),
					new Upper(new String("*")));
		    else
		      c=new Cardinality(new Lower(new Integer(1)),
					new Upper(new String("*")));
		((UConstEdge)u).set_card(c);
	      }
	    else
	      u=new UAltEdge();
	    u.set_id(id);
	    if(hTable.get(source)==null)
	      {System.out.println("problem source");return( "-- Error -- " );}
	    if(hTable.get(dest)==null)
	      {System.out.println("problem");return( "-- Error -- " );}
	    u.set_fromVertex((UVertex)hTable.get(source));
	    u.set_toVertex((UVertex)hTable.get(dest));
	    graph.add_uedge(u);
	  }
      }

	  	  
    if(graph!=null)
      {
	return ( graph.GetGraphString() );
      }
    else
      {
	return( "--- No Data ---");
      }
  }

    
}