001package org.vishia.gral.widget;
002
003import java.io.File;
004import java.util.List;
005
006import org.vishia.cmd.CmdExecuter;
007import org.vishia.cmd.CmdGetterArguments;
008import org.vishia.cmd.JZtxtcmdScript;
009import org.vishia.gral.base.GralWidget;
010import org.vishia.gral.ifc.GralMngBuild_ifc;
011import org.vishia.gral.ifc.GralUserAction;
012import org.vishia.util.DataAccess;
013import org.vishia.gral.ifc.GralTableLine_ifc;
014
015/**This class is a widget to select commands from a table list.
016 * It can be used in any application in a window, which is opened on demand 
017 * or which is open any time. The user can select a line with the command and click OK (Enter, Mouse).
018 * Then the command is send to the instance of {@link CmdExecuter} which is given by constructor
019 * and executed there in the thread of the {@link CmdExecuter}. It is possible to share the CmdQueue-instance
020 * with some other application parts. The execution is queued there.
021 * 
022 * The commands are shown in a table. The appearance of the table can be controlled by the application
023 * or maybe by the user. Only one command can be selected to execute.
024 * 
025 * All commands to select are contained in the public field {@link #cmdStore}. 
026 * This storage of commands can be filled calling {@link CmdStore#readCmdCfg(File)} or in any other way
027 * of that class. 
028 * 
029 * @author Hartmut Schorrig
030 *
031 */
032public class GralCommandSelector extends GralSelectList<JZtxtcmdScript.Subroutine>
033{
034  
035  
036  /**Version, history and license. This String is visible in the about info.
037   * <ul>
038   * <li>2017-01-01 Now supports only {@link JZtxtcmdScript.Subroutine}, no more CmdStore. 
039   *   The CmdStore is the older concept before JZcmd was found. JZcmd contains more capabilities.
040   * <li>2013-10-00 Hartmut created
041   * </ul>
042   * 
043   * <b>Copyright/Copyleft</b>:<br>
044   * For this source the LGPL Lesser General Public License,
045   * published by the Free Software Foundation is valid.
046   * It means:
047   * <ol>
048   * <li> You can use this source without any restriction for any desired purpose.
049   * <li> You can redistribute copies of this source to everybody.
050   * <li> Every user of this source, also the user of redistribute copies
051   *    with or without payment, must accept this license for further using.
052   * <li> But the LPGL is not appropriate for a whole software product,
053   *    if this source is only a part of them. It means, the user
054   *    must publish this part of source,
055   *    but doesn't need to publish the whole source of the own product.
056   * <li> You can study and modify (improve) this source
057   *    for own using or for redistribution, but you have to license the
058   *    modified sources likewise under this LGPL Lesser General Public License.
059   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
060   * </ol>
061   * If you intent to use this source without publishing its usage, you can get
062   * a second license subscribing a special contract with the author. 
063   * 
064   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
065   */
066  //@SuppressWarnings("hiding")
067  public static final String version = "2016-12-27";
068
069
070  /**Store of all possible commands given in the command file. 
071   * See {@link CmdStore#readCmdCfg(File)} */
072  //public final CmdStore cmdStore;
073  
074  /**Execution instance in another thread. */
075  //protected final CmdQueue cmdQueue;
076  protected final CmdExecuter cmdExecuter;
077  
078 /**
079   * @since 2016-12
080   */
081  protected final CmdGetterArguments getterArguments;
082  
083  /**The currently selected command.
084   * 
085   */
086  protected JZtxtcmdScript.Subroutine selectedCmd;
087  
088  
089  final Appendable out;
090  
091  public GralCommandSelector(String name, int rows, int[] columns, char size, CmdExecuter cmdExecuter, Appendable out, CmdGetterArguments getterArguments)
092  { super(name, rows, columns, size);
093    this.cmdExecuter = cmdExecuter;
094    this.out = out;
095    //this.cmdStore = new CmdStore();
096    //this.cmdQueue = cmdQueue;
097    this.getterArguments = getterArguments;
098   
099  }
100  
101  
102  @Override
103  public void createImplWidget_Gthread(){
104    super.createImplWidget_Gthread();
105    wdgdTable.specifyActionOnLineSelected(actionOnLineSelected);
106  }
107
108  
109  
110  
111  public void clear() { wdgdTable.clearTable(); addJZsub2SelectTable.clear(); }
112  
113  
114  
115  public JZtxtcmdScript.AddSub2List addJZsub2SelectTable = new JZtxtcmdScript.AddSub2List() {
116
117    int level = 1;
118    GralTableLine_ifc<JZtxtcmdScript.Subroutine> parentline = null, line = null;
119
120    @Override public void clear()
121    { parentline = null; line = null;
122    }
123
124    @Override
125    public void add2List(JZtxtcmdScript.JZcmdClass jzclass, int level)
126    { String[] texts = new String[2]; 
127      texts[0] = jzclass.cmpnName;
128      texts[1] = "+";
129      parentline = wdgdTable.addLine(jzclass.cmpnName, texts, null);
130      
131    }
132
133    @Override
134    public void add2List(JZtxtcmdScript.Subroutine jzsub, int level)
135    {
136      String[] texts = new String[2]; 
137      texts[0] = jzsub.name;
138      texts[1] = "";
139      if(parentline !=null) {
140        parentline.addChildLine(jzsub.name, texts, jzsub);
141      } else {
142        wdgdTable.addLine(jzsub.name, texts, jzsub);
143      }
144      
145    }
146    
147  };
148  
149  
150  
151  
152  /**Returns the currently selected command. */
153  public JZtxtcmdScript.Subroutine getCurrCmd(){ return selectedCmd; }
154  
155  
156  
157  /**Executes the command which is selected currently.
158   * Whether or not the selection is visible and recognized by the user, it should be defined in the application.
159   */
160  public void executeCurrCmdWithFiles(){
161    if(selectedCmd !=null){
162      actionOk(selectedCmd, null);  //Note: the argument line isn't used in the called method.
163    }
164  }
165  
166  
167  
168  
169  
170  
171  /**Action on click Ok at any line of table.
172   * @param userData It is the {@link GralTableLine_ifc#getUserData()}, which is the 
173   *   {@link CmdStore.CmdBlock}-instance which is assigned with the line. The method casts without check.
174   * @param line isn't used here. It can be null in direct invocations.  
175   * @see org.vishia.gral.widget.GralSelectList#actionOk(java.lang.Object, org.vishia.gral.ifc.GralTableLine_ifc)
176   */
177  @Override protected boolean actionOk(Object userData, GralTableLine_ifc<JZtxtcmdScript.Subroutine> line)
178  {
179    JZtxtcmdScript.Subroutine jzsub = (JZtxtcmdScript.Subroutine)userData;
180    //Map<String, DataAccess.Variable<Object>> jargs = getterArguments.getArguments(cmdBlock.)//cmdBlock.getArguments(getterFiles);
181    //File currFile = getterArguments.getCurrDir();
182    
183    //File currDir = currFile !=null? currFile.getParentFile(): null;
184    String sMsg = "GralCommandSelector - put cmd;" + jzsub.toString();
185    System.out.println(sMsg);
186    List<DataAccess.Variable<Object>> args = getterArguments.getArguments(jzsub);
187    cmdExecuter.addCmd(jzsub, args, out, getterArguments.getCurrDir());  //to execute.
188    /*
189    }
190    else {
191      File[] files = new File[3];
192      
193      getterFiles.prepareFileSelection();
194      files[0] = getterFiles.getFile1();
195      files[1] = getterFiles.getFile2();
196      files[2] = getterFiles.getFile3();
197      File currDir;
198      if(files[0] !=null){
199        currDir = files[0].getParentFile();
200      } else {
201        currDir = new File("/");
202      }
203      File currFile = getterFiles.getFile1();
204      
205      String sMsg = "GralCommandSelector - put cmd;" + cmdBlock.toString();
206      System.out.println(sMsg);
207      cmdQueue.addCmd(cmdBlock, files, currDir);
208    }
209    */
210    return true;
211  }
212  
213  
214  
215  @Override protected void actionLeft(Object userData, GralTableLine_ifc line)
216  {
217  }
218  
219  @Override protected void actionRight(Object userData, GralTableLine_ifc line)
220  {
221  }
222  
223  
224  @Override protected boolean actionUserKey(int key, Object userData, GralTableLine_ifc line)
225  {
226    // TODO Auto-generated method stub
227    return false;
228  }
229
230  
231  private final GralUserAction actionOnLineSelected = new GralUserAction("actionOnLineSelected"){
232    @Override public boolean userActionGui(int actionCode, GralWidget widgd, Object... params){ 
233      @SuppressWarnings("unchecked")
234      GralTableLine_ifc<JZtxtcmdScript.Subroutine> line = (GralTableLine_ifc<JZtxtcmdScript.Subroutine>)params[0];
235      if(line !=null){
236        selectedCmd = line.getUserData();
237      }
238      return true;
239    }
240  };
241  
242  public GralUserAction actionExecCmdWithFiles = new GralUserAction("actionExecCmdWithFiles"){
243    @Override public boolean userActionGui(int actionCode, GralWidget widgd, Object... params){ 
244      executeCurrCmdWithFiles();
245      return true;
246    }
247  };
248  
249  
250}