001package org.vishia.gral.cfg;
002
003import java.io.IOException;
004import java.io.Writer;
005import java.util.Map;
006
007import org.vishia.gral.base.GralPos;
008import org.vishia.msgDispatch.LogMessage;
009
010public class GralCfgWriter
011{
012  
013  /**Version and history
014   * <ul>
015   * <ul>2015-27-01 Hartmut new: Now a text field can be set editable. Usage of a boolean {@link GralCfgData#editable} 
016   *  instead changing the type of a text field from 'S' to 'T', but in {@link GralCfgWriter#writeShowField(org.vishia.gral.cfg.GralCfgData.GuiCfgShowField)}
017   *  it is written as a "InputTextline(...". It means next read of the config reads it as editable. 
018   * <li>2011-06-00 Hartmut created, 
019   * </ul>
020   *
021   * <b>Copyright/Copyleft</b>:<br>
022   * For this source the LGPL Lesser General Public License,
023   * published by the Free Software Foundation is valid.
024   * It means:
025   * <ol>
026   * <li> You can use this source without any restriction for any desired purpose.
027   * <li> You can redistribute copies of this source to everybody.
028   * <li> Every user of this source, also the user of redistribute copies
029   *    with or without payment, must accept this license for further using.
030   * <li> But the LPGL is not appropriate for a whole software product,
031   *    if this source is only a part of them. It means, the user
032   *    must publish this part of source,
033   *    but doesn't need to publish the whole source of the own product.
034   * <li> You can study and modify (improve) this source
035   *    for own using or for redistribution, but you have to license the
036   *    modified sources likewise under this LGPL Lesser General Public License.
037   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
038   * </ol>
039   * If you intent to use this source without publishing its usage, you can get
040   * a second license subscribing a special contract with the author. 
041   * 
042   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
043   */
044  public static final String version = "2015-01-27";
045
046  private String sLastPanel;
047  
048  final LogMessage log;
049
050  Writer writer;
051  
052  public GralCfgWriter(LogMessage log)
053  { this.log = log;
054  }
055  
056  
057  public String saveCfg(Writer dest, GralCfgData cfg)
058  { this.writer = dest;
059    String sError = null;
060    try{
061      writer.append("size(500,120); ");   //TODO it isn't used yet
062      writeDataReplace(cfg);
063      for(GralCfgElement cfge: cfg.listElementsInTextfileOrder) {
064        writeElement(dest, cfge);
065      }
066      /*
067      GralCfgElement cfge = cfg.firstElement;
068      while(cfge !=null){
069        writeElement(dest, cfge);
070        cfge = cfge.next;
071      }
072      */
073      dest.append("\n");
074    } catch(IOException exc){ log.sendMsg(-1, "exception writing config"); }
075    return sError;
076  }
077  
078  
079  
080  void writeDataReplace(GralCfgData cfg) throws IOException
081  { writer.append("\n");
082    if(cfg !=null)for( Map.Entry<String, String> entry: cfg.dataReplace.entrySet()){
083      writer.append("DataReplace: ").append(entry.getKey()).append(" = ").append(entry.getValue()).append(";\n");
084    }
085    writer.append("\n");
086    
087  }
088  
089  
090  void writeElement(Writer ww, GralCfgElement cfge) 
091  throws IOException
092  {
093    if(cfge.widgetType instanceof GralCfgData.GuiCfgImage){
094      ww.append("\n\n//================================================================================\n");
095    }
096    
097    if(cfge.widgetType.whatIs == 'w'){ 
098      GralCfgPanel cfgp = (GralCfgPanel)cfge;
099      writer.append("\n\n//================================================================================\n");
100      writer.append("Window: @").append(cfgp.windPos);
101      //writePosition(ww, cfge);
102      writer.append(": ");
103      writer.append(cfgp.name).append(", \"").append(cfgp.windTitle).append("\";\n\n");
104
105    } else {
106      //<Element>
107      writePosition(ww, cfge);
108      if(cfge.widgetType instanceof GralCfgData.GuiCfgShowField){ writeShowField((GralCfgData.GuiCfgShowField)cfge.widgetType); }
109      else if(cfge.widgetType instanceof GralCfgData.GuiCfgText){ writeText((GralCfgData.GuiCfgText)cfge.widgetType); }
110      else if(cfge.widgetType instanceof GralCfgData.GuiCfgLed){ writeLed((GralCfgData.GuiCfgLed)cfge.widgetType); }
111      else if(cfge.widgetType instanceof GralCfgData.GuiCfgImage){ writeImage((GralCfgData.GuiCfgImage)cfge.widgetType); }
112      else if(cfge.widgetType instanceof GralCfgData.GuiCfgInputFile){ writeInputFile((GralCfgData.GuiCfgInputFile)cfge.widgetType); }
113      //else if(cfge.widgetType instanceof GralCfgData.GuiCfgInputFile){ writeInputFile((GralCfgData.GuiCfgInputFile)cfge.widgetType); }
114      else if(cfge.widgetType instanceof GralCfgData.GuiCfgButton){ writeButton((GralCfgData.GuiCfgButton)cfge.widgetType); }
115      //else if(cfge.widgetType instanceof GuiCfgData.GuiCfg){ writeButton((GuiCfgData.GuiCfgButton)cfge.widgetType); }
116      else if(cfge.widgetType.whatIs == 'T'){ 
117        writer.append("InputTextline(");
118        writeParam(cfge.widgetType);
119        writer.append(");\n");
120  
121      }
122      else if(cfge.widgetType.whatIs == 't'){ 
123        writer.append("InputBox(");
124        writeParam(cfge.widgetType);
125        writer.append(");\n");
126  
127      }
128      else { 
129        writeUnknown(cfge.widgetType); 
130      }
131    }
132  }
133  
134  
135  void writePosition(Writer ww, GralCfgElement cfge) throws IOException
136  {
137    if(cfge.positionString !=null) {
138      ww.append(cfge.positionString);
139    } else {
140      GralCfgPosition pp = cfge.positionInput;
141      if(pp.yPos >=0 || pp.xPos >=0 || pp.ySizeDown !=0 || pp.xWidth !=0){
142        if(pp.panel !=null){ 
143          if(!pp.panel.equals(sLastPanel)) {
144            ww.append("\n\n//================================================================================\n");
145            sLastPanel = pp.panel;
146          }
147          ww.append("\n@").append(pp.panel).append(", "); 
148        } else {
149          ww.append("\n@");
150        }
151        if(pp.yPosRelative) ww.append("&");
152        if(pp.yPos>=0) ww.append(Integer.toString(pp.yPos));
153        if(pp.yPosFrac !=0) ww.append(".").append(Integer.toString(pp.yPosFrac));
154        if(pp.ySizeDown>0) ww.append("+").append(pp.ySizeDown == GralPos.useNatSize/*Integer.MAX_VALUE*/ ? "*" : Integer.toString(pp.ySizeDown));
155        else if(pp.ySizeDown<0) ww.append(Integer.toString(pp.ySizeDown)); //with negativ sign!
156        if(pp.ySizeFrac !=0) ww.append(".").append(Integer.toString(pp.ySizeFrac));
157        if(pp.yIncr_) ww.append("++");    
158    
159        ww.append(",");
160        
161        if(pp.xPosRelative) ww.append("&");
162        if(pp.xPos>=0) ww.append(Integer.toString(pp.xPos));
163        if(pp.xPosFrac !=0) ww.append(".").append(Integer.toString(pp.xPosFrac));
164        if(pp.xWidth>0) ww.append("+").append(pp.xWidth == GralPos.useNatSize/*Integer.MAX_VALUE*/ ? "*" : Integer.toString(pp.xWidth));
165        else if(pp.xWidth<0) ww.append(Integer.toString(pp.xWidth)); //width negativ sign!
166        if(pp.xSizeFrac !=0) ww.append(".").append(Integer.toString(pp.xSizeFrac));
167        if(pp.xIncr_) ww.append("++");    
168        
169        ww.append(": ");
170      }
171    }
172  }
173
174  
175  void writeShowField(GralCfgData.GuiCfgShowField ee) throws IOException
176  { //String sep = "";
177    if(ee.editable){
178      writer.append("InputTextline(");
179    } else {
180      writer.append("Show(");
181    }
182    writeParam(ee);
183    writer.append("); ");
184  }
185
186  
187  void writeLed(GralCfgData.GuiCfgLed ee) throws IOException
188  { //String sep = "";
189    writer.append("Led(");
190    writeParam(ee);
191    writer.append("); ");
192  }
193
194  
195  void writeImage(GralCfgData.GuiCfgImage ee) throws IOException
196  { //String sep = "";
197    writer.append("Imagefile(file=\"");
198    writer.append(ee.file_).append("\"");
199    writeParam(ee, ",");
200    writer.append(");\n");
201  }
202
203  
204  
205  void writeInputFile(GralCfgData.GuiCfgInputFile ee) throws IOException
206  { //String sep = "";
207    writer.append("InputFile(");
208    writeParam(ee);
209    writer.append(");\n");
210  }
211
212  
213  
214  void writeButton(GralCfgData.GuiCfgButton ee) throws IOException
215  { //String sep = "";
216    writer.append("Button(");
217    writeParam(ee);
218    writer.append(");\n");
219  }
220
221  
222  
223  void writeText(GralCfgData.GuiCfgText ee) throws IOException
224  { //String sep = "";
225    writer.append("Text(");
226    writeParam(ee);
227    writer.append("); ");
228  }
229
230  
231  
232  void writeUnknown(GralCfgData.GuiCfgWidget ee) throws IOException
233  { //String sep = "";
234    writer.append("Unknown(");
235    writeParam(ee);
236    writer.append("); ");
237  }
238
239  
240  private void writeParam(GralCfgData.GuiCfgWidget pp) throws IOException
241  {
242    writeParam(pp, "");
243  }
244  
245  
246  private void writeParam(GralCfgData.GuiCfgWidget pp, String sep) throws IOException
247  { //String sep = "";
248    if(pp.prompt !=null && pp.promptPosition !=null && pp.promptPosition.length() >=1) {
249      writer.append("%").append(pp.promptPosition).append(":\"")  .append(pp.prompt).append("\""); sep = ", "; 
250    }
251    if(pp.text !=null)      { writer.append(sep).append("\"")       .append(pp.text).append("\""); sep = ", "; }
252    if(pp.name !=null)      { writer.append(sep).append("name=")    .append(pp.name); sep = ", "; }
253    if(pp.cmd  !=null)      { writer.append(sep).append("cmd=\"")   .append(pp.cmd).append("\"");  sep = ", "; }
254    if(pp.showMethod !=null){ writer.append(sep).append("show=\"")  .append(pp.showMethod).append("\""); sep = ", "; }
255    if(pp.format !=null)    { writer.append(sep).append("format=\"").append(pp.format).append("\""); sep = ", "; }
256    if(pp.type !=null)      { writer.append(sep).append("type=")    .append(pp.type); sep = ", "; }
257    if(pp.data !=null)      { writer.append(sep).append("data=\"")  .append(pp.data).append("\""); sep = ", "; }
258    if(pp.userAction !=null){ writer.append(sep).append("action=\"")  .append(pp.userAction).append("\""); sep = ", "; }
259    if(pp.dragFiles !=null) { writer.append(sep).append("dragFiles=\"") .append(pp.dragFiles).append("\""); sep = ", "; }
260    if(pp.dragText !=null)  { 
261      writer.append(sep).append("dragText=\"")  .append(pp.dragText).append("\""); sep = ", "; }
262    if(pp.dropFiles !=null) { writer.append(sep).append("dropFiles=\"") .append(pp.dropFiles).append("\""); sep = ", "; }
263    if(pp.dropText !=null)  { writer.append(sep).append("dropText=\"")  .append(pp.dropText).append("\""); sep = ", "; }
264    if(pp.color0 !=null)    { writer.append(sep).append("color=")       .append(pp.color0.color); 
265      if(pp.color1 !=null)  { writer            .append("/")            .append(pp.color1.color); }  sep = ", "; 
266    }
267  }
268
269  
270  
271  
272  
273  
274  
275  
276}