Back to index

wims  3.65+svn20090927
PointConstructor.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.objects.*;
00012 import rene.gui.Global;
00013 
00014 public class PointConstructor extends ObjectConstructor
00015 {      boolean Fix;
00016        PointObject P;
00017        boolean ShowsValue,ShowsName;
00018        
00019        public void mousePressed (MouseEvent e, ZirkelCanvas zc)
00020        {      double x=zc.x(e.getX()),y=zc.y(e.getY());
00021               PointObject o=zc.selectCreatePoint(e.getX(),e.getY(),false,true);
00022               Dragging=false;
00023               if (o==null) return;
00024               else if (o.isPointOn())
00025                      // cerate a point on an object
00026               {      if (e.isShiftDown() && zc.isNewPoint())
00027                             o.setUseAlpha(true);
00028               }
00029               else if (o.moveable() && zc.isNewPoint() && !zc.showGrid())
00030                      // create a new point and let it be dragged
00031               {      P=o;
00032                      P.setDefaults();
00033                      Fix=e.isShiftDown();
00034                      ShowsValue=P.showValue();
00035                      ShowsName=P.showName();
00036                      Dragging=true;
00037                      zc.repaint();               
00038               }
00039               else if (o.moveable() && zc.isNewPoint() && zc.showGrid() && e.isShiftDown())
00040               {      P=o;
00041                      P.setDefaults();
00042                      try
00043                      {      P.setFixed(""+P.round(P.getX(),ZirkelCanvas.LengthsFactor),
00044                                    ""+P.round(P.getY(),ZirkelCanvas.LengthsFactor));
00045                             P.edit(zc);
00046                             P.validate();
00047                      }
00048                      catch (Exception ex) {}
00049               }
00050               else if (e.isShiftDown() && !zc.isNewPoint())
00051                      // create a fixed point at the position of a selected point
00052               {      PointObject p=
00053                             new PointObject(zc.getConstruction(),o.getX(),o.getY());
00054                      zc.addObject(p);
00055                      p.setDefaults();
00056                      p.setFixed(true);
00057               }
00058        }
00059        
00060        public void mouseDragged (MouseEvent e, ZirkelCanvas zc)
00061        {      if (!Dragging) return;
00062               if (Global.getParameter("options.movename",false))
00063               {      P.setShowValue(true);
00064                      P.setShowName(true);
00065               }
00066               P.move(zc.x(e.getX()),zc.y(e.getY()));
00067               zc.repaint();
00068        }
00069 
00070        public void mouseReleased (MouseEvent e, ZirkelCanvas zc)
00071        {      if (!Dragging) return;
00072               Dragging=false;
00073               P.setShowValue(ShowsValue);
00074               P.setShowName(ShowsName);
00075               P.updateText();
00076               zc.repaint();
00077               if (Fix)
00078               {      try
00079                      {      P.setFixed(""+P.round(P.getX(),ZirkelCanvas.LengthsFactor),
00080                                    ""+P.round(P.getY(),ZirkelCanvas.LengthsFactor));
00081                             P.edit(zc);
00082                             P.validate();
00083                      }
00084                      catch (Exception ex) {}
00085               }
00086        }
00087 
00088        public void showStatus (ZirkelCanvas zc)
00089        {      zc.showStatus(
00090                      Zirkel.name("message.point","Point: Set a point!"));
00091        }
00092 
00093        public boolean construct (XmlTree tree, Construction c)
00094               throws ConstructionException
00095        {      if (!testTree(tree,"Point")) return false;
00096               XmlTag tag=tree.getTag();
00097               if (!tag.hasParam("x") || !tag.hasParam("y"))
00098                      throw new ConstructionException("Point coordinates missing!");
00099               double x=0,y=0;
00100               try
00101               {      if (tag.hasParam("actx"))
00102                             x=new Double(tag.getValue("actx")).doubleValue();
00103                      if (tag.hasParam("acty"))
00104                             y=new Double(tag.getValue("acty")).doubleValue();
00105               }
00106               catch (Exception e) {}
00107               PointObject p=new PointObject(c,x,y);
00108               try
00109               {      x=new Expression(tag.getValue("x"),c,p).getValue();
00110                      y=new Expression(tag.getValue("y"),c,p).getValue();
00111                      p.move(x,y);
00112               }
00113               catch (Exception e) {}
00114               setType(tag,p);
00115               setName(tag,p);
00116               set(tree,p);
00117               c.add(p);
00118               setConditionals(tree,c,p);
00119               if (tag.hasParam("fixed"))
00120               {      p.setFixed(tag.getValue("x"),tag.getValue("y"));
00121               }
00122               if (tag.hasParam("increment"))
00123               {      try
00124                      {      p.setIncrement(new Double(tag.getValue("increment")).doubleValue());
00125                      }
00126                      catch (Exception e) {}
00127               }
00128               return true;
00129        }
00130        
00131        static public void setType (XmlTag tag, PointObject p)
00132        {      if (tag.hasParam("shape"))
00133               {      String s=tag.getValue("shape");
00134                      if (s.equals("square")) p.setType(0);
00135                      if (s.equals("diamond")) p.setType(1);
00136                      if (s.equals("circle")) p.setType(2);
00137                      if (s.equals("dot")) p.setType(3);
00138                      if (s.equals("cross")) p.setType(4);
00139                      if (s.equals("dcross")) p.setType(5);
00140               }
00141        }
00142 
00143        public String getTag () { return "Point"; }
00144        public void construct (Construction c, 
00145               String name, String params[], int nparams)
00146               throws ConstructionException
00147        {      if (nparams==0)
00148               {      PointObject p=new PointObject(c,
00149                             c.getX()+(Math.random()-0.5)*c.getW(),
00150                             c.getY()+(Math.random()-0.5)*c.getW());
00151                      if (!name.equals("")) p.setNameCheck(name);
00152                      c.add(p);
00153                      p.setDefaults();
00154                      return;
00155               }
00156               if (nparams==1)
00157               {      ConstructionObject o=c.find(params[0]);
00158                      if (o==null)
00159                             throw new ConstructionException(Zirkel.name("exception.notfound")+" "+
00160                                    params[0]);
00161                      if (!(o instanceof PrimitiveLineObject) && 
00162                             !(o instanceof PrimitiveCircleObject))
00163                             throw new ConstructionException(Zirkel.name("exception.type")+" "+
00164                                    params[0]);
00165                      PointObject p=new PointObject(c,
00166                             c.getX()+(Math.random()-0.5)*c.getW(),
00167                             c.getY()+(Math.random()-0.5)*c.getW(),
00168                             o);
00169                      if (!name.equals("")) p.setNameCheck(name);
00170                      c.add(p);
00171                      p.setDefaults();
00172                      return;
00173               }
00174               if (nparams!=2)
00175                      throw new ConstructionException(Zirkel.name("exception.nparams"));
00176               Expression e1=new Expression(params[0],c,null);
00177               Expression e2=new Expression(params[1],c,null);
00178               if (!e1.isValid() || !e2.isValid())
00179                      throw new ConstructionException(Zirkel.name("exception.expression"));
00180               PointObject p=new PointObject(c,0,0);
00181               try
00182               {      double x=new Double(params[0]).doubleValue();
00183                      double y=new Double(params[1]).doubleValue();
00184                      p.move(x,y);
00185               }
00186               catch (Exception e)
00187               {      p.setFixed(params[0],params[1]);
00188               }
00189               c.add(p);
00190               p.validate();
00191               p.setDefaults();
00192               if (!name.equals("")) p.setNameCheck(name);
00193        }
00194 
00195        public void reset (ZirkelCanvas zc)
00196        {      super.reset(zc);
00197               zc.setPrompt(Zirkel.name("prompt.point"));
00198        }
00199        
00200 }