Class DirTree

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, EventListener, Accessible, TreeSelectionListener

public final class DirTree extends JPanel implements TreeSelectionListener
A component that renders directories in the file system as a navigable tree view. The DirTree can be "locked" to a specific directory, in which case it will only display subdirectories of that directory. When "unlocked", the DirTree will display all filesystem roots (e.g. "/" on Linux-based systems, or all available drives on Windows).
Since:
2017-11-09
Author:
scorbo2
See Also:
  • Constructor Details

    • DirTree

      public DirTree()
      Creates a DirTree in "unlocked" mode, showing all filesystem roots. You can then use the lock() method to lock (chroot) the DirTree to a specific subdirectory.
    • DirTree

      public DirTree(File rootDir)
      Creates a new DirTree instance chrooted to the specified directory.
  • Method Details

    • createDirTree

      @Deprecated(since="swing-extras 2.7", forRemoval=true) public static DirTree createDirTree()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use the constructor instead. This factory method may be removed in a future release.
      Creates a defaulted DirTree instance for viewing the first available filesystem root. Don't use this! It dates back to when DirTree was heavily linux-specific. On Windows, this will only show the first available drive, which is probably not what you want. The no-args constructor is the safest way to instantiate a DirTree in a platform-independent way.
      Returns:
      The new DirTree instance.
    • createDirTree

      @Deprecated(since="swing-extras 2.7", forRemoval=true) public static DirTree createDirTree(File dir)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use the constructor instead. This factory method may be removed in a future release.
      Creates a DirTree instance chrooted to the specified directory. As of swing-extras 2.7, this is exactly equivalent to new DirTree(dir).
      Parameters:
      dir - The directory to which to lock this DirTree (use unlock() to unlock it).
      Returns:
      The new DirTree instance.
    • getTreeCellRenderer

      public TreeCellRenderer getTreeCellRenderer()
      Provides access to the TreeCellRenderer in the underlying JTree. By default, the renderer is whatever the current Look and Feel installs on the tree. During construction, if that default renderer is a DefaultTreeCellRenderer, DirTree clears its icons (sets them to null) to avoid showing per-node icons.
      Returns:
      The TreeCellRenderer used by the underlying JTree.
    • setTreeCellRenderer

      public DirTree setTreeCellRenderer(TreeCellRenderer renderer)
      Allow setting a custom TreeCellRenderer on the underlying JTree. By default, DirTree uses the renderer installed by the current Look and Feel and, if that renderer is a DefaultTreeCellRenderer, it clears its icons (sets them to null) to avoid showing per-node icons. If you replace the renderer and still want an iconless appearance, you should configure your custom renderer accordingly (for example, by setting icons to null on a DefaultTreeCellRenderer).
      Parameters:
      renderer - The TreeCellRenderer to use for rendering tree nodes.
      Returns:
      This DirTree instance, for chaining.
    • setBackground

      public void setBackground(Color color)
      This is overridden so that we can ensure the underlying JTree will pick up the desired background color. If you've changed the background, and you want to revert to letting the current Look and Feel choose the color selection for you, you can use LookAndFeelManager.getLafColor() with "Tree.background" as the key name.
      Overrides:
      setBackground in class JComponent
      Parameters:
      color - the desired background Color.
    • getFilesystemRoots

      public static File[] getFilesystemRoots()
      Returns all filesystem roots for the current platform. On Unix-like systems, this typically returns a single root ("/"). On Windows, this returns all available drives.
      Returns:
      Array of all filesystem roots.
    • getLockDir

      public File getLockDir()
      Returns the current lock directory, if the tree is locked, otherwise null.
      Returns:
      The current lock directory, or null if not locked.
    • getCurrentDir

      public File getCurrentDir()
      Returns the currently selected directory, or null if nothing is selected.
      Returns:
      The current directory, or null if nothing selected.
    • addDirTreeListener

      public DirTree addDirTreeListener(DirTreeListener listener)
      Registers a DirTreeListener with this DirTree.
      Parameters:
      listener - The DirTreeListener which will receive events from us.
    • removeDirTreeListener

      public DirTree removeDirTreeListener(DirTreeListener listener)
      Unregisters a DirTreeListener from this instance.
      Parameters:
      listener - The DirTreeListener to unregister.
    • setEnabled

      public void setEnabled(boolean enabled)
      Calls to setEnabled are not forwarded from parent components to child components. So, if someone invokes setEnabled on this DirTree, we have to forward that message to our contained JTree and scroll pane.
      Overrides:
      setEnabled in class JComponent
    • isLocked

      public boolean isLocked()
      Indicates whether this DirTree is "locked" to a specific subdirectory (similar to chroot). Use unlock() to revert to the default display where all directories will be shown.
      Returns:
      Whether our display is filtered to a specific subdirectory (see getRootDirectory()).
    • getAllowLock

      public boolean getAllowLock()
      Indicates whether this DirTree allows locking (chrooting). Default is true.
      Returns:
      Whether this DirTree allows locking.
    • getAllowUnlock

      public boolean getAllowUnlock()
      Indicates whether this DirTree allows unlocking (de-chrooting). Default is true.
      Returns:
      Whether this DirTree allows unlocking.
    • setAllowLock

      public DirTree setAllowLock(boolean allow)
      Enables or disables locking within this DirTree (chrooting). Passing false will unlock the DirTree if it is currently locked, and will prevent the "lock" menu item from appearing in the popup menu. Disallowing a tree lock will also disallow unlocking.
      Parameters:
      allow - Whether to enable locking.
    • setAllowUnlock

      public DirTree setAllowUnlock(boolean allow)
      Enables or disables unlocking within this DirTree (chrooting). Disabling this will prevent the "unlock" menu item from appearing in the popup menu.
      Parameters:
      allow - Whether to enable unlocking.
    • getShowHiddenDirs

      @Deprecated(since="swing-extras 2.8") public boolean getShowHiddenDirs()
      Deprecated.
      Use getShowHidden() instead.
      Indicates whether hidden directories are shown in this DirTree. Default is true.
    • getShowHidden

      public boolean getShowHidden()
      Indicates whether hidden items (directories and files) are shown in this DirTree. Default is true.
    • setShowHiddenDirs

      @Deprecated(since="swing-extras 2.8") public DirTree setShowHiddenDirs(boolean showHiddenDirs)
      Deprecated.
      Controls whether hidden directories are shown in this DirTree. Default is true. What constitutes a "hidden" directory is platform-dependent.
    • setShowHidden

      public DirTree setShowHidden(boolean showHidden)
      Controls whether hidden items (directories and files) are shown in this DirTree. Default is true. What constitutes a "hidden" item is platform-dependent.
    • getShowFiles

      public boolean getShowFiles()
      Indicates whether files are shown in this DirTree. Default is false. When enabled, files are shown as leaf nodes within directories.
    • setShowFiles

      public DirTree setShowFiles(boolean showFiles)
      Controls whether files are shown in this DirTree. Default is false. When enabled, files are shown as leaf nodes within directories.
      Parameters:
      showFiles - Whether to show files in the tree.
    • getFileFilter

      public FileFilter getFileFilter()
      Returns the current FileFilter used to restrict which files are shown. Returns null if no filter is set (all files are shown).
    • setFileFilter

      public DirTree setFileFilter(FileFilter fileFilter)
      Sets an optional FileFilter to restrict which files are shown in the tree. Only relevant when showFiles is enabled. If null, all files are shown.
      Parameters:
      fileFilter - The FileFilter to apply, or null to show all files.
    • reload

      public void reload()
      Reloads this DirTree. When the reload is complete, the currently selected directory (if any) will be re-selected.
    • lock

      public void lock(File newRootDir)
      "Locks" this DirTree to the given directory, meaning that only subdirectories of the given directory will be shown. This is similar to chrooting. If the given directory does not exist, or is null, this method does nothing.
      Parameters:
      newRootDir - The new root directory.
    • unlock

      public void unlock()
      Unlocking the DirTree reverts it back to showing all filesystem roots.
    • selectAndScrollTo

      public boolean selectAndScrollTo(File dir)
      Selects and scrolls to the given directory. Does nothing if the directory doesn't exist.
      Parameters:
      dir - The directory to select.
    • valueChanged

      public void valueChanged(TreeSelectionEvent e)
      Overridden from TreeSelectionListener, so we can listen for selection events within our JTree and notify our own listeners that the selection has changed.
      Specified by:
      valueChanged in interface TreeSelectionListener
      Parameters:
      e - The TreeSelectionEvent.