    001package org.vishia.commander;
    002
    003import java.io.File;
    004import java.io.IOException;
    005
    006import org.vishia.fileRemote.FileRemote;
    007import org.vishia.gral.base.GralButton;
    008import org.vishia.gral.base.GralPos;
    009import org.vishia.gral.base.GralWidget;
    010import org.vishia.gral.base.GralWindow;
    011import org.vishia.gral.ifc.GralTextField_ifc;
    012import org.vishia.gral.ifc.GralUserAction;
    013import org.vishia.gral.ifc.GralWindow_ifc;
    014import org.vishia.util.KeyCode;
    015
    016/**The F7-Key mkdir is used for mkfile too. This class builds the mkDirFile- dialog window
    017 * and contains its functionality.
    018 * @author Hartmut Schorrig
    019 *
    020 */
    021public class FcmdMkDirFile
    022{
    023  protected final Fcmd main;
    024
    025  GralWindow_ifc windMk;
    026
    027  GralTextField_ifc widgParentPath, widgName;
    028
    029  GralButton widgButtonClose, widgButtonMkFile, widgButtonMkDir;
    030
    031  public FcmdMkDirFile(Fcmd main)
    032  { this.main = main;
    033  }
    034
    035
    036  /**Builds the content of the confirm-delete window. The window is created static. It is shown
    037   * whenever it is used.  */
    038  void buildWindowConfirmMk()
    039  {
    040    main._gralMng.selectPanel("primaryWindow");
    041    main._gralMng.setPosition(-19, 0, -47, 0, 1, 'r'); //right buttom, about half less display width and hight.
    042    int windProps = GralWindow.windConcurrently;
    043    GralWindow window =  main._gralMng.createWindow("windMk", "mkdir/file - The.file.Commander", windProps);
    044    main._gralMng.setPosition(4, GralPos.size -3.8f, 1, -1, 0, 'd', 0.4f);
    045    widgParentPath = main._gralMng.addTextField("mkParentPath", false, "parent path:", "t");
    046    widgName = main._gralMng.addTextField("mkName", true, "name:", "t");
    047    main._gralMng.setPosition(-3.5f, -0.5f, 1, GralPos.size + 10, 0, 'r', 2);
    048    widgButtonClose = main._gralMng.addButton(null, actionButton, "c", null, "close");
    049    widgButtonMkDir = main._gralMng.addButton(null, actionButton, "d", null, "mkdir");
    050    widgButtonMkFile = main._gralMng.addButton(null, actionButton, "f", null, "create file");
    051
    052    windMk = window;
    053
    054  }
    055
    056
    057  /**Opens the view window and fills its content.
    058   * @param src The path which is selected as source. It may be a directory or a file.
    059   */
    060  void dialogMkDirFile(File src){
    061    FcmdFileCard card = main.getLastSelectedFileCards()[0];
    062    if(card !=null){
    063      File dir = card.getCurrentDir();
    064      widgParentPath.setText(dir.getAbsolutePath());
    065    } else {
    066      widgParentPath.setText("?? nothing selected ??");
    067    }
    068    main.gui.setHelpUrl(main.cargs.dirHtmlHelp + "/Fcmd.html#Topic.FcmdHelp.mkdirfile.");
    069    windMk.setFocus(); //setWindowVisible(true);
    070  }
    071
    072
    073  /**Action for Key F7 for open dialog. Its like Norton Commander.
    074   */
    075  GralUserAction actionOpenDialog = new GralUserAction("actionOpenDialog")
    076  {
    077    @Override public boolean userActionGui(int keyCode, GralWidget infos, Object... params)
    078    { dialogMkDirFile(null);
    079      return true;
    080      // /
    081    }
    082  };
    083
    084
    085
    086  /**Action for Buttons */
    087  GralUserAction actionButton = new GralUserAction("actionButton")
    088  {
    089    @Override public boolean userActionGui(int keyCode, GralWidget widg, Object... params)
    090    { if(KeyCode.isControlFunctionMouseUpOrMenu(keyCode)){
    091        switch(widg.sCmd.charAt(0)){
    092          case 'f':{
    093            String path = widgParentPath.getText();
    094            String name = widgName.getText();
    095            FileRemote file = main.fileCluster.getFile(path, name);
    096            boolean bOk = false;
    097            try{ bOk = file.createNewFile(); }
    098            catch(IOException exc){ main.mainCmd.writeError(exc.getLocalizedMessage()); }
    099            if(!bOk){ main.mainCmd.writeError("file exists already");}
    100
    101          }break;
    102          case 'd':{
    103            String path = widgParentPath.getText();
    104            String name = widgName.getText();
    105            FileRemote dir = main.fileCluster.getFile(path, name);
    106            boolean bOk = false;
    107            try{ bOk = dir.mkdir(); }
    108            catch(SecurityException exc){ main.mainCmd.writeError(exc.getLocalizedMessage()); }
    109            if(!bOk){ main.mainCmd.writeError("directory exists already");}
    110          }break;
    111        }
    112        windMk.setWindowVisible(false);
    113      }
    114      return true;
    115      // /
    116    }
    117  };
    118
    119
    120
    121
    122}