Back to index

wims  3.65+svn20090927
FunctionConstructor.java
Go to the documentation of this file.
00001 package rene.zirkel.constructors;
00002 
00003 // file: QuadricConstructor.java
00004 
00005 import rene.util.xml.*;
00006 import rene.zirkel.*;
00007 import rene.zirkel.construction.*;
00008 import rene.zirkel.expression.Expression;
00009 import rene.zirkel.objects.*;
00010 
00011 public class FunctionConstructor extends ObjectConstructor
00012 {      public boolean construct (XmlTree tree, Construction c)
00013               throws ConstructionException
00014        {      if (!testTree(tree,"Function")) return false;
00015               XmlTag tag=tree.getTag();
00016               if (tag.hasParam("f")) // function
00017               {      if (!tag.hasParam("var"))
00018                                    throw new ConstructionException("Function invalid!");
00019                      try
00020                      {      String y=tag.getValue("f");
00021                             String var=tag.getValue("var");
00022                             UserFunctionObject f=new UserFunctionObject(c);
00023                             f.setDefaults();
00024                             setName(tag,f);
00025                             set(tree,f);
00026                             c.add(f);
00027                             double xpos,ypos;
00028                             try
00029                             {      if (tag.hasParam("fixed"))
00030                                    {      f.setFixed(tag.getValue("x"),tag.getValue("y"));
00031                                    }
00032                                    else
00033                                    {      xpos=new Expression(tag.getValue("x"),c,f).getValue();
00034                                           ypos=new Expression(tag.getValue("y"),c,f).getValue();
00035                                           f.move(xpos,ypos);
00036                                    }
00037                             }
00038                             catch (Exception e) {}
00039                             if (tag.hasParam("filled")) f.setFilled(true);
00040                             f.setExpressions(var,y);
00041                             setConditionals(tree,c,f);
00042                             f.updateText();
00043                             return true;
00044                      }
00045                      catch (Exception e)
00046                      {      throw new ConstructionException("Function invalid!");
00047                      }
00048               }
00049               else // curve
00050               {      if (!tag.hasParam("min") &&
00051                             !tag.hasParam("max") &&
00052                             !tag.hasParam("d") &&
00053                             !tag.hasParam("var") &&
00054                             !tag.hasParam("x") &&
00055                             !tag.hasParam("y"))
00056                                    throw new ConstructionException("Function invalid!");
00057                      try
00058                      {      String x=tag.getValue("x");
00059                             String y=tag.getValue("y");
00060                             String var=tag.getValue("var");
00061                             String d=tag.getValue("d");
00062                             String min=tag.getValue("min");
00063                             String max=tag.getValue("max");
00064                             FunctionObject f=new FunctionObject(c);
00065                             f.setDefaults();
00066                             setType(tag,f);
00067                             setName(tag,f);
00068                             set(tree,f);
00069                             c.add(f);
00070                             if (tag.hasParam("filled")) f.setFilled(true);
00071                             f.setExpressions(var,x,y);
00072                             f.setRange(min,max,d);
00073                             f.setSpecial(tag.hasTrueParam("special"));
00074                             setConditionals(tree,c,f);
00075                             if (tag.hasParam("center")) f.setCenter(tag.getValue("center"));
00076                             f.updateText();
00077                             return true;
00078                      }
00079                      catch (Exception e)
00080                      {      throw new ConstructionException("Function invalid!");
00081                      }
00082               }
00083        }
00084 
00085        static public void setType (XmlTag tag, FunctionObject p)
00086        {      if (tag.hasParam("shape"))
00087               {      String s=tag.getValue("shape");
00088                      if (s.equals("square")) p.setType(0);
00089                      if (s.equals("diamond")) p.setType(1);
00090                      if (s.equals("circle")) p.setType(2);
00091                      if (s.equals("dot")) p.setType(3);
00092                      if (s.equals("cross")) p.setType(4);
00093                      if (s.equals("dcross")) p.setType(5);
00094               }
00095        }
00096 
00097        public String getTag () { return "Function"; }
00098 
00099        public void construct (Construction c, 
00100               String name, String params[], int nparams)
00101               throws ConstructionException
00102        {      if (nparams!=6)
00103                      throw new ConstructionException(Zirkel.name("exception.nparams"));
00104               boolean added=false;
00105               try
00106               {      FunctionObject F=new FunctionObject(c);
00107                      c.add(F);
00108                      added=true;
00109                      if (!name.equals("")) F.setNameCheck(name);
00110                      F.setRange(params[0],params[1],params[2]);
00111                      F.setExpressions(params[3],params[4],params[5]);
00112                      F.setDefaults();
00113               }
00114               catch (ConstructionException e)
00115               {      if (added) c.back();
00116                      throw e;
00117               }
00118               catch (Exception e)
00119               {      if (added) c.back();
00120                      throw new ConstructionException("Function Invalid!");
00121               }
00122        }
00123 }