package graphbrowser;

import java.awt.event.*;
import java.applet.*;
import java.awt.*;

import graph.*;


public class GraphPropDialog extends Dialog implements ActionListener,ItemListener {


  Label     label1,label2,label3;
  TextField text1,text2,text3;

  Choice    mime;
  Button    okButton,cancelButton;
  String    value1,value2;
  boolean   pmode;
  MIME_Type mt;

  public static String graphc="Graph";
  public static String htmlc="HTML Content";
  public static String textc="PLAIN TEXT Content";

  public GraphPropDialog(Frame parent,String title,MIME_Type mt) {
      super(parent,title,true); // model dialog
      pmode=true;
      reset();
  }

  public GraphPropDialog(Frame parent,String title) {
      super(parent,title,true); // model dialog
      pmode=false;
      reset();
  }


  private String translate(MIME_Type mt) {
    if (mt.getType()==mt.GRAPH) return graphc;
    else if (mt.getType()==mt.TEXT)
        if (mt.getSubtype()==mt.HTML) return htmlc;
            else if (mt.getSubtype()==mt.PLAIN) return textc;
    return graphc;
  }
  public void reset() {

      value1=new String("");
      //value2=new String("");
      label1=new Label("Label");
      label2=new Label("Content");
      text1=new TextField(10);
      //text2=new TextField(10);
      //text3=new TextField(10);

      mime=new Choice();
      mime.addItem(graphc);
      mime.addItem(htmlc);
      mime.addItem(textc);
      //mime.addItem();

      okButton=new Button("OK");
      //cancelButton=new Button("Cancel");

      setLayout(new BorderLayout() );

      Panel p1=new Panel();
      p1.setLayout(new GridLayout(2,2,0,2) );
      p1.add(label1);
      p1.add(text1);

      p1.add(label2);
      p1.add(mime);

      if (pmode) { mime.select(translate(mt)); mime.setEnabled(false); }
        else { mime.select(graphc);
                mt=new MIME_Type(); }

      //p1.add(label3);
      //p1.add(text3);

      //text2.setEchoChar('*'); // for password use
      //text3.setEchoChar('*'); // for password use

      Panel p2=new Panel();
      p2.setLayout(new FlowLayout(FlowLayout.CENTER));
      p2.add(okButton);
      //p2.add(cancelButton);

      // add event listeners
      okButton.addActionListener(this);
      //cancelButton.addActionListener(this);
      mime.addItemListener(this);
      //text2.addActionListener(this);

      this.setEnabled(true);
      text1.setEnabled(true);  // enable editing of text1

      this.add(p1,"North");
      this.add(p2,"South");
      this.pack();

      //this.show();
  }

  public void setLabel(String value) {
    text1.setText(value);
    //text2.setText("");
    //text3.setText("");
  }

  public void actionPerformed(ActionEvent e) {

    //public boolean action(Event event, Object object) {
     Object esource=e.getSource();
    //Object esource=event.target;

    if ( (esource == okButton) ) {
        // error checking and setting the value
        value1=text1.getText();
        if ( value1.equals("") )return;// true;
        this.setVisible(false);
        return;// true;
    }

  }

  public void itemStateChanged(ItemEvent e) {
    String selected=(String) e.getItem();
    if (selected ==null) return;
    if (selected.equals(graphc))
        { mt.setType(MIME_Type.GRAPH);
          mt.setSubtype(MIME_Type.NET);}
     else if (selected.equals(htmlc))
        { mt.setType(MIME_Type.TEXT);
          mt.setSubtype(MIME_Type.HTML);}
     else if (selected.equals(textc))
        { mt.setType(MIME_Type.TEXT);
          mt.setSubtype(MIME_Type.PLAIN);}
  }
  // callback for external data-fetching use

  public String getLabel() {
    return value1;
  }

  public MIME_Type getType() {
    return mt;
  }

}