Class FileDialog

java.lang.Object
org.eclipse.swt.widgets.Dialog
org.eclipse.swt.widgets.FileDialog

public class FileDialog extends Dialog
Instances of this class allow the user to navigate the file system and select or enter a file name.
Styles:
SAVE, OPEN, MULTI
Events:
(none)

Note: Only one of the styles SAVE and OPEN may be specified.

IMPORTANT: This class is not intended to be subclassed.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) static final char
     
    (package private) static final char
     
    (package private) String
     
    (package private) String[]
     
    (package private) String[]
     
    (package private) int
     
    (package private) String[]
     
    (package private) String
     
    (package private) String
     
    (package private) long
     
    (package private) boolean
     
    (package private) static final char
     
    (package private) boolean
     

    Fields inherited from class Dialog

    parent, style, title
  • Constructor Summary

    Constructors
    Constructor
    Description
    FileDialog(Shell parent)
    Constructs a new instance of this class given only its parent.
    FileDialog(Shell parent, int style)
    Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) String
     
    Returns the path of the first file that was selected in the dialog relative to the filter path, or an empty string if no such file has been selected.
    Returns a (possibly empty) array with the paths of all files that were selected in the dialog relative to the filter path.
    Returns the file extensions which the dialog will use to filter the files it shows.
    int
    Get the 0-based index of the file extension filter which was selected by the user, or -1 if no filter was selected.
    Returns the names that describe the filter extensions which the dialog will use to filter the files it shows.
    Returns the directory path that the dialog will use, or an empty string if this is not set.
    boolean
    Returns the flag that the dialog will use to determine whether to prompt the user for file overwrite if the selected file already exists.
    private boolean
    isGlobPattern(String extension)
    Check whether the file extension is a glob pattern.
    Makes the dialog visible and brings it to the front of the display.
    Makes the dialog visible and brings it to the front of the display.
    (package private) Optional<String>
    Open the file chooser dialog using the GtkFileChooserNative API (GTK3.20+) for running applications without direct filesystem access (such as Flatpak).
    (package private) void
     
    void
    Set the initial filename which the dialog will select by default when opened to the argument, which may be null.
    void
    setFilterExtensions(String... extensions)
    Set the file extensions which the dialog will use to filter the files it shows to the argument, which may be null.
    void
    setFilterIndex(int index)
    Set the 0-based index of the file extension filter which the dialog will use initially to filter the files it shows to the argument.
    void
    Sets the names that describe the filter extensions which the dialog will use to filter the files it shows to the argument, which may be null.
    void
    Sets the directory path that the dialog will use to the argument, which may be null.
    void
    setOverwrite(boolean overwrite)
    Sets the flag that the dialog will use to determine whether to prompt the user for file overwrite if the selected file already exists.
    (package private) void
    setURIMode(boolean uriMode)
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • filterNames

      String[] filterNames
    • filterExtensions

      String[] filterExtensions
    • filterPath

      String filterPath
    • fileName

      String fileName
    • fileNames

      String[] fileNames
    • fullPath

      String fullPath
    • filterIndex

      int filterIndex
    • overwrite

      boolean overwrite
    • uriMode

      boolean uriMode
    • handle

      long handle
    • SEPARATOR

      static final char SEPARATOR
    • EXTENSION_SEPARATOR

      static final char EXTENSION_SEPARATOR
      See Also:
    • FILE_EXTENSION_SEPARATOR

      static final char FILE_EXTENSION_SEPARATOR
      See Also:
  • Constructor Details

    • FileDialog

      public FileDialog(Shell parent)
      Constructs a new instance of this class given only its parent.
      Parameters:
      parent - a shell which will be the parent of the new instance
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the parent is null
      SWTException -
      • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent
      • ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass
    • FileDialog

      public FileDialog(Shell parent, int style)
      Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

      The style value is either one of the style constants defined in class SWT which is applicable to instances of this class, or must be built by bitwise OR'ing together (that is, using the int "|" operator) two or more of those SWT style constants. The class description lists the style constants that are applicable to the class. Style bits are also inherited from superclasses.

      Parameters:
      parent - a shell which will be the parent of the new instance
      style - the style of dialog to construct
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the parent is null
      SWTException -
      • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent
      • ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass
      See Also:
  • Method Details

    • computeResultChooserDialog

      String computeResultChooserDialog(long file)
    • getFileName

      public String getFileName()
      Returns the path of the first file that was selected in the dialog relative to the filter path, or an empty string if no such file has been selected.
      Returns:
      the relative path of the file
    • getFileNames

      public String[] getFileNames()
      Returns a (possibly empty) array with the paths of all files that were selected in the dialog relative to the filter path.
      Returns:
      the relative paths of the files
    • getFilterExtensions

      public String[] getFilterExtensions()
      Returns the file extensions which the dialog will use to filter the files it shows.
      Returns:
      the file extensions filter
    • getFilterIndex

      public int getFilterIndex()
      Get the 0-based index of the file extension filter which was selected by the user, or -1 if no filter was selected.

      This is an index into the FilterExtensions array and the FilterNames array.

      Returns:
      index the file extension filter index
      Since:
      3.4
      See Also:
    • getFilterNames

      public String[] getFilterNames()
      Returns the names that describe the filter extensions which the dialog will use to filter the files it shows.
      Returns:
      the list of filter names
    • getFilterPath

      public String getFilterPath()
      Returns the directory path that the dialog will use, or an empty string if this is not set. File names in this path will appear in the dialog, filtered according to the filter extensions.
      Returns:
      the directory path string
      See Also:
    • getOverwrite

      public boolean getOverwrite()
      Returns the flag that the dialog will use to determine whether to prompt the user for file overwrite if the selected file already exists.
      Returns:
      true if the dialog will prompt for file overwrite, false otherwise
      Since:
      3.4
    • open

      public String open()
      Makes the dialog visible and brings it to the front of the display.
      Returns:
      a string describing the absolute path of the first selected file, or null if the dialog was cancelled or an error occurred
      Throws:
      SWTException -
      • ERROR_WIDGET_DISPOSED - if the dialog has been disposed
      • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog
    • openDialog

      public Optional<String> openDialog()
      Makes the dialog visible and brings it to the front of the display. Equal to open() but also exposes for state information like user cancellation.
      Returns:
      an Optional that either contains the absolute path of the first selected file or is empty in case the dialog was canceled
      Throws:
      SWTException -
      • ERROR_WIDGET_DISPOSED - if the dialog has been disposed
      • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the dialog
      • ERROR_INVALID_RETURN_VALUE - if the dialog was not cancelled and did not return a valid path
      Since:
      3.126
    • openNativeChooserDialog

      Optional<String> openNativeChooserDialog()
      Open the file chooser dialog using the GtkFileChooserNative API (GTK3.20+) for running applications without direct filesystem access (such as Flatpak). API for GtkFileChoosernative does not give access to any GtkWindow or GtkWidget for the dialog, thus this method omits calls that requires such access. These are be handled by the GtkNativeDialog API.
      Returns:
      an Optional that contains a string describing the absolute path of the first selected file or is empty if the dialog is canceled
    • presetChooserDialog

      void presetChooserDialog()
    • isGlobPattern

      private boolean isGlobPattern(String extension)
      Check whether the file extension is a glob pattern. For example, *.* is a glob pattern which corresponds to all files *.jp* corresponds to all the files with extension starting with jp like jpg,jpeg etc *.jp? corresponds to 3 letter extension starting with jp with any 3rd letter *.[pq]ng this corresponds to *.png and *.qng
      Parameters:
      extension - file extension as a string
      Returns:
      true if the extension contains any of the glob pattern wildcards
    • setFileName

      public void setFileName(String string)
      Set the initial filename which the dialog will select by default when opened to the argument, which may be null. The name will be prefixed with the filter path when one is supplied.
      Parameters:
      string - the file name
    • setFilterExtensions

      public void setFilterExtensions(String... extensions)
      Set the file extensions which the dialog will use to filter the files it shows to the argument, which may be null.

      The strings are platform specific. For example, on some platforms, an extension filter string is typically of the form "*.extension", where "*.*" matches all files. For filters with multiple extensions, use semicolon as a separator, e.g. "*.jpg;*.png".

      Note: On Mac, setting the file extension filter affects how app bundles are treated by the dialog. When a filter extension having the app extension (.app) is selected, bundles are treated as files. For all other extension filters, bundles are treated as directories. When no filter extension is set, bundles are treated as files.

      Parameters:
      extensions - the file extension filter
      See Also:
    • setFilterIndex

      public void setFilterIndex(int index)
      Set the 0-based index of the file extension filter which the dialog will use initially to filter the files it shows to the argument.

      This is an index into the FilterExtensions array and the FilterNames array.

      Parameters:
      index - the file extension filter index
      Since:
      3.4
      See Also:
    • setFilterNames

      public void setFilterNames(String... names)
      Sets the names that describe the filter extensions which the dialog will use to filter the files it shows to the argument, which may be null.

      Each name is a user-friendly short description shown for its corresponding filter. The names array must be the same length as the extensions array.

      Parameters:
      names - the list of filter names, or null for no filter names
      See Also:
    • setFilterPath

      public void setFilterPath(String string)
      Sets the directory path that the dialog will use to the argument, which may be null. File names in this path will appear in the dialog, filtered according to the filter extensions. If the string is null, then the operating system's default filter path will be used.

      Note that the path string is platform dependent. For convenience, either '/' or '\' can be used as a path separator.

      Parameters:
      string - the directory path
      See Also:
    • setOverwrite

      public void setOverwrite(boolean overwrite)
      Sets the flag that the dialog will use to determine whether to prompt the user for file overwrite if the selected file already exists.

      Note: On some platforms where suppressing the overwrite prompt is not supported, the prompt is shown even when invoked with overwrite false.

      Parameters:
      overwrite - true if the dialog will prompt for file overwrite, false otherwise
      Since:
      3.4
    • setURIMode

      void setURIMode(boolean uriMode)