// File: AltVertPartReorderDialog.java
// Class: AltVertPartReorderDialog
// Author: Kedar Patankar

package EDU.neu.ccs.demeter.tools.apstudio.graphedit;

import java.awt.Frame;
import java.awt.Panel;
import java.awt.Button;
import java.awt.List;
import java.awt.Event;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.Dimension;
import java.awt.Dialog;
import java.awt.Toolkit;
import java.awt.Insets;
import java.awt.Component;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Label;

import java.util.Vector;

import com.sun.java.swing.JButton;
import com.sun.java.swing.ImageIcon;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;

/* Purpose : Allow reordering of parts */

public class AltVertPartReorderDialog extends Dialog implements ActionListener//,FocusListener
{
	// Components
	private Button _ok,_cancel;
	private List _constPartList,_altPartList;
	private JButton _altUp,_altDown,_constUp,_constDown;
	private int _noOfConstItems, _noOfAltItems;
	private Vector v1,v2;
	private boolean _changed=false;
//references
	private Editor _editor;
	private UVertex _vertex;
	private Panel _mainPanel;

	// Constructor
	public AltVertPartReorderDialog(Frame frame,String title,UVertex _v)
	{
		super(frame,title,true);
		_editor=(Editor)frame;
		String INSTALL_DIR=_editor.getInstallDir();
		setLayout( new BorderLayout() );
		_vertex=_v;
		
		// Create the dialog control buttons
		Panel button_panel = new Panel();
		_ok=new Button("Ok");
		_cancel=new Button("Cancel");
		_ok.addActionListener(this);
		_cancel.addActionListener(this);
		button_panel.add(_ok);
		button_panel.add(_cancel);

		_mainPanel = new Panel();
		GridBagLayout gbl = new GridBagLayout();
		_mainPanel.setLayout(gbl);
		_constPartList = new List(5,false);
		_altPartList = new List(5,false);
		_constPartList.addActionListener(this);
		_altPartList.addActionListener(this);
//		_constPartList.addFocusListener(this);
//		_altPartList.addFocusListener(this);

		_altUp = new JButton(new ImageIcon(Toolkit.getDefaultToolkit().getImage(INSTALL_DIR+"/images/up.gif")));
		_altUp.setToolTipText("Up");
		_altUp.setMargin(new Insets(0,0,0,0));
		_altUp.addActionListener(this);
		_altDown = new JButton(new ImageIcon(Toolkit.getDefaultToolkit().getImage(INSTALL_DIR+"/images/down.gif")));
		_altDown.setToolTipText("Down");
		_altDown.setMargin(new Insets(0,0,0,0));
		_altDown.addActionListener(this);
		_constUp = new JButton(new ImageIcon(Toolkit.getDefaultToolkit().getImage(INSTALL_DIR+"/images/up.gif")));
		_constUp.setToolTipText("Up");
		_constUp.setMargin(new Insets(0,0,0,0));
		_constUp.addActionListener(this);
		_constDown = new JButton(new ImageIcon(Toolkit.getDefaultToolkit().getImage(INSTALL_DIR+"/images/down.gif")));
		_constDown.setToolTipText("Down");
		_constDown.setMargin(new Insets(0,0,0,0));
		_constDown.addActionListener(this);

		add(new Label("Associations.."),0,0,1,1,0.0,0.0);
		add(new Label("Alternations.."),2,0,1,1,0.0,0.0);
		add(_constPartList,0,1,1,5,1.0,1.0);
		add(_constDown,1,1,1,1,0,0);
		add(_constUp,1,2,1,1,0,0);
		add(_altPartList,2,1,1,5,1.0,1.0);
		add(_altDown,3,1,1,1,0,0);
		add(_altUp,3,2,1,1,0,0);

		add("Center",_mainPanel);
		add("South",button_panel );
		pack();
// jdk 1.1 type window event handling
		addWindowListener(new TVAdapter()); // using inner class for window handling.


		v1 = new Vector();
		v2 = new Vector();
		Document d =_editor.curDocument();
		if(_v instanceof UAltVertex)
		{
			for(int i=0;i<((UAltVertex)_v).insertion_pointer;i++)
			{
				v1.addElement((UID)_v.get_outArcIdList().elementAt(i));
				UEdge e = d.net().getArc(((UID)_v.get_outArcIdList().elementAt(i)));
				String to  = d.net().getNode(e.get_toVertex()).get_vertexname().get_name().toString();
				_altPartList.addItem(to);
			}
			for(int i=((UAltVertex)_v).insertion_pointer;i<_v.get_outArcIdList().size();i++)
			{
				v2.addElement((UID)_v.get_outArcIdList().elementAt(i));
				UEdge e = d.net().getArc(((UID)_v.get_outArcIdList().elementAt(i)));
				String to  = d.net().getNode(e.get_toVertex()).get_vertexname().get_name().toString();
				_constPartList.addItem(to);
			}
		}
		_noOfConstItems=_constPartList.getItemCount();
		_noOfAltItems=_altPartList.getItemCount();
		if(_noOfAltItems>0)
			_altPartList.select(0);
		if(_noOfConstItems>0);
			_constPartList.select(0);
		
		if(_noOfAltItems < 2)
		{
			_altUp.setEnabled(false);
			_altDown.setEnabled(false);
		}
		if(_noOfConstItems < 2)
		{
			_constUp.setEnabled(false);
			_constDown.setEnabled(false);
		}
//		for(int j=0;j<_noOfAltItems+_noOfConstItems;j++)
//			System.out.println(v.elementAt(j));

	}

