/*
 * (C) IBM Corp. 1997-1998
 *
 * The program is provided "as is" without any warranty express or
 * implied, including the warranty of non-infringement and the implied
 * warranties of merchantibility and fitness for a particular purpose.
 * IBM will not be liable for any damages suffered by you as a result
 * of using the Program. In no event will IBM be liable for any
 * special, indirect or consequential damages or lost profits even if
 * IBM has been advised of the possibility of their occurrence. IBM
 * will not be liable for any third party claims against you.
 */

package com.ibm.xml.cdfedit;

import com.ibm.xml.parser.*;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.awt.event.*;

/**
 *
 * @version 1.1
 * @author Naohiko Uramoto
 */
public class SelectMenu implements ActionListener {

  String m_title;
  TXElement element;
  XMLComponent xc;  // component in cut buffer

  static String status;
  static XMLComponentEdt selectedXc;

  public SelectMenu(String title, TXElement el, XMLComponentEdt xlc) {
    m_title = title;
    element = el;
    xc = xlc;
    selectedXc = null;
    status = "null";
  }

  public void setSelectedXc (XMLComponentEdt sxc) {
    selectedXc = sxc;
  }

  public XMLComponentEdt getSelectedXc () {
    return selectedXc;
  }

  public String getStatus() {
    return status;
  }

  public void setStatus(String stat) {
    status = stat;
  }

