Back to index

wims  3.65+svn20090927
AreaConstructor.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 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.objects.AreaObject;
00014 import rene.zirkel.objects.ConstructionObject;
00015 import rene.zirkel.objects.PointObject;
00016 
00017 public class AreaConstructor extends ObjectConstructor
00018 {      Vector Points=new Vector();
00019        
00020        public void mousePressed (MouseEvent e, ZirkelCanvas zc)
00021        {      if (!zc.Visual) return;
00022               double x=zc.x(e.getX()),y=zc.y(e.getY());
00023               PointObject P=zc.selectCreatePoint(e.getX(),e.getY());
00024               if (P!=null)
00025               {      P.setSelected(true);
00026                      Enumeration en=Points.elements();
00027                      while (en.hasMoreElements())
00028                      {      if (en.nextElement()==P)
00029                             {      if (Points.size()>=3)
00030                                    {      AreaObject o=new AreaObject(zc.getConstruction(),Points);
00031                                           zc.addObject(o);
00032                                           o.setDefaults();            
00033                                           if (!o.isSolid()) o.setBack(true);
00034                                    }
00035                                    reset(zc);
00036                                    return;
00037                             }
00038                      }
00039                      Points.addElement(P);
00040                      zc.repaint();
00041               }
00042        }
00043 
00044        public String getTag () { return "Polygon"; }
00045 
00046        public void construct (Construction c, 
00047               String name, String params[], int nparams)
00048               throws ConstructionException
00049        {      if (nparams<3)
00050                      throw new ConstructionException(Zirkel.name("exception.nparams"));
00051               Vector v=new Vector();
00052               for (int i=0; i<nparams; i++)
00053               {      ConstructionObject o=c.find(params[i]);
00054                      if (o==null)
00055                             throw new ConstructionException(Zirkel.name("exception.notfound")+" "+
00056                                    params[i]);
00057                      if (!(o instanceof PointObject))
00058                             throw new ConstructionException(Zirkel.name("exception.type")+" "+
00059                                    params[i]);
00060                      v.addElement(o);
00061               }
00062               AreaObject o=new AreaObject(c,v);
00063               if (!name.equals("")) o.setNameCheck(name);
00064               c.add(o);
00065               o.setDefaults();
00066               o.setBack(true);
00067        }
00068 
00069        public boolean construct (XmlTree tree, Construction c)
00070               throws ConstructionException
00071        {      if (!testTree(tree,"Polygon")) return false;
00072               XmlTag tag=tree.getTag();
00073               try
00074               {      int i=1;
00075                      Vector v=new Vector();
00076                      while (true)
00077                      {      PointObject p=(PointObject)c.find(tag.getValue("point"+i));
00078                             if (p==null) break;
00079                             v.addElement(p);
00080                             i++;
00081                      }
00082                      AreaObject o=new AreaObject(c,v);
00083                      o.setBack(true);
00084                      setName(tag,o);
00085                      set(tree,o);
00086                      c.add(o);
00087                      setConditionals(tree,c,o);
00088               }
00089               catch (ConstructionException e)
00090               {      throw e;
00091               }
00092               catch (Exception e)
00093               {      throw new ConstructionException("Polygon parameters illegal!");
00094               }
00095               return true;
00096        }
00097 
00098 
00099        public void showStatus (ZirkelCanvas zc)
00100        {      zc.showStatus(Zirkel.name("message.area"));
00101               zc.setPrompt("="+Zirkel.name("prompt.area"));
00102        }
00103 
00104        public void reset (ZirkelCanvas zc)
00105        {      super.reset(zc);
00106               Points=new Vector();
00107               zc.showStatus(Zirkel.name("message.area"));
00108        }
00109        
00110 }