Back to index

wims  3.65+svn20090927
UserFunctionObject.java
Go to the documentation of this file.
00001 package rene.zirkel.objects;
00002 
00003 // file: Functionbject.java
00004 
00005 import java.awt.*;
00006 import java.awt.event.*;
00007 import java.util.*;
00008 
00009 import rene.gui.*;
00010 import rene.util.xml.*;
00011 import rene.zirkel.*;
00012 import rene.zirkel.construction.*;
00013 import rene.zirkel.dialogs.*;
00014 import rene.zirkel.expression.*;
00015 import rene.zirkel.graphics.*;
00016 import rene.dialogs.*;
00017 
00018 class FunctionEditDialog extends ObjectEditDialog
00019 {      TextField Var,EY,X,Y;
00020        IconBar IC;
00021        ZirkelCanvas ZC;
00022        Checkbox Fixed;
00023 
00024        public FunctionEditDialog (ZirkelCanvas zc, UserFunctionObject o)
00025        {      super(zc.getFrame(),Zirkel.name("edit.function.title"),o,"function");
00026               ZC=zc;
00027        }
00028        
00029        public void addFirst (Panel P)
00030        {      UserFunctionObject f=(UserFunctionObject)O;
00031        
00032               Var=new TextFieldAction(this,"var",""+f.getVar(),30);
00033               P.add(new MyLabel(Zirkel.name("function.vars"))); P.add(Var);
00034               EY=new TextFieldAction(this,"ey",""+f.getEY(),30);
00035               P.add(new MyLabel(Zirkel.name("function.f"))); P.add(EY);
00036               
00037               X=new TextFieldAction(this,"X",""+f.round(f.getX()));
00038               P.add(new MyLabel(Zirkel.name("edit.point.x"))); P.add(X);
00039               Y=new TextFieldAction(this,"Y",""+f.round(f.getY()));
00040               P.add(new MyLabel(Zirkel.name("edit.point.y"))); P.add(Y);
00041               
00042               Fixed=new Checkbox("");
00043               Fixed.setState(f.fixed());
00044               P.add(new MyLabel(Zirkel.name("edit.fixed")));
00045               P.add(Fixed);
00046               if (f.fixed())
00047               {      X.setText(f.getEXpos());
00048                      Y.setText(f.getEYpos());
00049               }
00050 
00051        }
00052        
00053        public void setAction ()
00054        {      UserFunctionObject f=(UserFunctionObject)O;
00055               f.setExpressions(Var.getText(),EY.getText());           
00056               try
00057               {      double x=new Double(X.getText()).doubleValue();
00058                      double y=new Double(Y.getText()).doubleValue();
00059                      f.move(x,y);
00060               }
00061               catch (Exception e) {}
00062               if (Fixed.getState()==true) f.setFixed(X.getText(),Y.getText());
00063               else
00064               {      try
00065                      {      double x=new Expression(X.getText(),
00066                                    f.getConstruction(),f).getValue();
00067                             double y=new Expression(Y.getText(),
00068                                    f.getConstruction(),f).getValue();
00069                             f.move(x,y);
00070                             f.setFixed(false);
00071                      }
00072                      catch (Exception e) {}
00073               }
00074        }
00075        
00076        public void focusGained (FocusEvent e)
00077        {      Var.requestFocus();
00078        }
00079        
00080 }
00081 
00088 public class UserFunctionObject extends ConstructionObject
00089        implements MoveableObject
00090 {      Expression EY=null;
00091        double X[]={0};
00092        String Var[]={"x"};
00093        protected double Xpos,Ypos;
00094        protected boolean Fixed;
00095        protected Expression EXpos,EYpos;
00096 
00097        public UserFunctionObject (Construction c)
00098        {      super(c);
00099               validate();
00100               updateText();
00101        }
00102        
00103        public String getTag () { return "Function"; }
00104        public int getN () { return N.next(); }
00105        
00106        public void updateText ()
00107        {      setText(getDisplayValue());
00108        }
00109        
00110        public void validate ()
00111        {      if (EY!=null)
00112               {      Valid=EY.isValid();
00113               }
00114               else Valid=false;    
00115               if (Fixed && EXpos!=null && EXpos.isValid())
00116               
00117               {      try
00118                      {      Xpos=EXpos.getValue();
00119                      }
00120                      catch (Exception e) { Valid=false; return; }
00121               }
00122               if (Fixed && EYpos!=null && EYpos.isValid())
00123               {      try
00124                      {      Ypos=EYpos.getValue();
00125                      }
00126                      catch (Exception e) { Valid=false; return; }
00127               }
00128        }
00129 
00130        public void setExpressions (String t, String ey)
00131        {      StringTokenizer tok=new StringTokenizer(t);
00132               Var=new String[tok.countTokens()];
00133               X=new double[tok.countTokens()];
00134               int i=0;
00135               while (tok.hasMoreTokens())
00136               {      Var[i++]=tok.nextToken();
00137               }
00138               EY=new Expression(ey,getConstruction(),this,Var);
00139               validate();
00140        }
00141 
00142        public String getEY ()
00143        {      if (EY!=null) return EY.toString();
00144               else return "0";
00145        }
00146 
00147        double C,R,W,H;
00148        
00149        public void paint (MyGraphics g, ZirkelCanvas zc)
00150        {      if (!Valid || mustHide(zc)) return;
00151               FontMetrics fm=g.getFontMetrics();
00152               W=H=fm.getHeight();
00153               C=zc.col(Xpos); R=zc.row(Ypos);
00154               g.setColor(this);
00155               setFont(g);
00156               g.drawString(AngleObject.translateToUnicode(getDisplayValue()),C,R);
00157               R-=H;
00158        }
00159        
00160        public double getValue ()
00161               throws ConstructionException
00162        {      if (!Valid) throw new InvalidException("exception.invalid");
00163               return X[0];
00164        }
00165        
00166        public double getValue (String var)
00167               throws ConstructionException
00168        {      if (!Valid) throw new InvalidException("exception.invalid");
00169               for (int i=0; i<Var.length; i++)
00170                      if (var.equals(Var[i])) return X[i];
00171               return X[0];
00172        }
00173 
00174        public String getDisplayValue ()
00175        {      String s="";
00176               if (showName())
00177               {      if (getAlias()!=null) s=getAlias()+" : ";
00178                      s=s+getName()+"("+Var[0];
00179                      for (int i=1; i<Var.length; i++) s=s+","+Var[i];
00180                      s=s+")";
00181                      if (showValue()) s=s+"=";
00182               }
00183               if (showValue()) s=s+((EY==null)?"":EY.toString());
00184               return s; 
00185        }
00186        
00187        public boolean nearto (int cc, int rr, ZirkelCanvas zc)
00188        {      if (!displays(zc)) return false;
00189               return C<=cc && R<=rr && cc<=C+W && rr<=R+H;
00190        }
00191        
00192        public boolean EditAborted;
00193               
00194        public void edit (ZirkelCanvas zc)
00195        {      ObjectEditDialog d;
00196               while (true)
00197               {      d=new FunctionEditDialog(zc,this);
00198                      d.setVisible(true);
00199                      EditAborted=false;
00200                      if (d.isAborted())
00201                      {      EditAborted=true;
00202                             break;
00203                      }
00204                      else if (!EY.isValid())
00205                      {      Frame F=zc.getFrame();
00206                             Warning w=new Warning(F,EY.getErrorText(),
00207                                    Zirkel.name("warning"),true);
00208                             w.center(F);
00209                             w.setVisible(true);
00210                      }
00211                      if ((EXpos!=null && !EXpos.isValid()))
00212                      {      Frame F=zc.getFrame();
00213                             Warning w=new Warning(F,EXpos.getErrorText(),
00214                                    Zirkel.name("warning"),true);
00215                             w.center(F);
00216                             w.setVisible(true);
00217                      }
00218                      else if ((EYpos!=null && !EYpos.isValid()))
00219                      {      Frame F=zc.getFrame();
00220                             Warning w=new Warning(F,EYpos.getErrorText(),
00221                                    Zirkel.name("warning"),true);
00222                             w.center(F);
00223                             w.setVisible(true);
00224                      }
00225                      else break;
00226               }
00227               validate();
00228               updateText();
00229               zc.getConstruction().updateCircleDep();
00230               zc.repaint();
00231               if (d.wantsMore())
00232               {      new EditConditionals(zc.getFrame(),this);
00233                      validate();
00234               }
00235        }
00236 
00237        public void printArgs (XmlWriter xml)
00238        {      xml.printArg("f",EY.toString());
00239               if (Fixed && EXpos!=null && EXpos.isValid()) xml.printArg("x",EXpos.toString());
00240               else xml.printArg("x",""+Xpos);
00241               if (Fixed && EYpos!=null && EYpos.isValid()) xml.printArg("y",EYpos.toString());
00242               else xml.printArg("y",""+Ypos);    
00243               if (Fixed) xml.printArg("fixed","true");
00244               xml.printArg("var",getVar());
00245        }
00246        
00247        public void translate ()
00248        {      try
00249               {      EY=new Expression(EY.toString(),getConstruction(),this,Var);
00250                      ConstructionObject O=getTranslation();
00251                      setTranslation(this);
00252                      if (Fixed)
00253                      {      try
00254                             {      setFixed(EXpos.toString(),EYpos.toString());
00255                                    EXpos.translate(); EYpos.translate();
00256                             }
00257                             catch (Exception e) {}
00258                      }
00259                      validate();
00260                      setTranslation(O);
00261               }
00262               catch (Exception e)
00263               {      System.out.println();
00264                      System.out.println(getName());
00265                      System.out.println(e);
00266                      e.printStackTrace();
00267               }
00268        }
00269        
00270        public void setFixed (boolean flag)
00271        {      Fixed=flag; 
00272               if (!Fixed) EXpos=EYpos=null;
00273               updateText();
00274        }
00275        
00276        public void setFixed (String x, String y)
00277        {      Fixed=true;
00278               EXpos=new Expression(x,getConstruction(),this);
00279               EYpos=new Expression(y,getConstruction(),this);
00280               updateText();
00281        }
00282        
00283        public boolean fixed () { return Fixed; }
00284 
00285        public String getEXpos ()
00286        {      if (EXpos!=null) return EXpos.toString();
00287               else return ""+round(Xpos);
00288        }
00289 
00290        public String getEYpos ()
00291        {      if (EYpos!=null) return EYpos.toString();
00292               else return ""+round(Ypos);
00293        }
00294 
00295 
00296        public boolean onlynearto (int x, int y, ZirkelCanvas zc)
00297        {      return false;
00298               //return nearto(x,y,zc);
00299        }
00300 
00301        public boolean equals (ConstructionObject o)
00302        {      return false;
00303        }
00304 
00305        public Enumeration depending ()
00306        {      DL.reset();
00307               addDepending(EY);
00308               if (Fixed)
00309               {      addDepending(EXpos);
00310                      addDepending(EYpos);
00311               }
00312               return DL.elements();
00313        }
00314        
00315        public void addDepending (Expression E)
00316        {      if (E!=null)
00317               {      Enumeration e=E.getDepList().elements();
00318                      while (e.hasMoreElements())
00319                      {      DL.add((ConstructionObject)e.nextElement());
00320                      }
00321               }
00322        }
00323 
00324        public boolean hasUnit ()
00325        {      return false;
00326        }
00327        
00328        public double evaluateF (double x[])
00329               throws ConstructionException
00330        {      int n=x.length;
00331               if (n>X.length) n=X.length;
00332               for (int i=0; i<n; i++) X[i]=x[i];
00333               for (int i=n; i<X.length; i++) X[i]=0;
00334               try
00335               {      return EY.getValue();
00336               }
00337               catch (Exception e)
00338               {      throw new ConstructionException("");
00339               }
00340        }
00341 
00342        public double evaluateF (double x)
00343               throws ConstructionException
00344        {      X[0]=x;
00345               for (int i=1; i<X.length; i++) X[i]=0;
00346               try
00347               {      return EY.getValue();
00348               }
00349               catch (Exception e)
00350               {      throw new ConstructionException("");
00351               }
00352        }
00353 
00354        public boolean maybeTransparent ()
00355        {      return true;
00356        }
00357 
00358        public boolean canDisplayName ()
00359        {      return true;
00360        }
00361        
00362        public boolean isFilledForSelect ()
00363        {      return false;
00364        }
00365        
00366        public String getVar ()
00367        {      String vars=Var[0];
00368               for (int i=1; i<Var.length; i++) vars=vars+" "+Var[i];
00369               return vars;
00370        }
00371 
00372        public void dragTo(double x, double y) 
00373        {      move(oldx+(x-startx),oldy+(y-starty));
00374        }
00375 
00376        public void move (double x, double y) 
00377        {      Xpos=x; Ypos=y;
00378        }
00379 
00380        double oldx,oldy,startx,starty;
00381        
00382        public boolean moveable() 
00383        {      return !Fixed;
00384        }
00385 
00386        public void startDrag(double x, double y) 
00387        {      oldx=Xpos; oldy=Ypos;
00388               startx=x; starty=y;
00389        }
00390 
00391        public double getX () { return Xpos; }
00392        public double getY () { return Ypos; }
00393 
00394 }