
import java.applet.*;
import java.awt.*;
import java.util.*;

import netscape.javascript.*;


//public class JavaLiveConnect extends java.applet.Applet
public class JavaLiveConnect extends java.applet.Applet
{

   String text = "I'm a simple applet";
   String circleID = "oneCircle";

   public void init()
   {
      text = "I'm a simple applet";
      setBackground(Color.cyan);
      initialize("svgWindow");
      setAttribute(circleID, "fill", "green");
   }

   public void start()
   {
      System.out.println("starting...");
   }

   public void stop()
   {
      System.out.println("stopping...");
   }

   public void destroy()
   {
      System.out.println("preparing to unload...");
   }

   public void paint(Graphics g)
   {
      System.out.println("Paint");
      g.setColor(Color.blue);
      g.drawRect(0, 0,
      getSize().width -1,
      getSize().height -1);
      g.setColor(Color.red);
      g.drawString(text, 15, 25);
   }


   public void ShowStatus(String status)
   {
      text = status;
      setBackground(Color.yellow);
   }



   JSObject docSVG = null;
   String strSVGObjectID = null;

   public void initialize(String strObjectID)
   {
      this.strSVGObjectID = strObjectID;
   }

   private void getSVGDocReference()
   {
      ShowStatus("getSVGDocReference");

      try
      {
         if (strSVGObjectID == null)
         {
         return;
         }

         ShowStatus(strSVGObjectID);
         if (docSVG == null)
         {
            JSObject window = JSObject.getWindow(this);
         ShowStatus(strSVGObjectID);

            JSObject doc = (JSObject) window.getMember("document");
         ShowStatus("got document" + " : " + strSVGObjectID);
            JSObject imageSVG = (JSObject) doc.getMember(strSVGObjectID);
         ShowStatus("got image");
            docSVG = (JSObject) imageSVG.call("getSVGDocument", null);
         ShowStatus("got svgDoc");
         }
      }
      catch (Throwable e)
      {
         String thisErr = "Error Getting SVG: " + e;
         ShowStatus(thisErr);
         System.out.println(thisErr);
      }
   }



   public void setAttribute(String sID, String sAttribute, String sValue)
   {

      ShowStatus("step1");
      try
      {

         ShowStatus("step2");
         getSVGDocReference();

         //ShowStatus("step3");
         Object oParams[] = new Object[1];
         oParams[0] = sID;

         JSObject shape = null;

         shape = (JSObject) docSVG.call("getElementById", oParams);

         //ShowStatus("step4");

         if (shape != null)
         {
            Object oAttrOutParams[] = new Object[2];
            oAttrOutParams[0] = sAttribute;
            oAttrOutParams[1] = sValue;
            shape.call("setAttribute", oAttrOutParams);
         }
      }
      catch (Throwable e)
      {
         System.out.println("Error Setting Attribute: [" + sID + "." + sAttribute + "] " + e);
      }
   }



   public String getAttribute(String sID, String sAttribute)
   {
      String sResult = "";

      try
      {
         getSVGDocReference();

         Object oParams[] = new Object[1];
         oParams[0] = sID;

         JSObject shape = null;

         shape = (JSObject) docSVG.call("getElementById", oParams);

         if (shape != null)
         {
            Object oAttrInParams[] = new Object[1];

            oAttrInParams[0] = sAttribute;
            sResult = (String) shape.call("getAttribute", oAttrInParams);
         }
      }
      catch (Throwable e)
      {
         System.out.println("Error Getting Attribute: [" + sID + "." + sAttribute + "] " + e);
      }

      return sResult;
   }



   public void setStyleProperty(String sID, String sProperty, String sValue)
   {
      try
      {
         getSVGDocReference();

         Object oParams[] = new Object[1];
         oParams[0] = sID;

         JSObject shape = null;

         shape = (JSObject) docSVG.call("getElementById", oParams);

         if (shape != null)
         {
            JSObject style = (JSObject) shape.call("getStyle", null);

            Object oStyleOutParams[] = new Object[2];
            oStyleOutParams[0] = sProperty;
            oStyleOutParams[1] = sValue;
            style.call("setProperty", oStyleOutParams);
         }
      }
      catch (Throwable e)
      {
         System.out.println("Error Setting Property: [" + sID + "." + sProperty + "] " + e);
      }
   }



   public String getStyleProperty(String sID, String sProperty)
   {
   String sResult = "";

      try
      {
         getSVGDocReference();

         Object oParams[] = new Object[1];
         oParams[0] = sID;

         JSObject shape = null;

         shape = (JSObject) docSVG.call("getElementById", oParams);

         if (shape != null)
         {
            JSObject style = (JSObject) shape.call("getStyle", null);

            Object oStyleInParams[] = new Object[1];
            oStyleInParams[0] = sProperty;
            sResult = (String) style.call("getProperty", oStyleInParams);
         }
      }
      catch (Throwable e)
      {
         System.out.println("Error Setting Property: [" + sID + "." + sProperty + "] " + e);
      }

      return sResult;
   }



   public void setText(String sID, String sNewValue)
   {
      try
      {
         getSVGDocReference();

         Object oParams[] = new Object[1];
         oParams[0] = sID;

         JSObject text = null;

         text = (JSObject) docSVG.call("getElementById", oParams);

         if (text != null)
         {
            JSObject textChild = null;
            textChild = (JSObject) text.call("getFirstChild", null);
            Object oTextOutParams[] = new Object[1];

            oTextOutParams[0] = sNewValue;
            textChild.call("setData", oTextOutParams);

         }
      }
      catch (Throwable e)
      {
         System.out.println("Error Setting Text : [" + sID + "] " + e);
      }
   }
}

/*
[15:03] bracey: If the object follows the conventions, there should be get calls for all its properties.  If not, there are Java methods for serializing objects.  Look up the class ObjectOutputStream.
[15:04] schepda: I don't know its properties tho... isn't there some discovery process?
[15:04] bracey: You initialize the ObjectOutputStream with some kind of output stream (FileOutputStream?) and then use its writeObject(Object) method to serialize the object somewhere.  You could probably send the object to the display too.
[15:06] bracey: Yes, there is something called reflection that lets you call methods without knowing exactly what kind of methods you have, or even what kind of object you're working with.
*/