Example usage for javafx.scene.control TreeView getSelectionModel

List of usage examples for javafx.scene.control TreeView getSelectionModel

Introduction

In this page you can find the example usage for javafx.scene.control TreeView getSelectionModel.

Prototype

public final MultipleSelectionModel<TreeItem<T>> getSelectionModel() 

Source Link

Document

Returns the currently installed selection model.

Usage

From source file:musicmetadatak1009705.FolderTreeView.java

public VBox treeStack() throws IOException {
    VBox vbox = new VBox();
    File[] drives = File.listRoots();
    ArrayList<File> fileListing;
    /*for (File dir : drives) {
     System.out.println(dir.toString());
     fileListing = restrictingList(dir);
     }*///from  ww w.j a  va 2 s  .c om
    fileListing = restrictingList(new File("D:\\"));

    ArrayList<TreeItem> treeItems = new ArrayList<>();
    for (File dir : drives) {
        //System.out.println(dir.toString());
        treeItems.add(createNode(dir));
    }
    TreeView<File> tree = proxyCreateNode(treeItems);
    vbox.getChildren().add(tree);

    tree.getSelectionModel().selectedItemProperty()
            .addListener((ObservableValue<? extends TreeItem<File>> observable, TreeItem<File> oldValue,
                    TreeItem<File> newValue) -> {
                System.out.println("Selected Text: " + newValue.getValue());
                TreeItem<File> selectedItem = newValue;
                System.out.println("Selected Text: " + selectedItem.getValue());
                try {
                    //FileTreeView fileTreeView = new FileTreeView();
                    System.out.println("File Tree View Event");
                    fileTreeView.GetFilesFromFolder((newValue.getValue().toString()));
                    //fileTreeView.SetFileTreeView();
                } catch (IOException ex) {
                    System.err.println("Unable to pass fileTreeView Value \nPassed Value: "
                            + selectedItem.getValue().toString() + "\r\nError Message:\r\n" + ex.toString());
                    Logger.getLogger(FolderTreeView.class.getName()).log(Level.SEVERE, null, ex);
                }
            } /* @Override
              public void changed(
              ObservableValue<? extends TreeItem<File>> observable, TreeItem<String> old_val, TreeItem<File> new_val) {
              }*/ );
    return vbox;
}

From source file:be.makercafe.apps.makerbench.Main.java

/**
 * Creates the viewer control//from   w ww .  j  a  v a 2  s . c o m
 *
 * @return
 */
private TreeView<String> createViewer() {
    TreeView<String> viewer = new TreeView<String>(setRootFolder(new File(this.pathMakerbenchHome)));
    viewer.setMinWidth(200.0);
    viewer.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
    viewer.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent mouseEvent) {

            if (mouseEvent.getClickCount() == 2) {
                ResourceTreeItem<String> item = (ResourceTreeItem<String>) viewer.getSelectionModel()
                        .getSelectedItem();
                //               System.out.println("Selected Text : " + item.getValue());

                createEditor(item.getValue(), item.getPath(), null);
            } else if (mouseEvent.getButton() == MouseButton.SECONDARY) {
                ResourceTreeItem<String> item = (ResourceTreeItem<String>) viewer.getSelectionModel()
                        .getSelectedItem();
                if (item.getPath().toFile().isFile()) {
                    viewer.getContextMenu().getItems().get(0).setDisable(true);
                    viewer.getContextMenu().getItems().get(1).setDisable(true);
                    viewer.getContextMenu().getItems().get(2).setDisable(true);
                } else {
                    viewer.getContextMenu().getItems().get(0).setDisable(false);
                    viewer.getContextMenu().getItems().get(1).setDisable(false);
                    viewer.getContextMenu().getItems().get(2).setDisable(false);
                }
            }

        }
    });
    viewer.setContextMenu(rootContextMenu);
    viewer.setEditable(false);
    viewer.setShowRoot(false);

    return viewer;
}

From source file:acmi.l2.clientmod.xdat.Controller.java

private PropertySheet createPropertySheet(TreeView<Object> elements) {
    PropertySheet properties = new PropertySheet();
    properties.setSkin(new PropertySheetSkin(properties));

    elements.getSelectionModel().selectedItemProperty().addListener((selected, oldValue, newSelection) -> {
        properties.getItems().clear();//from  ww  w .j  av a2  s .c  o m

        if (newSelection == null)
            return;

        Object obj = newSelection.getValue();

        if (obj instanceof ListHolder)
            return;

        if (!map.containsKey(obj.getClass())) {
            map.put(obj.getClass(), loadProperties(obj));
        }
        List<PropertySheetItem> props = map.get(obj.getClass());
        props.forEach(property -> {
            property.setObject(obj);
            ChangeListener<Object> addToHistory = (observable1, oldValue1, newValue) -> editor.getHistory()
                    .valueChanged(treeItemToScriptString(newSelection), property.getName(), newValue);
            property.addListener(addToHistory);

            selected.addListener(new InvalidationListener() {
                @Override
                public void invalidated(Observable observable) {
                    property.removeListener(addToHistory);
                    observable.removeListener(this);
                }
            });
        });
        properties.getItems().setAll(props);
    });

    return properties;
}

