Back to index

wims  3.65+svn20090927
SegmentConstructor.java
Go to the documentation of this file.
00001 package rene.zirkel.constructors;
00002 
00003 // file: SegmentConstructor.java
00004 
00005 import rene.util.xml.XmlTag;
00006 import rene.util.xml.XmlTree;
00007 import rene.zirkel.Zirkel;
00008 import rene.zirkel.ZirkelCanvas;
00009 import rene.zirkel.construction.Construction;
00010 import rene.zirkel.construction.ConstructionException;
00011 import rene.zirkel.expression.Expression;
00012 import rene.zirkel.objects.ConstructionObject;
00013 import rene.zirkel.objects.PointObject;
00014 import rene.zirkel.objects.SegmentObject;
00015 
00016 public class SegmentConstructor extends LineConstructor
00017 {      boolean Fixed=false;
00018        public SegmentConstructor ()
00019        {      this(false);  
00020        }
00021        public SegmentConstructor (boolean fixed)
00022        {      Fixed=fixed;
00023        }
00024        public ConstructionObject create (Construction c, PointObject p1, PointObject p2)
00025        {      return new SegmentObject(c,p1,p2);
00026        }
00027        public boolean isFixed ()
00028        {      return Fixed;
00029        }
00030        public void setFixed (ZirkelCanvas zc, ConstructionObject o)
00031        {      if (o instanceof SegmentObject)
00032               {      SegmentObject s=(SegmentObject)o;
00033                      if (s.canFix())
00034                             try
00035                             {      s.validate();
00036                                    s.setFixed(true,""+s.getLength());
00037                                    s.edit(zc);
00038                                    s.validate();
00039                                    zc.repaint();
00040                             }
00041                             catch (Exception e) {}
00042                      else
00043                      {      zc.warning(Zirkel.name("error.fixedsegment"));
00044                      }
00045               }
00046        }
00047        public void showStatus (ZirkelCanvas zc)
00048        {      if (!Fixed)
00049               {      if (P1==null) zc.showStatus(
00050                             Zirkel.name("message.segment.first"));
00051                      else zc.showStatus(
00052                             Zirkel.name("message.segment.second"));
00053               }
00054               else
00055               {      if (P1==null) zc.showStatus(
00056                             Zirkel.name("message.fixedsegment.first"));
00057                      else zc.showStatus(
00058                             Zirkel.name("message.fixedsegment.second"));
00059               }
00060        }
00061        public boolean construct (XmlTree tree, Construction c)
00062               throws ConstructionException
00063        {      if (!testTree(tree,"Segment")) return false;
00064               XmlTag tag=tree.getTag();
00065               if (!tag.hasParam("from") || !tag.hasParam("to"))
00066                      throw new ConstructionException("Segment endpoints missing!");
00067               try
00068               {      PointObject p1=(PointObject)c.find(tag.getValue("from")); 
00069                      PointObject p2=(PointObject)c.find(tag.getValue("to"));
00070                      SegmentObject o=new SegmentObject(c,p1,p2);
00071                      setName(tag,o);
00072                      set(tree,o);
00073                      c.add(o);
00074                      setConditionals(tree,c,o);
00075                      o.setArrow(tag.hasParam("arrow"));
00076                      if (tag.hasParam("fixed"))
00077                      {      try
00078                             {      o.setFixed(true,tag.getValue("fixed"));
00079                             }
00080                             catch (Exception e)
00081                             {      throw new ConstructionException("Fixed value illegal!");
00082                             }
00083                      }
00084               }
00085               catch (ConstructionException e)
00086               {      throw e;
00087               }
00088               catch (Exception e)
00089               {      throw new ConstructionException("Segment endpoints illegal!");
00090               }
00091               return true;
00092        }
00093        
00094        public String getPrompt ()
00095        {      return Zirkel.name("prompt.segment");
00096        }
00097        
00098        public String getTag () { return "Segment"; }
00099        public void construct (Construction c, 
00100               String name, String params[], int nparams)
00101               throws ConstructionException
00102        {      if (nparams!=2 && nparams!=3)
00103                      throw new ConstructionException(Zirkel.name("exception.nparams"));
00104               ConstructionObject
00105                      P1=c.find(params[0]);
00106               if (P1==null)
00107                      throw new ConstructionException(Zirkel.name("exception.notfound")+" "+
00108                             params[0]);
00109               if (!(P1 instanceof PointObject))
00110                      throw new ConstructionException(Zirkel.name("exception.type")+" "+
00111                             params[0]);
00112               ConstructionObject
00113                      P2=c.find(params[1]);
00114               if (P2==null)
00115               {      Expression ex=new Expression(params[1],c,null);
00116                      if (!ex.isValid())
00117                             throw new ConstructionException(Zirkel.name("exception.expression"));
00118                      double x=ex.getValue();
00119                      P2=new PointObject(c,((PointObject)P1).getX()+x,
00120                             ((PointObject)P1).getY());
00121                      c.add(P2);
00122                      P2.setDefaults();
00123                      SegmentObject s=new SegmentObject(c,(PointObject)P1,(PointObject)P2);
00124                      s.setDefaults();
00125                      s.setFixed(true,params[1]);
00126                      c.add(s);
00127                      if (!name.equals("")) s.setNameCheck(name);
00128                      return;
00129               }
00130               if (!(P2 instanceof PointObject))
00131                      throw new ConstructionException(Zirkel.name("exception.type")+" "+
00132                             params[1]);
00133               SegmentObject s=new SegmentObject(c,(PointObject)P1,(PointObject)P2);
00134               if (nparams==3)
00135               {      if (!s.canFix())
00136                             throw new ConstructionException(Zirkel.name("exception.canfix"));
00137                      s.setFixed(true,params[2]);
00138                      if (!s.isValidFix())
00139                             throw new ConstructionException(Zirkel.name("exception.fix")+" "+
00140                                    params[2]);
00141                      s.validate();                      
00142               }
00143               c.add(s);
00144               s.setDefaults();
00145               if (!name.equals("")) s.setNameCheck(name);
00146        }
00147 }