    001package org.vishia.guiInspc;
    002
    003import java.io.File;
    004import java.util.LinkedList;
    005import java.util.List;
    006
    007import org.vishia.gral.base.GralTextField;
    008import org.vishia.mainCmd.MainCmd;
    009import org.vishia.mainCmd.MainCmdLogging_ifc;
    010import org.vishia.mainCmd.ReportWrapperLog;
    011import org.vishia.msgDispatch.LogMessage;
    012import org.vishia.util.FileSystem;
    013import org.vishia.zbnf.ZbnfJavaOutput;
    014
    015/**This classes contains a configuration of some variables.
    016 * They are filled from a file which contains the paths.
    017 * They can be saved in a file.
    018 * The variables can be used to show in a curve view, to show in an extra window etc.
    019 * The advantage: It is flexible which variables are present.
    020 * @author Hartmut Schorrig
    021 *
    022 */
    023public class InspcGuiFieldsFromFile
    024{
    025  /**Version, history and license.
    026   * <ul>
    027   * <li>2014-04-29 Hartmut cleanup, now really used in a professional application.
    028   * <li>2013-03-17 Hartmut creating
    029   * </ul>
    030   * <br><br>
    031   *
    032   * <b>Copyright/Copyleft</b>:
    033   * For this source the LGPL Lesser General Public License,
    034   * published by the Free Software Foundation is valid.
    035   * It means:
    036   * <ol>
    037   * <li> You can use this source without any restriction for any desired purpose.
    038   * <li> You can redistribute copies of this source to everybody.
    039   * <li> Every user of this source, also the user of redistribute copies
    040   *    with or without payment, must accept this license for further using.
    041   * <li> But the LPGL is not appropriate for a whole software product,
    042   *    if this source is only a part of them. It means, the user
    043   *    must publish this part of source,
    044   *    but don't need to publish the whole source of the own product.
    045   * <li> You can study and modify (improve) this source
    046   *    for own using or for redistribution, but you have to license the
    047   *    modified sources likewise under this LGPL Lesser General Public License.
    048   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
    049   * </ol>
    050   * If you are intent to use this sources without publishing its usage, you can get
    051   * a second license subscribing a special contract with the author.
    052   *
    053   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
    054   */
    055  //@SuppressWarnings("hiding")
    056  public final static String sVersion = "2014-04-30";
    057
    058
    059  /**ZBNF result:
    060   */
    061  public static class MappingItem
    062  { public String path;
    063    public String comment;
    064    public float scaleFloat = 0.0f;
    065    public int nrofBits = -1;
    066    public BitSignal bitSignal = null;
    067  }
    068
    069
    070  /**ZBNF result:
    071   */
    072  public static class BitSignal
    073  { public int mask;
    074    public float hi = 0.0f;
    075    public float lo = 0.0f;
    076  }
    077
    078
    079
    080
    081
    082  /**ZBNF result: one line.
    083   */
    084  public static class Channel
    085  {
    086    public int chn;
    087    public List<MappingItem> mappingItem = new LinkedList<MappingItem>();
    088  }
    089
    090
    091  /**ZBNF result: the whole file
    092   */
    093  public static class ZbnfResultFile {
    094    /**ZBNF result:  */
    095    public List<Channel> channel = new LinkedList<Channel>();
    096  };
    097
    098
    099
    100  final ZbnfResultFile cfgFileInputData = new ZbnfResultFile();
    101
    102  LogMessage log;
    103
    104  MainCmdLogging_ifc report;
    105
    106  public final String syntaxMappingFile = "main::={ <Channel> } \\e. "
    107    + "Channel::=@K<#?chn> : { <MappingItem> }. "
    108    + "MappingItem::=<* =?path> = [float <#f?scaleFloat>| intBits <#?nrofBits> | bit <bitSignal>] ; [// <*\\n?comment> ]."
    109    + "bitSignal::= <#x?mask> \\? <#f?hi> [ : <#f?lo>].";
    110
    111
    112  public InspcGuiFieldsFromFile(){
    113    report = MainCmd.getLogging_ifc();
    114    assert(report !=null);  //it is null if the application does not use MainCmd.
    115    log = report;
    116  }
    117
    118  public InspcGuiFieldsFromFile(LogMessage log){
    119    this.log = log;
    120    this.report = new ReportWrapperLog(log);
    121  }
    122
    123
    124
    125
    126  /**Parses the config file for HSI and fill data.
    127   * @param fileCfg
    128   * @param data Proper data instance for parse result, the fields should be match to the semantic.
    129   * @return true if ok. false then a log message was sent.
    130   * @throws IllegalArgumentException
    131   */
    132  public boolean parse(File fileCfg, ZbnfResultFile data) throws IllegalArgumentException
    133  {
    134    boolean bOk = true;
    135    if(!fileCfg.exists()){
    136      bOk = false;
    137      log.sendMsg(0, "HsiMapping - File not found: %s;", fileCfg.getAbsoluteFile());
    138      throw new IllegalArgumentException();
    139    }
    140    ZbnfJavaOutput parser = new ZbnfJavaOutput(report);
    141    String sError = parser.parseFileAndFillJavaObject(data.getClass(), data, fileCfg, syntaxMappingFile);
    142    if(sError != null)
    143    { log.sendMsg(0, sError);
    144      bOk = false;
    145    }
    146    return bOk;
    147  }
    148
    149
    150
    151}