Back to index

wims  3.65+svn20090927
CircleObject.java
Go to the documentation of this file.
00001 package rene.zirkel.objects;
00002 
00003 // file: CircleObject.java
00004 
00005 import java.awt.*;
00006 import java.util.*;
00007 
00008 import rene.util.xml.*;
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.expression.Expression;
00014 import rene.zirkel.expression.InvalidException;
00015 import rene.dialogs.*;
00016 
00017 public class CircleObject extends PrimitiveCircleObject
00018        implements MoveableObject
00019 {      protected PointObject P2;
00020        boolean Fixed=false;
00021        Expression E;
00022        boolean ExpressionFailed;
00023        boolean Ellipse=false;
00024 
00025        public CircleObject (Construction c, PointObject p1, PointObject p2)
00026        {      super(c,p1);
00027               P2=p2;
00028               validate();
00029               updateText();
00030        }
00031        
00032        public String getTag () { return "Circle"; }
00033        
00034        public void updateText ()
00035        {      if (Fixed)
00036               {      if (E==null)  
00037                             setText(text3(Zirkel.name("text.circle.fixed"),
00038                                    M.getName(),P2.getName(),""+round(R)));
00039                      else
00040                             setText(text3(Zirkel.name("text.circle.fixed"),
00041                                    M.getName(),P2.getName(),"\""+E.toString()+"\""));
00042               }      
00043               else 
00044                      setText(text2(Zirkel.name("text.circle"),M.getName(),P2.getName()));
00045        }
00046 
00047        public void validate ()
00048        {      super.validate();
00049               ExpressionFailed=false;
00050               if (!M.valid() || !P2.valid()) { Valid=false; return; }
00051               else
00052               {      Valid=true;
00053                      X=M.getX(); Y=M.getY();
00054                      double X2=P2.getX(),Y2=P2.getY();
00055                      // compute normalized vector in the direction of the line:
00056                      double DX=X2-X,DY=Y2-Y; 
00057                      R=Math.sqrt(DX*DX+DY*DY);
00058                      if (Fixed)
00059                      {      if (!P2.moveableBy(this))
00060                             {      Fixed=false;
00061                             }
00062                             else try
00063                             {      double FixR=E.getValue();
00064                                    if (FixR<0) FixR=0;
00065                                    if (R<1e-10) P2.move(X+FixR,Y);
00066                                    else P2.move(X+DX*FixR/R,Y+DY*FixR/R);
00067                                    R=FixR;
00068                                    P2.movedBy(this);
00069                             }
00070                             catch (Exception e)
00071                             {      R=0; P2.move(X,Y); ExpressionFailed=true; Valid=false;
00072                             }
00073                      }
00074               }
00075        }
00076 
00077        public void printArgs (XmlWriter xml)
00078        {      xml.printArg("through",P2.getName());
00079               if (Fixed && E!=null) xml.printArg("fixed",E.toString());
00080               super.printArgs(xml);
00081        }
00082        
00083        public boolean canFix ()
00084        {      return /* M.moveableBy(this) || */ P2.moveableBy(this);
00085        }
00086        
00087        public boolean fixed () { return Fixed; }
00088        public void setFixed (boolean flag, String s)
00089               throws ConstructionException
00090        {      if (!flag || s.equals(""))
00091               {      Fixed=false;
00092                      E=null;
00093               }
00094               else
00095               {      E=new Expression(s,getConstruction(),this);
00096                      if (!E.isValid())
00097                             throw new ConstructionException(E.getErrorText());
00098                      Fixed=true;
00099               }
00100               updateText();
00101        }
00102 
00103        public boolean nearto (int c, int r, ZirkelCanvas zc)
00104        {      if (ExpressionFailed && M.valid()) return M.nearto(c,r,zc);
00105               return super.nearto(c,r,zc);
00106        }
00107        
00108        public void edit (ZirkelCanvas zc)
00109        {      super.edit(zc);
00110               if (E!=null && !E.isValid())
00111               {      Frame F=zc.getFrame();
00112                      Warning w=new Warning(F,E.getErrorText(),
00113                             Zirkel.name("warning"),true);
00114                      w.center(F);
00115                      w.setVisible(true);
00116               }
00117        }
00118        
00119        public boolean isValidFix ()
00120        {      return E!=null && E.isValid();
00121        }
00122        
00123        public void updateCircleDep ()
00124        {      addDep(P2);
00125        }
00126 
00127        public void translate ()
00128        {      super.translate();
00129               P2=(PointObject)P2.getTranslation();
00130               try
00131               {      setFixed(Fixed,E.toString());
00132                      E.translate();
00133               }
00134               catch (Exception e) { Fixed=false; }
00135        }
00136 
00137        public String getStringLength ()
00138        {      if (E!=null) return E.toString();
00139               else return ""+roundDisplay(R);
00140        }
00141 
00142        public double getValue ()
00143               throws ConstructionException
00144        {      if (!Valid) throw new InvalidException("exception.invalid");
00145               else return R;
00146        }
00147 
00148        public Enumeration depending ()
00149        {      super.depending();
00150               if (!Fixed || E==null) return depset(P2);
00151               else
00152               {      depset(P2);
00153                      Enumeration e=E.getDepList().elements();
00154                      while (e.hasMoreElements())
00155                      {      DL.add((ConstructionObject)e.nextElement());
00156                      }
00157                      return DL.elements();
00158               }
00159        }
00160 
00161        public PointObject getP2 ()
00162        {      return P2;
00163        }
00164        
00165        public boolean contains (PointObject p)
00166        {      return p==P2;
00167        }
00168 
00169        public void dragTo (double x, double y) 
00170        {      M.move(x1+(x-x3),y1+(y-y3));
00171               P2.move(x2+(x-x3),y2+(y-y3));
00172        }
00173 
00174        public void move (double x, double y) 
00175        {
00176        }
00177 
00178        public boolean moveable () 
00179        {      if (!Fixed && M.moveable() && P2.moveable()) return true;
00180               return false;
00181        }
00182 
00183        double x1,y1,x2,y2,x3,y3;
00184        
00185        public void startDrag (double x, double y) 
00186        {      x1=M.getX(); y1=M.getY();
00187               x2=P2.getX(); y2=P2.getY();
00188               x3=x; y3=y;
00189        }
00190        
00191        public void snap (ZirkelCanvas zc)
00192        {      if (moveable())
00193               {      M.snap(zc); P2.snap(zc);
00194               }
00195        }
00196 
00197 }