Back to index

wims  3.65+svn20090927
TextObject.java
Go to the documentation of this file.
00001 package rene.zirkel.objects;
00002 
00003 // file: PointObject.java
00004 
00005 import java.awt.*;
00006 import java.awt.event.*;
00007 import java.util.*;
00008 import java.io.*;
00009 
00010 import rene.gui.*;
00011 
00012 import rene.util.xml.*;
00013 import rene.zirkel.*;
00014 import rene.zirkel.construction.*;
00015 import rene.zirkel.dialogs.*;
00016 import rene.zirkel.expression.*;
00017 import rene.zirkel.graphics.*;
00018 
00019 class TextObjectDialog extends HelpCloseDialog
00020 {      TextArea A;
00021        TextObject TO;
00022        boolean Settings=false;
00023        boolean More;
00024        
00025        public TextObjectDialog (Frame f, TextObject o)
00026        {      super(f,Zirkel.name("edit.text.title"),true);
00027               TO=o;
00028               setLayout(new BorderLayout());
00029               A=new TextArea(25,60);
00030               A.setBackground(Global.Background);
00031               add("Center",new Panel3D(A));
00032               A.setText(o.getLines());
00033               Panel south=new MyPanel();
00034               south.add(new ButtonAction(this,Global.name("edit.more"),"More"));
00035               south.add(new ButtonAction(this,Global.name("ok"),"OK"));
00036               south.add(new ButtonAction(this,Global.name("close"),"Close"));
00037               south.add(new ButtonAction(this,Global.name("edit.text.settings"),"Settings"));
00038               addHelp(south,"text");
00039               add("South",new Panel3D(south));
00040               pack();
00041               center(f);
00042        }
00043 
00044        public void doAction (String o)
00045        {      if (o.equals("OK") || o.equals("More"))
00046               {      TO.setLines(A.getText());
00047                      More=o.equals("More");
00048                      doclose();
00049               }
00050               if (o.equals("Settings"))
00051               {      Settings=true;
00052                      doAction("OK");
00053               }
00054               else super.doAction(o);
00055        }
00056        
00057        public boolean wantSettings ()
00058        {      return Settings;
00059        }      
00060 
00061        public boolean wantsMore ()
00062        {      return More;
00063        }
00064 }
00065 
00066 
00067 class TextEditDialog extends ObjectEditDialog
00068 {      TextField X,Y;
00069        Checkbox Fixed;
00070        IconBar TypeIB;
00071        
00072        public TextEditDialog (Frame f, TextObject o)
00073        {      super(f,Zirkel.name("edit.text.title"),o);
00074        }
00075        
00076        public void addFirst (Panel P)
00077        {      TextObject t=(TextObject)O;
00078        
00079               X=new TextFieldAction(this,"X",""+t.getX(),30);
00080               P.add(new MyLabel(Zirkel.name("edit.point.x"))); P.add(X);
00081               Y=new TextFieldAction(this,"Y",""+t.getY(),30);
00082               P.add(new MyLabel(Zirkel.name("edit.point.y"))); P.add(Y);
00083 
00084               Fixed=new Checkbox("");
00085               Fixed.setState(t.fixed());
00086               P.add(new MyLabel(Zirkel.name("edit.fixed")));
00087               P.add(Fixed);
00088               if (t.fixed())
00089               {      X.setText(t.getEX());
00090                      Y.setText(t.getEY());
00091               }
00092        }
00093        
00094        public void doAction (String o)
00095        {      if ((o.equals("Y") || o.equals("X")) && Fixed!=null)
00096               {      Fixed.setState(true);
00097                      super.doAction("OK");
00098               }
00099               else super.doAction(o);
00100        }
00101 
00102        public void setAction ()
00103        {      TextObject t=(TextObject)O;
00104               
00105               if (Fixed!=null && Fixed.getState()==true)
00106               {      t.setFixed(X.getText(),Y.getText());
00107               }
00108               else
00109               {      try
00110                      {      double x=new Expression(X.getText(),
00111                                    t.getConstruction(),t).getValue();
00112                             double y=new Expression(Y.getText(),
00113                                    t.getConstruction(),t).getValue();
00114                             t.move(x,y);
00115                      }
00116                      catch (Exception e) {}
00117               }
00118               if (Fixed!=null && Fixed.getState()==false)
00119               {      t.setFixed(false);
00120               }
00121               
00122        }
00123 
00124        public void focusGained (FocusEvent e)
00125        {      if (Fixed!=null && Fixed.getState()) X.requestFocus();
00126               else super.focusGained(e);
00127        }
00128 
00129        public boolean showsValue ()
00130        {      return false;
00131        }
00132 }
00133 
00134 public class TextObject extends ConstructionObject
00135        implements MoveableObject
00136 {      protected double X,Y;
00137        Vector T; // Vector of expression strings
00138        static Count N=new Count();
00139        double C,R,W,H;
00140        protected Expression EX,EY;
00141        protected boolean Fixed;
00142        protected boolean DoShow; // for replays
00143        
00144        public TextObject (Construction c, double x, double y)
00145        {      super(c);
00146               X=x; Y=y;
00147               T=new Vector();
00148               setColor(ColorIndex);
00149               Valid=true;
00150        }
00151 
00152        public boolean nearto (int x, int y, ZirkelCanvas zc)
00153        {      if (!displays(zc)) return false;
00154               return C<=x && R<=y && x<=C+W && y<=R+H;
00155        }
00156        
00157        public String getTag () { return "Text"; }
00158        public int getN () { return N.next(); }
00159        
00160        public void updateText ()
00161        {      setText(getLines(),true);                 
00162        }
00163        
00164        public void move (double x, double y)
00165        {      X=x; Y=y;
00166        }
00167        
00168        public void paint (MyGraphics g, ZirkelCanvas zc)
00169        {      C=zc.col(X); R=zc.row(Y);
00170               if (mustHide(zc)) return;
00171               boolean hidden=Hidden;
00172               if (DoShow) Hidden=false;
00173               g.setColor(this);
00174               setFont(g);
00175               if (DoShow) Hidden=hidden;
00176               FontMetrics fm=g.getFontMetrics();
00177               W=H=fm.getHeight();
00178               Enumeration e=T.elements();
00179               double r=R;
00180               if (!e.hasMoreElements())
00181               {      String s="-";
00182                      g.drawString(s,C,r);
00183                      r+=H;
00184               }             
00185               while (e.hasMoreElements())
00186               {      String s=((ExpressionString)e.nextElement()).evaluate();
00187                      boolean tex=false,bold=false,large=false;
00188                      if (s.startsWith("***"))
00189                      {      s=s.substring(3); bold=large=true;
00190                      }
00191                      else if (s.startsWith("**"))
00192                      {      s=s.substring(2); large=true;
00193                      }
00194                      else if (s.startsWith("*"))
00195                      {      s=s.substring(1); bold=true;
00196                      }
00197                      if (g instanceof MyGraphics13)
00198                      {      if (isStrongSelected())
00199                             {      ((MyGraphics13)g).drawMarkerRect(C,r,10,10);
00200                                    g.setColor(this);
00201                             }
00202                             g.setFont(large,bold);
00203                             r+=((MyGraphics13)g).drawStringExtended(s,C,r);
00204                      }
00205                      else
00206                      {      g.setFont(large,bold);
00207                             fm=g.getFontMetrics();
00208                             int h=fm.getHeight();
00209                             g.drawString(AngleObject.translateToUnicode(s),C,r);
00210                             r+=H;
00211                      }
00212               }
00213               H=r-R;
00214        }
00215        
00216        public boolean mustHide (ZirkelCanvas zc)
00217        {      return super.mustHide(zc) && !(Valid && DoShow);
00218        }
00219        
00220        public void validate ()
00221        {      Valid=true;
00222               if (Fixed && EX!=null && EX.isValid())
00223               {      try
00224                      {      X=EX.getValue();
00225                      }
00226                      catch (Exception e)
00227                      {      Valid=false; return;
00228                      }
00229               }
00230               if (Fixed && EY!=null && EY.isValid())
00231               {      try
00232                      {      Y=EY.getValue();
00233                      }
00234                      catch (Exception e)
00235                      {      Valid=false; return;
00236                      }
00237               }
00238        }
00239        
00240        public String getLines ()
00241        {      String S="";
00242               Enumeration e=T.elements();
00243               while (e.hasMoreElements())
00244               {      String s=((ExpressionString)e.nextElement()).toString();
00245                      S=S+s;
00246                      if (e.hasMoreElements()) S=S+"\n";
00247               }
00248               return S;
00249        }
00250        
00251        public void setLines (String o)
00252        {      Vector w=new Vector();
00253               try
00254               {      BufferedReader in=new BufferedReader(
00255                             new StringReader(o));
00256                      while (true)
00257                      {      String s=in.readLine();
00258                             if (s==null) break;
00259                             w.addElement(new ExpressionString(s,this));
00260                      }
00261                      in.close();
00262               }
00263               catch (Exception e) {}
00264               T=w;
00265               updateText();
00266        }
00267        
00268        public String getDisplayValue ()
00269        {      return "("+roundDisplay(X)+","+roundDisplay(Y)+")";
00270        }
00271        
00272        public double getX () { return X; }
00273        public double getY () { return Y; }
00274 
00275        public void edit (ZirkelCanvas zc)
00276        {      TextObjectDialog d=new TextObjectDialog(zc.getFrame(),this);
00277               d.setVisible(true);
00278               zc.repaint();
00279               if (d.wantSettings())
00280               {      new TextEditDialog(zc.getFrame(),this).setVisible(true);
00281                      zc.validate();
00282                      zc.repaint();
00283               }
00284               if (d.wantsMore())
00285               {      new EditConditionals(zc.getFrame(),this);
00286                      validate();
00287               }
00288        }
00289 
00290        public void printArgs (XmlWriter xml)
00291        {      if (Fixed && EX!=null && EX.isValid()) xml.printArg("x",EX.toString());
00292               else xml.printArg("x",""+X);
00293               if (Fixed && EY!=null && EY.isValid()) xml.printArg("y",EY.toString());
00294               else xml.printArg("y",""+Y);
00295               if (Fixed) xml.printArg("fixed","true");
00296        }
00297 
00298        public boolean moveable ()
00299        {      return !Fixed;
00300        }      
00301 
00302        public boolean fixed () { return Fixed; }
00303 
00304        public void setFixed (boolean flag)
00305        {      Fixed=flag; 
00306               if (!Fixed) EX=EY=null;
00307               updateText();
00308        }
00309        
00310        public void setFixed (String x, String y)
00311        {      Fixed=true;
00312               EX=new Expression(x,getConstruction(),this);
00313               EY=new Expression(y,getConstruction(),this);
00314               updateText();
00315        }
00316 
00317        public String getEX ()
00318        {      if (EX!=null) return EX.toString();
00319               else return ""+round(X);
00320        }
00321 
00322        public String getEY ()
00323        {      if (EY!=null) return EY.toString();
00324               else return ""+round(Y);
00325        }
00326 
00327        public void setDoShow (boolean flag)
00328        {      DoShow=flag;
00329        }
00330 
00331        public void snap (ZirkelCanvas zc)
00332        {      double d=zc.getGridSize()/2;
00333               X=Math.round(X/d)*d;
00334               Y=Math.round(Y/d)*d;
00335        }
00336 
00337        public void translate ()
00338        {      if (Fixed)
00339               {      try
00340                      {      setFixed(EX.toString(),EY.toString());
00341                             EX.translate(); EY.translate();
00342                      }
00343                      catch (Exception e) {}
00344               }
00345               setLines(getLines());
00346               Enumeration e=T.elements();
00347               while (e.hasMoreElements())
00348               {      ((ExpressionString)e.nextElement()).translate();
00349               }
00350        }
00351        
00352        public Enumeration depending ()
00353        {      super.depending();
00354               Enumeration e=T.elements();
00355               while (e.hasMoreElements())
00356               {      ((ExpressionString)e.nextElement()).addDep(this);
00357               }
00358               if (Fixed)
00359               {      if (EX!=null) EX.addDep(this);
00360                      if (EY!=null) EY.addDep(this);
00361               }
00362               return DL.elements();
00363        }
00364        
00365        public boolean canDisplayName ()
00366        {      return false;
00367        }
00368 
00369        double oldx,oldy,startx,starty;
00370        
00371        public void startDrag (double x, double y) 
00372        {      oldx=X; oldy=Y;
00373               startx=x; starty=y;
00374        }
00375 
00376        public void dragTo (double x, double y) 
00377        {      move(oldx+(x-startx),oldy+(y-starty));
00378        }
00379 }