001package org.vishia.gral.swt;
002
003import java.io.File;
004
005import org.eclipse.swt.SWT;
006import org.eclipse.swt.widgets.DirectoryDialog;
007import org.eclipse.swt.widgets.FileDialog;
008import org.eclipse.swt.widgets.Shell;
009import org.vishia.gral.ifc.GralFileDialog_ifc;
010
011public class SwtFileDialog implements GralFileDialog_ifc
012{
013
014        private FileDialog fileDialog;
015        
016        private DirectoryDialog dirDialog;
017
018        private final Shell shell;
019        
020        private String sOpenResult;
021        
022        @Override public boolean open(String sTitle, int mode)
023        {
024    int modeSwt = 0;
025    
026    if((mode & GralFileDialog_ifc.multi)!=0) { modeSwt |= SWT.MULTI; }
027    
028    if((mode & GralFileDialog_ifc.directory)!=0) {
029      this.dirDialog = new DirectoryDialog(shell, SWT.MULTI);
030      if(sTitle != null){
031        dirDialog.setText(sTitle);
032      }
033      this.fileDialog = null;
034    } else {
035      this.dirDialog = null;
036      this.fileDialog = new FileDialog(shell, SWT.OPEN | modeSwt);
037      if(sTitle != null){
038        fileDialog.setText(sTitle);
039      }
040    }
041          return true;
042        }
043        
044        
045        public SwtFileDialog(Shell shell)
046        { this.shell = shell;
047        }
048        
049
050        /* (non-Javadoc)
051         * @see org.vishia.gral.FileDialogIfc#show(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
052         */
053        @Override public String show(String sBaseDir, String sLocalDir, String sMask, String sTitle)
054        {
055    if(fileDialog !=null){
056      if(sBaseDir !=null || sLocalDir !=null){
057        String sStartDir = (sBaseDir !=null ? sBaseDir : "") + (sLocalDir !=null ? sLocalDir : "");
058        fileDialog.setFileName(sStartDir); 
059      }
060      if(sTitle != null){
061        fileDialog.setText(sTitle);
062      }
063      shell.setVisible(true);
064      shell.setActive();
065      sOpenResult = fileDialog.open();  //it is opened, and this thread waits.
066      //String sDir = fileDialog.getFilterPath();
067    } else {
068      if(sTitle != null){
069        dirDialog.setText(sTitle);
070      }
071      shell.setVisible(true);
072      shell.setActive();
073      sOpenResult = dirDialog.open();  //it is opened, and this thread waits.
074      //String sDir = dirDialog.getFilterPath();
075      
076    }
077                return sOpenResult;
078        }
079
080
081  @Override public String[] getMultiSelection()
082  { return fileDialog.getFileNames();
083  }
084
085
086  @Override public String getSelection()
087  { if(fileDialog !=null){
088      if(sOpenResult == null){ return null; }    //aborted selection.
089      else {
090        String sPath = fileDialog.getFilterPath();
091        String sName = fileDialog.getFileName();
092        return sPath + "/" + sName;
093      }
094    } else {
095      return sOpenResult;  //selected dir is the value from open()
096    }
097  }
098        
099        
100        
101}