  public void actionPerformed(ActionEvent e) {

    Container con = xc.getParent();
    Frame frm = null;
    while( (con = con.getParent()) != null) {
      if (con instanceof Frame) {
	frm = (Frame ) con; 
	frm.setCursor(new Cursor(Cursor.WAIT_CURSOR));
      }
    }
    if (e.getActionCommand().equals("copy")) {
      cdfeStatus.setStatus(cdfeStatus.SAVED);

      TXElement copiedElement = (TXElement )element.clone();
      //
      String urlstr = copiedElement.getAttribute("HREF");
      try {
	if (urlstr == null) 
	  urlstr = "http://xxx.com";
	URL u = new URL(urlstr);
	XMLComponentEdt newpc;
	if (xc instanceof ProcITEM) {
	  //	  System.out.println("item copy");
          TXElement titlel = (TXElement)copiedElement.getElementNamed("TITLE");

	  newpc = new ProcITEM(u, titlel.getText(), copiedElement);
	  //Gateway g = new Gateway();
	  ((XMLComponentM)newpc).setGateway(((XMLComponentM )xc).getGateway());
	  setSelectedXc((XMLComponentEdt ) newpc);

	} 
	else if (xc instanceof ProcCHANNEL) {
	  //	  System.out.println("channel copy");
	  //	  System.out.flush();
	  Transform tf = new Transform("", "com.ibm.xml.cdfedit.Proc");
	  XMLComponent copiedxc = tf.makeComponent(copiedElement);
	  //	  System.out.println(((ProcCHANNEL )copiedxc).getTitle());
	  ((XMLComponentM) copiedxc).setGateway( ((XMLComponentM )xc).getGateway());
	  setSelectedXc((XMLComponentEdt ) copiedxc);
	}
      } catch (Exception ex) {
	System.err.println("url");
	System.err.flush();
      }

    } 
    else if (e.getActionCommand().equals("new channel")) {
      TXElement newel = new TXElement("New Channel");
      ProcCHANNEL newpc;
      try {
	URL u = new URL("http://www.w3.org/pub/WWW/TR/WD-xml-lang");
	newpc = new ProcCHANNEL(u, "** New Channel. Edit Me! **", newel);
	Gateway g = new Gateway();
	((XMLComponentM)newpc).setGateway(g);

	// add Childrenlist
	((XMLComponentEdt )xc).addXMLComponent((XMLComponentEdt )newpc, "after");
	newpc.setTitleFgColor(Color.green);
	((XMLComponentEdt )newpc).newBox();
	((XMLComponentEdt )newpc).pbox.show();

	xc.doLayout();
	Container co = xc.getParent();
	do {
	  co.doLayout();
	} while (null != (co = co.getParent()));
	// reset string color to black from blue
      } catch (Exception ex) {
	System.err.println("url");
      }
    } 
    else if (e.getActionCommand().equals("new item")) {
      TXElement newel = new TXElement("New Item");
      ProcITEM newit;
      try {
	URL u = new URL("http://www.w3.org/pub/WWW/TR/WD-xml-lang");
	newit = new ProcITEM(u, "** New item Edit Me! **", newel);
	Gateway g = new Gateway();
	((XMLComponentM)newit).setGateway(g);

	// add Childrenlist
	((XMLComponentEdt )xc).addXMLComponent((XMLComponentEdt )newit, "after");
	//	  newpc.setTitleFgColor(Color.green);
	((XMLComponentEdt )newit).newBox();
	((XMLComponentEdt )newit).pbox.show();
	((XMLComponentEdt )newit).pbox.etypeTF.setText("ITEM");

	xc.doLayout();
	Container co = xc.getParent();
	do {
	  co.doLayout();
	} while (null != (co = co.getParent()));
	// reset string color to black from blue
      } catch (Exception ex) {
	System.err.println("url");
      }
    } 
    else if (e.getActionCommand().equals("view/edit")) {
      ((XMLComponentEdt )xc).newBox();
      ((XMLComponentEdt )xc).pbox.show();
      /*
      if (xc instanceof ProcITEM) {
	 ((XMLComponentEdt )xc).pbox.etypeTF.setText("ITEM");
      } else {
	 ((XMLComponentEdt )xc).pbox.etypeTF.setText("CHANNEL");
      }
      Element el = element.searchChildren("TITLE");
      String estr;
      if (el != null) {
	estr = el.getText();
	if (estr != null) 
	  ((XMLComponentEdt )xc).pbox.titleTF.setText(estr);
	else 
	  ((XMLComponentEdt )xc).pbox.titleTF.setText("");
      } else 
	  ((XMLComponentEdt )xc).pbox.abstTF.setText("");
      el = element.searchChildren("AUTHOR");
      if (el != null) {
	estr = el.getAttribute("Value");
	if (estr != null) 
	  ((XMLComponentEdt )xc).pbox.authorTF.setText(estr);
	else 
	  ((XMLComponentEdt )xc).pbox.authorTF.setText("");
      } else
	((XMLComponentEdt )xc).pbox.authorTF.setText("");
      el = element.searchChildren("ABSTRACT");
      if (el != null) {
	estr = el.getText();
	if (estr != null) 
	  ((XMLComponentEdt )xc).pbox.abstTF.setText(estr);
	else 
	  ((XMLComponentEdt )xc).pbox.abstTF.setText("");
      } else 
	((XMLComponentEdt )xc).pbox.abstTF.setText("");

      estr = element.getAttribute("HREF");
      if (estr != null) 
	((XMLComponentEdt )xc).pbox.hrefTF.setText(estr);
      else 
	((XMLComponentEdt )xc).pbox.hrefTF.setText("");
	*/
      
      //      status = "copy";
    } 
    // ****** Paste Process (1) *****         
    else if (e.getActionCommand().equals("paste as last child")) {
      cdfeStatus.setStatus(cdfeStatus.UNSAVED);
      if (selectedXc != null) {
	Container co = xc.getParent();
	// XMLComponentEdt xcm = (XMLComponentEdt )co;
	//	System.out.println("SELECT="+ ((ProcCHANNEL )selectedXc).getTitle());
	//	Parent pp = selectedXc.getElement().getParent();
	//	System.out.println("SELECT2="+ ((Element )pp).getText());
      
	// Check if the node can move
	if (((XMLComponentEdt )xc).CanMoveToComponentP(selectedXc)) {
	  //	  System.out.println("paste2");
	  // add Childrenlist
	  ((XMLComponentEdt )xc).addXMLComponentLast(selectedXc);
	  xc.doLayout();
	  do {
	    co.doLayout();
	  } while (null != (co = co.getParent()));
	}
	// reset string color to black from blue
	//	if (xcs instanceof ProcCHANNEL) 
	//  ((ProcCHANNEL )xcs).setFgLabelColor(Color.black);
	
	// buffer is cleared!!
	selectedXc = null;
      }
    }
    // ****** Paste Process (2) *****         
    else if (e.getActionCommand().equals("paste after")) {
      cdfeStatus.setStatus(cdfeStatus.UNSAVED);
      //      System.out.println("Paste after");
      if (selectedXc != null) {
	// System.out.println("Paste after2");

	Container co = xc.getParent();
	// XMLComponentEdt xcm = (XMLComponentEdt )co;

	// add Childrenlist

	((XMLComponentEdt )xc).addXMLComponent(selectedXc, "after");
	xc.doLayout();
	do {
	  co.doLayout();
	} while (null != (co = co.getParent()));

	// reset string color to black from blue
	//	if (xcs instanceof ProcCHANNEL) 
	//  ((ProcCHANNEL )xcs).setFgLabelColor(Color.black);
	
	// buffer is cleared!!
	selectedXc = null;
      }
    }
    // ****** Paste Process (3) *****         
    else if (e.getActionCommand().equals("paste before")) {
      //      System.out.println("Paste before");
      cdfeStatus.setStatus(cdfeStatus.UNSAVED);
      if (selectedXc != null) {

	Container co = xc.getParent();
	// XMLComponentEdt xcm = (XMLComponentEdt )co;

	// add Childrenlist
	((XMLComponentEdt )xc).addXMLComponent(selectedXc, "before");
	xc.doLayout();
	do {
	  co.doLayout();
	} while (null != (co = co.getParent()));

	// reset string color to black from blue
	//	if (xcs instanceof ProcCHANNEL) 
	//  ((ProcCHANNEL )xcs).setFgLabelColor(Color.black);
	
	// buffer is cleared!!
	selectedXc = null;
      }
    }
    // ****** Delete/Cut Process *****         
    else if (e.getActionCommand().equals("delete") || e.getActionCommand().equals("cut")) {
      cdfeStatus.setStatus(cdfeStatus.SAVED);
      // push to cut buffer
      if (e.getActionCommand().equals("cut")) {
	setSelectedXc((XMLComponentEdt )xc);
	//	System.out.println("SELL=" + ((ProcCHANNEL )selectedXc).getTitle());
      }
      status = "done";
      // System.out.println("cut");
      // System.out.flush();
      Parent p = (Parent)element.getParentNode();
      //  System.out.println("s="+ ((Element )p).getText());
      if (!(p instanceof TXDocument)) { // Top channel cannot be removed
	// System.out.println("cut2");
	//	System.out.println( element.getText());
	// System.out.flush();
	((TXElement )p).removeChild(element);
	//	System.out.println("DELETE: " + m_title);
	//	System.out.println("cut2.5");
	// System.out.flush();
	
	/*
	  while (!(p instanceof Document)) {
	  p = ((Element )p).getParent();
	  }
	  System.err.println("reached");
	  Document doc = (Document )p;
	  doc.print(new PrintWriter(new OutputStreamWriter(System.out)));
	*/
	
	// remove xc from parent panel
	Container co = xc.getParent();
	//	xc.m_isopen = false;
	co.remove(xc);
	XMLComponentEdt xcm = (XMLComponentEdt )co;
	xcm.removeXMLComponent(xc);
	// re-layout tree
	do {
	  co.doLayout();
	} while (null != (co = co.getParent()));
	
      }
    }

    // reset cursor
    if (frm != null) 
      frm.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  }



}





