Back to index

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