Back to index

wims  3.65+svn20090927
DemoRunner.java
Go to the documentation of this file.
00001 package rene.zirkel;
00002 
00003 import java.awt.*;
00004 import java.awt.event.*;
00005 import java.io.*;
00006 import java.net.*;
00007 import java.util.*;
00008 
00009 import rene.util.xml.*;
00010 import rene.zirkel.construction.*;
00011 import rene.zirkel.objects.*;
00012 import rene.zirkel.tools.*;
00013 
00014 public class DemoRunner
00015        implements Runnable, MouseListener
00016 {      ZirkelCanvas ZC;
00017        ZirkelApplet ZA;
00018        boolean Stopped=false,Continue=false,Hold=false;
00019        int delay=10;
00020        Label L;
00021        XmlTree Tree;
00022 
00023        public DemoRunner (ZirkelCanvas zc, ZirkelApplet za, String filename, Label label)
00024        {      ZC=zc; ZA=za; L=label;
00025               try
00026               {      URL url;
00027                      if (filename.toUpperCase().startsWith("HTTP"))
00028                             url=new URL(filename);
00029                      else
00030                             url=new URL(ZA.getCodeBase(),filename);
00031                      InputStream in=url.openStream();
00032                      XmlReader xml=new XmlReader();
00033                      xml.init(in);
00034                      XmlTree tree=xml.scan();
00035                      Enumeration e=tree.getContent();
00036                      while (e.hasMoreElements())
00037                      {      tree=(XmlTree)e.nextElement();
00038                             if (tree.getTag() instanceof XmlTagPI) continue;
00039                             if (!tree.getTag().name().equals("Demo"))
00040                                    throw new ConstructionException("Demo tag not found");
00041                             else
00042                             {      XmlTag tag=tree.getTag();
00043                                    if (tag.hasParam("delay"))
00044                                    {      try
00045                                           {      delay=Integer.parseInt(tag.getValue("delay"));
00046                                           }
00047                                           catch (Exception ex) {}
00048                                    }
00049                                    break;
00050                             }
00051                      }
00052                      Tree=tree;
00053                      e=tree.getContent();
00054                      if (!e.hasMoreElements()) return;
00055                      while (e.hasMoreElements())
00056                      {      tree=(XmlTree)e.nextElement();
00057                             if (!tree.getTag().name().equals("File"))
00058                                    throw new ConstructionException("Illegal tag "+
00059                                           tree.getTag().name());
00060                      }                    
00061                      in.close();
00062               }
00063               catch (ConstructionException e)
00064               {      label.setText(e.toString());
00065               }      
00066               catch (Exception e)
00067               {      label.setText("Error loading "+filename);
00068               }
00069               zc.addMouseListener(this);  
00070               new Thread(this).start();
00071        }
00072        
00073        public void run ()
00074        {      Enumeration e=Tree.getContent();
00075               ZC.setFrozen(true);
00076               while (true)
00077               {      Continue=false;
00078                      int D=delay;
00079                      if (e.hasMoreElements())
00080                      {      try
00081                             {      XmlTree tree=(XmlTree)e.nextElement();
00082                                    XmlTag tag=tree.getTag();
00083                                    String filename=tag.getValue("name");
00084                                    URL url;
00085                                    if (filename.toUpperCase().startsWith("HTTP"))
00086                                           url=new URL(filename);
00087                                    else
00088                                           url=new URL(ZA.getCodeBase(),filename);
00089                                    InputStream in=url.openStream();
00090                                    ZC.clearMacros();
00091                                    ZC.load(in);
00092                                    in.close();
00093                                    ZC.recompute();
00094                                    if (tag.hasParam("delay"))
00095                                    {      try
00096                                           {      delay=Integer.parseInt(tag.getValue("delay"));
00097                                           }
00098                                           catch (Exception ex) {}
00099                                    }                                  
00100                                    Enumeration en=tree.getContent();
00101                                    while (en.hasMoreElements())
00102                                    {      tree=(XmlTree)en.nextElement();
00103                                           if (tree.getTag() instanceof XmlTagText)
00104                                           {      L.setText(((XmlTagText)tree.getTag()).getContent());
00105                                           }
00106                                    }
00107                                    startZC();
00108                             }
00109                             catch (Exception ex)
00110                             {      L.setText("Error loading file!");
00111                             }
00112                             try
00113                             {      for (int i=0; i<delay*2 || Hold; i++)
00114                                    {      Thread.sleep(500);
00115                                           if (i==0)
00116                                           {      ZC.setFrozen(false);
00117                                                  ZC.repaint();
00118                                           }
00119                                           if (Stopped) return;
00120                                           if (Continue)
00121                                           {      Hold=false;
00122                                                  break;
00123                                           }
00124                                    }
00125                                    ZC.setFrozen(true);
00126                             }
00127                             catch (Exception ex) {}
00128                             delay=D;
00129                      }
00130                      else
00131                      {      e=Tree.getContent();
00132                      }
00133               }
00134        }
00135 
00136        AnimatorTool A;
00137        
00138        public void startZC ()
00139        {      Construction C=ZC.getConstruction();
00140               ZC.setInteractive(false);
00141               if (C.TrackP!=null)
00142               {      try
00143                      {      ConstructionObject
00144                                    P=C.find(C.TrackP);
00145                             if (!((P instanceof PointObject) 
00146                                    || (P instanceof PrimitiveLineObject)))
00147                                           throw new ConstructionException("");                           
00148                             ConstructionObject po[]=
00149                                    new ConstructionObject[C.TrackPO.size()];
00150                             for (int i=0; i<po.length; i++)
00151                             {      ConstructionObject o=C.find(
00152                                           (String)C.TrackPO.elementAt(i));
00153                                    if (o==null || !((o instanceof PointObject) 
00154                                           || (o instanceof PrimitiveLineObject)
00155                                           || (o instanceof PointObject)
00156                                           ))
00157                                                  throw new ConstructionException("");
00158                                    po[i]=o;
00159                             }
00160                             PointObject
00161                                    PM=(PointObject)C.find(C.TrackPM);
00162                             if (C.TrackO!=null)
00163                             {      ConstructionObject O=C.find(C.TrackO);
00164                                    if (P==null || PM==null || O==null)
00165                                           throw new ConstructionException("");
00166                                    ObjectTracker ot=new ObjectTracker(P,PM,O,ZC,
00167                                           C.Animate,C.Paint,po);
00168                                    if (C.Animate) ot.Interactive=false;
00169                                    ot.setOmit(C.Omit);
00170                                    ZC.setTool(ot);
00171                                    ZC.allowRightMouse(false);
00172                                    ZC.validate();
00173                                    ZC.repaint();
00174                             }
00175                             else
00176                             {      if (P==null)
00177                                           throw new ConstructionException("");
00178                                    ZC.setTool(new Tracker(P,po));
00179                                    if (PM!=null) PM.setSelected(true);
00180                                    ZC.validate();
00181                                    ZC.repaint();
00182                             }
00183                      }
00184                      catch (Exception e)
00185                      {      e.printStackTrace(); 
00186                      }                    
00187               }
00188               else if (C.AnimateP!=null)
00189               {      try
00190                      {      PointObject
00191                                    P=(PointObject)C.find(C.AnimateP);
00192                             if (P==null || !P.moveable())
00193                                    throw new ConstructionException("");
00194                             Enumeration e=C.AnimateV.elements();
00195                             while (e.hasMoreElements())
00196                             {      String s=(String)e.nextElement();
00197                                    ConstructionObject o=C.find(s);
00198                                    if (o==null || !(o instanceof SegmentObject
00199                                           || o instanceof PrimitiveCircleObject ||
00200                                           o instanceof PointObject))
00201                                                  throw new ConstructionException("");                           
00202                             }
00203                             ZC.setTool(A=new AnimatorTool(P,C.AnimateV,ZC,C.AnimateNegative,
00204                                           C.AnimateOriginal,C.AnimateDelay));
00205                             ZC.allowRightMouse(false);
00206                             A.setInteractive(false);
00207                      }
00208                      catch (Exception e)
00209                      {      e.printStackTrace();
00210                      }                    
00211               }
00212               ZC.repaint();
00213        }
00214 
00215        public void stop ()
00216        {      Stopped=true;
00217        }
00218        
00219        public void mousePressed (MouseEvent e) {}
00220        public void mouseReleased (MouseEvent e) {}
00221        public void mouseEntered (MouseEvent e) {}
00222        public void mouseExited (MouseEvent e) {}
00223        public void mouseClicked (MouseEvent e)
00224        {      if (e.isMetaDown()) Hold=true;
00225               else Continue=true;
00226        }
00227 }