Back to index

wims  3.65+svn20090927
Macro.java
Go to the documentation of this file.
00001 package rene.zirkel.macro;
00002 
00003 import java.util.*;
00004 
00005 import rene.util.xml.*;
00006 import rene.zirkel.ZirkelCanvas;
00007 import rene.zirkel.construction.Construction;
00008 import rene.zirkel.construction.ConstructionException;
00009 import rene.zirkel.objects.*;
00010 
00021 public class Macro extends Construction
00022        implements Cloneable
00023 {      public String Name;
00024        public String Prompts[];
00025        public ConstructionObject Params[];
00026        public String PromptFor[]=new String[0];
00027        public String PromptName[]=new String[0];
00028        public String LastParams[];
00029        public boolean Fixed[];
00030        boolean Protected=false;
00031        boolean HideDuplicates=true;
00032 
00039        public Macro (ZirkelCanvas zc, String name, String comment, String s[])
00040        {      Name=name; Comment=comment;
00041               Prompts=s;
00042        }
00043        
00044        public String getName () { return Name; }
00045        public void setName (String name)
00046        {      Name=name;
00047        }
00048        public String getComment () { return Comment; }
00049        public String[] getPrompts () { return Prompts; }
00050 
00055        public void setParams (ConstructionObject p[]) { Params=p; }
00056        public ConstructionObject[] getParams () { return Params; }
00057 
00061        public void initLast ()
00062        {      LastParams=new String[Params.length];
00063        }
00064        public void setLast (String name, int i)
00065        {      try
00066               {      LastParams[i]=name;
00067               }
00068               catch (Exception e) {}
00069        }
00070        public String getLast (int i)
00071        {      if (LastParams!=null && LastParams[i]!=null) return LastParams[i];
00072               else return "";
00073        }
00074 
00075        public void setPromptFor (String s[])
00076        {      PromptFor=s;
00077               PromptName=new String[PromptFor.length];
00078               for (int i=0; i<PromptFor.length; i++) PromptName[i]=PromptFor[i];
00079        }
00080        public void setPromptName (int i, String s)
00081        {      PromptName[i]=s;
00082        }
00083        public boolean promptFor (String s)
00084        {      return getPromptFor(s)>=0;  
00085        }
00086        public int getPromptFor (String s)
00087        {      for (int i=0; i<PromptFor.length; i++)
00088                      if (PromptFor[i].equals(s)) return i;
00089               return -1;
00090        }
00091        public String getPromptName (String s)
00092        {      for (int i=0; i<PromptFor.length; i++)
00093                      if (PromptFor[i].equals(s)) return PromptName[i];
00094               return "";
00095        }
00096        public int countPrompts ()
00097        {      return PromptFor.length;    
00098        }
00099 
00103        public void saveMacro (XmlWriter xml)
00104        {      // Start the macro with its name as parameter
00105               xml.startTagStart("Macro");
00106               xml.printArg("Name",Name);
00107               if (!HideDuplicates) xml.printArg("showduplicates","true");
00108               xml.startTagEndNewLine();
00109               // Write the parameters and their prompts
00110               for (int i=0; i<Params.length; i++)
00111               {      xml.startTagStart("Parameter");
00112                      xml.printArg("name",Params[i].getName());
00113                      if (Fixed!=null && Fixed[i] && 
00114                                    LastParams!=null && LastParams[i]!=null)
00115                             xml.printArg("fixed",LastParams[i]);
00116                      xml.startTagEnd();
00117                      xml.print(Prompts[i]);
00118                      xml.endTagNewLine("Parameter");
00119               }
00120               // Write a comment
00121               if (!getComment().equals(""))
00122               {      xml.startTagNewLine("Comment");
00123                      xml.printParagraphs(getComment(),60);
00124                      xml.endTagNewLine("Comment");
00125               }
00126               // Write the objects. I.e., secondary parameters, primary
00127               // parameters and constructed things, including targets as in
00128               // any other construction.
00129               xml.startTagNewLine("Objects");
00130               save(xml);
00131               xml.endTagNewLine("Objects");
00132               // Save the objects prompted for
00133               if (PromptFor.length>0)
00134               {      xml.startTagStart("PromptFor");
00135                      for (int i=0; i<PromptFor.length; i++)
00136                      {      xml.printArg("object"+i,PromptFor[i]);
00137                             xml.printArg("prompt"+i,PromptName[i]);
00138                      }
00139                      xml.finishTagNewLine();
00140               }
00141               // End the macro
00142               xml.endTagNewLine("Macro");
00143        }
00144 
00148        public Macro (ZirkelCanvas zc, XmlTree tree)
00149               throws ConstructionException
00150        {      // See, if this is a macro tree, and has a name.
00151               XmlTag tag=tree.getTag();
00152               if (!tag.name().equals("Macro"))
00153                      throw new ConstructionException("No macro!");
00154               if (!tag.hasParam("Name"))
00155                      throw new ConstructionException("Name missing!");
00156               Name=tag.getValue("Name");
00157               if (tag.hasParam("showduplicates"))
00158                      HideDuplicates=false;
00159               // Walk through content
00160               Enumeration e=tree.getContent();
00161               while (e.hasMoreElements())
00162               {      XmlTree t=(XmlTree)e.nextElement();
00163                      tag=t.getTag();
00164                      // Read the objects, the comment
00165                      if (tag.name().equals("Objects")) // Objects
00166                      {      readConstruction(t);
00167                             break;
00168                      }
00169                      else if (tag.name().equals("Comment")) // Comment
00170                      {      try
00171                             {      setComment(t.parseComment());
00172                             }
00173                             catch (Exception ex)
00174                             {      throw new ConstructionException("Illegal Comment");
00175                             }
00176                      }
00177               }
00178               // Read the parameters.
00179               int ParamCount=0;
00180               // First count the paramters.
00181               e=tree.getContent();
00182               while (e.hasMoreElements())
00183               {      XmlTree t=(XmlTree)e.nextElement();
00184                      tag=t.getTag();
00185                      if (tag.name().equals("Parameter"))
00186                      {      if (!tag.hasParam("name"))
00187                                    throw new ConstructionException(
00188                                           "Parameter name missing!");
00189                             ParamCount++;
00190                      }
00191                      else if (tag.name().equals("PromptFor"))
00192                      {      if (tag.hasParam("object"))
00193                             {      String s[]=new String[1];
00194                                    s[0]=tag.getValue("object");
00195                                    setPromptFor(s);
00196                                    if (tag.hasParam("prompt"))
00197                                    setPromptName(0,tag.getValue("prompt"));
00198                             }
00199                             else
00200                             {      int n=0;
00201                                    while (tag.hasParam("object"+n)) n++;
00202                                    String s[]=new String[n];
00203                                    for (int i=0; i<n; i++)
00204                                    {      s[i]=tag.getValue("object"+i);
00205                                    }
00206                                    setPromptFor(s);
00207                                    for (int i=0; i<n; i++)
00208                                    {      if (tag.hasParam("prompt"+i))
00209                                                  setPromptName(i,tag.getValue("prompt"+i));
00210                                    }                                  
00211                             }
00212                      }
00213               }
00214               // Then read the parameters.
00215               Params=new ConstructionObject[ParamCount];
00216               initLast();
00217               Prompts=new String[ParamCount];
00218               for (int pr=0; pr<ParamCount; pr++) Prompts[pr]="";
00219               int i=0;
00220               e=tree.getContent();
00221               while (e.hasMoreElements())
00222               {      XmlTree t=(XmlTree)e.nextElement();
00223                      tag=t.getTag();
00224                      if (tag.name().equals("Parameter"))
00225                      {      // Search a parameter by this name
00226                             Params[i]=find(tag.getValue("name"));
00227                             if (Params[i]==null)
00228                                    throw new ConstructionException(
00229                                           "Illegal parameter "+tag.getValue("name")+"!");
00230                             if (tag.hasParam("fixed"))
00231                             {      if (Fixed==null)
00232                                    {      Fixed=new boolean[ParamCount];
00233                                           for (int j=0; j<ParamCount; j++) Fixed[j]=false;
00234                                    }
00235                                    Fixed[i]=true;
00236                                    LastParams[i]=tag.getValue("fixed");
00237                             }
00238                             Enumeration en=t.getContent();
00239                             while (en.hasMoreElements())
00240                             {      tree=(XmlTree)en.nextElement();
00241                                    if (tree.getTag() instanceof XmlTagText)
00242                                    {      Prompts[i]=((XmlTagText)tree.getTag()).getContent();
00243                                    }
00244                             }
00245                             i++;
00246                      }
00247               }
00248        }
00249        
00253        public Vector getTargets ()
00254        {      Vector v=new Vector();
00255               Enumeration e=V.elements();
00256               while (e.hasMoreElements())
00257               {      ConstructionObject o=(ConstructionObject)e.nextElement();
00258                      if (o.isTarget())
00259                             v.addElement(o);
00260               }
00261               return v;
00262        }
00263 
00264        public boolean hasFixed ()
00265        {      for (int i=0; i<Prompts.length; i++)
00266                      if (Prompts[i].startsWith("=")) return true;
00267               if (Fixed==null) return false;
00268               for (int i=0; i<Fixed.length; i++)
00269                      if (Fixed[i]) return true;
00270               return false;
00271        }
00272        public boolean isFixed (int i)
00273        {      if (Fixed==null) return false;
00274               return Fixed[i];
00275        }
00276        public void setFixed (int i, boolean f)
00277        {      if (Fixed==null) return;
00278               Fixed[i]=f;
00279        }
00280        
00281        public Object clone ()
00282        {      try
00283               {      return super.clone();
00284               }
00285               catch (Exception e) {}
00286               return null;
00287        }
00288        
00289        public boolean isProtected ()
00290        {      return Protected;
00291        }
00292        public void setProtected (boolean flag)
00293        {      Protected=flag;
00294        }
00295        
00296        public boolean hideDuplicates ()
00297        {      return HideDuplicates;
00298        }
00299        public void hideDuplicates (boolean flag)
00300        {      HideDuplicates=flag;
00301        }
00302 }