Example usage for javafx.scene.control TreeItem isExpanded

List of usage examples for javafx.scene.control TreeItem isExpanded

Introduction

In this page you can find the example usage for javafx.scene.control TreeItem isExpanded.

Prototype

public final boolean isExpanded() 

Source Link

Document

Returns the expanded state of this TreeItem.

Usage

From source file:com.heliosdecompiler.helios.gui.controller.FileTreeController.java

@FXML
public void initialize() {
    this.rootItem = new TreeItem<>(new TreeNode("[root]"));
    this.root.setRoot(this.rootItem);
    this.root.setCellFactory(new TreeCellFactory<>(node -> {
        if (node.getParent() == null) {
            ContextMenu export = new ContextMenu();

            MenuItem exportItem = new MenuItem("Export");

            export.setOnAction(e -> {
                File file = messageHandler.chooseFile().withInitialDirectory(new File("."))
                        .withTitle(Message.GENERIC_CHOOSE_EXPORT_LOCATION_JAR.format())
                        .withExtensionFilter(new FileFilter(Message.FILETYPE_JAVA_ARCHIVE.format(), "*.jar"),
                                true)//from   w  w w.  j ava 2 s . c om
                        .promptSave();

                OpenedFile openedFile = (OpenedFile) node.getMetadata().get(OpenedFile.OPENED_FILE);

                Map<String, byte[]> clone = new HashMap<>(openedFile.getContents());

                backgroundTaskHelper.submit(
                        new BackgroundTask(Message.TASK_SAVING_FILE.format(node.getDisplayName()), true, () -> {
                            try {
                                if (!file.exists()) {
                                    if (!file.createNewFile()) {
                                        throw new IOException("Could not create export file");
                                    }
                                }

                                try (ZipOutputStream zipOutputStream = new ZipOutputStream(
                                        new FileOutputStream(file))) {
                                    for (Map.Entry<String, byte[]> ent : clone.entrySet()) {
                                        ZipEntry zipEntry = new ZipEntry(ent.getKey());
                                        zipOutputStream.putNextEntry(zipEntry);
                                        zipOutputStream.write(ent.getValue());
                                        zipOutputStream.closeEntry();
                                    }
                                }

                                messageHandler.handleMessage(Message.GENERIC_EXPORTED.format());
                            } catch (IOException ex) {
                                messageHandler.handleException(Message.ERROR_IOEXCEPTION_OCCURRED.format(), ex);
                            }
                        }));
            });

            export.getItems().add(exportItem);
            return export;
        }
        return null;
    }));

    root.addEventHandler(KeyEvent.KEY_RELEASED, event -> {
        if (event.getCode() == KeyCode.ENTER) {
            TreeItem<TreeNode> selected = this.root.getSelectionModel().getSelectedItem();
            if (selected != null) {
                if (selected.getChildren().size() != 0) {
                    selected.setExpanded(!selected.isExpanded());
                } else {
                    getParentController().getAllFilesViewerController().handleClick(selected.getValue());
                }
            }
        }
    });

    Tooltip tooltip = new Tooltip();
    StringBuilder search = new StringBuilder();

    List<TreeItem<TreeNode>> searchContext = new ArrayList<>();
    AtomicInteger searchIndex = new AtomicInteger();

    root.focusedProperty().addListener((observable, oldValue, newValue) -> {
        if (!newValue) {
            tooltip.hide();
            search.setLength(0);
        }
    });

    root.boundsInLocalProperty().addListener((observable, oldValue, newValue) -> {
        Bounds bounds = root.localToScreen(newValue);
        tooltip.setAnchorX(bounds.getMinX());
        tooltip.setAnchorY(bounds.getMinY());
    });

    root.addEventHandler(KeyEvent.KEY_PRESSED, event -> {
        if (tooltip.isShowing() && event.getCode() == KeyCode.UP) {
            if (searchIndex.decrementAndGet() < 0) {
                searchIndex.set(searchContext.size() - 1);
            }
        } else if (tooltip.isShowing() && event.getCode() == KeyCode.DOWN) {
            if (searchIndex.incrementAndGet() >= searchContext.size()) {
                searchIndex.set(0);
            }
        } else {
            return;
        }
        event.consume();

        root.scrollTo(root.getRow(searchContext.get(searchIndex.get())));
        root.getSelectionModel().select(searchContext.get(searchIndex.get()));
    });

    root.addEventHandler(KeyEvent.KEY_TYPED, event -> {
        if (event.getCharacter().charAt(0) == '\b') {
            if (search.length() > 0) {
                search.setLength(search.length() - 1);
            }
        } else if (event.getCharacter().charAt(0) == '\u001B') { //esc
            tooltip.hide();
            search.setLength(0);
            return;
        } else if (search.length() > 0
                || (search.length() == 0 && StringUtils.isAlphanumeric(event.getCharacter()))) {
            search.append(event.getCharacter());
            if (!tooltip.isShowing()) {
                tooltip.show(root.getScene().getWindow());
            }
        }

        if (!tooltip.isShowing())
            return;

        String str = search.toString();
        tooltip.setText("Search for: " + str);

        searchContext.clear();

        ArrayDeque<TreeItem<TreeNode>> deque = new ArrayDeque<>();
        deque.addAll(rootItem.getChildren());

        while (!deque.isEmpty()) {
            TreeItem<TreeNode> item = deque.poll();
            if (item.getValue().getDisplayName().contains(str)) {
                searchContext.add(item);
            }
            if (item.isExpanded() && item.getChildren().size() > 0)
                deque.addAll(item.getChildren());
        }

        searchIndex.set(0);
        if (searchContext.size() > 0) {
            root.scrollTo(root.getRow(searchContext.get(0)));
            root.getSelectionModel().select(searchContext.get(0));
        }
    });

    openedFileController.loadedFiles().addListener((MapChangeListener<String, OpenedFile>) change -> {
        if (change.getValueAdded() != null) {
            updateTree(change.getValueAdded());
        }
        if (change.getValueRemoved() != null) {
            this.rootItem.getChildren()
                    .removeIf(ti -> ti.getValue().equals(change.getValueRemoved().getRoot()));
        }
    });
}

