private void work()

in kit/src/main/java/com/oracle/javafx/scenebuilder/kit/editor/panel/library/ImportWindowController.java [392:536]


    private void work() {
        exploringTask = new Task<List<JarReport>>() {

            @Override
            protected List<JarReport> call() throws Exception {
                final List<JarReport> res = new ArrayList<>();
                numOfImportedJar = importFiles.size();
                // The classloader takes in addition all already existing
                // jar files stored in the user lib dir.
                final List<File> allFiles = buildListOfAllFiles(importFiles);
                final URLClassLoader classLoader = getClassLoaderForFiles(allFiles);
                int index = 1;
                for (File file : importFiles) {
                    if (isCancelled()) {
                        updateMessage(I18N.getString("import.work.cancelled"));
                        break;
                    }
                    updateMessage(I18N.getString("import.work.exploring", file.getName()));
//                    System.out.println("[" + index + "/" + max + "] Exploring file " + file.getName()); //NOI18N
                    if (file.isDirectory()) {
                        final FolderExplorer explorer = new FolderExplorer(file.toPath());
                        final JarReport jarReport = explorer.explore(classLoader);
                        res.add(jarReport);
                    }
                    else {
                        final JarExplorer explorer = new JarExplorer(Paths.get(file.getAbsolutePath()));
                        final JarReport jarReport = explorer.explore(classLoader);
                        res.add(jarReport);
                    }
                    updateProgress(index, numOfImportedJar);
                    index++;
                }

                updateProgress(numOfImportedJar, numOfImportedJar);
                updateImportClassLoader(classLoader);
                return res;
            }
        };

        Thread th = new Thread(exploringTask);
        th.setDaemon(true);
        processingProgressIndicator.progressProperty().bind(exploringTask.progressProperty());

        // We typically enter this handler when dropping jar files such as
        // rt.jar from Java Runtime.
        exploringTask.setOnFailed(t -> {
            // See in setOnSucceeded the explanation for the toFront below.
            getStage().toFront();
            updateNumOfItemsLabelAndSelectionToggleState();
        });
        
        // We construct the import list only if exploration of jar files does well.
        // If Cancel is called during the construction of the list then the import
        // window is closed but the construction itself will continue up to the
        // end. Do we want to make it interruptible ?
        exploringTask.setOnSucceeded(t -> {
            assert Platform.isFxApplicationThread();
            // This toFront() might not be necessary because import window is modal
            // and is chained to the document window. Anyway experience showed
            // we need it (FX 8 b106). This is suspicious, to be investigated ...
            // But more tricky is why toFront() is called here. Mind that when toFront()
            // is called while isShowing() returns false isn't effective: that's
            // why toFront called at the end of controllerDidCreateStage() or
            // controllerDidLoadContentFxml() wasn't an option. Below is the
            // earliest place it has been proven effective, at least on my machine.
            getStage().toFront();
            
            try {
                // We get the set of items which are already excluded prior to the current import.
                UserLibrary userLib = ((UserLibrary) libPanelController.getEditorController().getLibrary());
                alreadyExcludedItems = userLib.getFilter();
                
                List<JarReport> jarReportList = exploringTask.get(); // blocking call
                final Callback<ImportRow, ObservableValue<Boolean>> importRequired
                        = row -> row.importRequired();
                importList.setCellFactory(CheckBoxListCell.forListView(importRequired));

                boolean importingGluonControls = false;
                for (JarReport jarReport : jarReportList) {
                    Path file = jarReport.getJar();
                    String jarName = file.getName(file.getNameCount() - 1).toString();
                    StringBuilder sb = new StringBuilder(
                            I18N.getString("log.info.explore." + (Files.isDirectory(file) ? "folder" : "jar") + ".results", jarName))
                            .append("\n");
                    for (JarReportEntry e : jarReport.getEntries()) {
                        sb.append("> ").append(e.toString()).append("\n");
                        if ((e.getStatus() == JarReportEntry.Status.OK) && e.isNode()) {
                            boolean checked = true;
                            final String canonicalName = e.getKlass().getCanonicalName();
                            // If the class we import is already listed as an excluded one
                            // then it must appear unchecked in the list.
                            if (alreadyExcludedItems.contains(canonicalName) || 
                                    artifactsFilter.contains(canonicalName)) {
                                checked = false;
                                if (alreadyExcludedItems.contains(canonicalName)) {
                                    alreadyExcludedItems.remove(canonicalName);
                                }
                            }
                            final ImportRow importRow = new ImportRow(checked, e, null);
                            importList.getItems().add(importRow);
                            importRow.importRequired().addListener((ChangeListener<Boolean>) (ov, oldValue,
                                    newValue) -> {
                                        final int numOfComponentToImport = getNumOfComponentToImport(importList);
                                        updateOKButtonTitle(numOfComponentToImport);
                                        updateSelectionToggleText(numOfComponentToImport);
                                    });
                        } else {
                            if (e.getException() != null) {
                                StringWriter sw = new StringWriter();
                                PrintWriter pw = new PrintWriter(sw);
                                e.getException().printStackTrace(pw);
                                sb.append(">> " + sw.toString());
                            }
                        }
                    }
                    LOGGER.info(sb.toString());

                    if (jarReport.hasGluonControls()) {
                        importingGluonControls = true;
                    }
                }

                if (importingGluonControls) {
                    ImportingGluonControlsAlert alert = new ImportingGluonControlsAlert(owner);
                    alert.showAndWait();
                }

                // Sort based on the simple class name.
                Collections.sort(importList.getItems(), new ImportRowComparator());

                final int numOfComponentToImport = getNumOfComponentToImport(importList);
                updateOKButtonTitle(numOfComponentToImport);
                updateOKCancelDefaultState(numOfComponentToImport);
                updateSelectionToggleText(numOfComponentToImport);
                updateNumOfItemsLabelAndSelectionToggleState();
            } catch (InterruptedException | ExecutionException | IOException ex) {
                getStage().close();
                showErrorDialog(ex);
            }

            unsetProcessing();
        });

        th.start();
    }