From source file:acmi.l2.clientmod.xdat.Controller.java

private void updateContextMenu(ContextMenu contextMenu, TreeView<Object> elements) {
    contextMenu.getItems().clear();//from ww w .  j  a v a  2  s .c  om

    TreeItem<Object> root = elements.getRoot();
    TreeItem<Object> selected = elements.getSelectionModel().getSelectedItem();

    if (selected == null) {
        if (root != null)
            contextMenu.getItems().add(createAddMenu("Add ..", elements, root));
    } else {
        Object value = selected.getValue();
        if (value instanceof ListHolder) {
            contextMenu.getItems().add(createAddMenu("Add ..", elements, selected));
        } else if (selected.getParent() != null && selected.getParent().getValue() instanceof ListHolder) {
            MenuItem add = createAddMenu("Add to parent ..", elements, selected.getParent());

            MenuItem delete = new MenuItem("Delete");
            delete.setOnAction(event -> {
                ListHolder parent = (ListHolder) selected.getParent().getValue();

                int index = parent.list.indexOf(value);
                editor.getHistory().valueRemoved(treeItemToScriptString(selected.getParent()), index);

                parent.list.remove(index);
                selected.getParent().getChildren().remove(selected);

                elements.getSelectionModel().selectPrevious();
                elements.getSelectionModel().selectNext();
            });
            contextMenu.getItems().addAll(add, delete);
        }
        if (value instanceof ComponentFactory) {
            MenuItem view = new MenuItem("View");
            view.setOnAction(event -> {
                if (value instanceof L2Context)
                    ((L2Context) value).setResources(l2resources.getValue());
                Stage stage = new Stage();
                stage.setTitle(value.toString());
                Scene scene = new Scene(((ComponentFactory) value).getComponent());
                scene.getStylesheets().add(getClass().getResource("l2.css").toExternalForm());
                stage.setScene(scene);
                stage.show();
            });
            contextMenu.getItems().add(view);
        }
    }
}

From source file:acmi.l2.clientmod.xdat.Controller.java

private ContextMenu createContextMenu(TreeView<Object> elements) {
    ContextMenu contextMenu = new ContextMenu();
    InvalidationListener il = observable1 -> updateContextMenu(contextMenu, elements);
    elements.rootProperty().addListener(il);
    elements.getSelectionModel().selectedItemProperty().addListener(il);
    return contextMenu;
}

From source file:acmi.l2.clientmod.xdat.Controller.java

private MenuItem createAddMenu(String name, TreeView<Object> elements, TreeItem<Object> selected) {
    ListHolder listHolder = (ListHolder) selected.getValue();

    MenuItem add = new MenuItem(name);
    add.setOnAction(event -> {/*from   w  w w . ja  va2 s . co  m*/
        Stream<ClassHolder> st = SubclassManager.getInstance().getClassWithAllSubclasses(listHolder.type)
                .stream().map(ClassHolder::new);
        List<ClassHolder> list = st.collect(Collectors.toList());

        Optional<ClassHolder> choice;

        if (list.size() == 1) {
            choice = Optional.of(list.get(0));
        } else {
            ChoiceDialog<ClassHolder> cd = new ChoiceDialog<>(list.get(0), list);
            cd.setTitle("Select class");
            cd.setHeaderText(null);
            choice = cd.showAndWait();
        }
        choice.ifPresent(toCreate -> {
            try {
                IOEntity obj = toCreate.clazz.newInstance();

                listHolder.list.add(obj);
                TreeItem<Object> treeItem = createTreeItem(obj);
                selected.getChildren().add(treeItem);
                elements.getSelectionModel().select(treeItem);
                elements.scrollTo(elements.getSelectionModel().getSelectedIndex());

                editor.getHistory().valueCreated(treeItemToScriptString(selected), toCreate.clazz);
            } catch (ReflectiveOperationException e) {
                log.log(Level.WARNING, String.format("Couldn't instantiate %s", toCreate.clazz.getName()), e);
                Dialogs.show(Alert.AlertType.ERROR, "ReflectiveOperationException", null,
                        "Couldn't instantiate " + toCreate.clazz);
            }
        });
    });

    return add;
}