	private void add(Component comp,int x,int y, int w, int h, double weightx, double weighty)
	{
		GridBagLayout gbl = (GridBagLayout)_mainPanel.getLayout();
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.gridx = x;
		c.gridy = y;
		c.gridwidth = w;
		c.gridheight = h;
		c.weightx = weightx;
		c.weighty = weighty;
		_mainPanel.add(comp);
		gbl.setConstraints(comp,c);
	}
  

	private void close() { setVisible(false);dispose(); }

	public void setVisible(boolean status)
	{
		if(status)
		{
			_ok.requestFocus();
			Dimension frameSize = getParent().getSize();
			Point     frameLoc  = getParent().getLocation();
			Dimension mySize    = getSize();
			int       x,y;

			x = frameLoc.x + (frameSize.width/2) -(mySize.width/2); 
			y = frameLoc.y + (frameSize.height/2)-(mySize.height/2); 
			setBounds(x,y,getSize().width,getSize().height);
		}
		super.setVisible(status);
	}
	
// jdk 1.1 style event handling.
/*	public void focusGained(FocusEvent event)
	{
		Object source = event.getSource();
		if(source == _constPartList)
		{
			int i = _constPartList.getSelectedIndex();
			if(i==-1) // This removes tabbing. we are interested only in mouse click
				return;
			i = _altPartList.getSelectedIndex();
			if(i!=-1)
				_altPartList.deselect(i);
		}
		else
		if(source == _altPartList)
		{
			int i = _altPartList.getSelectedIndex();
			if(i==-1) // This removes tabbing. we are interested only in mouse click
				return;
			i = _constPartList.getSelectedIndex();
			if(i!=-1)
				_constPartList.deselect(i);
		}
	}

	public void focusLost(FocusEvent event)
	{
	}
*/
	public void actionPerformed(ActionEvent event)
	{

		Object source = event.getSource();
		if(source == _ok)
		{
			if(_changed)
			{
				Vector v = new Vector();

				for(int i=0;i<_noOfAltItems;i++)
					v.addElement(v1.elementAt(i));

				for(int i=0;i<_noOfConstItems;i++)
					v.addElement(v2.elementAt(i));
				
				_vertex.set_outArcIdList(v);
				_editor.curDocument().cdNeedsSaving(true);
			}
			close();
		}
		else
		if(source == _altUp)
		{
			int current = _altPartList.getSelectedIndex();
			if(current == 0)
				return;
			else
			{
				int next = current-1;
				String toBeMovedUp = _altPartList.getItem(current);
				String toBeMovedDown = _altPartList.getItem(next);
				_altPartList.replaceItem(toBeMovedUp,next);
				_altPartList.replaceItem(toBeMovedDown,current);
				_altPartList.deselect(current);
				_altPartList.select(next);
				Object o = v1.elementAt(current);
				v1.setElementAt(v1.elementAt(next),current);
				v1.setElementAt(o,next);
				_changed=true;
			}
		}
		else
		if(source == _altDown)
		{
			int current = _altPartList.getSelectedIndex();
			if(current == _noOfAltItems-1)
				return;
			else
			{
				int next = current+1;
				String toBeMovedDown = _altPartList.getItem(current);
				String toBeMovedUp = _altPartList.getItem(next);
				_altPartList.deselect(current);
				_altPartList.replaceItem(toBeMovedUp,current);
				_altPartList.replaceItem(toBeMovedDown,next);
				_altPartList.select(next);
				Object o = v1.elementAt(current);
				v1.setElementAt(v1.elementAt(next),current);
				v1.setElementAt(o,next);
				_changed=true;
			}
		}
		else
		if(source == _constUp)
		{
			int current = _constPartList.getSelectedIndex();
			if(current == 0)
				return;
			else
			{
				int next = current-1;
				String toBeMovedUp = _constPartList.getItem(current);
				String toBeMovedDown = _constPartList.getItem(next);
				_constPartList.replaceItem(toBeMovedUp,next);
				_constPartList.replaceItem(toBeMovedDown,current);
				_constPartList.deselect(current);
				_constPartList.select(next);
				Object o = v2.elementAt(current);
				v2.setElementAt(v2.elementAt(next),current);
				v2.setElementAt(o,next);
				_changed=true;
			}
		}
		else
		if(source == _constDown)
		{
			int current = _constPartList.getSelectedIndex();
			if(current == _noOfConstItems-1)
				return;
			else
			{
				int next = current+1;
				String toBeMovedDown = _constPartList.getItem(current);
				String toBeMovedUp = _constPartList.getItem(next);
				_constPartList.deselect(current);
				_constPartList.replaceItem(toBeMovedUp,current);
				_constPartList.replaceItem(toBeMovedDown,next);
				_constPartList.select(next);
				Object o = v2.elementAt(current);
				v2.setElementAt(v2.elementAt(next),current);
				v2.setElementAt(o,next);
				_changed=true;
			}
		}
		else
			close();
	}


// inner class VDAdapter

	class TVAdapter extends WindowAdapter
	{
		public void windowClosing(WindowEvent event)
		{
			close();
		}
	}
} /* end class TextView */

