I am trying to create a FileFilter that will only allow the user to open a directory that contains a certain file. The use case is that these directories are workspaces that have a file called smart.workspace inside.
Currently my filter is as follows...
class SMARTWorkspaceFilter extends javax.swing.filechooser.FileFilter {
String description = "SMART Workspace";
String fileNameFilter = "smart.workspace";
SMARTWorkspaceFilter() {
}
@Override
public boolean accept(File file) {
log.debug("Testing file: " + file.getName());
if (file.isFile()) {
return false;
}
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
log.debug("Directory: " + f.isDirectory());
log.debug("Name: " + f.getName());
if (f.isDirectory()) {
return true;
}
if (f.getName().equals(fileNameFilter)) {
return true;
}
}
}
return false;
}
@Override
public String getDescription() {
return description;
}
}
Obviously my problem is that to allow the user to navigate to the workspace folder I have to allow for sub directories.
For the file chooser I am using the option DIRECTORIES_ONLY
.
Is it possible to only allow the user to select a directory based on the directories contents?
For example the directory 'workspace' exists at C://Folder1/Folder2/wokspace, I would want to allow the FileChooser to 'start' at C:// and allow the user to navigate to the 'workspace' folder and accept it. The FileChooser shouldn't allow the acceptance of Folder1 or Folder2 but still allow the navigation through Folder1 and Folder2.