// File: CdPanel.java
// Classes: CdPanel
// Author: Kedar Patankar

package edu.neu.ccs.demeter.tools.apstudio.graphedit;

import java.util.Vector;
import java.util.Hashtable;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Panel;
import java.awt.Dimension;
import java.awt.CardLayout;
import java.awt.ScrollPane;
import java.awt.Adjustable;

/* Purpose : MDI interface provider. Holds all the open documents */

public class CdPanel extends Panel
{
	private Editor _editor;

	private UmlPalette _palette;
	private Panel _documentPanel;
	
	private int _documentCount;
	private int _noOfDocuments;
	private Vector _documentNames;
	private Vector _outStanding;
	private Hashtable _documentTable;
	private Hashtable _scrollTable;
	private CardLayout _cardLayout;
  
	private String _currentDocname;

  /** Construct a new Editor to edit the given NetList */
	public CdPanel(Editor editor)
	{
		_documentNames=new Vector();
		_outStanding=new Vector();
		_documentTable=new Hashtable();
		_scrollTable=new Hashtable();
		_documentCount=0;
		_noOfDocuments=0;
		_editor=editor;
		_currentDocname = null;
// objects are initialized
		setLayout(new BorderLayout());
		_documentPanel=new Panel();
		_cardLayout=new CardLayout();
		_documentPanel.setLayout(_cardLayout);
		add("Center",_documentPanel);
		_palette=new UmlPalette(_editor);
		_palette.setBackground(Color.gray);
		add("South",_palette);
// components of this panel are added		
	}

	public void showUmlToolbar(boolean status)
	{
		if(status)
		{
			add("South",_palette);
			validate();
		}
		else
		{
			remove(_palette);
			validate();
		}
	}

	public Vector get_outStanding(){return _outStanding;}

	public Vector get_openDocnames(){return _documentNames;}

	public void addInOutstanding(String s)
	{
		 _outStanding.removeElement(s);
		 _outStanding.addElement(s);
	}
	public void removeFromOutstanding(String s){_outStanding.removeElement(s);}

	public int getNoOfOpenDocuments(){ return _noOfDocuments;}
	public boolean isPresent(String s){return _documentTable.get(s)!=null;}
	public int nextDocumentNumber(){return ++_documentCount;}
	public void refresh(){ _palette.refresh();}
	public Document curDocument(){ return getDocument(_currentDocname);}
  
	public Document getDocument(String s)
	{
		if(s==null)
			return null;
		return (Document)_documentTable.get(s);
	}

	public void updateDocname(String previousName,String presentName)
	{
		Document d=(Document)_documentTable.remove(previousName);
		ScrollPane sp = (ScrollPane)_scrollTable.remove(previousName);
		_documentNames.removeElement(previousName);
		_documentPanel.remove(sp);

		get_outStanding().removeElement(previousName);
		d.setDocName(presentName);

		_documentPanel.add(presentName,sp);
		_documentNames.addElement(presentName);
		_scrollTable.put(presentName,sp);
		_documentTable.put(presentName,d);
	}

	public void showDocument(String s)
	{
		_cardLayout.show(_documentPanel,s);
		Document document = (Document)_documentTable.get(s);

		switch(document.selection().size())
		{
			case 0 : _editor.menuFornoSelection();document.showDefaultPropertySheet();break;
			case 1 : _editor.menuForsingleSelection();
					DiagramElement de = document.selectionsingle();
					if (de instanceof Perspective)
						document.showVertexPropertySheet((Perspective)de);
					else
						document.showEdgePropertySheet((ArcPerspective)de);
					break;
			default: _editor.menuFormultipleSelection();document.showDefaultPropertySheet();
		}
		_currentDocname = s;
		setVisible(true);
	}

	public void attach(String name,Document d)
	{
		ScrollPane sp = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
		Adjustable vadjust = sp.getVAdjustable();
		Adjustable hadjust = sp.getHAdjustable();
		vadjust.setUnitIncrement( 50 );
		hadjust.setUnitIncrement( 50 );
		sp.add(d);
		sp.setSize(d.getPreferredSize()); // To fill the entire window. Initial size of Scrollpane = 100x100
// Bug :
// The scroll-bars are not shown when opened for the first time.

		_scrollTable.put(name,sp);
		_documentPanel.add(name,sp);
		_documentNames.addElement(name);
		_documentTable.put(name,d);
		_noOfDocuments +=1;
		showDocument(name);
	}
	
	public String detach(String name,Document d)
	{
		ScrollPane sp = (ScrollPane)_scrollTable.remove(name);
		_documentPanel.remove(sp);
		_documentTable.remove(name);
		_documentNames.removeElement(name);
		_noOfDocuments -=1;
		get_outStanding().removeElement(name);
		if(_noOfDocuments>=1)
		{
			String title=(String)_documentNames.elementAt(0);
			showDocument(title);
			return title;
		}
		else
		{
			_editor.showBlankPropertySheet();
			_currentDocname=null;
			return null;
		}
	}
}/* end class CdPanel */

