    001package org.vishia.gral.widget;
    002
    003import java.io.File;
    004
    005import org.vishia.fileRemote.FileRemote;
    006import org.vishia.gral.base.GralButton;
    007import org.vishia.gral.base.GralPos;
    008import org.vishia.gral.base.GralTextField;
    009import org.vishia.gral.base.GralWindow;
    010import org.vishia.gral.ifc.GralFileDialog_ifc;
    011import org.vishia.gral.ifc.GralMngBuild_ifc;
    012import org.vishia.gral.ifc.GralTableLine_ifc;
    013import org.vishia.gral.ifc.GralUserAction;
    014import org.vishia.gral.ifc.GralWidget_ifc;
    015import org.vishia.gral.ifc.GralWindow_ifc;
    016import org.vishia.util.KeyCode;
    017
    018
    019/**A window for search-in-file dialogue.
    020 * It is instantiated calling {@link GralFileSelector#createWindowConfirmSearchGthread(GralMngBuild_ifc)}.
    021 * The user can invoke {@link #confirmSearchInFiles(GralFileSelector, Appendable)} to open that window.
    022 *
    023 */
    024public class GralFileSelectWindow implements GralFileDialog_ifc
    025{
    026  /**Version, history and license. The version number is a date written as yyyymmdd as decimal number.
    027   * Changes:
    028   * <ul>
    029   * <li>2013-03-28 Hartmut implements {@link GralFileDialog_ifc} because it should be so. TODO tuning with the
    030   *   other possibility {@link org.vishia.gral.swt.SwtFileDialog} which uses the standard file dialog of windows or linux
    031   * <li>2013-03-28 Hartmut creating, it was a static inner class of {@link GralFileSelector}
    032   * </ul>
    033   * <br><br>
    034   *
    035   * <b>Copyright/Copyleft</b>:
    036   * For this source the LGPL Lesser General Public License,
    037   * published by the Free Software Foundation is valid.
    038   * It means:
    039   * <ol>
    040   * <li> You can use this source without any restriction for any desired purpose.
    041   * <li> You can redistribute copies of this source to everybody.
    042   * <li> Every user of this source, also the user of redistribute copies
    043   *    with or without payment, must accept this license for further using.
    044   * <li> But the LPGL ist not appropriate for a whole software product,
    045   *    if this source is only a part of them. It means, the user
    046   *    must publish this part of source,
    047   *    but don't need to publish the whole source of the own product.
    048   * <li> You can study and modify (improve) this source
    049   *    for own using or for redistribution, but you have to license the
    050   *    modified sources likewise under this LGPL Lesser General Public License.
    051   *    You mustn't delete this Copyright/Copyleft inscription in this source file.
    052   * </ol>
    053   * If you are intent to use this sources without publishing its usage, you can get
    054   * a second license subscribing a special contract with the author.
    055   *
    056   * @author Hartmut Schorrig = hartmut.schorrig@vishia.de
    057   */
    058  //@SuppressWarnings("hiding")
    059  public final static int version = 20130328;
    060
    061
    062  private final GralWindow_ifc wind;
    063
    064  private final GralFileSelector fileSelector;
    065
    066  private final GralTextField widgFilename;
    067
    068  private final GralButton widgButtonOk;
    069
    070
    071  GralUserAction actionOkForUser;
    072
    073  private File dir;
    074
    075  /**Creates the window to confirm search in files. This window can be created only one time
    076   * for all file panels, if the application has more as one. On activating the directory
    077   * and the file panel to show results should be given. But only one search process can be run
    078   * simultaneously.
    079   * @return The created window.
    080   */
    081   public GralFileSelectWindow(String name, GralMngBuild_ifc mng){
    082     mng.selectPanel("primaryWindow");
    083     mng.setPosition(-24, 0, -67, 0, 1, 'r'); //right buttom, about half less display width and hight.
    084     wind = mng.createWindow("windSelectFile", "select file", GralWindow.windExclusive | GralWindow.windResizeable );
    085     mng.setPosition(0, -3, 0, 0, 0, 'd', 0.0f);
    086     fileSelector = new GralFileSelector(name + "-selectFile", 100, new int[]{2,0,-6,-12}, null);
    087     fileSelector.setToPanel(mng);
    088     fileSelector.specifyActionOnFileSelected(actionSelectFile);
    089     fileSelector.setActionOnEnterFile(actionOk);
    090     mng.setPosition(-2, 0, 0, -7, 0, 'r',1);
    091     widgFilename = mng.addTextField(name+"-name", true, null, null);
    092     mng.setPosition(-3, 0, -8, GralPos.size + 7, 0, 'r',1);
    093     widgButtonOk = mng.addButton(name+"-ok", null, "Ok");
    094     widgButtonOk.setActionChange(actionOk);
    095
    096   }
    097
    098
    099
    100
    101  /**Prepares the action and appearance and shows the window.
    102   * The param actionSelect is called if the user presses the Button on right bottom. The button is marked with
    103   * "select" or "write" depending on the param bForWrite. For actionSelect the method
    104   * {@link GralUserAction#exec(int, GralWidget_ifc, Object...)} is called with null for the widget (a widget should not
    105   *   necessary for the user) but with the selected File(s) as Object parameter.
    106   *   If one file is selected the first Object is instanceof File. If more as one files are selected
    107   *   a List<File> or File[] is provided. The user should check this type.
    108   *
    109   * @param startDir
    110   * @param sTitle shown in title bar of the window
    111   * @param bForWrite true then the name field is editable and "Save" is shown on button. False then the name Field
    112   *   is readonly and "select" is shown on button
    113   * @param actionSelect Action which is called on Button ok. The first Object of exec(...,object) is the selected File
    114   *   or a File[] or List<File> if more as one file is selected.
    115   */
    116  public void openDialog(FileRemote startDir, String sTitle, boolean bForWrite, GralUserAction actionSelect){
    117    wind.setTitle(sTitle);
    118    actionOkForUser = actionSelect;
    119    if(bForWrite){
    120      widgButtonOk.setText("write");
    121    } else {
    122      widgButtonOk.setText("select");
    123    }
    124    fileSelector.fillIn(startDir, false);
    125    wind.setWindowVisible(true);
    126    fileSelector.setFocus();
    127  }
    128
    129
    130  @Override
    131  public boolean open(String sTitle, int mode)
    132  {
    133    // TODO Auto-generated method stub
    134    return false;
    135  }
    136
    137
    138
    139
    140  @Override
    141  public String show(String sBaseDir, String sLocalDir, String sMask, String sTitle)
    142  {
    143    // TODO Auto-generated method stub
    144    return null;
    145  }
    146
    147
    148  @Override
    149  public String[] getMultiSelection()
    150  {
    151    // TODO Auto-generated method stub
    152    return null;
    153  }
    154
    155
    156
    157
    158  @Override
    159  public String getSelection()
    160  {
    161    // TODO Auto-generated method stub
    162    return null;
    163  }
    164
    165
    166
    167  public void closeWindow(){
    168    wind.closeWindow();
    169  }
    170
    171
    172  /**Action of OK button or enter button. It returns the file in the shown directory
    173   * with the name written in the widgFilename-textfield.
    174   * It is not the selected file. But the textfield is filled with the name of the selected file
    175   * if a file was selected in the table.
    176   *
    177   */
    178  GralUserAction actionOk = new GralUserAction("GralFileSelector-actionOk"){
    179    @Override public boolean exec(int actionCode, GralWidget_ifc widgd, Object... params){
    180      if(KeyCode.isControlFunctionMouseUpOrMenu(actionCode)){  //supress both mouse up and down reaction
    181        FileRemote dir = fileSelector.getCurrentDir();
    182        String sFilename = widgFilename.getText();
    183        FileRemote file;
    184        if(sFilename.length()==0 || sFilename.equals("..")){
    185          file = dir;
    186        } else {
    187          file = dir.child(sFilename);
    188        }
    189        actionOkForUser.exec(KeyCode.menuEntered, null, file);
    190      }
    191      return true;
    192    }
    193  };
    194
    195
    196
    197  GralUserAction actionSelectFile = new GralUserAction("GralFileSelector-actionSelectFile"){
    198    /**The action called from {@link GralTable}.
    199     * @param params [0] is the Table line. The content of table cells are known here,
    200     *   because it is the file table itself. The {@link GralTableLine_ifc#getUserData()}
    201     *   returns the {@link FileRemote} file Object.
    202     * @see org.vishia.gral.ifc.GralUserAction#userActionGui(int, org.vishia.gral.base.GralWidget, java.lang.Object[])
    203     */
    204    @Override public boolean exec(int actionCode, GralWidget_ifc widgd, Object... params){
    205      GralTableLine_ifc line = (GralTableLine_ifc) params[0];
    206      String sFileCell = line.getCellText(GralFileSelector.kColFilename);
    207      Object oData = line.getUserData();
    208      if(false && oData instanceof File){
    209        String sName = ((File)oData).getName();
    210        widgFilename.setText(sName);
    211      } else {
    212        widgFilename.setText(sFileCell);
    213      }
    214      return true;
    215    }
    216  };
    217
    218
    219
    220
    221}