Back to index

wims  3.65+svn20090927
IntersectionConstructor.java
Go to the documentation of this file.
00001 package rene.zirkel.constructors;
00002 
00003 // file: InersectionConstructor.java
00004 
00005 import java.awt.event.*;
00006 
00007 import rene.util.xml.*;
00008 import rene.util.MyVector;
00009 import rene.zirkel.Zirkel;
00010 import rene.zirkel.ZirkelCanvas;
00011 import rene.zirkel.construction.Construction;
00012 import rene.zirkel.construction.ConstructionException;
00013 import rene.zirkel.objects.CircleIntersectionObject;
00014 import rene.zirkel.objects.ConstructionObject;
00015 import rene.zirkel.objects.IntersectionObject;
00016 import rene.zirkel.objects.LineCircleIntersectionObject;
00017 import rene.zirkel.objects.LineIntersectionObject;
00018 import rene.zirkel.objects.PointonObjectIntersectionObject;
00019 import rene.zirkel.objects.PrimitiveCircleObject;
00020 import rene.zirkel.objects.PrimitiveLineObject;
00021 
00022 public class IntersectionConstructor extends ObjectConstructor
00023 {      ConstructionObject P1=null,P2=null;
00024 
00025        public void mousePressed (MouseEvent e, ZirkelCanvas zc)
00026        {      if (!zc.Visual) return;
00027               boolean immediate=false;
00028               if (P1==null)
00029               {      MyVector v=zc.selectPointonObjects(e.getX(),e.getY());
00030                      if (v.size()==2)
00031                      {      P1=(ConstructionObject)v.elementAt(0);
00032                             P2=(ConstructionObject)v.elementAt(1);
00033                             if (P1.equals(P2) ||
00034                                    (P1.isFilled() && P2.isFilled()))
00035                             {      P1=P2=null;
00036                             }
00037                             else immediate=true;
00038                      }
00039               }
00040               if (P1==null)
00041               {      P1=select(e.getX(),e.getY(),zc);
00042                      if (P1!=null)
00043                      {      P1.setSelected(true);
00044                             zc.repaint();
00045                             showStatus(zc);
00046                      }
00047               }
00048               else
00049               {      if (P2==null) P2=select(e.getX(),e.getY(),zc);
00050                      if (P2!=null)
00051                      {      if (P2==P1)
00052                             {      P2=null; return;
00053                             }
00054                             IntersectionObject o[]=construct(P1,P2,zc.getConstruction());
00055                             if (o!=null)
00056                             {      IntersectionObject oc=null;
00057                                    if (immediate && o.length>1)
00058                                    {      if (o[1].nearto(e.getX(),e.getY(),zc))
00059                                           {      o[0]=null; oc=o[1];
00060                                           }
00061                                           else
00062                                           {      o[1]=null; oc=o[0];
00063                                           }
00064                                    }
00065                                    for (int i=0; i<o.length; i++)
00066                                    {      if (o[i]!=null)
00067                                           {      o[i].setDefaults();
00068                                                  zc.addObject(o[i]);
00069                                                  o[i].validate(zc.x(e.getX()),zc.y(e.getY()));
00070                                           }
00071                                    }
00076                                    if (oc!=null) oc.autoAway();
00077                             }
00078                             P1=P2=null;
00079                             zc.clearSelected();
00080                             showStatus(zc);
00081                      }
00082               }
00083        }
00084        
00085        public void mouseMoved (MouseEvent e, ZirkelCanvas zc, boolean simple)
00086        {      zc.indicateIntersectedObjects(e.getX(),e.getY());
00087        }
00088 
00089        public static IntersectionObject[] construct (
00090               ConstructionObject P1, ConstructionObject P2,
00091               Construction c)
00092        {      IntersectionObject o[]=null;
00093               if (P1 instanceof PrimitiveLineObject)
00094               {      if (P2 instanceof PrimitiveLineObject)
00095                      {      o=new IntersectionObject[1];
00096                             o[0]=new LineIntersectionObject(c,
00097                                           (PrimitiveLineObject)P1,(PrimitiveLineObject)P2);
00098                      }
00099                      else if (P2 instanceof PrimitiveCircleObject)
00100                      {      o=new IntersectionObject[2];
00101                             o[0]=new LineCircleIntersectionObject(c,
00102                                           (PrimitiveLineObject)P1,(PrimitiveCircleObject)P2,
00103                                           true);
00104                             o[1]=new LineCircleIntersectionObject(c,
00105                                           (PrimitiveLineObject)P1,(PrimitiveCircleObject)P2,
00106                                           false);
00107                      }
00108                      else return construct(P2,P1,c);
00109               }
00110               else if (P1 instanceof PrimitiveCircleObject)
00111               {      if (P2 instanceof PrimitiveCircleObject)
00112                      {      o=new IntersectionObject[2];
00113                             o[0]=new CircleIntersectionObject(c,
00114                                           (PrimitiveCircleObject)P1,(PrimitiveCircleObject)P2,
00115                                           true);
00116                             o[1]=new CircleIntersectionObject(c,
00117                                           (PrimitiveCircleObject)P1,(PrimitiveCircleObject)P2,
00118                                           false);
00119                      }
00120                      else if (P2 instanceof PrimitiveLineObject)
00121                      {      o=new IntersectionObject[2];
00122                             o[0]=new LineCircleIntersectionObject(c,
00123                                           (PrimitiveLineObject)P2,(PrimitiveCircleObject)P1,
00124                                           true);
00125                             o[1]=new LineCircleIntersectionObject(c,
00126                                           (PrimitiveLineObject)P2,(PrimitiveCircleObject)P1,
00127                                           false);
00128                      }
00129                      else return construct(P2,P1,c);
00130               }
00131               else
00132               {      o=new PointonObjectIntersectionObject[1];
00133                      o[0]=new PointonObjectIntersectionObject(c,P1,P2);
00134               }
00135               return o;
00136        }
00137        
00138        public ConstructionObject select (int x, int y, ZirkelCanvas zc)
00139        // select a line or circle at x,y
00140        {      return zc.selectPointonObject(x,y,false);
00141        }
00142        
00143        public void reset (ZirkelCanvas zc)
00144        // reset the tool
00145        {      super.reset(zc);
00146               if (zc.Visual)
00147               {      P1=P2=null;
00148                      showStatus(zc);
00149               }
00150               else
00151               {      zc.setPrompt(Zirkel.name("prompt.intersection"));
00152               }
00153        }
00154        public void showStatus (ZirkelCanvas zc)
00155        {      if (P1==null) zc.showStatus(
00156                      Zirkel.name("message.intersection.first","Intersection: Select first object!"));
00157               else zc.showStatus(
00158                      Zirkel.name("message.intersection.second","Intersection: Select second object!"));
00159        }
00160        
00161        public boolean construct (XmlTree tree, Construction c)
00162               throws ConstructionException
00163        {      if (!testTree(tree,"Intersection")) return constructOther(tree,c);
00164               XmlTag tag=tree.getTag();
00165               if (!tag.hasParam("first") || !tag.hasParam("second"))
00166                      throw new ConstructionException("Intersection parameters missing!");
00167               try
00168               {      ConstructionObject o1=c.find(tag.getValue("first")); 
00169                      ConstructionObject o2=c.find(tag.getValue("second"));
00170                      IntersectionObject o[]=construct(o1,o2,c);
00171                      if (o==null) throw new Exception("");
00172                      String name="",nameOther="";
00173                      if (tag.hasParam("name")) name=tag.getValue("name");
00174                      if (tag.hasParam("other")) nameOther=tag.getValue("other");
00175                      if (o.length>1)
00176                      {      if (tag.hasParam("which"))
00177                             {      IntersectionObject oo;
00178                                    if (tag.getValue("which").equals("second")) oo=o[1];
00179                                    else oo=o[0];
00180                                    if (!name.equals("")) oo.setName(name);
00181                                    PointConstructor.setType(tag,oo);
00182                                    setName(tag,oo);
00183                                    set(tree,oo);
00184                                    c.add(oo);
00185                                    setConditionals(tree,c,oo);
00186                                    if (tag.hasParam("awayfrom"))
00187                                           oo.setAway(tag.getValue("awayfrom"),true);
00188                                    else if (tag.hasParam("closeto"))
00189                                           oo.setAway(tag.getValue("closeto"),false);
00190                                    if (tag.hasParam("valid")) oo.setRestricted(false);
00191                                    if (tag.hasParam("alternate")) oo.setAlternate(true);
00192                             }
00193                             else if (tag.hasParam("other"))
00194                             {      if (!name.equals("")) o[0].setName(name);
00195                                    if (!nameOther.equals("")) o[1].setName(nameOther);
00196                                    if (tag.hasParam("awayfrom"))
00197                                    {      o[0].setAway(tag.getValue("awayfrom"),true);
00198                                           o[1].setAway(tag.getValue("awayfrom"),false);
00199                                    }
00200                                    else if (tag.hasParam("closeto"))
00201                                    {      o[1].setAway(tag.getValue("awayfrom"),true);
00202                                           o[0].setAway(tag.getValue("awayfrom"),false);
00203                                    }
00204                                    for (int i=0; i<o.length; i++)
00205                                    {      if (o[i]==null) continue;
00206                                           PointConstructor.setType(tag,o[i]);
00207                                           set(tree,o[i]);
00208                                           c.add(o[i]);
00209                                           setConditionals(tree,c,o[i]);
00210                                    }                                  
00211                             }
00212                      }
00213                      else
00214                      {      if (!name.equals("")) o[0].setName(name);
00215                             PointConstructor.setType(tag,o[0]);
00216                             setName(tag,o[0]);
00217                             set(tree,o[0]);
00218                             c.add(o[0]);
00219                             setConditionals(tree,c,o[0]);
00220                             if (tag.hasParam("valid")) o[0].setRestricted(false);
00221                             try
00222                             {      double x=new Double(tag.getValue("x")).doubleValue();
00223                                    double y=new Double(tag.getValue("y")).doubleValue();
00224                                    o[0].setXY(x,y);
00225                             }
00226                             catch (Exception e) {}
00227                      }
00228               }
00229               catch (ConstructionException e)
00230               {      throw e;
00231               }
00232               catch (Exception e)
00233               {      e.printStackTrace();
00234                      throw new ConstructionException("Intersection parameters illegal!");
00235               }
00236               return true;
00237        }
00238 
00239        public boolean constructOther (XmlTree tree, Construction c)
00240               throws ConstructionException
00241        {      if (!testTree(tree,"OtherIntersection")) return false;
00242               XmlTag tag=tree.getTag();
00243               if (tag.hasParam("name"))
00244               {      ConstructionObject o=c.find(tag.getValue("name"));      
00245                      if (o==null || !(o instanceof IntersectionObject))
00246                             throw new ConstructionException("OtherIntersection not found!");
00247                      IntersectionObject oo=(IntersectionObject)o;
00248                      PointConstructor.setType(tag,oo);
00249                      o.setDefaults();
00250                      set(tree,o);
00251                      ConstructionObject ol=c.lastButOne();
00252                      if (tag.hasParam("awayfrom"))
00253                      {      oo.setAway(tag.getValue("awayfrom"),true);
00254                             if (ol!=null && (ol instanceof IntersectionObject))
00255                                    ((IntersectionObject)ol).setAway(tag.getValue("awayfrom"),
00256                                           false);
00257                      }
00258                      else if (tag.hasParam("closeto"))
00259                      {      oo.setAway(tag.getValue("closeto"),false);
00260                             if (ol!=null && (ol instanceof IntersectionObject))
00261                                    ((IntersectionObject)ol).setAway(tag.getValue("awayfrom"),
00262                                           true);
00263                      }
00264                      if (tag.hasParam("valid"))
00265                      {      oo.setRestricted(false);
00266                      }
00267               }
00268               else
00269                      throw new ConstructionException("OtherIntersection must have a name!");                    
00270               return true;
00271        }
00272 
00273        public String getTag () { return "Intersection"; }
00274        public void construct (Construction c, 
00275               String name, String params[], int nparams)
00276               throws ConstructionException
00277        {      if (nparams!=2 && nparams!=3)
00278                      throw new ConstructionException(Zirkel.name("exception.nparams"));
00279               ConstructionObject
00280                      P1=c.find(params[0]);
00281               if (P1==null)
00282                      throw new ConstructionException(Zirkel.name("exception.notfound")+" "+
00283                             params[0]);
00284               ConstructionObject
00285                      P2=c.find(params[1]);
00286               if (P2==null)
00287                      throw new ConstructionException(Zirkel.name("exception.notfound")+" "+
00288                             params[0]);
00289               IntersectionObject o[]=construct(P1,P2,c);
00290               if (o==null)
00291                      throw new ConstructionException(Zirkel.name("exception.type"));
00292               if (o.length==1)
00293               {      c.add(o[0]);
00294                      o[0].setDefaults();
00295                      if (!name.equals("")) o[0].setName(name);
00296               }
00297               else
00298               {      if (name.equals(""))
00299                      {      for (int i=0; i<o.length; i++)
00300                             {      c.add(o[i]);
00301                                    o[i].setDefaults();
00302                             }
00303                      }
00304                      else
00305                      {      String names[]=new String[2];
00306                             int n;
00307                             if ((n=name.indexOf(','))>=0)
00308                             {      names[0]=name.substring(n+1).trim();
00309                                    names[1]=name.substring(0,n).trim();
00310                             }
00311                             else
00312                             {      names[0]=name;
00313                                    names[1]="";
00314                             }
00315                             for (int i=0; i<o.length; i++)
00316                             {      if (names[i].equals("")) continue;
00317                                    c.add(o[i]);
00318                                    o[i].setDefaults();
00319                                    o[i].setName(names[i]);
00320                             }
00321                      }
00322               }
00323        }
00324 
00325 }