From source file:de.dkfz.roddy.client.fxuiclient.RoddyUIController.java

private TreeItem<FXICCWrapper> isProcessable(TreeItem<FXICCWrapper> currentNode, boolean hideUnprocessable,
        String idFilter, String analysisIDFilter) {
    WildcardFileFilter wffID = new WildcardFileFilter(idFilter);
    WildcardFileFilter wffAID = new WildcardFileFilter(analysisIDFilter);
    FXICCWrapper cWrapper = currentNode.getValue();
    boolean isVisible = false;
    TreeItem<FXICCWrapper> copyOfTreeItem = new TreeItem<>(currentNode.getValue());
    copyOfTreeItem.setExpanded(currentNode.isExpanded());
    //        System.out.println(currentNode.getValue().getID() + " " + currentNode.getChildren().size());
    //At first: Check, if the node has children and if one of those children is visible.
    for (TreeItem<FXICCWrapper> treeItem : currentNode.getChildren()) {
        TreeItem<FXICCWrapper> childVisible = isProcessable(treeItem, hideUnprocessable, idFilter,
                analysisIDFilter);/*from   w w  w .  ja va2 s.c  om*/
        if (childVisible != null)
            copyOfTreeItem.getChildren().add(childVisible);
    }

    //If there are no visible children, then check the node itself.
    if (copyOfTreeItem.getChildren().size() == 0) {
        //            System.out.println(cWrapper.getID());
        //Is this a project node or an analysis node?
        isVisible = wffID.accept(new File(cWrapper.getID()));
        if (!isVisible)
            return null;
        if (cWrapper.isAnalysisWrapper()) {
            isVisible = wffAID.accept(new File(cWrapper.getAnalysisID()));
            if (!isVisible)
                return null;
        } else {
            if (hideUnprocessable) {
                isVisible = false;
                return null;
            } else {
                if (cWrapper.hasAnalyses())
                    return null;
            }
        }
        //            if (isVisible && !cWrapper.hasAnalyses()) {
        //                if (currentNode.getChildren().size() > 0)
        //                    isVisible = false;
        //            }
    } else {
        isVisible = true;
    }

    if (isVisible)
        return copyOfTreeItem;
    return null;
}

From source file:org.eclipse.jubula.rc.javafx.tester.adapter.TreeOperationContext.java

