import java.awt.*;
import java.awt.event.*;

import java.net.*;
import graph.*;
import jow.*;
import jowgraphservice.*;
import jowadmin.*;

// ***********************************************************
// *********************** TextWindow ************************
// ***********************************************************


class TextWindow extends Frame implements Runnable,ActionListener,WindowListener  {

  GraphApplet source;
  JOWGraphServiceInterface jowGraphService;
  ContentObject content;
  JOWText textobject;
  MIME_Type type;

  TextWindow ( GraphApplet ga, JOWGraphServiceInterface ds, URL url )  {
    super ( "Text Window" );
    jowGraphService = ds;
    source = ga;
    try {
      content = (ContentObject)ds.openGraphContent( url);
      type = content.getType();
    } catch (JOWException e) {
        System.out.println("Text Window: Open Text Fail");
      }

    this.setTitle("Text Window:"+content.getLabel());
    this.addWindowListener(this);
  }  // end TextWindow ( DomainService ds, URL url )

  TextWindow ( GraphApplet ga, JOWGraphServiceInterface ds, Graph g, MIME_Type t )  {
    super ( "Text Window" );
    jowGraphService = ds;
    source = ga;
    content = new ContentObject ( g, "A New ContentObject: Text" );  // default
    content.setType ( t );
    type = t;
    this.addWindowListener(this);
  }  // end TextWindow( GraphApplet ga, JOWGraphServiceInterface ds, Graph g, MIME_Type t ) constructor

  TextWindow ( GraphApplet ga, JOWGraphServiceInterface ds, ContentObject co )  {
    super ( "Text Window" );
    jowGraphService = ds;
    source = ga;
    content = co;
    type = co.getType();
    this.addWindowListener(this);
  }  // end TextWindow( GraphApplet ga, JOWGraphServiceInterface ds, Graph g, MIME_Type t ) constructor

  public void start () {
    buildWindow ();
  }  // end start

  public void stop () {
    //source.removeWindow ();
    this.hide ();
    this.dispose ();
  }  // end stop

  public void run () {
  }  // end run

// ***************************** Menu Action Methods

  private void dumpData() {
      String strdata = textarea.getText();
      byte[] bytedata = strdata.getBytes();
      textobject.iFileSize = bytedata.length;
      textobject.bFileData = bytedata;
  }
  public void SystemSave () {
   System.out.println ("TextWindow: Save ContentObject");
    try {
      dumpData();
      content.setContent ( textobject );
      printMsg();
      jowGraphService.saveGraphContent( content );
    } catch (JOWException e) {
        System.out.println("Text Window: Close Fail");
      }
  }  // end SystemSave

  public void SystemClose () {
   System.out.println ("TextWindow: Save and Close ContentObject");
   try {
      dumpData();
      content.setContent ( textobject );
      //printMsg();
    //MessageDialog msgd=new MessageDialog(this,"Alarm","Do you really want to save the graph before closing it");
    //msgd.show();
    //boolean ok=msgd.getValue();
    boolean ok=true;
      jowGraphService.closeGraphContent( content,ok );
   //System.out.println ("Just after call to DoaminService");
    } catch (JOWException e) {
        System.out.println("Text Window: Close Fail");
      }
    //source.removeWindow ();
    this.hide ();
    this.dispose ();
  }  // end SystemClose

public void printMsg()  {
    System.out.println("Save/Close:");
    System.out.println("  UID:"+content.getUID());
    //System.out.println("  content:"+(String)content.getContent());
}



// ***************************** Handle Actions and Events
/*
  public boolean action(Event event, Object object) {

    if (event.target instanceof MenuItem) {
      // System Menu Items
      if ( event.target == systemSave ) {
        SystemSave ();
        return true;
      }  //  end SystemSave
      if ( event.target == systemClose ) {
        SystemClose ();
        return true;
      }  //  end SystemClose
    }  // end MenuItems

    return false;  // action not processed here

  }  // end action



public boolean handleEvent(Event evt) {

    if ( evt.id == Event.WINDOW_DESTROY ) {
        SystemClose ();
        return true;
    }  // end if

		return super.handleEvent(evt);

}  // end handleEvent
*/

public void actionPerformed(ActionEvent e) {
    Object esource=e.getSource();

    if (esource instanceof MenuItem) {
      // System Menu Items
      if ( esource == systemSave ) {
        SystemSave ();
      }  //  end SystemSave
      if ( esource == systemClose ) {
        SystemClose ();
      }  //  end SystemClose
    }  // end MenuItems
}


// implement the WindowListener interface
public void windowActivated(WindowEvent e) {
}

public void windowClosed(WindowEvent e) {
}

public void windowClosing(WindowEvent e) {
  if (e.getSource()==this) {
        SystemClose ();
 }  // end if
}

public void windowDeactivated(WindowEvent e) {
}

public void windowDeiconified(WindowEvent e) {
}

public void windowIconified(WindowEvent e) {
}

public void windowOpened(WindowEvent e) {
}


// ***************************** BuildWindow

   /*
   Since TextWindow extends Frame, BuildWindow uses self
   as the outer container.  Hence, events are captured in
   this class.
   */


//  Global variables for window


   Menu systemMenu;
     MenuItem systemSave, systemClose;
   Menu helpMenu;
      MenuItem helpHelp;
   TextArea textarea;
   String text;

  public void buildWindow () {

  Panel menuPanel;  // area for menus

    this.setLayout (new BorderLayout());
    menuPanel = new Panel();
    menuPanel.setLayout (new FlowLayout());

    MenuBar mb = new MenuBar();

    systemMenu = new Menu("System");
      systemSave = new MenuItem ( "Save" );
      systemMenu.add( systemSave );
      systemClose = new MenuItem ( "Close" );
      systemMenu.add( systemClose );

    helpMenu = new Menu ("Help");
      helpHelp= new MenuItem ( "Help" );
      helpMenu.add( helpHelp);

    mb.add(systemMenu);
    mb.add(helpMenu);
    mb.setHelpMenu(helpMenu);
    textobject = (JOWText) content.getContent();
    if ( textobject == null ) {
        textobject = new JOWText();
        if (type.getSubtype() == MIME_Type.HTML )  {
                textobject.setSubtypeAsHTMLText();
                text = new String("<HTML>\n<HEAD>\n<TITLE>title</TITLE>\n</HEAD>\n<BODY BGCOLOR='FFFFFF'>\n<CENTER>\n<H1>Hello, World, from WWWNG!</H1>\n</CENTER>\n</BODY>\n</HTML>");
        }
            else {
               textobject.setSubtypeAsPlainText();
               text = new String("Enter text here.");
            }
    }
    else { text = (String) textobject.getStringValue(); }

    textarea = new TextArea( text, 100, 60, 1 );

    this.setMenuBar(mb);
    this.add("Center", textarea);

    this.setBackground(Color.white);
    this.setForeground(Color.blue);


    systemSave.addActionListener(this);
    systemClose.addActionListener(this);
    helpHelp.addActionListener(this);



    this.resize(600, 500);
    this.show();

  }  // end buildWindow

}  // end TextWindow

