Class BaseFileChooser

    • Field Detail

      • m_PromptOverwriteFile

        protected boolean m_PromptOverwriteFile
        whether to ask to overwrite an existing file (using the save dialog).
      • m_AutoAppendExtension

        protected boolean m_AutoAppendExtension
        whether to automatically append extension.
      • m_AllowGlobFilters

        protected boolean m_AllowGlobFilters
        whether glob filters (eg "*.txt") are allowed.
      • m_DefaultExtension

        protected String m_DefaultExtension
        the default extension.
      • m_PanelBookmarksAndFilter

        protected JPanel m_PanelBookmarksAndFilter
        the panel with the bookmarks and filter.
      • m_PanelFilter

        protected FilterPanel m_PanelFilter
        the panel for the filter.
    • Constructor Detail

      • BaseFileChooser

        public BaseFileChooser()
        Constructs a BaseFileChooser pointing to the user's default directory. This default depends on the operating system. It is typically the "My Documents" folder on Windows, and the user's home directory on Unix.
      • BaseFileChooser

        public BaseFileChooser​(String currentDirectoryPath)
        Constructs a BaseFileChooser using the given path. Passing in a null string causes the file chooser to point to the user's default directory. This default depends on the operating system. It is typically the "My Documents" folder on Windows, and the user's home directory on Unix.
        Parameters:
        currentDirectoryPath - a String giving the path to a file or directory
      • BaseFileChooser

        public BaseFileChooser​(File currentDirectory)
        Constructs a BaseFileChooser using the given File as the path. Passing in a null file causes the file chooser to point to the user's default directory. This default depends on the operating system. It is typically the "My Documents" folder on Windows, and the user's home directory on Unix.
        Parameters:
        currentDirectory - a File object specifying the path to a file or directory
    • Method Detail

      • initialize

        protected void initialize()
        For initializing some stuff.
      • getDefaultAccessoryDimension

        protected Dimension getDefaultAccessoryDimension()
        Returns the preferred dimension.
        Returns:
        the dimension, null if to use default
      • createAccessoryPanel

        protected JComponent createAccessoryPanel()
        Creates an accessory panel displayed next to the files.
        Returns:
        the panel or null if none available
      • setPromptOverwriteFile

        public void setPromptOverwriteFile​(boolean value)
        Sets whether the user gets prompted by the save dialog if the selected file already exists.
        Parameters:
        value - if true, then the user will get prompted if file already exists
      • getPromptOverwriteFile

        public boolean getPromptOverwriteFile()
        Returns whether the user gets prompted by the save dialog if the selected file already exists.
        Returns:
        true if the user will get prompted
      • setAutoAppendExtension

        public void setAutoAppendExtension​(boolean value)
        Sets whether to automatically append the currently selected file extension or the default one (if All-Filter is used).
        Parameters:
        value - if true, then the file extension will be added automatically
      • getAutoAppendExtension

        public boolean getAutoAppendExtension()
        Returns whether to automatically append the currently selected file extension or the default one (if All-Filter is used).
        Returns:
        true if the file extension will be added automatically
      • setDefaultExtension

        public void setDefaultExtension​(String value)
        Sets the default extension. Is used if m_AutoAppendExtension is true and the All-Filter is selected.
        Parameters:
        value - the extension (without dot), use null to unset
      • getDefaultExtension

        public String getDefaultExtension()
        Returns the default extension. Is used if m_AutoAppendExtension is true and the All-Filter is selected.
        Returns:
        the extension, can be null
      • setAllowGlobFilters

        public void setAllowGlobFilters​(boolean value)
        Sets whether the user can enter glob filters like "*.txt".
        Parameters:
        value - if true, then the user can enter glob filters
      • getAllowGlobFilters

        public boolean getAllowGlobFilters()
        Returns whether the user can enter glob filters like "*.txt".
        Returns:
        true if the user can enter glob filters
      • isGlobFilter

        protected boolean isGlobFilter​(FileFilter filter)
        Returns whether the filter is a "glob" filter, e.g., when the user enters "*.txt" manually.
        Parameters:
        filter - the filter to check
        Returns:
        true if a glob filter
      • checkFilter

        protected boolean checkFilter​(FileFilter filter)
        Checks the filter whether it can be set.
        Parameters:
        filter - the filter to set
        Returns:
        true if check passed and valid
      • fixPrefix

        protected File fixPrefix​(File file)
        Fixes the file prefix if the user accidentally pasted a filename starting with "file:///".
        Parameters:
        file - the fix to check and fix
        Returns:
        the potentially fixed file
      • getSelectedPlaceholderFile

        public PlaceholderFile getSelectedPlaceholderFile()
        Returns the selected file as PlaceholderFile. This can be set either by the programmer via setFile or by a user action, such as either typing the filename into the UI or selecting the file from a list in the UI.
        Returns:
        the selected file, converted to PlaceholderFile
      • setSelectedFiles

        public void setSelectedFiles​(File[] selectedFiles)
        Sets the list of selected files if the file chooser is set to allow multiple selection.
        Specified by:
        setSelectedFiles in interface FileChooser
        Overrides:
        setSelectedFiles in class JFileChooser
        Parameters:
        selectedFiles - the files to select initially
      • getSelectedFiles

        public File[] getSelectedFiles()
        Returns a list of selected files if the file chooser is set to allow multiple selection.

        Fixes the problem with JFileChooser of not returning anything sometimes when only a single file is selected.
        Specified by:
        getSelectedFiles in interface FileChooser
        Overrides:
        getSelectedFiles in class JFileChooser
        Returns:
        the selected files
      • getSelectedPlaceholderFiles

        public PlaceholderFile[] getSelectedPlaceholderFiles()
        Returns a list of selected files as PlaecholderFile objects if the file chooser is set to allow multiple selection.
        Returns:
        the selected files, converted to PlaceholderFile objects
      • hasCorrectExtension

        protected boolean hasCorrectExtension​(File file,
                                              String[] extensions)
        Checks the filename, whether it has an extension from the provided list.
        Parameters:
        file - the file to check
        extensions - the extensions to check against
        Returns:
        true if valid extension
      • setCurrentDirectory

        public void setCurrentDirectory​(File dir)
        Sets the current directory. Passing in null sets the file chooser to point to the user's default directory. This default depends on the operating system. It is typically the "My Documents" folder on Windows, and the user's home directory on Unix. If the file passed in as currentDirectory is not a directory, the parent of the file will be used as the currentDirectory. If the parent is not traversable, then it will walk up the parent tree until it finds a traversable directory, or hits the root of the file system.
        Specified by:
        setCurrentDirectory in interface FileChooser
        Overrides:
        setCurrentDirectory in class JFileChooser
        Parameters:
        dir - the current directory to point to
        See Also:
        JFileChooser.getCurrentDirectory()
      • findExistingDir

        public static File findExistingDir​(File dir)
        Returns a directory that exists, starting with the provided one. If path cannot be used at all, they current working directory ("user.dir") is returned.
        Parameters:
        dir - the starting dir
        Returns:
        the existing dir
      • accept

        public boolean accept​(File f)
        Returns true if the file should be displayed. If a filter string is entered, then this must be present in the file name (case-insensitive).
        Specified by:
        accept in interface FileChooser
        Overrides:
        accept in class JFileChooser
        Parameters:
        f - the File
        Returns:
        true if the file should be displayed, otherwise false
        See Also:
        FileFilter.accept(java.io.File)
      • isJComponent

        public boolean isJComponent()
        Returns whether the file chooser is a JComponent and can be embedded in other components.
        Specified by:
        isJComponent in interface FileChooser
        Returns:
        true if JComponent-derived
      • handleException

        protected static void handleException​(String msg,
                                              Throwable t)
        Outputs the error in the console window.
        Parameters:
        msg - the message
        t - the exception