@Override
public boolean isVisible(final Object node) {
    boolean result = EventThreadQueuerJavaFXImpl.invokeAndWait("isVisible", //$NON-NLS-1$
            new Callable<Boolean>() {

                @Override/*from  www  .  jav a  2 s .c o m*/
                public Boolean call() throws Exception {
                    TreeItem<?> item = (TreeItem<?>) node;
                    return item.isExpanded() && ((TreeView<?>) getTree()).isVisible();
                }
            });

    return result;
}

From source file:org.eclipse.jubula.rc.javafx.tester.adapter.TreeOperationContext.java

@Override
public boolean isExpanded(final Object node) {
    boolean result = EventThreadQueuerJavaFXImpl.invokeAndWait("isExpanded", new Callable<Boolean>() { //$NON-NLS-1$

        @Override/*from   w  w w  .j  a  v  a2 s.c om*/
        public Boolean call() throws Exception {
            TreeItem<?> item = (TreeItem<?>) node;
            return item.isExpanded();
        }
    });

    return result;
}

From source file:org.eclipse.jubula.rc.javafx.tester.adapter.TreeOperationContext.java

@Override
public void expandNode(final Object node) {
    scrollNodeToVisible(node);/*w  w w  .j  a  v  a2 s .co m*/
    Object result = EventThreadQueuerJavaFXImpl.invokeAndWait("expandNode", //$NON-NLS-1$
            new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    List<? extends TreeCell> tCells = ComponentHandler.getAssignableFrom(TreeCell.class);
                    for (TreeCell<?> cell : tCells) {
                        TreeItem<?> item = cell.getTreeItem();
                        if (item != null && item.equals(node) && !item.isExpanded()) {
                            TreeView<?> tree = ((TreeView<?>) getTree());
                            // Update the layout coordinates otherwise
                            // we would get old position values
                            tree.layout();
                            return cell.getDisclosureNode();
                        }
                    }
                    return null;
                }
            });
    if (result != null) {
        getRobot().click(result, null, ClickOptions.create().setClickCount(1).setMouseButton(1));
    }
    EventThreadQueuerJavaFXImpl.invokeAndWait("expandNodeCheckIfExpanded", //$NON-NLS-1$
            new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    TreeItem<?> item = (TreeItem<?>) node;
                    if (!((TreeView<?>) getTree()).isDisabled() && !item.isExpanded()) {
                        log.warn("Expand node fallback used for: " //$NON-NLS-1$
                                + item.getValue());

                        item.setExpanded(true);
                    }
                    return null;
                }
            });
}

From source file:org.eclipse.jubula.rc.javafx.tester.adapter.TreeOperationContext.java

@Override
public void collapseNode(final Object node) {
    scrollNodeToVisible(node);/*from  w w  w  .j  a  v a2 s  .  c  o  m*/
    Object result = EventThreadQueuerJavaFXImpl.invokeAndWait("collapseNode", new Callable<Object>() { //$NON-NLS-1$

        @Override
        public Object call() throws Exception {
            List<? extends TreeCell> tCells = ComponentHandler.getAssignableFrom(TreeCell.class);
            for (TreeCell<?> cell : tCells) {
                TreeItem<?> item = cell.getTreeItem();
                if (item != null && item.equals(node) && item.isExpanded()) {
                    TreeView<?> tree = ((TreeView<?>) getTree());
                    // Update the layout coordinates otherwise
                    // we would get old position values
                    tree.layout();
                    return cell.getDisclosureNode();
                }
            }
            return null;
        }
    });
    if (result != null) {
        getRobot().click(result, null, ClickOptions.create().setClickCount(1).setMouseButton(1));
    }
    EventThreadQueuerJavaFXImpl.invokeAndWait("collapseNodeCheckIfCollapsed", new Callable<Void>() { //$NON-NLS-1$

        @Override
        public Void call() throws Exception {
            TreeItem<?> item = (TreeItem<?>) node;
            if (!((TreeView<?>) getTree()).isDisabled() && item.isExpanded()) {
                log.warn("Collapse node fallback used for: " //$NON-NLS-1$
                        + item.getValue());

                item.setExpanded(false);
            }
            return null;
        }
    });
}

From source file:org.eclipse.jubula.rc.javafx.tester.adapter.TreeTableOperationContext.java

@Override
public boolean isVisible(final Object node) {
    boolean result = EventThreadQueuerJavaFXImpl.invokeAndWait("isVisible", //$NON-NLS-1$
            new Callable<Boolean>() {

                @Override/*from  ww  w .j  a v a2 s  .co m*/
                public Boolean call() throws Exception {
                    TreeItem<?> item = (TreeItem<?>) node;
                    return item.isExpanded() && getTree().isVisible();
                }
            });
    return result;
}

