4747import java .util .Date ;
4848import java .util .Enumeration ;
4949import java .util .List ;
50+ import java .util .function .Predicate ;
5051
5152import javax .swing .GroupLayout ;
5253import javax .swing .JButton ;
8384import javax .swing .tree .TreePath ;
8485import javax .swing .tree .TreeSelectionModel ;
8586
87+ import org .apache .commons .lang3 .SystemUtils ;
8688import org .openide .util .NbBundle ;
8789
8890import uk .gov .nationalarchives .droid .util .FileUtil ;
@@ -108,14 +110,24 @@ public class ResourceSelectorDialog extends JDialog {
108110 "Name" , "Size" , "Last modified" , };
109111
110112 private static final Class <?>[] TYPES = new Class [] {
111- File .class , Long .class , Date .class , };
113+ File .class , Long .class , Date .class , };
112114
113- private FileSystemView fsv = FileSystemView .getFileSystemView ();
115+ private final FileSystemView fsv = FileSystemView .getFileSystemView ();
114116
115117 private List <File > selectedFiles = new ArrayList <File >();
116118
117119 private int response = JFileChooser .CANCEL_OPTION ;
118-
120+
121+ private final Predicate <File > shouldTraverse = file -> {
122+ if (!file .isDirectory ()) {
123+ return false ;
124+ }
125+ if (!file .canRead ()) {
126+ return false ;
127+ }
128+ return !SystemUtils .IS_OS_WINDOWS || fsv .isFileSystem (file );
129+ };
130+
119131 /**
120132 * Creates new form ResourceSelector.
121133 * @param parent parent window
@@ -167,8 +179,7 @@ public List<File> getSelectedFiles() {
167179
168180 private DefaultTreeModel getTreeModel () {
169181 DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode (new File ("File System" ), true );
170- final DefaultTreeModel treeModel = new DefaultTreeModel (rootNode , true );
171- return treeModel ;
182+ return new DefaultTreeModel (rootNode , true );
172183
173184 }
174185
@@ -178,22 +189,20 @@ private void initTree() {
178189 DefaultTreeModel treeModel = (DefaultTreeModel ) tree .getModel ();
179190 DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode ) treeModel .getRoot ();
180191 File [] roots = fsv .getRoots ();
181- //File[] roots = File.listRoots();
182- for (int i = 0 ; i < roots .length ; i ++) {
183- File rootFile = roots [i ];
192+ for (File rootFile : roots ) {
184193 boolean rootAllowsChildren = rootFile .isDirectory ();
185194 final DefaultMutableTreeNode newChild = new DefaultMutableTreeNode (rootFile , rootAllowsChildren );
186195 if (rootAllowsChildren ) {
187196 rootNode .add (newChild );
188197 }
189-
198+
190199 File [] children = rootFile .listFiles ();
191200 if (children != null ) {
192201 List <File > sortedChildren = ResourceDialogUtil .sortFiles (children );
193-
202+
194203 for (File child : sortedChildren ) {
195- final File [] listFiles = child . listFiles ();
196- if ( child .isDirectory ()) {
204+ if ( shouldTraverse . test ( child )) {
205+ final File [] listFiles = child .listFiles ();
197206 boolean allowsChildren = listFiles != null && listFiles .length > 0 ;
198207 DefaultMutableTreeNode subFolder = new DefaultMutableTreeNode (child , allowsChildren );
199208 newChild .add (subFolder );
@@ -210,7 +219,7 @@ private void initTree() {
210219 tree .addTreeSelectionListener (new FileTreeSelectionListener ());
211220
212221 }
213-
222+
214223 @ SuppressWarnings ("serial" )
215224 private void initTable () {
216225 table .setDefaultRenderer (File .class , new DefaultTableCellRenderer () {
@@ -270,7 +279,6 @@ public void keyPressed(KeyEvent e) {
270279 private String toText (List <File > files ) {
271280 StringBuilder sb = new StringBuilder ();
272281 for (File f : files ) {
273- //sb.append(QUOTE + f.getName() + QUOTE + ' ');
274282 sb .append (QUOTE + fsv .getSystemDisplayName (f ) + QUOTE + ' ' );
275283 }
276284 return sb .toString ();
0 commit comments