    001package org.vishia.simSelector;
    002
    003import java.io.File;
    004import java.io.PrintStream;
    005import java.util.List;
    006import java.util.Map;
    007import java.util.TreeMap;
    008
    009import javax.script.ScriptException;
    010
    011import org.vishia.cmd.JZtxtcmdExecuter;
    012import org.vishia.cmd.JZtxtcmdScript;
    013import org.vishia.gral.base.GralButton;
    014import org.vishia.gral.base.GralGraphicTimeOrder;
    015import org.vishia.gral.base.GralMng;
    016import org.vishia.gral.base.GralTable;
    017import org.vishia.gral.base.GralTextBox;
    018import org.vishia.gral.base.GralTextField;
    019import org.vishia.gral.base.GralWindow;
    020import org.vishia.gral.ifc.GralFactory;
    021import org.vishia.gral.ifc.GralUserAction;
    022import org.vishia.gral.ifc.GralWidget_ifc;
    023import org.vishia.gral.swt.SwtFactory;
    024import org.vishia.jztxtcmd.JZtxtcmd;
    025import org.vishia.mainCmd.PrintStreamAdapter;
    026import org.vishia.msgDispatch.LogMessage;
    027import org.vishia.msgDispatch.LogMessageStream;
    028import org.vishia.util.DataAccess;
    029import org.vishia.util.KeyCode;
    030
    031/*Test with jzcmd: call jzcmd with this java file with its full path:
    032D:/vishia/Java/srcJava_vishiaGui/org/vishia/gral/test/SimSelector.java
    033==JZcmd==
    034java org.vishia.gral.test.SimSelector.main(null);
    035==endJZcmd==
    036 */
    037
    038
    039public class SimSelector
    040{
    041  GralMng gralMng;
    042
    043  GralWindow window;
    044
    045  @SuppressWarnings("unchecked")
    046  GralTable<Map<String, DataAccess.Variable<Object>>>[] wdgTables = new GralTable[6];
    047
    048
    049  GralButton btnReadConfig, btnGenSim, btnCleanOut, btnGenTestcase;
    050
    051  GralTextField wdgSelects;
    052
    053  GralTextBox output;
    054
    055  JZtxtcmdExecuter executer = new JZtxtcmdExecuter();
    056
    057  boolean isTableInitialized;
    058
    059  File fileConfig;
    060
    061  PrintStream outOld, errOld, outNew = null, errNew = null;
    062
    063  public static void main(String[] args){
    064    if(args.length < 1) {
    065      System.err.println("parameter 1: path/to/config.jzcmd.bat");
    066    }
    067    SimSelector main = new SimSelector();
    068    main.fileConfig = new File(args[0]);
    069    main.openWindow1();
    070    main.waitForClosePrimaryWindow();
    071  }
    072
    073
    074  SimSelector()
    075  {
    076    for(int iTable = 0; iTable < wdgTables.length; ++iTable) {
    077      //String pos = "@PrimaryWindow, 6..40, " + 20 * iTable + ".." + (18 + 20 * iTable);
    078      String name = "table" + iTable;
    079      int[] columnWidths = new int[3];
    080      columnWidths[0] = 8;
    081      columnWidths[1] = 8;
    082      columnWidths[2] = 0;
    083      wdgTables[iTable] = new GralTable<>(name, columnWidths);
    084    }
    085    btnReadConfig = new GralButton("readConfig", "read config", actionReadConfig);
    086    btnGenSim = new GralButton("genSim", "gen stimuli", actionGenSim);
    087    btnGenTestcase = new GralButton("genTestCase", "gen testcases.m", actionGenTestcases);
    088    btnCleanOut = new GralButton("cleanOut", "clean output", actionCleanOut);
    089    output = new GralTextBox("output");
    090  }
    091
    092
    093
    094  public static void openWindow(){
    095    SimSelector main = new SimSelector();
    096    main.openWindow1();
    097  }
    098
    099
    100  public void waitForClosePrimaryWindow()
    101  {
    102    while(GralMng.get().gralDevice.isRunning()){
    103      try{ Thread.sleep(100);}
    104      catch (InterruptedException e)
    105      { //dialogZbnfConfigurator.terminate();
    106      }
    107      if(isTableInitialized) {
    108        isTableInitialized = false;
    109        readConfig();
    110      }
    111    }
    112    if(outNew !=null) { System.setOut(outOld); outNew.close(); }
    113    if(errNew !=null) { System.setErr(errOld); errNew.close(); }
    114
    115  }
    116
    117
    118
    119
    120  void readConfig()
    121  {
    122    JZtxtcmdExecuter.ExecuteLevel level = null;
    123    try {
    124      JZtxtcmd jzcmd = new JZtxtcmd();
    125      JZtxtcmdScript script = jzcmd.compile(fileConfig, null);
    126      executer.initialize(script, false, null);
    127      level = executer.execute_Scriptclass("ToGui");
    128
    129    } catch( ScriptException exc) {
    130      System.err.println(exc.getMessage());
    131    }
    132    if(level !=null) {
    133      for(int iList = 0; iList < wdgTables.length; ++iList ) {
    134        //boolean bErr = false;
    135        wdgTables[iList].clearTable();;
    136        String nameList = "tdata" + (iList +1);
    137        DataAccess.Variable<Object> vlist1 = level.localVariables.get(nameList);
    138        if(vlist1 !=null && vlist1.value() instanceof List) {
    139          List<?> list1 = (List<?>)vlist1.value();
    140          for(Object listElement: list1) {
    141            if(listElement instanceof Map) {
    142              @SuppressWarnings("unchecked")
    143              Map<String, DataAccess.Variable<Object>> set1 = (Map<String, DataAccess.Variable<Object>>) listElement;
    144              DataAccess.Variable<Object> descrv = set1.get("descr");
    145              DataAccess.Variable<Object> namev = set1.get("name");
    146              DataAccess.Variable<Object> selectv = set1.get("select");
    147              if(descrv !=null && namev !=null) {
    148                String[] lineTexts = new String[3];
    149                lineTexts[0] = namev.value().toString();
    150                lineTexts[1] = selectv.value().toString();
    151                lineTexts[2] = descrv.value().toString();
    152                wdgTables[iList].addLine(lineTexts[0], lineTexts, set1);
    153              }
    154            }
    155            System.out.println(listElement.toString());
    156          }
    157        }
    158        wdgTables[iList].repaint();
    159      }
    160    }
    161
    162
    163    /*
    164  IndexMultiTable<String, DataAccess.Variable<Object>> args = new IndexMultiTable<>(IndexMultiTable.providerString);
    165  DataAccess.Variable<Object> tdata1 = new DataAccess.Variable<Object>('L', "tdata1", new LinkedList<String>());
    166  args.put(tdata1.name(), tdata1);
    167  DataAccess.Variable<Object> tdata2 = new DataAccess.Variable<Object>('L', "tdata2", new LinkedList<String>());
    168  args.put(tdata2.name(), tdata2);
    169  DataAccess.Variable<Object> tdata3 = new DataAccess.Variable<Object>('L', "tdata3", new LinkedList<String>());
    170  args.put(tdata3.name(), tdata3);
    171  DataAccess.Variable<Object> tdata4 = new DataAccess.Variable<Object>('L', "tdata4", new LinkedList<String>());
    172  args.put(tdata4.name(), tdata4);
    173  JZcmd.execSub(fileScript, "toGui", args, executer.scriptLevel());
    174     */
    175  }
    176
    177
    178
    179
    180  void genStimuli()
    181  {
    182    String[] identifier = new String[wdgTables.length];
    183    Map<String, DataAccess.Variable<Object>> idents = new TreeMap<String, DataAccess.Variable<Object>>();
    184    for(int iTable = 0; iTable < wdgTables.length; ++iTable) {
    185      GralTable<Map<String, DataAccess.Variable<Object>>>.TableLineData line = wdgTables[iTable].getCurrentLine();
    186      if(line !=null) {
    187        Map<String, DataAccess.Variable<Object>> data = line.getUserData();
    188        identifier[iTable] = data.get("name").value().toString();
    189        String key = "key" + (iTable+1);
    190        idents.put(key, new DataAccess.Variable<Object>('S', key, identifier[iTable]));
    191    } }
    192    try{
    193      Appendable out = output;
    194      executer.execSub(null, "genStimuli", idents, false, out, null);
    195    } catch(ScriptException exc) {
    196      System.err.println(exc.getMessage());
    197    }
    198  }
    199
    200
    201  void genTestcases()
    202  {
    203    //String[] identifier = new String[wdgTables.length];
    204    Map<String, DataAccess.Variable<Object>> args = new TreeMap<String, DataAccess.Variable<Object>>();
    205    args.put("select", new DataAccess.Variable<Object>('S', "select", wdgSelects.getText()));
    206    try{
    207      Appendable out = output;
    208      executer.execSub(null, "genTestcases", args, false, out, null);
    209    } catch(ScriptException exc) {
    210      System.err.println(exc.getMessage());
    211    }
    212  }
    213
    214
    215
    216
    217  GralUserAction actionReadConfig = new GralUserAction("readConfig")
    218  { @Override public boolean exec(int actionCode, GralWidget_ifc widgd, Object... params)
    219    { if(KeyCode.isControlFunctionMouseUpOrMenu(actionCode)){
    220        readConfig();
    221      }
    222      return true;
    223    }
    224  };
    225
    226
    227  GralUserAction actionGenSim = new GralUserAction("genStimuli")
    228  { @Override public boolean exec(int actionCode, GralWidget_ifc widgd, Object... params)
    229    { if(KeyCode.isControlFunctionMouseUpOrMenu(actionCode)){
    230        genStimuli();
    231      }
    232      return true;
    233    }
    234  };
    235
    236
    237  GralUserAction actionGenTestcases = new GralUserAction("genTestcases")
    238  { @Override public boolean exec(int actionCode, GralWidget_ifc widgd, Object... params)
    239    { if(KeyCode.isControlFunctionMouseUpOrMenu(actionCode)){
    240        genTestcases();
    241      }
    242      return true;
    243    }
    244  };
    245
    246
    247  GralUserAction actionCleanOut = new GralUserAction("genStimuli")
    248  { @Override public boolean exec(int actionCode, GralWidget_ifc widgd, Object... params)
    249    { if(KeyCode.isControlFunctionMouseUpOrMenu(actionCode)){
    250        output.setText("");
    251      }
    252      return true;
    253    }
    254  };
    255
    256
    257
    258
    259  @SuppressWarnings("deprecation")
    260  private void openWindow1(){
    261    GralFactory gralFactory = new SwtFactory();
    262    LogMessage log = new LogMessageStream(System.out);
    263    window = gralFactory.createWindow(log, "Select Simulation", 'C', 100, 50, 600, 400);
    264    gralMng = window.gralMng();
    265    gralMng.gralDevice.addDispatchOrder(initGraphic);
    266    //initGraphic.awaitExecution(1, 0);
    267
    268  }
    269
    270
    271  GralGraphicTimeOrder initGraphic = new GralGraphicTimeOrder("GralArea9Window.initGraphic"){
    272    @Override public void executeOrder()
    273    {
    274      // gralMng.selectPanel(window);
    275      gralMng.setPosition(2, 5, 2, 19, 0, 'r', 2);
    276      btnReadConfig.createImplWidget_Gthread();
    277      btnCleanOut.createImplWidget_Gthread();
    278      btnGenSim.createImplWidget_Gthread();
    279      btnGenTestcase.createImplWidget_Gthread();
    280      gralMng.setPosition(6, 8, 60, 79, 0, 'd');
    281      wdgSelects = gralMng.addTextField("test", true, null, "r");
    282      wdgSelects.setText("t");
    283      //int last = 1; //tables.length
    284      for(int iTable = 0; iTable < wdgTables.length; ++iTable) {
    285        int xtable = iTable %3;
    286        int ytable = iTable /3;
    287        gralMng.setPosition(21*ytable + 10, 21*ytable + 30, xtable * 30, xtable * 30 +30, 0, 'd');
    288
    289        wdgTables[iTable].createImplWidget_Gthread();
    290        wdgTables[iTable]._wdgImpl.repaintGthread();
    291      }
    292      gralMng.setPosition(52, 0, 0, 0, 0, 'U');
    293      output.createImplWidget_Gthread();
    294
    295      outOld = System.out; errOld = System.err;
    296      System.setOut(outNew = new PrintStreamAdapter("", output));
    297      System.setErr(errNew = new PrintStreamAdapter("", output));
    298      isTableInitialized = true;
    299      //
    300      //GralTextField input = new GralTextField();
    301    }
    302
    303
    304
    305  };
    306}
    307