Back to index

wims  3.65+svn20090927
ConstructionObject.java
Go to the documentation of this file.
00001 package rene.zirkel.objects;
00002 
00003 // file: ConstructionObject.java
00004 
00005 import java.awt.*;
00006 import java.util.*;
00007 
00008 import rene.lister.*;
00009 import rene.util.FileName;
00010 import rene.util.MyVector;
00011 import rene.util.xml.*;
00012 import rene.util.sort.*;
00013 import rene.zirkel.Zirkel;
00014 import rene.zirkel.ZirkelCanvas;
00015 import rene.zirkel.ZirkelFrame;
00016 import rene.zirkel.construction.Construction;
00017 import rene.zirkel.construction.ConstructionException;
00018 import rene.zirkel.construction.Count;
00019 import rene.zirkel.construction.DepList;
00020 import rene.zirkel.expression.Expression;
00021 import rene.zirkel.expression.ExpressionString;
00022 import rene.zirkel.expression.NoValueException;
00023 import rene.zirkel.graphics.MyGraphics;
00024 
00025 class Conditional
00026 {      String Tag;
00027        Expression Expr;
00028        public Conditional (String t, Expression e)
00029        {      Tag=t; Expr=e;
00030        } 
00031        public String getTag () { return Tag; }
00032        public Expression getExpression () { return Expr; }
00033        public void setExpression (Expression e) { Expr=e; }
00034 }
00035 
00036 public class ConstructionObject
00037        implements Cloneable, SortObject, Element
00038 {      
00039        protected int ColorType=0;
00040        public static final int NORMAL=0,THICK=1,THIN=2,INVISIBLE=3;
00041        protected Color C=Color.black;
00042        protected int ColorIndex=0;
00043        
00044        protected boolean Selected=false; // is selected (highlight red!)
00045        protected boolean StrongSelected=false; // to highlight the object even more
00046        protected boolean Indicated=false; // mouse is over it (highlight darker!)
00047        protected boolean ShowIndicatedName=false; // shows the name (or name and value)
00048        protected boolean LabelSelected=false; // label is selected (highlight red!)
00049        protected boolean Valid=true; // e.g. for intersections
00050        protected boolean Hidden=false; // not visible or lighter
00051        protected boolean SuperHidden=false; // not visible at all
00052        protected boolean BreakHide=false; // Hide due to hiding break
00053        protected boolean ShowName=false; // shows the name (or name and value)
00054        protected boolean ShowValue=false; // shows the value (or name and value)
00055        protected boolean Selectable=true; // may be selected
00056        
00057        protected String Name="Generic";
00058        protected static int Count=1; // To generate a unique name
00059        
00060        protected String Text="???"; // Description (human readable)
00061        
00062        private int XOffset=0,YOffset=0; // Deprecated INT offset (translated to double)
00063        protected double XcOffset=0.0,YcOffset=0.0; // Offsets for the text display
00064        protected boolean KeepClose; // Keep the text display close to the object
00065        protected boolean DisplaysText=false; // Does this object display text?
00066        protected double TX1,TY1,TX2,TY2; // text rectangle.
00067        
00068        protected Construction Cn; // Construction that this Object belongs to
00069        
00070        protected boolean MainParameter,Parameter; // for macros and jobs
00071        protected boolean Target; // for macros and jobs
00072        
00073        public boolean Flag,RekFlag,IsDrawn,HasZ; // for various processing purposes
00074        public int Scratch; // for various processing purposes
00075        
00076        protected boolean Keep=false; // for job parameters.
00077        protected boolean JobTarget=false; // for job targets
00078        
00079        protected ConstructionObject Translation;
00080        
00081        protected boolean Back=false; // draws in the background (first round)
00082        protected boolean Obtuse=true; // for angles and circles between two points.
00083 
00084        protected boolean Break=false; // Break point in front of this object?
00085        protected boolean HideBreak=false; // Hide construction before this break!
00086        
00087        protected boolean Solid=false; // Not transparent?
00088        
00089        static Count N=new Count();
00090        
00091        protected boolean inConstruction; // Is in the construction (or deleted)
00092        
00093        protected String Unit=""; // Use a unit display
00094        
00095        protected boolean Bold=false; // Use a bold font for this item
00096        protected boolean Large=false; // Use a large font for this item
00097        
00098        protected Vector Conditionals; // For conditional formatting
00099        
00100        protected int NCount=0; // Unique number
00101        protected boolean gotNCount=false; // Have already a number (from load file)
00102        
00103        public boolean isGotNCount() {
00104               return gotNCount;
00105        }
00106 
00107        public void setGotNCount(boolean gotNCount) {
00108               this.gotNCount = gotNCount;
00109        }
00110 
00111        public ConstructionObject (Construction c)
00112        {      Cn=c;
00113               inConstruction=true;
00114               setName();
00115        }
00116        
00117        public ConstructionObject (Construction c, String name)
00118        {      Cn=c;
00119               inConstruction=true;
00120               setName(name);
00121        }
00122        
00126        public void setName ()
00127        {      if (Cn.LongNames)
00128                      Name=Zirkel.name("name."+getTag())+" "+getN();
00129               else
00130                      Name=Zirkel.name("name.short."+getTag())+getN();
00131        }
00132        public int getN () { return N.next(); }
00133        
00134        public String getTag() { return "Generic"; }
00135        
00140        public ConstructionObject getTranslation ()
00141        {      return Translation;
00142        }
00143        public void setTranslation (ConstructionObject t)
00144        {      Translation=t;
00145        }
00146        
00150        public void translate ()
00151        {
00152        }
00153 
00154        public void paint (MyGraphics g, ZirkelCanvas zc) {}
00155 
00159        public boolean nearto (int x, int y, ZirkelCanvas zc)
00160        {      return false;
00161        }
00165        public boolean nearto (int x, int y, boolean ignorefill, ZirkelCanvas zc)
00166        {      return nearto(x,y,zc);
00167        }
00172        public boolean onlynearto (int x, int y, ZirkelCanvas zc)
00173        {      return true;
00174        }
00175 
00176        public boolean textcontains (int x, int y, ZirkelCanvas zc)
00177        {      if (!DisplaysText || !Valid || 
00178                      (isHidden() && !zc.showHidden())) return false;
00179               return x>TX1 && x<TX2 && y>TY1 && y<TY2;
00180        }
00181 
00182        public void setSelected (boolean flag) { Selected=flag; StrongSelected=false; }
00183        public void setStrongSelected (boolean flag) { StrongSelected=flag; }
00184        public boolean isStrongSelected () { return StrongSelected; }
00185        public boolean selected () { return Selected || StrongSelected; }
00186        public void setLabelSelected (boolean flag) { LabelSelected=flag; }
00187        public boolean isSelectable ()
00188        {      return valid() && !JobTarget && Selectable; 
00189        }
00190        public void validate () { Valid=true; }
00191        public boolean valid ()
00192        {      return Valid; 
00193        }
00194        public boolean displays (ZirkelCanvas zc)
00195        {      if (SuperHidden) return false;
00196               if (!zc.showHidden() && (testConditional("hidden") || testConditional("superhidden"))) 
00197                      return false;
00198               return Valid && !BreakHide && (!Hidden || zc.showHidden());
00199        }
00200        
00201        public boolean indicated () { return Indicated; }
00202        public void setIndicated (boolean f)
00203        {      Indicated=f; 
00204        }
00205        public void setIndicated (boolean f, boolean showname)
00206        {      Indicated=f; ShowIndicatedName=showname;
00207        }
00208        
00213        public double changedBy ()
00214        {      return 0.0;
00215        }
00216 
00220        static public String text1 (String format, String s1)
00221        {      StringTokenizer t=tokenize(format);
00222               String s="Illegal Format";
00223               try
00224               {      s=t.nextToken()+s1;
00225                      s=s+t.nextToken();
00226               }
00227               catch (Exception e) {}
00228               return s.trim();
00229        }
00230        static public String text2 (String format, String s1, String s2)
00231        {      StringTokenizer t=tokenize(format);
00232               String s="Illegal Format";
00233               try
00234               {      s=t.nextToken()+s1+t.nextToken()+s2;
00235                      s=s+t.nextToken();
00236               }
00237               catch (Exception e) {}
00238               return s.trim();
00239        }
00240        static public String text3 (String format, String s1, String s2, String s3)
00241        {      StringTokenizer t=tokenize(format);
00242               String s="Illegal Format";
00243               try
00244               {      s=t.nextToken()+s1+t.nextToken()+s2+t.nextToken()+s3;
00245                      s=s+t.nextToken();
00246               }
00247               catch (Exception e) {}
00248               return s.trim();
00249        }
00250        static public String text4 (String format, String s1, String s2, String s3, String s4)
00251        {      StringTokenizer t=tokenize(format);
00252               String s="Illegal Format";
00253               try
00254               {      s=t.nextToken()+s1+t.nextToken()+s2+t.nextToken()+s3+t.nextToken()+s4;
00255                      s=s+t.nextToken();
00256               }
00257               catch (Exception e) {}
00258               return s;
00259        }
00260        static public String textAny (String format, String ss[])
00261        {      StringTokenizer t=tokenize(format);
00262               String s="Illegal Format";
00263               try
00264               {      s=t.nextToken();
00265                      for (int i=0; i<ss.length; i++)
00266                      {      s=s+ss[i];
00267                             if (i<ss.length-1) s=s+", ";
00268                      }
00269                      s=s+t.nextToken();
00270               }
00271               catch (Exception e) {}
00272               return s;
00273        }
00274        static private StringTokenizer tokenize (String format)
00275        {      if (format.startsWith("%"))
00276               {      format=" "+format;
00277               }
00278               if (format.endsWith("%"))
00279               {      format=format+" ";
00280               }
00281               return new StringTokenizer(format,"%");
00282        }
00283        
00284        public void setText (String s, boolean user)
00285        {      if (!user && Text.endsWith(" ")) return;
00286               Text=s;
00287        }
00288        public void setText (String s)
00289        {      setText(s,false);
00290        }
00291        public String getText ()
00292        {      return Text;
00293        }
00294        
00295        public boolean labelSelected ()
00296        {      return LabelSelected;
00297        }
00298 
00302        public void setColor (int index)
00303        {      if (index<0) C=Color.pink;
00304               else
00305               {      if (ColorType==THIN) C=ZirkelFrame.LightColors[index];
00306                      else C=ZirkelFrame.Colors[index];
00307                      ColorIndex=index;
00308               }
00309        }
00310 
00315        public int getColorIndex (boolean original)
00316        {      if (!original && haveConditionals())
00317               {      for (int i=0; i<ZirkelFrame.ColorStrings.length; i++)
00318                      {      if (testConditional(ZirkelFrame.ColorStrings[i]))
00319                                    return i;
00320                      }
00321               }
00322               return ColorIndex; 
00323        }
00324        public int getColorIndex ()
00325        {      return getColorIndex(false);
00326        }
00327 
00328        public Color getColor ()
00329        {      if (isJobTarget()) return ZirkelFrame.TargetColor;
00330               else if (selected()) return ZirkelFrame.SelectColor;
00331               else if (getColorType()==ConstructionObject.THIN)
00332               {      int i=getColorIndex();
00333                      if (isHidden()) return ZirkelFrame.BrighterLightColors[i];
00334                      else return ZirkelFrame.LightColors[i];                 
00335               }             
00336               else
00337               {      int i=getColorIndex();
00338                      if (isHidden()) return ZirkelFrame.BrighterColors[i];
00339                      else return ZirkelFrame.Colors[i];               
00340               }
00341        }
00342 
00343 
00344        public boolean mustHide (ZirkelCanvas zc)
00345        {      if (!Valid) return true;
00346               if (ColorIndex>0 && zc.getShowColor()!=0 && 
00347                      zc.getShowColor()!=ColorIndex) return true;
00348               if (JobTarget && Hidden) return true;
00349               return SuperHidden || testConditional("superhidden") || BreakHide ||
00350                      zc.hides(this) || (isHidden() && !zc.showHidden());
00351        }
00352 
00353        public void setHidden (boolean flag)
00354        {      Hidden=flag; 
00355               if (!flag) SuperHidden=false;
00356        }
00357        public void setSuperHidden (boolean flag)
00358        {      SuperHidden=flag;
00359        }
00360        public void toggleHidden ()
00361        {      Hidden=!Hidden; 
00362               if (!Hidden) SuperHidden=false;
00363        }
00364        public boolean isHidden (boolean original)
00365        {      if (!original && testConditional("visible")) return false;
00366               if (!original && (testConditional("hidden") || testConditional("superhidden"))) 
00367                      return true;
00368               return Hidden || SuperHidden; 
00369        }
00370        public boolean isHidden ()
00371        {      return isHidden(false);
00372        }
00373        public boolean isSuperHidden (boolean original)
00374        {      if (!original && testConditional("superhidden")) 
00375                      return true;
00376               return SuperHidden; 
00377        }
00378        public boolean isSuperHidden ()
00379        {      return isSuperHidden(false);
00380        }
00381        
00382        public String getName () { return Name; }
00383        public void setName (String n)
00384        {      Name="";
00385               if (Cn!=null)
00386               {      ConstructionObject o=Cn.find(n);
00387                      while (o!=null && o!=this)
00388                      {      n=n+"*";
00389                             o=Cn.find(n);
00390                      }
00391               }
00392               Name=n; 
00393        }
00394        public void setNameCheck (String n)
00395               throws ConstructionException
00396        {      boolean extend=false;
00397               Name="";
00398               if (Cn!=null)
00399               {      ConstructionObject o=Cn.find(n);
00400                      if (o!=null && o!=this)
00401                      {      n=n+"*";
00402                             o=Cn.find(n);
00403                             extend=true;
00404                      }
00405               }
00406               Name=n; 
00407               if (extend)
00408                      throw new ConstructionException(Zirkel.name("exception.double"));
00409        }      
00410        public void setNameWithNumber (String n)
00411        {      Name="";
00412               if (Cn!=null)
00413               {      ConstructionObject o=Cn.find(n);
00414                      if (o!=null)
00415                      {      while (o!=null && o!=this)
00416                             {      setName(); n=Name; Name="";
00417                                    o=Cn.find(n);
00418                             }
00419                             Name=n;
00420                      }
00421                      else Name=n;
00422               }
00423               else Name=n;
00424        }
00425 
00426        public void edit (ZirkelCanvas zc)
00427        {      zc.repaint();
00428        }
00429 
00434        public void drawLabel (MyGraphics g, String s)
00435        {      g.drawStringExtended(s,TX1,TY1);
00436               TX2=TX1+g.stringWidth(s);
00437               TY2=TY1+g.stringHeight(s);
00438        }
00439        
00440        public void setFont (MyGraphics g)
00441        {      g.setFont(Large,Bold);
00442        }
00443        
00447        public void drawCenteredLabel (MyGraphics g, String s, ZirkelCanvas zc,
00448               double x, double y, double xo, double yo)
00449        {      int ws=g.stringWidth(s),hs=g.stringHeight(s);
00450               TX1=zc.col(x+xo)-ws/2; 
00451               TY1=zc.row(y+yo)-hs/2;
00452               g.drawStringExtended(s,TX1,TY1);
00453               TX2=TX1+ws;
00454               TY2=TY1+hs;
00455        }
00456 
00462        public void drawLabel (MyGraphics g, String s, ZirkelCanvas zc,
00463               double x, double y, double vx, double vy, double xo, double yo)
00464        {      int ws=g.stringWidth(s),hs=g.stringHeight(s);
00465               double w=zc.dx(ws),h=zc.dy(hs);
00466               double d1=Math.abs(-w*vy+h*vx);
00467               double d2=Math.abs(w*vy+h*vx);
00468               double d=d1;
00469               if (d2>d) d=d2;
00470               double dd=zc.dy(2*zc.pointSize());
00471               TX1=zc.col(x-vy*(d+3*dd)/2+xo)-ws/2; 
00472               TY1=zc.row(y+vx*(d+3*dd)/2+yo)-hs/2;
00473               g.drawStringExtended(s,TX1,TY1);
00474               TX2=TX1+ws; TY2=TY1+hs;
00475        }
00476 
00482        public void drawPointLabel (MyGraphics g, String s, ZirkelCanvas zc,
00483               double x, double y, double vx, double vy, double xo, double yo)
00484        {      int ws=g.stringWidth(s),hs=g.stringHeight(s);
00485               double w=zc.dx(ws),h=zc.dy(hs);
00486               double d1=Math.abs(-w*vy+h*vx);
00487               double d2=Math.abs(w*vy+h*vx);
00488               double d=d1;
00489               if (d2>d) d=d2;
00490               double dd=zc.dy(2*zc.pointSize());
00491               if (Math.abs(d*vx)>h+2*dd)
00492               {      d=Math.abs((h+2*dd)/vx);
00493               }
00494               double xp=x-vy*d/2,yp=y+vx*d/2;
00495               TX1=zc.col(xp-vy*dd*1.5+xo)-ws/2; 
00496               TY1=zc.row(yp+vx*dd*1.5+yo)-hs/2;
00497               g.drawStringExtended(s,TX1,TY1);
00498               TX2=TX1+ws; TY2=TY1+hs;
00499        }
00500 
00504        public void save (XmlWriter xml)
00505        {      xml.startTagStart(getTag());
00506               xml.printArg("name",Name);
00507               if (AliasES!=null) xml.printArg("alias",AliasES.toString());
00508               xml.printArg("n",""+NCount);
00509               if (ColorIndex!=0) xml.printArg("color",""+ColorIndex);
00510               if (ColorType==THICK) xml.printArg("type","thick");
00511               if (ColorType==THIN) xml.printArg("type","thin");
00512               if (ColorType==INVISIBLE) xml.printArg("type","invisible");
00513               if (SuperHidden) xml.printArg("hidden","super");
00514               else if (Hidden) xml.printArg("hidden","true");
00515               if (ShowName) xml.printArg("showname","true");
00516               if (ShowValue) xml.printArg("showvalue","true");
00517               if (Back) xml.printArg("background","true");
00518               if (XcOffset!=0.0) xml.printArg("xcoffset",""+XcOffset);
00519               if (YcOffset!=0.0) xml.printArg("ycoffset",""+YcOffset);
00520               if (KeepClose) xml.printArg("keepclose","true");
00521               if (Parameter) xml.printArg("parameter","true");
00522               if (MainParameter) xml.printArg("mainparameter","true");
00523               if (Target) xml.printArg("target","true");
00524               if (Break)
00525               {      if (HideBreak) xml.printArg("hidebreak","true");
00526                      else xml.printArg("break","true");
00527               }
00528               if (Solid && maybeTransparent() && isFilled()) 
00529                      xml.printArg("solid","true");
00530               if (!Unit.equals("")) xml.printArg("unit",Unit);
00531               if (Bold) xml.printArg("bold","true");
00532               if (Large) xml.printArg("large","true");
00533               if (haveConditionals())
00534               {      int i=0;
00535                      Enumeration e=Conditionals.elements();
00536                      while (e.hasMoreElements())
00537                      {      Conditional c=(Conditional)e.nextElement();
00538                             xml.printArg("ctag"+i,c.getTag());
00539                             xml.printArg("cexpr"+i,c.getExpression().toString());
00540                             i++;
00541                      }
00542               }
00543               printArgs(xml);
00544               xml.startTagEnd();
00545               xml.print(Text);
00546               xml.endTag(getTag());
00547               xml.println();
00548        }
00549 
00550        public void printArgs (XmlWriter xml)
00551        {
00552        }
00553        public void updateText()
00554        {
00555        }
00556        
00557        public void setConstruction (Construction c)
00558        {      Cn=c; 
00559               inConstruction=true;
00560               setNameWithNumber(Name); // make sure the name is not double
00561        }
00562        
00563        public boolean showName (boolean original)
00564        {      if (!original && testConditional("showname")) return true;
00565               return ShowName || (Indicated && ShowIndicatedName); 
00566        }
00567        public boolean showName ()
00568        {      return showName(false);
00569        }
00570        public void setShowName (boolean f) { ShowName=f; }
00571        public boolean showValue (boolean original) 
00572        {      if (!original && testConditional("showvalue")) return true;
00573               return ShowValue; 
00574        }
00575        public boolean showValue ()
00576        {      return showValue(false);
00577        }
00578        public void setShowValue (boolean f) { ShowValue=f; }
00579 
00584        public String getDisplayText ()
00585        {      String name=Name;
00586               if (AliasES!=null)
00587               {      name=AliasES.evaluate();
00588               }
00589               else if (name.indexOf("~")>0)
00590               {      name=name.substring(0,name.indexOf("~"));
00591               }
00592               if (showName())
00593               {      if (showValue())
00594                      {      String value=getDisplayValue();
00595                             if (value.equals("")) return name;
00596                             if (name.endsWith("$")) name=name.substring(0,name.length()-1);
00597                             if (name.endsWith("_") && name.length()>1)
00598                             {      name=name.substring(0,name.length()-1);
00599                             }
00600                             else
00601                             {      name=name+" = ";
00602                             }
00603                             if (Unit.equals(""))
00604                                    return name+getDisplayValue();
00605                             else
00606                                    return name+getDisplayValue()+Unit;
00607                      }
00608                      else
00609                      {      if (name.indexOf("~")>0 && AliasES==null)
00610                             {      name=name.substring(0,name.indexOf("~"));
00611                             }
00612                             return name;
00613                      }
00614               }
00615               else if (showValue())
00616               {      if (Unit.equals(""))
00617                             return getDisplayValue();
00618                      else
00619                             return getDisplayValue()+Unit;
00620               }
00621               return "";
00622        }
00623 
00630        public String replaceExpressions (String s)
00631        {      // System.out.println(s);
00632               boolean latex=s.startsWith("$");
00633               int searchpos=0;
00634               while (s.indexOf("%",searchpos)>=0)
00635               {      int h1=s.indexOf("%",searchpos);
00636                      String s1=s.substring(h1+1);
00637                      int h2=s1.indexOf("%");
00638                      if (h2==0)
00639                      {      s=s.substring(0,h1)+"%"+s1.substring(1);
00640                             searchpos+=1;
00641                      }
00642                      else if (h2>=0)
00643                      {      String var=s1.substring(0,h2);
00644                             boolean showeq=false,forceplus=false;
00645                             if (var.startsWith("~")) // show equation
00646                             {      var=var.substring(1); showeq=true;
00647                             }
00648                             else if (var.startsWith("+")) // force +
00649                             {      var=var.substring(1); forceplus=true;
00650                             }
00651                             ConstructionObject o=getConstruction().find(var);
00652                             // System.out.println(o+" "+var);
00653                             String repl="";
00654                             if (o!=null) DL.add(o);
00655                             try
00656                             {      if (showeq) repl=o.getEquation();
00657                                    else
00658                                    {      if (o!=null)
00659                                           {      double x=round(o.getValue());
00660                                                  if (forceplus && x>=0) repl="+";
00661                                                  if (x==Math.floor(x+0.1)) repl=repl+(int)(x);
00662                                                  else repl=repl+x;
00663                                           }
00664                                           else
00665                                           {      Expression ex=new Expression(var,getConstruction(),this);
00666                                                  if (ex.isValid())
00667                                                  {      Enumeration e=ex.getDepList().elements();
00668                                                         while (e.hasMoreElements())
00669                                                         {      DL.add((ConstructionObject)e.nextElement());
00670                                                         }
00671                                                  }
00672                                                  double x=round(ex.getValue());
00673                                                  if (forceplus && x>=0) repl="+";
00674                                                  if (x==Math.floor(x+0.1)) repl=repl+(int)(x);
00675                                                  else repl=repl+x;
00676                                           }
00677                                    }
00678                             }
00679                             catch (Exception ex)
00680                             {      if (latex) repl="\\circ";
00681                                    else repl="???";
00682                                    // ex.printStackTrace();
00683                             }
00684                             s=s.substring(0,h1)+repl+s1.substring(h2+1);
00685                             searchpos=h1+repl.length();
00686                      }
00687                      else break;
00688               }
00689               return s;
00690        }
00691        
00698        public void addDepInExpressions (String s)
00699        {      replaceExpressions(s);
00700        }
00701        
00708        public String translateExpressions (String s)
00709        {      //System.out.println(s);
00710               boolean latex=s.startsWith("$");
00711               int searchpos=0;
00712               while (s.indexOf("%",searchpos)>=0)
00713               {      //System.out.println(searchpos);
00714                      int h1=s.indexOf("%",searchpos);
00715                      String s1=s.substring(h1+1);
00716                      int h2=s1.indexOf("%");
00717                      if (h2==0)
00718                      {      s=s.substring(0,h1)+"%"+s1.substring(1);
00719                             searchpos+=1;
00720                      }
00721                      else if (h2>=0)
00722                      {      String var=s1.substring(0,h2);
00723                             boolean showeq=false,forceplus=false;
00724                             String repl="";
00725                             if (var.startsWith("~")) // show equation
00726                             {      var=var.substring(1); showeq=true;
00727                                    repl="+";
00728                             }
00729                             else if (var.startsWith("+")) // force +
00730                             {      var=var.substring(1); forceplus=true;
00731                                    repl="~";
00732                             }
00733                             ConstructionObject o=getConstruction().find(var);
00734                             if (o!=null) o=o.getTranslation();
00735                             // System.out.println(o+" "+var);
00736                             if (showeq) repl=repl+o.getName();
00737                             else
00738                             {      if (o!=null)
00739                                    {      repl=repl+o.getName();
00740                                    }
00741                                    else
00742                                    {      Expression ex=new Expression(var,getConstruction(),this);
00743                                           if (ex.isValid())
00744                                           {      ex.translate();
00745                                                  repl=repl+ex.toString();
00746                                           }
00747                                           else
00748                                           {      return s;
00749                                           }
00750                                    }
00751                             }
00752                             // System.out.println(repl);
00753                             s=s.substring(0,h1)+"%"+repl+"%"+s1.substring(h2+1);
00754                             searchpos=h1+repl.length()+2;
00755                             // System.out.println(s);
00756                      }
00757                      else break;
00758               }
00759               return s;
00760        }
00761        
00767        public String getDisplayValue ()
00768        {      return "";
00769        }
00770        public String getSizeDisplay ()
00771        {      return getDisplayValue()+getUnit();
00772        }
00773        
00777        public double round (double x)
00778        {      return round(x,ZirkelCanvas.EditFactor);
00779        }
00780        public double roundDisplay (double x)
00781        {      return round(x,ZirkelCanvas.LengthsFactor);
00782        }
00783        public double round (double x, double Rounder)
00784        {      return Math.round(x*Rounder)/Rounder;
00785        }
00786        
00787        // The following procedures are for label offset
00788        
00789        public void setcOffset (double x, double y)
00790        {      XcOffset=x; YcOffset=y;
00791        }
00792        public double xcOffset () { return XcOffset; }
00793        public double ycOffset () { return YcOffset; }
00794 
00795        public void setOffset (int x, int y) // deprecated
00796        {      XOffset=x; YOffset=y;
00797        }
00798 
00799        public boolean isKeepClose()
00800        {      return KeepClose;
00801        }
00802        public void setKeepClose (double x, double y) // Override!
00803        {      KeepClose=true;
00804               XcOffset=x; YcOffset=y;
00805        }
00806        public void setKeepClose (boolean flag)
00807        {      if (KeepClose && !flag) { XcOffset=0; YcOffset=0; }
00808               KeepClose=flag;
00809        }
00810        public boolean canKeepClose () // Override!
00811        {      return false;
00812        }
00813 
00814        public void translateOffset (ZirkelCanvas zc)
00815        {      if (XOffset!=0 || YOffset!=0)
00816               {      XcOffset=zc.dx(XOffset);
00817                      YcOffset=zc.dy(-YOffset);
00818                      XOffset=YOffset=0;
00819               }
00820        }
00821 
00822        public int getColorType (boolean original)
00823        {      if (!original && testConditional("thin")) return THIN;
00824               if (!original && testConditional("thick")) return THICK;
00825               if (!original && testConditional("normal")) return NORMAL;
00826               return ColorType; 
00827        }
00828        public int getColorType ()
00829        {      return getColorType(false);
00830        }
00831        
00832        public void setColorType (int type)
00833        {      ColorType=type;
00834               setColor(ColorIndex);
00835        }
00836        
00837        public boolean isParameter () { return Parameter || MainParameter; }
00838        public boolean isMainParameter () { return MainParameter; }
00839        public void clearParameter ()
00840        {      Parameter=false; MainParameter=false;
00841        }
00842        public void setMainParameter ()
00843        {      Enumeration e=secondaryParams();
00844               while (e.hasMoreElements())
00845               {      ConstructionObject o=(ConstructionObject)e.nextElement();
00846                      o.setParameter();
00847               }
00848               MainParameter=true;
00849        }
00850        public void setParameter ()
00851        {      Parameter=true;
00852        }
00853        
00854        public static DepList DL=new DepList();
00855        
00860        public Enumeration depending ()
00861        {      DL.reset();
00862               if (haveConditionals())
00863               {      Enumeration e=Conditionals.elements();
00864                      while (e.hasMoreElements())
00865                      {      Conditional c=(Conditional)e.nextElement();
00866                             Enumeration ex=c.getExpression().getDepList().elements();
00867                             while (ex.hasMoreElements())
00868                             {      DL.add((ConstructionObject)ex.nextElement());
00869                             }
00870                      }
00871               }
00872               if (AliasES!=null) AliasES.addDep(this);
00873               return DL.elements();
00874        }
00875        
00879        public boolean dependsOn (ConstructionObject o)
00880        {      Enumeration e=depending();
00881               while (e.hasMoreElements())
00882               {      ConstructionObject o1=(ConstructionObject)e.nextElement();
00883                      if (o1==o) return true;
00884               }
00885               return false;
00886        }
00887        
00892        public Enumeration secondaryParams ()
00893        {      DL.reset();
00894               return DL.elements();
00895        }
00896        
00897        public ConstructionObject[] getDepArray ()
00898        {      depending();
00899               return DL.getArray();
00900        }
00901        
00902        public Enumeration depset (ConstructionObject o1)
00903        {      DL.add(o1);
00904               return DL.elements();
00905        }
00906        
00907        public Enumeration depset (ConstructionObject o1, ConstructionObject o2)
00908        {      DL.add(o1);
00909               DL.add(o2);
00910               return DL.elements();
00911        }
00912        
00913        public Enumeration depset (ConstructionObject o1, ConstructionObject o2,
00914               ConstructionObject o3)
00915        {      DL.add(o1);
00916               DL.add(o2);
00917               DL.add(o3);
00918               return DL.elements();
00919        }
00920        
00921        public void setFlag (boolean flag) { Flag=flag; }
00922        public boolean isFlag () { return Flag; }
00923        
00924        public void setRekFlag (boolean flag) { RekFlag=flag; }
00925        public boolean isRekFlag () { return RekFlag; }
00926        
00927        public void setTarget (boolean flag) { Target=flag; }
00928        public boolean isTarget () { return Target; }
00929        
00930        public void setKeep (boolean flag) { Keep=flag; }
00931        public boolean isKeep () { return Keep; } 
00932        
00933        public boolean equals (ConstructionObject o)
00934        {      return false;
00935        }
00936        public boolean equals (double x, double y)
00937        {      return Math.abs(x-y)<1e-8;
00938        }
00939        
00940        int OldColorIndex;
00941        public void setJobTarget (boolean flag)
00942        {      if (flag)
00943               {      OldColorIndex=ColorIndex;
00944                      setColor(-1);
00945               }
00946               else if (JobTarget) setColor(OldColorIndex);
00947               JobTarget=flag;
00948        }
00949        public boolean isJobTarget ()
00950        {      return JobTarget;
00951        }
00952        
00953        public void updateCircleDep ()
00954        {
00955        }
00956        
00957        public void clearCircleDep ()
00958        {
00959        }
00960        
00965        public ConstructionObject copy ()
00966        {      ConstructionObject o=null;
00967               try
00968               {      o=(ConstructionObject)clone();
00969                      setTranslation(o);
00970                      o.translateConditionals();
00971                      o.translate();
00972                      o.setName();
00973                      o.updateText();
00974                      o.setBreak(false);
00975                      // o.setTarget(false);
00976               }
00977               catch (Exception e) {}
00978               return o;
00979        }      
00980 
00981        public Construction getConstruction ()
00982        {      return Cn;
00983        }      
00984        
00989        public double getValue ()
00990               throws ConstructionException
00991        {      throw new NoValueException(Valid);
00992        }
00993        
00994        public void setDefaults ()
00995        {      setShowName(Cn.ShowNames);
00996               setShowValue(Cn.ShowValues);
00997               setColor(Cn.DefaultColor);
00998               setColorType(Cn.DefaultColorType);
00999               setHidden(Cn.Hidden);
01000               setObtuse(Cn.Obtuse);
01001               setSolid(Cn.Solid);
01002               setLarge(Cn.LargeFont);
01003               setBold(Cn.BoldFont);
01004        }
01005        
01006        public void setTargetDefaults ()
01007        {      setShowName(Cn.ShowNames);
01008               setShowValue(Cn.ShowValues);
01009               setColor(Cn.DefaultColor);
01010               setColorType(Cn.DefaultColorType);
01011        }
01012 
01013        public boolean contains (PointObject p)
01014        {      return false;
01015        }
01016 
01017        public boolean canFix ()
01018        {      return false;
01019        }      
01020        
01021        public void setFixed (String o)
01022        {
01023        }
01024 
01025        public boolean isBack (boolean original)
01026        {      if (!original && testConditional("background")) return true;
01027               return Back;
01028        }
01029        public boolean isBack ()
01030        {      return isBack(false);
01031        }
01032        public void setBack (boolean flag)
01033        {      Back=flag;
01034        }
01035 
01036        public void setInConstruction (boolean flag)
01037        {      inConstruction=flag;
01038        }
01039        public boolean isInConstruction ()
01040        {      return inConstruction;
01041        }
01042        
01049        public void laterBind (Construction c)
01050        {
01051        }
01052        
01059        public void laterTranslate (Construction c)
01060        {      
01061        }
01062 
01063        public void setFilled (boolean flag)
01064        {
01065        }      
01066        public boolean isFilled ()
01067        {      return false;
01068        }
01069        public boolean isFilledForSelect ()
01070        {      return isFilled();
01071        }
01072 
01073        public boolean getObtuse ()
01074        {      return Obtuse;
01075        }
01076        public void setObtuse (boolean flag)
01077        {      Obtuse=flag;
01078        }
01079 
01080        public String getDescription ()
01081        {      return "";
01082        }
01083        
01084        public void round ()
01085        {
01086        }
01087 
01088        public boolean isFixed ()
01089        {      return false;
01090        }      
01091        
01092        public boolean isBreak ()
01093        {      return Break;
01094        }
01095        
01096        public void setBreak (boolean flag)
01097        {      Break=flag;
01098               HideBreak=false;
01099        }
01100        
01101        public boolean isHideBreak ()
01102        {      return HideBreak;
01103        }
01104        
01105        public void setHideBreak (boolean flag)
01106        {      HideBreak=flag;
01107               Break=flag;
01108        }
01109        
01110        public boolean isSolid (boolean original)
01111        {      if (!original && testConditional("solid")) return true;
01112               return Solid;
01113        }
01114        public boolean isSolid ()
01115        {      return isSolid(false);
01116        }
01117        
01118        public void setSolid (boolean solid)
01119        {      Solid=solid;
01120        }
01121        
01122        public boolean maybeTransparent ()
01123        {      return false;
01124        }
01125        
01126        public boolean locallyLike (ConstructionObject o)
01127        {      return false;
01128        }
01129 
01130        public double Value=1000;
01131 
01132        public int compare (SortObject o)
01133        {      ConstructionObject ob=(ConstructionObject)o;
01134               if (ob.Value<Value) return 1;
01135               else if (ob.Value==Value) return 0;
01136               else return -1;
01137        }
01138        
01139        public void setSelectable (boolean flag)
01140        {      Selectable=flag;
01141        }
01142 
01143        public void snap (ZirkelCanvas zc)
01144        {
01145        }
01146        
01147        public String getUnit ()
01148        {      return Unit;
01149        }      
01150        public void setUnit (String s)
01151        {      Unit=s;
01152        }
01153        public boolean hasUnit ()
01154        {      return true;
01155        }
01156        
01157        public boolean isBreakHide ()
01158        {      return BreakHide;
01159        }
01160        public void setBreakHide (boolean flag)
01161        {      BreakHide=flag;
01162        }
01163        
01164        public String getEquation ()
01165        {      return getDisplayValue();
01166        }
01167 
01168        public String helpDisplayValue (boolean start, double x, String tag)
01169        {      String s="";
01170               if (Math.abs(x)<1e-10) return s;
01171               if (x<0) s="-";
01172               else if (!start) s="+";
01173               x=Math.abs(x);
01174               if (Math.abs(x-1)>1e-10)
01175                      s=s+roundDisplay(x);
01176               if (tag.equals("")) return s;
01177               else if (s.equals("") || s.equals("-")) return s+tag;
01178               else return s+"*"+tag;
01179        }
01180        
01181        public String helpDisplayNumber (boolean start, double x)
01182        {      String s="";
01183               if (Math.abs(x)<1e-10) return s;
01184               if (x<0) s="-";
01185               else if (!start) s="+";
01186               x=Math.abs(x);
01187               return s+roundDisplay(x);
01188        }
01189 
01190        public boolean visible (ZirkelCanvas zc)
01191        {      return selected() || ColorType!=INVISIBLE
01192                      || zc.showHidden(); 
01193        }      
01194        
01195        public boolean isBold ()
01196        {      return Bold;
01197        }
01198        public void setBold (boolean bold)
01199        {      Bold=bold;
01200        }
01201        
01202        public boolean isLarge ()
01203        {      return Large; 
01204        }
01205        public void setLarge (boolean large)
01206        {      Large=large;
01207        } 
01208 
01209        // Routines for conditional formatting
01210        
01211        public void addConditional (String tag, Expression expr)
01212        {      Conditional c=new Conditional(tag,expr);
01213               if (Conditionals==null) Conditionals=new Vector();
01214               Conditionals.addElement(c);
01215        }
01216        
01217        public Expression getConditional (String tag)
01218        {      if (Conditionals==null) return null;
01219               Enumeration e=Conditionals.elements();
01220               while (e.hasMoreElements())
01221               {      Conditional c=(Conditional)e.nextElement();
01222                      if (c.getTag().equals(tag))
01223                             return c.getExpression();
01224               }
01225               return null;
01226        }
01227        
01228        public boolean haveConditional (String tag)
01229        {      Expression ex=getConditional(tag);
01230               return (ex!=null);
01231        }
01232        
01237        public boolean testConditional (String tag)
01238        {      Expression ex=getConditional(tag);
01239               if (ex==null) return false;
01240               try
01241               {      if (ex.getValue()!=0) return true;
01242               }
01243               catch (Exception e) {}
01244               return false;
01245        }
01246        
01247        public void clearConditionals ()
01248        {      Conditionals=null;
01249        }
01250        
01251        public void translateConditionals ()
01252        {      if (Conditionals!=null)
01253               {      Enumeration e=Conditionals.elements();
01254                      clearConditionals();
01255                      while (e.hasMoreElements())
01256                      {      Conditional c=(Conditional)e.nextElement();
01257                             String expr=c.getExpression().toString();
01258                             Expression enew=new Expression(expr,getConstruction(),this);
01259                             enew.translate();
01260                             addConditional(c.getTag(),enew);
01261                      }
01262               }
01263               if (AliasES!=null)
01264               {      setAlias(AliasES.toString());
01265                      AliasES.translate();
01266               }
01267        }
01268        
01269        public boolean haveConditionals ()
01270        {      return Conditionals!=null;
01271        }
01272        
01273        public void checkConditionals ()
01274               throws ConstructionException
01275        {      if (Conditionals==null) return;
01276               Enumeration e=Conditionals.elements();
01277               while (e.hasMoreElements())
01278               {      Conditional c=(Conditional)e.nextElement();
01279                      if (!c.getExpression().isValid())
01280                             throw new ConstructionException(c.getExpression().getErrorText());
01281                      c.getExpression().getValue();
01282               }             
01283        }
01284        
01290        public double getZ ()
01291               throws ConstructionException
01292        {      Expression e=getConditional("z");
01293               if (e==null)
01294                      throw new ConstructionException("");
01295               return e.getValue();
01296        }
01297        
01298        public boolean canDisplayName ()
01299        {      return true;
01300        }
01301 
01302        public int getNCount() {
01303               return NCount;
01304        }
01305 
01306        public void setNCount(int count) {
01307               NCount = count;
01308        }
01309 
01310        ExpressionString AliasES=null;
01311        
01312        public String getAlias ()
01313        {      if (AliasES==null) return null;
01314               else return AliasES.toString();
01315        }
01316        
01317        public void setAlias (String s)
01318        {      if (s==null) AliasES=null;
01319               else AliasES=new ExpressionString(s,this);
01320        }
01321 
01327        public MyVector VRek;
01328        public boolean RekValidating=false;
01329        
01330        public static final int DescriptionState=0,FormulaState=1,SizeState=2;
01331 
01335        public String getElementString (int state) 
01336        {      String s="";
01337               switch (state)
01338               {      case DescriptionState : s=getName()+" : "+FileName.chop(getText(),80); break;
01339                      case SizeState : s=getName()+" : "+FileName.chop(getSizeDisplay(),80); break;
01340                      case FormulaState : s=getName()+" : "+FileName.chop(getEquation(),80); break;
01341               }
01342               if (isSuperHidden()) s="( "+s+" )";
01343               if (isHideBreak()) s="** "+s;
01344               else if (isBreak()) s="* "+s;
01345               if (!valid()) s="? "+s;
01346               if (this instanceof MoveableObject && ((MoveableObject)this).moveable()) s="> "+s;
01347               return s;
01348        }
01349        public String getElementString() 
01350        {      return getElementString(0);
01351        }
01352        
01356        public Color getElementColor() 
01357        {      if (isJobTarget()) return ZirkelFrame.TargetColor;
01358               else if (indicated()) return ZirkelFrame.IndicateColor;
01359               else if (selected()) return ZirkelFrame.SelectColor;
01360               return getColor();
01361        }
01362 
01363        public boolean SpecialParameter=false;
01364        public boolean isSpecialParameter () { return SpecialParameter; }
01365        public void setSpecialParameter (boolean flag) { SpecialParameter=flag; }
01366        
01372        static public PointObject getPointObject (Expression E)
01373        {      if (E==null) return null;
01374               ConstructionObject o=E.getObject();
01375               if (o==null || !(o instanceof PointObject)) return null;
01376               return (PointObject)o;
01377        }
01378        
01379        // provide a reusable array for each construction object
01380        
01381        MyVector MV;
01382        boolean MVInUse=false;
01383        
01388        public MyVector getVector ()
01389        {      if (MVInUse) return new MyVector();
01390               if (MV==null) MV=new MyVector();
01391               MVInUse=true;
01392               return MV;
01393        }
01394        
01398        public void discardMV ()
01399        {      MVInUse=false;
01400               MV.removeAllElements();
01401        }
01402        
01403 }