/**
   * Searches recursively all files in the specified directory. <br>
   * <br>
   * All {@link File}s, which match the {@link FileFilter} that comes with the {@link
   * EntryFromFileCreatorManager}, are taken into the resulting tree. <br>
   * <br>
   * The result will be a tree structure of nodes of the type {@link CheckableTreeNode}. <br>
   * <br>
   * The user objects that are attached to the nodes is the {@link FileNodeWrapper}, which wrapps
   * the {@link File}-Object. <br>
   * <br>
   * For ensuring the capability to cancel the work of this recursive method, the first position in
   * the integer array 'state' must be set to 1, to keep the recursion running. When the states
   * value changes, the methode will resolve its recursion and return what it has saved so far.
   */
  public CheckableTreeNode searchDirectory(
      File directory, UnlinkedPDFFileFilter ff, int[] state, ChangeListener changeListener) {
    /* Cancellation of the search from outside! */
    if (state == null || state.length < 1 || state[0] != 1) {
      return null;
    }
    /* Return null if the directory is not valid. */
    if (directory == null || !directory.exists() || !directory.isDirectory()) {
      return null;
    }

    File[] files = directory.listFiles(ff);
    CheckableTreeNode root = new CheckableTreeNode(null);

    int filesCount = 0;

    File[] subDirectories = directory.listFiles(directoryFilter);
    for (int i = 0; i < subDirectories.length; i++) {
      CheckableTreeNode subRoot = searchDirectory(subDirectories[i], ff, state, changeListener);
      if (subRoot != null && subRoot.getChildCount() > 0) {
        filesCount += ((FileNodeWrapper) subRoot.getUserObject()).fileCount;
        root.add(subRoot);
      }
    }

    root.setUserObject(new FileNodeWrapper(directory, files.length + filesCount));

    for (int i = 0; i < files.length; i++) {
      root.add(new CheckableTreeNode(new FileNodeWrapper(files[i])));
      if (changeListener != null) changeListener.stateChanged(new ChangeEvent(this));
    }

    return root;
  }
  @Test
  @Ignore
  public void testInsertTestData() throws Exception {

    entry1 = new BibtexEntry();
    JabRefPreferences jabRefPreferences = JabRefPreferences.getInstance();
    ExternalFileType fileType = jabRefPreferences.getExternalFileTypeByExt("PDF");
    FileListEntry fileListEntry =
        new FileListEntry("", ImportDataTest.FILE_IN_DATABASE.getAbsolutePath(), fileType);

    FileListTableModel model = new FileListTableModel();
    model.addEntry(0, fileListEntry);

    entry1.setField("file", model.getStringRepresentation());

    database.insertEntry(entry1);

    // #################### SETUP END ##################### //

    UnlinkedFilesCrawler crawler = new UnlinkedFilesCrawler(database);
    CheckableTreeNode treeNode =
        crawler.searchDirectory(ImportDataTest.EXISTING_FOLDER, new EntryFromPDFCreator());

    Assert.assertNotNull(treeNode);

    /** Select all nodes manually. */
    @SuppressWarnings("unchecked")
    Enumeration<CheckableTreeNode> enumeration = treeNode.breadthFirstEnumeration();
    while (enumeration.hasMoreElements()) {
      CheckableTreeNode nextElement = enumeration.nextElement();
      nextElement.setSelected(true);
    }

    List<File> resultList = getFileListFromNode(treeNode);

    Assert.assertFalse(resultList.isEmpty());
    Assert.assertTrue(resultList.contains(ImportDataTest.FILE_NOT_IN_DATABASE));
    Assert.assertFalse(resultList.contains(ImportDataTest.FILE_IN_DATABASE));
  }