001package org.vishia.commander;
002
003import java.io.File;
004
005import org.vishia.gral.base.GralButton;
006import org.vishia.gral.base.GralMng;
007import org.vishia.gral.base.GralPos;
008import org.vishia.gral.base.GralWidget;
009import org.vishia.gral.base.GralWindow;
010import org.vishia.gral.ifc.GralTextField_ifc;
011import org.vishia.gral.ifc.GralUserAction;
012import org.vishia.gral.ifc.GralWidget_ifc;
013import org.vishia.gral.ifc.GralWindow_ifc;
014import org.vishia.util.KeyCode;
015
016public class FcmdSettings
017{
018  /**Version, history and license
019   * <ul>
020   * <li>2016-08-28 Hartmut chg: extra button now for the jzcmd.cfg, 
021   *   extra button {@link #widgOkError} to open the infoBox, but open the infoBox automatically on error.
022   *   The Button for open Infobox will get a button text "error" if an error in the config is found. 
023   *   It is nice to have on editing the scripts for commands to see whether it is okay or not and to detect the cause of errors.
024   * <li>2012-10-27 Hartmut created
025   * </ul>
026   * 
027   * 
028   * <b>Copyright/Copyleft</b>:
029   * For this source the LGPL Lesser General Public License,
030   * published by the Free Software Foundation is valid.
031   * It means:
032   * <ol>
033   * <li> You can use this source without any restriction for any desired purpose.
034   * <li> You can redistribute copies of this source to everybody.
035   * <li> Every user of this source, also the user of redistribute copies
036   *    with or without payment, must accept this license for further using.
037   * <li> But the LPGL ist not appropriate for a whole software product,
038   *    if this source is only a part of them. It means, the user
039   *    must publish this part of source,
040   *    but don't need to publish the whole source of the own product.
041   * <li> You can study and modify (improve) this source
042   *    for own using or for redistribution, but you have to license the
043   *    modified sources likewise under this LGPL Lesser General Public License.
044   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
045   * </ol>
046   * If you are intent to use this sources without publishing its usage, you can get
047   * a second license subscribing a special contract with the author. 
048   * 
049   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
050   * 
051   * 
052   */
053  public static final String sVersion = "2016-08-28";
054  
055  protected final Fcmd main;
056
057  
058  GralWindow_ifc windSettings;
059  GralTextField_ifc widgRefreshTime;
060  
061  GralTextField_ifc widgCfgPath;
062  
063  /**Path to call the editor for standard editing texts. */
064  GralTextField_ifc widgEditorPath;
065  
066  
067  
068  GralButton widgOkError, widgOk; 
069  
070  /**Buttons for edit and apply the several configuration files.
071   * Edit opens the file in the standard editor. Apply set the content to the File Commander. 
072   */
073  GralButton widgEditCmd, widgApplyCmd, widgEditExt, widgApplyExt, widgEditPaths, widgApplyPaths;
074
075  int secondsRefresh = 5;
076  
077  
078  public FcmdSettings(Fcmd main)
079  { this.main = main;
080  }
081
082
083  
084  /**Builds the content of the file property window. The window is created static. It is shown
085   * whenever it is used.  */
086  void buildWindow()
087  { main._gralMng.selectPanel("primaryWindow");
088    int windProps = GralWindow.windConcurrently | GralWindow.windOnTop;
089    GralWindow window =  new GralWindow("10+29, 10+47", "windSettings", "Settings - The.file.Commander", windProps);
090    window.createImplWidget_Gthread();
091    windSettings = window; 
092    main._gralMng.setPosition(3.5f, GralPos.size -3, 1, -1, 0, 'd');
093    widgRefreshTime = main._gralMng.addTextField(null, true, "refresh time file panel", "t");
094    widgEditorPath = main._gralMng.addTextField(null, true, "standard editor path", "t");
095    widgCfgPath = main._gralMng.addTextField(null, false, "configuration directory path", "t");
096    
097    //main._gralMng.setPosition(GralPos.refer + 3.0f, GralPos.size -2.0f, 1, GralPos.size + 8, 0, 'r', 0.5f);
098    main._gralMng.setPosition(-7, GralPos.size -2.0f, 1, GralPos.size + 8, 0, 'r', 0.5f);
099    widgEditCmd = main._gralMng.addButton("editCmd", actionEditCfgFile, "cmdjz.cfg", null, "edit");
100    widgApplyCmd = main._gralMng.addButton("applyCmd", actionApplyCfgCmd, "cmdjz.cfg", null, "apply");
101    main._gralMng.addText("cmd cfg file");
102    
103    main._gralMng.setPosition(GralPos.refer + 3.0f, GralPos.size -2.0f, 1, GralPos.size + 8, 0, 'r', 0.5f);
104    widgEditCmd = main._gralMng.addButton("editCmd", actionEditCfgFile, "extjz.cfg", null, "edit");
105    widgApplyCmd = main._gralMng.addButton("applyExt", actionApplyCfgExt, "extjz.cfg", null, "apply");
106    main._gralMng.addText(".ext cfg file");
107    
108    main._gralMng.setPosition(GralPos.refer + 3.0f, GralPos.size -2.0f, 1, GralPos.size + 8, 0, 'r', 0.5f);
109    widgEditCmd = main._gralMng.addButton("editPaths", actionEditCfgFile, "path.cfg", null, "edit");
110    widgApplyCmd = main._gralMng.addButton("applyPaths", actionApplyCfgPath, "path.cfg", null, "apply");
111    main._gralMng.addText("favor paths file");
112    
113    main._gralMng.setPosition(-10, GralPos.size -2, -18, -1, 0, 'd', 0.5f);
114    widgOkError = main._gralMng.addButton("ok_error", actionOpenInfo, "infoBox");
115    main._gralMng.setPosition(-1, GralPos.size -2.5f, -9, -1, 0, 'd', 0.5f);
116    widgOk = main._gralMng.addButton("close", actionButton, "close");
117    widgOk.setCmd("close");
118  }
119
120  /**Opens the view window and fills its content.
121   * @param src The path which is selected as source. It may be a directory or a file.
122   */
123  void openDialog(File src)
124  {
125    widgOkError.setText("infoBox");
126    widgRefreshTime.setText("" + secondsRefresh);
127    widgCfgPath.setText(main.cargs.dirCfg.getAbsolutePath());
128    windSettings.setFocus(); //setWindowVisible(true);
129
130  }
131  
132  
133  /**Action for Close. 
134   */
135  GralUserAction actionButton = new GralUserAction("FcmdSettings-close")
136  {
137    @Override public boolean exec(int keyCode, GralWidget_ifc widg, Object... params)
138    { if(KeyCode.isControlFunctionMouseUpOrMenu(keyCode)){
139        if(widg == widgOk){
140          windSettings.closeWindow();
141        }
142      }
143      return true;
144  } };
145
146  
147  /**Action for Close. 
148   */
149  GralUserAction actionOpenInfo = new GralUserAction("FcmdSettings-info")
150  {
151    @Override public boolean exec(int keyCode, GralWidget_ifc widg, Object... params)
152    { if(KeyCode.isControlFunctionMouseUpOrMenu(keyCode)){
153        GralMng.get().showInfo(null);
154      }
155      return true;
156  } };
157
158  
159  /**Action for open the dialog window. It fills the value of the fields from internal variable. 
160   */
161  GralUserAction actionOpenDialog = new GralUserAction("FcmdSettings-actionOpenDialog")
162  {
163    @Override public boolean exec(int keyCode, GralWidget_ifc infos, Object... params){ 
164      if(KeyCode.isControlFunctionMouseUpOrMenu(keyCode)){
165        openDialog(main.currentFile());
166      }
167      return true;
168    }
169  };
170
171
172  /**Action for open the editor for the configuration file. 
173   * The name of the configuration file is contained in the widget. */
174  GralUserAction actionEditCfgFile = new GralUserAction("FcmdSettings-actionEditCfgFile")
175  {
176    @Override public boolean exec(int keyCode, GralWidget_ifc widg, Object... params){ 
177      if(KeyCode.isControlFunctionMouseUpOrMenu(keyCode)){
178        File fileCfg = new File(main.cargs.dirCfg, ((GralWidget)widg).getCmd());
179        main.openExtEditor(fileCfg);
180        /*
181        CmdStore.CmdBlock cmdBlock = main.buttonCmds.getCmd("edit");
182        if (cmdBlock == null) {
183          main.mainCmd.writeError("internal problem - don't find 'edit' command. ");
184        } else {
185          File[] files = new File[1];
186          files[0] = new File(main.cargs.dirCfg, ((GralWidget)widg).getCmd());
187          File dir = FileSystem.getDir(files[0]);
188          main.executer.cmdQueue.addCmd(cmdBlock, files, dir); // to execute.
189        }
190        */
191      }
192      return true;
193    }
194  };
195
196  
197  
198
199  /**Action to set the content of the cmd configuration file. */
200  GralUserAction actionApplyCfgCmd = new GralUserAction("FcmdSettings-actionApplyCfgCmd")
201  {
202    @Override public boolean exec(int keyCode, GralWidget_ifc widg, Object... params){ 
203      if(KeyCode.isControlFunctionMouseUpOrMenu(keyCode)){
204        String sFileCfg = ((GralWidget)widg).getCmd();
205        main.executer.cmdSelector.clear();
206        String sError = main.executer.readCmdCfgSelectList(main.executer.cmdSelector.addJZsub2SelectTable, new File(main.cargs.dirCfg, sFileCfg), main.console);
207        if(sError != null) {
208          main.showInfoBox(sError);
209          widgOkError.setText("error");
210        } else {
211          widgOkError.setText("success");
212          main.setTextInfoBox("ok read " + main.cargs.dirCfg + "/" + sFileCfg);
213        }
214      }
215      return true;
216    }
217  };
218
219
220  /**Action to set the content of the extension configuration file. */
221  GralUserAction actionApplyCfgExt = new GralUserAction("FcmdSettings-actionApplyCfgExt")
222  {
223    @Override public boolean exec(int keyCode, GralWidget_ifc widg, Object... params){ 
224      if(KeyCode.isControlFunctionMouseUpOrMenu(keyCode)){
225        main.executer.readCfgExt(new File(main.cargs.dirCfg, "extjz.cfg"));
226      }
227      return true;
228    }
229  };
230
231
232  /**Action to set the content of the favor path file. */
233  GralUserAction actionApplyCfgPath = new GralUserAction("FcmdSettings-actionApplyCfgPath")
234  {
235    @Override public boolean exec(int keyCode, GralWidget_ifc widg, Object... params){ 
236      if(KeyCode.isControlFunctionMouseUpOrMenu(keyCode)){
237        main.favorPathSelector.actionReadFavoritePathes.exec(KeyCode.menuEntered, null);
238      }
239      return true;
240    }
241  };
242
243
244  
245}