Back to index

wims  3.65+svn20090927
LineConstructor.java
Go to the documentation of this file.
00001 package rene.zirkel.constructors;
00002 
00003 // file: LineConstructor.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.macro.*;
00011 import rene.zirkel.objects.*;
00012 import rene.gui.Global;
00013 
00014 public class LineConstructor extends ObjectConstructor
00015 {      PointObject P1=null,P2=null;
00016        ConstructionObject O;
00017        boolean Fix;
00018        boolean ShowsValue,ShowsName,Moved;
00019 
00020        public void mousePressed (MouseEvent e, ZirkelCanvas zc)
00021        {      if (!zc.Visual) return;
00022               if (P1==null)
00023               {      P1=select(e.getX(),e.getY(),zc);
00024                      if (P1!=null)
00025                      {      P1.setSelected(true);
00026                             zc.repaint();
00027                      }
00028                      showStatus(zc);
00029               }
00030               else
00031               {      P2=select(e.getX(),e.getY(),zc);
00032                      if (P2!=null)
00033                      {      if (P2==P1)
00034                             {      P2=null; return;
00035                             }
00036                             ConstructionObject o=create(zc.getConstruction(),P1,P2);
00037                             zc.addObject(o);
00038                             o.setDefaults();
00039                             Fix=e.isShiftDown() || isFixed();
00040                             if (P2.moveable() && !P2.isPointOn() && zc.isNewPoint()) 
00041                             {      Dragging=true; Moved=false;
00042                                    O=o;
00043                                    ShowsValue=o.showValue();
00044                                    ShowsName=o.showName();
00045                                    if ((Fix && Global.getParameter("options.movefixname",true))
00046                                           ||
00047                                           (!Fix && Global.getParameter("options.movename",false)))
00048                                    {      o.setShowValue(true);
00049                                           o.setShowName(true);
00050                                    }
00051                             }
00052                             else
00053                             {      Dragging=false;
00054                                    if (Fix) setFixed(zc,o);
00055                                    P1=P2=null;
00056                                    zc.clearSelected();
00057                                    showStatus(zc);
00058                             }
00059                      }
00060                      else Dragging=false;
00061               }
00062        }
00063        
00064        public boolean waitForLastPoint ()
00065        {      return P1!=null && P2==null;
00066        }
00067        
00068        public void finishConstruction (MouseEvent e, ZirkelCanvas zc)
00069        {      P2=select(e.getX(),e.getY(),zc);
00070               if (P2!=null)
00071               {      ConstructionObject o=create(zc.getConstruction(),P1,P2);
00072                      zc.addObject(o);
00073                      o.setDefaults();
00074                      zc.validate();
00075                      zc.repaint();
00076                      P2=null;
00077               }      
00078        }
00079        
00080        public void mouseDragged (MouseEvent e, ZirkelCanvas zc)
00081        {      if (!Dragging) return;
00082               else
00083               {      Moved=true;
00084                      P2.move(zc.x(e.getX()),zc.y(e.getY()));
00085                      zc.validate();
00086                      zc.repaint();
00087               }
00088        }
00089        
00090        public void mouseReleased (MouseEvent e, ZirkelCanvas zc)
00091        {      if (!Dragging) return;
00092               Dragging=false;
00093               O.setShowValue(ShowsValue);
00094               O.setShowName(ShowsName);
00095               if (Fix) O.round();
00096               zc.repaint();
00097               if (Fix && !Moved) setFixed(zc,O);
00098               reset(zc);
00099        }
00100        
00101        public boolean isFixed ()
00102        {      return false;
00103        }
00104        public void setFixed (ZirkelCanvas zc, ConstructionObject o)
00105        {
00106        }
00107        public PointObject select (int x, int y, ZirkelCanvas zc)
00108        {      return zc.selectCreatePoint(x,y);
00109        }
00110        public ConstructionObject create (Construction c,
00111               PointObject p1, PointObject p2)
00112        {      return new LineObject(c,p1,p2);
00113        }
00114        public void reset (ZirkelCanvas zc)
00115        {      if (!zc.Visual)
00116               {      zc.setPrompt(getPrompt());
00117               }
00118               else
00119               {      zc.clearSelected();
00120                      P1=P2=null;
00121                      showStatus(zc);
00122               }
00123        }
00124        public String getPrompt()
00125        {      return Zirkel.name("prompt.line");
00126        }
00127        public void showStatus (ZirkelCanvas zc)
00128        {      if (P1==null) zc.showStatus(
00129                      Zirkel.name("message.line.first","Line: Set the first point!"));
00130               else zc.showStatus(
00131                      Zirkel.name("message.line.second","Line: Set the second point!"));
00132        }
00133 
00134        public boolean construct (XmlTree tree, Construction c)
00135               throws ConstructionException
00136        {      if (!testTree(tree,"Line")) return false;
00137               XmlTag tag=tree.getTag();
00138               if (!tag.hasParam("from") || !tag.hasParam("to"))
00139               {      if (!(c instanceof Macro))
00140                             throw new ConstructionException("Line points missing!");
00141                      PrimitiveLineObject o=new PrimitiveLineObject(c);
00142                      setName(tag,o);
00143                      set(tree,o);
00144                      c.add(o);
00145                      setConditionals(tree,c,o);
00146               }
00147               else try
00148               {      PointObject p1=(PointObject)c.find(tag.getValue("from")); 
00149                      PointObject p2=(PointObject)c.find(tag.getValue("to"));
00150                      LineObject o=new LineObject(c,p1,p2);
00151                      if (tag.hasParam("partial")) o.setPartial(true);
00152                      setName(tag,o);
00153                      set(tree,o);
00154                      c.add(o);
00155                      setConditionals(tree,c,o);
00156               }
00157               catch (ConstructionException e)
00158               {      throw e;
00159               }
00160               catch (Exception e)
00161               {      throw new ConstructionException("Line points illegal!");
00162               }
00163               return true;
00164        }
00165 
00166        public String getTag () { return "Line"; }
00167        public void construct (Construction c, 
00168               String name, String params[], int nparams)
00169               throws ConstructionException
00170        {      if (nparams!=2)
00171                      throw new ConstructionException(Zirkel.name("exception.nparams"));
00172               ConstructionObject
00173                      P1=c.find(params[0]);
00174               if (P1==null)
00175                      throw new ConstructionException(Zirkel.name("exception.notfound")+" "+
00176                             params[0]);
00177               ConstructionObject
00178                      P2=c.find(params[1]);
00179               if (P2==null)
00180                      throw new ConstructionException(Zirkel.name("exception.notfound")+" "+
00181                             params[1]);
00182               if (!(P1 instanceof PointObject))
00183                      throw new ConstructionException(Zirkel.name("exception.type")+" "+
00184                             params[0]);
00185               if (!(P2 instanceof PointObject))
00186                      throw new ConstructionException(Zirkel.name("exception.type")+" "+
00187                             params[1]);
00188               LineObject s=new LineObject(c,(PointObject)P1,(PointObject)P2);
00189               c.add(s);
00190               s.setDefaults();
00191               if (!name.equals("")) s.setNameCheck(name);
00192        }
00193 }