Back to index

wims  3.65+svn20090927
CircleConstructor.java
Go to the documentation of this file.
00001 package rene.zirkel.constructors;
00002 
00003 // file: PointConstructor.java
00004 
00005 import java.awt.event.*;
00006 
00007 import rene.util.xml.*;
00008 import rene.zirkel.*;
00009 import rene.zirkel.construction.*;
00010 import rene.zirkel.expression.*;
00011 import rene.zirkel.macro.*;
00012 import rene.zirkel.objects.*;
00013 import rene.gui.Global;
00014 
00015 public class CircleConstructor extends ObjectConstructor
00016 {      PointObject P1=null,P2=null;
00017        boolean Fixed=false;
00018        public CircleConstructor (boolean fixed)
00019        {      Fixed=fixed;
00020        }
00021        public CircleConstructor ()
00022        {      this(false);
00023        }
00024        FixedCircleObject C;
00025        ConstructionObject O;
00026        boolean ShowsValue,ShowsName;
00027        int Moved;
00028        public void mousePressed (MouseEvent e, ZirkelCanvas zc)
00029        {      if (!zc.Visual) return;
00030               if (P1==null)
00031               {      P1=select(e.getX(),e.getY(),zc);
00032                      if (P1!=null)
00033                      {      P1.setSelected(true);
00034                             zc.repaint();
00035                      }
00036                      showStatus(zc);
00037               }
00038               else
00039               {      if (e.isShiftDown() || Fixed)
00040                      {      FixedCircleObject c=new FixedCircleObject(zc.getConstruction(),
00041                                    P1,zc.x(e.getX()),zc.y(e.getY()));
00042                             zc.addObject(c);
00043                             c.setDefaults();
00044                             zc.repaint();
00045                             O=C=c;
00046                             ShowsValue=c.showValue();
00047                             ShowsName=c.showName();
00048                             if (Global.getParameter("options.movefixname",true))
00049                             {      C.setShowValue(true);
00050                                    C.setShowName(true);
00051                             }
00052                             Dragging=true;
00053                             Moved=0;
00054                             P2=null;
00055                      }
00056                      else
00057                      {      P2=select(e.getX(),e.getY(),zc);
00058                             if (P2!=null)
00059                             {      if (P2==P1)
00060                                    {      P2=null; return;
00061                                    }
00062                                    CircleObject c=new CircleObject(zc.getConstruction(),P1,P2);
00063                                    zc.addObject(c);
00064                                    c.setDefaults();
00065                                    c.validate();
00066                                    zc.repaint();
00067                                    if (P2.moveable() && !P2.isPointOn() && zc.isNewPoint())
00068                                    {      ShowsValue=c.showValue();
00069                                           ShowsName=c.showName();
00070                                           if (Global.getParameter("options.movename",false))
00071                                           {      c.setShowValue(true);
00072                                                  c.setShowName(true);
00073                                           }
00074                                           O=c;
00075                                           Dragging=true;
00076                                           Moved=0;
00077                                    }
00078                                    else
00079                                    {      P1.setSelected(false);
00080                                           P1=P2=null;
00081                                           showStatus(zc);
00082                                    }
00083                             }
00084                      }
00085               }
00086        }
00087        
00088        public boolean waitForLastPoint ()
00089        {      return P1!=null && P2==null;
00090        }
00091        
00092        public void finishConstruction (MouseEvent e, ZirkelCanvas zc)
00093        {      P2=select(e.getX(),e.getY(),zc);
00094               if (P2!=null)
00095               {      CircleObject c=new CircleObject(zc.getConstruction(),P1,P2);
00096                      zc.addObject(c);
00097                      c.setDefaults();
00098                      c.validate();
00099                      zc.repaint();
00100               }
00101               P2=null;
00102        }
00103        
00104        public boolean waitForPoint ()
00105        {      return P1==null || !Fixed;
00106        }
00107        
00108        public void mouseDragged (MouseEvent e, ZirkelCanvas zc)
00109        {      if (!Dragging) return;
00110               Moved++;
00111               if (P2==null)
00112               {      C.init(zc.getConstruction(),zc.x(e.getX()),zc.y(e.getY()));
00113                      if (C instanceof FixedCircleObject)
00114                             ((FixedCircleObject)C).setDragable(Moved>5);
00115               }
00116               else
00117               {      P2.move(zc.x(e.getX()),zc.y(e.getY()));
00118                      zc.validate();
00119               }
00120               zc.repaint();
00121        }
00122        
00123        public void mouseReleased (MouseEvent e, ZirkelCanvas zc)
00124        {      if (!Dragging) return;
00125               Dragging=false;
00126               O.setShowValue(ShowsValue);
00127               O.setShowName(ShowsName);
00128               zc.repaint();
00129               if (P2==null)
00130               {      P1.setSelected(false);
00131                      P1=null;
00132                      C.round();
00133                      if (Moved<=5)
00134                      {      C.edit(zc);
00135                             if (C instanceof FixedCircleObject &&
00136                                    ((FixedCircleObject)C).isEditAborted())
00137                             {      zc.delete(C);
00138                                    zc.repaint();
00139                                    reset(zc);
00140                             }
00141                      }
00142                      C.validate();
00143                      zc.check();
00144               }
00145               else
00146               {      P1.setSelected(false);
00147                      P2.updateText();
00148                      P1=P2=null;
00149               }
00150               O.updateCircleDep();
00151               zc.repaint();
00152               zc.showStatus();     
00153        }
00154        
00155        public PointObject select (int x, int y, ZirkelCanvas zc)
00156        {      return zc.selectCreatePoint(x,y);
00157        }
00158        
00159        public void reset (ZirkelCanvas zc)
00160        {      super.reset(zc);
00161               if (zc.Visual)
00162               {      P1=P2=null;
00163                      showStatus(zc);
00164               }
00165               else
00166               {      zc.setPrompt(Zirkel.name("prompt.circle"));
00167               }
00168        }
00169        public void showStatus (ZirkelCanvas zc)
00170        {      if (Fixed)
00171               {      if (P1==null) zc.showStatus(
00172                             Zirkel.name("message.fixedcircle.midpoint"));
00173                      else zc.showStatus(
00174                             Zirkel.name("message.fixedcircle.radius"));
00175               }
00176               else
00177               {      if (P1==null) zc.showStatus(
00178                             Zirkel.name("message.circle.midpoint"));
00179                      else zc.showStatus(
00180                             Zirkel.name("message.circle.radius"));
00181               }
00182        }
00183        public boolean construct (XmlTree tree, Construction c)
00184               throws ConstructionException
00185        {      if (!testTree(tree,"Circle")) return false;
00186               XmlTag tag=tree.getTag();
00187               try
00188               {      if (!tag.hasParam("midpoint"))
00189                                    throw new ConstructionException("Circle parameters missing!");
00190                      if (!tag.hasParam("through"))
00191                      {      if (tag.hasParam("fixed"))
00192                             {      PointObject p1=(PointObject)c.find(tag.getValue("midpoint")); 
00193                                    FixedCircleObject o=new FixedCircleObject(c,p1,0,0);
00194                                    c.add(o);
00195                                    setConditionals(tree,c,o);
00196                                    setName(tag,o);
00197                                    set(tree,o);
00198                                    if (tag.hasParam("partial")) o.setPartial(true);
00199                                    if (tag.hasParam("filled")) o.setFilled(true);
00200                                    if (tag.hasTrueParam("dragable")) o.setDragable(true);
00201                                    if (tag.hasTrueParam("drawable")) o.setDragable(true); // downward compatibility
00202                                    o.setFixed(tag.getValue("fixed"));
00203                                    if (tag.hasParam("start") && tag.hasParam("end"))
00204                                           o.setRange(tag.getValue("start"),tag.getValue("end"));
00205                                    if (tag.hasParam("acute")) o.setObtuse(false);
00206                                    if (tag.hasParam("chord")) o.setArc(false);
00207                             }
00208                             else
00209                             {      if (!(c instanceof Macro))
00210                                           throw new ConstructionException("Circle parameters missing!");
00211                                    PointObject p1=(PointObject)c.find(tag.getValue("midpoint")); 
00212                                    PrimitiveCircleObject o=new PrimitiveCircleObject(c,p1);
00213                                    setName(tag,o);
00214                                    set(tree,o);
00215                                    c.add(o);
00216                                    setConditionals(tree,c,o);
00217                             }
00218                      }
00219                      else
00220                      {      PointObject p1=(PointObject)c.find(tag.getValue("midpoint")); 
00221                             PointObject p2=(PointObject)c.find(tag.getValue("through"));
00222                             CircleObject o=new CircleObject(c,p1,p2);
00223                             setName(tag,o);
00224                             set(tree,o);
00225                             c.add(o);
00226                             setConditionals(tree,c,o);
00227                             if (tag.hasParam("partial")) o.setPartial(true);
00228                             if (tag.hasParam("filled")) o.setFilled(true);
00229                             if (tag.hasParam("start") && tag.hasParam("end"))
00230                                    o.setRange(tag.getValue("start"),tag.getValue("end"));
00231                             if (tag.hasParam("acute")) o.setObtuse(false);
00232                             if (tag.hasParam("chord")) o.setArc(false);
00233                             if (tag.hasParam("fixed"))
00234                             {      try
00235                                    {      o.setFixed(true,tag.getValue("fixed"));
00236                                    }
00237                                    catch (Exception e)
00238                                    {      throw new ConstructionException("Fixed value illegal!");
00239                                    }
00240                             }
00241                      }
00242               }
00243               catch (ConstructionException e)
00244               {      throw e;
00245               }
00246               catch (Exception e)
00247               {      throw new ConstructionException("Circle parameters illegal!");
00248               }
00249               return true;
00250        }
00251        
00252        public String getTag () { return "Circle"; }
00253        public void construct (Construction c, 
00254               String name, String params[], int nparams)
00255               throws ConstructionException
00256        {      if (nparams>3 || nparams==0)
00257                      throw new ConstructionException(Zirkel.name("exception.nparams"));
00258               ConstructionObject
00259                      P1=c.find(params[0]);
00260               if (P1==null)
00261                      throw new ConstructionException(Zirkel.name("exception.notfound")+" "+
00262                             params[0]);
00263               if (!(P1 instanceof PointObject))
00264                      throw new ConstructionException(Zirkel.name("exception.type")+" "+
00265                             params[0]);
00266               if (nparams==1)
00267               {      PrimitiveCircleObject s=new PrimitiveCircleObject(c,(PointObject)P1);
00268                      c.add(s);
00269                      s.setDefaults();
00270                      if (!name.equals("")) s.setNameCheck(name);
00271                      return;
00272               }             
00273               ConstructionObject
00274                      P2=c.find(params[1]);
00275               if (P2==null)
00276               {      Expression ex=new Expression(params[1],c,null);
00277                      if (!ex.isValid())
00278                             throw new ConstructionException(Zirkel.name("exception.expression"));
00279                      FixedCircleObject s=new FixedCircleObject(c,(PointObject)P1,0,0);
00280                      c.add(s);
00281                      s.setDefaults();
00282                      s.setFixed(params[1]);
00283                      s.validate();
00284                      if (!name.equals("")) s.setNameCheck(name);
00285                      return;
00286               }
00287               if (P2 instanceof SegmentObject)
00288               {      Circle3Object s=new Circle3Object(c,
00289                             ((SegmentObject)P2).getP1(),
00290                             ((SegmentObject)P2).getP2(),
00291                             (PointObject)P1);
00292                      c.add(s);
00293                      s.setDefaults();
00294                      if (!name.equals("")) s.setNameCheck(name);
00295                      return;
00296               }
00297               if (!(P2 instanceof PointObject))
00298                      throw new ConstructionException(Zirkel.name("exception.type")+" "+
00299                             params[1]);
00300               if (nparams==3)
00301               {      ConstructionObject P3=c.find(params[2]);
00302                      if (P3==null || !(P3 instanceof PointObject))
00303                      {      CircleObject s=new CircleObject(c,(PointObject)P1,(PointObject)P2);
00304                             if (!s.canFix())
00305                                    throw new ConstructionException(Zirkel.name("exception.canfix"));
00306                             s.setFixed(true,params[2]);
00307                             if (!s.isValidFix())
00308                                    throw new ConstructionException(Zirkel.name("exception.fix")+" "+
00309                                           params[2]);
00310                             c.add(s);
00311                             s.validate();
00312                             if (!name.equals("")) s.setNameCheck(name);
00313                             s.setDefaults();
00314                             return;
00315                      }
00316                      else
00317                      {      Circle3Object cr=new Circle3Object(c,(PointObject)P2,(PointObject)P3,
00318                                    (PointObject)P1);
00319                             c.add(cr);
00320                             cr.setDefaults();
00321                             if (!name.equals("")) cr.setNameCheck(name);
00322                             return;
00323                      }             
00324               }
00325               CircleObject s=new CircleObject(c,(PointObject)P1,(PointObject)P2);
00326               c.add(s);
00327               s.setDefaults();
00328               if (!name.equals("")) s.setName(name);
00329        }
00330 
00331 }