From source file:org.eclipse.jubula.rc.javafx.tester.adapter.TreeTableOperationContext.java

@Override
public void expandNode(final Object node) {
    scrollNodeToVisible(node);/*from   w  ww . ja v a2s. c o  m*/
    Object result = EventThreadQueuerJavaFXImpl.invokeAndWait("expandNode", //$NON-NLS-1$
            new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    TreeItem<?> item = (TreeItem<?>) node;
                    TreeTableView<?> treeTable = getTree();
                    List<TreeTableCell> cells = new NodeTraverseHelper<TreeTableCell>()
                            .getInstancesOf(treeTable, TreeTableCell.class);
                    for (TreeTableCell<?, ?> treeTableCell : cells) {
                        // Nullchecks because of the virtual flow cells
                        // are created which might not be associated
                        // with a row or an item
                        TreeTableRow<?> ttRow = treeTableCell.getTreeTableRow();
                        if (ttRow == null) {
                            continue;
                        }
                        TreeItem<?> checkItem = ttRow.getTreeItem();
                        if (checkItem == null) {
                            continue;
                        }
                        if (item != null && checkItem.equals(item) && !item.isExpanded()) {
                            return treeTableCell.getTreeTableRow().getDisclosureNode();
                        }
                    }
                    return null;
                }

            });
    if (result != null) {
        getRobot().click(result, null, ClickOptions.create().setClickCount(1).setMouseButton(1));
    }
    EventThreadQueuerJavaFXImpl.invokeAndWait("expandNodeCheckIfExpanded", //$NON-NLS-1$
            new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    TreeItem<?> item = (TreeItem<?>) node;
                    if (!getTree().isDisabled() && !item.isExpanded()) {
                        log.warn("Expand node fallback used for: " //$NON-NLS-1$
                                + item.getValue());
                        item.setExpanded(true);
                    }
                    return null;
                }
            });
}

From source file:org.eclipse.jubula.rc.javafx.tester.adapter.TreeTableOperationContext.java

@Override
public void collapseNode(final Object node) {
    scrollNodeToVisible(node);/*ww  w . j av a 2 s  .  c  o  m*/
    Object result = EventThreadQueuerJavaFXImpl.invokeAndWait("collapseNode", //$NON-NLS-1$
            new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    TreeItem<?> item = (TreeItem<?>) node;
                    TreeTableView<?> treeTable = getTree();
                    List<TreeTableCell> cells = new NodeTraverseHelper<TreeTableCell>()
                            .getInstancesOf(treeTable, TreeTableCell.class);
                    for (TreeTableCell<?, ?> treeTableCell : cells) {
                        // Nullchecks because of the virtual flow cells
                        // are created which might not be associated
                        // with a row or an item
                        TreeTableRow<?> ttRow = treeTableCell.getTreeTableRow();
                        if (ttRow == null) {
                            continue;
                        }
                        TreeItem<?> checkItem = ttRow.getTreeItem();
                        if (checkItem == null) {
                            continue;
                        }
                        if (item != null && checkItem.equals(item) && item.isExpanded()) {
                            return treeTableCell.getTreeTableRow().getDisclosureNode();
                        }
                    }
                    return null;
                }

            });
    if (result != null) {
        getRobot().click(result, null, ClickOptions.create().setClickCount(1).setMouseButton(1));
    }
    EventThreadQueuerJavaFXImpl.invokeAndWait("collapseNodeCheckIfExpanded", //$NON-NLS-1$
            new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    TreeItem<?> item = (TreeItem<?>) node;
                    if (!getTree().isDisabled() && item.isExpanded()) {
                        log.warn("Expand node fallback used for: " //$NON-NLS-1$
                                + item.getValue());

                        item.setExpanded(true);
                    }
                    return null;
                }
            });
}

From source file:org.jevis.jeconfig.plugin.classes.ClassTree.java

private void getAllExpanded(List<TreeItem<JEVisClass>> list, TreeItem<JEVisClass> item) {
    if (item.isExpanded()) {
        list.add(item);//from   w ww  .j  a v  a 2  s  . c  om
        for (TreeItem<JEVisClass> i : item.getChildren()) {
            getAllExpanded(list, i);
        }
    }
}