@Test
  public void ensureValueIsUpdatedByCorrectSelectionModelWhenSelectionModelIsChanged() {
    box.getItems().addAll("Apple", "Orange", "Banana");
    SelectionModel sm1 = box.getSelectionModel();
    sm1.select(1);
    assertEquals("Orange", box.getValue());
    SingleSelectionModel sm2 = new ChoiceBox.ChoiceBoxSelectionModel(box);
    box.setSelectionModel(sm2);

    sm1.select(2); // value should not change as we are using old SM
    assertEquals("Orange", box.getValue());

    sm2.select(0); // value should change, as we are using new SM
    assertEquals("Apple", box.getValue());
  }
Example #2
0
 /**
  * Sets the currently selected component, This will result in a change to the selection model.
  *
  * @param sel the <code>Component</code> to select
  * @beaninfo description: The selected component on the popup menu expert: true hidden: true
  */
 public void setSelected(Component sel) {
   SingleSelectionModel model = getSelectionModel();
   int index = getComponentIndex(sel);
   model.setSelectedIndex(index);
 }
Example #3
0
  public MyTab createTab() {

    MyTab tab =
        new MyTab() {
          @Override
          public ButtonType close() {
            if (Objects.nonNull(this.getPath()))
              closedPaths.add(Optional.ofNullable(current.currentTab().getPath()));

            ButtonType closeType = super.close();

            Platform.runLater(
                () -> {
                  ObservableList<Tab> tabs = controller.getTabPane().getTabs();
                  if (tabs.isEmpty()) {
                    controller.newDoc(null);
                  }
                });

            return closeType;
          }
        };

    tab.setOnCloseRequest(
        event -> {
          event.consume();
          tab.close();
        });

    MenuItem menuItem0 = new MenuItem("Close");
    menuItem0.setOnAction(
        actionEvent -> {
          tab.close();
        });

    MenuItem menuItem1 = new MenuItem("Close All");
    menuItem1.setOnAction(
        actionEvent -> {
          ObservableList<Tab> tabs = controller.getTabPane().getTabs();
          ObservableList<Tab> clonedTabs = FXCollections.observableArrayList(tabs);
          if (clonedTabs.size() > 0) {
            clonedTabs.forEach(
                (closedTab) -> {
                  MyTab myTab = (MyTab) closedTab;
                  myTab.close();
                });
          }
        });

    MenuItem menuItem2 = new MenuItem("Close Others");
    menuItem2.setOnAction(
        actionEvent -> {
          ObservableList<Tab> blackList = FXCollections.observableArrayList();
          blackList.addAll(controller.getTabPane().getTabs());

          blackList.remove(tab);

          blackList.forEach(
              t -> {
                MyTab closeTab = (MyTab) t;
                closeTab.close();
              });
        });
    //
    //        MenuItem menuItem3 = new MenuItem("Close Unmodified");
    //        menuItem3.setOnAction(actionEvent -> {
    //
    //            ObservableList<Tab> clonedTabs = FXCollections.observableArrayList();
    //            clonedTabs.addAll(controller.getTabPane().getTabs());
    //
    //
    //            for (Tab clonedTab : clonedTabs) {
    //                MyTab myTab = (MyTab) clonedTab;
    //                if (!myTab.getTabText().contains(" *"))
    //                    threadService.runActionLater(()->{
    //                        myTab.close();
    //                    });
    //            }
    //        });

    MenuItem menuItem4 = new MenuItem("Select Next Tab");
    menuItem4.setOnAction(
        actionEvent -> {
          TabPane tabPane = controller.getTabPane();
          if (tabPane.getSelectionModel().isSelected(tabPane.getTabs().size() - 1))
            tabPane.getSelectionModel().selectFirst();
          else tabPane.getSelectionModel().selectNext();
        });

    MenuItem menuItem5 = new MenuItem("Select Previous Tab");
    menuItem5.setOnAction(
        actionEvent -> {
          SingleSelectionModel<Tab> selectionModel = controller.getTabPane().getSelectionModel();
          if (selectionModel.isSelected(0)) selectionModel.selectLast();
          else selectionModel.selectPrevious();
        });

    MenuItem menuItem6 = new MenuItem("Reopen Closed Tab");
    menuItem6.setOnAction(
        actionEvent -> {
          if (closedPaths.size() > 0) {
            int index = closedPaths.size() - 1;
            closedPaths.get(index).filter(pathResolver::isAsciidoc).ifPresent(this::addTab);
            closedPaths.get(index).filter(pathResolver::isMarkdown).ifPresent(this::addTab);
            closedPaths.get(index).filter(pathResolver::isImage).ifPresent(this::addImageTab);
            closedPaths.remove(index);
          }
        });

    MenuItem menuItem7 = new MenuItem("Open File Location");

    menuItem7.setOnAction(
        event -> {
          current
              .currentPath()
              .ifPresent(
                  path -> {
                    controller
                        .getHostServices()
                        .showDocument(path.getParent().toUri().toASCIIString());
                  });
        });

    MenuItem menuItem8 = new MenuItem("New File");
    menuItem8.setOnAction(controller::newDoc);

    MenuItem gotoWorkdir = new MenuItem("Go to Workdir");
    gotoWorkdir.setOnAction(
        event -> {
          current.currentPath().map(Path::getParent).ifPresent(directoryService::changeWorkigDir);
        });

    ContextMenu contextMenu = new ContextMenu();
    contextMenu
        .getItems()
        .addAll(
            menuItem0,
            menuItem1,
            menuItem2,
            new SeparatorMenuItem(),
            menuItem4,
            menuItem5,
            menuItem6,
            new SeparatorMenuItem(),
            gotoWorkdir,
            new SeparatorMenuItem(),
            menuItem7,
            menuItem8);

    tab.contextMenuProperty().setValue(contextMenu);
    Label label = tab.getLabel();

    label.setOnMouseClicked(
        mouseEvent -> {
          if (mouseEvent.getButton().equals(MouseButton.SECONDARY)) {
            tab.select();
          } else if (mouseEvent.getClickCount() > 1) {
            controller.adjustSplitPane();
          }
        });

    return tab;
  }