private MenuItem menuItem(String title, Runnable r) {
   MenuItem mi = new MenuItem(title);
   mi.setOnAction(
       event -> {
         r.run();
       });
   return mi;
 }
  @FXML
  void initialize() {

    //
    // Defines for event list
    //
    removeButton.setDisable(true);
    editButton.setDisable(true);
    listView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
    listView
        .getSelectionModel()
        .selectedItemProperty()
        .addListener(
            new ChangeListener() {
              @Override
              public void changed(ObservableValue observableValue, Object o, Object o2) {
                // Selected
                if (listView.getSelectionModel().getSelectedItem() == null) {
                  removeButton.setDisable(true);
                  editButton.setDisable(true);
                } else {
                  removeButton.setDisable(false);
                  editButton.setDisable(false);
                }

                //
                if (listView.getItems().size() != 0) {
                  clearButton.setDisable(false);
                } else {
                  clearButton.setDisable(true);
                }
              }
            });

    //
    // Defines for date picker
    //
    datePicker.setOnAction(
        new EventHandler<ActionEvent>() {
          @Override
          public void handle(ActionEvent actionEvent) {
            listView.getItems().clear();

            List<EventController.EventInfo> list =
                EventController._eventMap.get(datePicker.getValue());
            if (list == null) {
              // No items
            } else {

              // Sorting by time
              TreeMap<String, String> sortedMap = new TreeMap<String, String>();
              for (EventController.EventInfo info : list) {
                sortedMap.put(info.Time, info.Description);
              }

              for (Map.Entry<String, String> entry : sortedMap.entrySet()) {
                mainApplication.addEvent(entry.getKey(), entry.getValue());
              }
            }
          }
        });

    //
    // Defines for clear button
    //
    clearButton.setOnAction(
        new EventHandler<ActionEvent>() {
          @Override
          public void handle(ActionEvent actionEvent) {
            List<EventController.EventInfo> list =
                EventController._eventMap.get(datePicker.getValue());
            if (list == null) {
              // No items
            } else {
              listView.getItems().clear();
              list.clear();
            }
          }
        });

    //
    // Defines for add button
    //
    addButton.setOnAction(
        new EventHandler<ActionEvent>() {
          @Override
          public void handle(ActionEvent actionEvent) {
            mainApplication.displayAdd();
          }
        });

    //
    // Defines for remove button
    //
    removeButton.setOnAction(
        new EventHandler<ActionEvent>() {
          @Override
          public void handle(ActionEvent actionEvent) {
            // Not null
            TempClass selectedTemp = itemMap.get(listView.getSelectionModel().getSelectedItem());
            if (listView.getSelectionModel().getSelectedItem() != null) {
              listView.getItems().remove(listView.getSelectionModel().getSelectedItem());
              EventController.RemoveEvent(
                  datePicker.getValue(), selectedTemp.Time, selectedTemp.Description);
            }
          }
        });

    //
    // Defines for edit button
    //
    editButton.setOnAction(
        new EventHandler<ActionEvent>() {
          @Override
          public void handle(ActionEvent actionEvent) {
            // Not null
            Object selectedItem = listView.getSelectionModel().getSelectedItem();
            if (selectedItem != null) {
              TempClass temp = itemMap.get(selectedItem);
              mainApplication.displayEdit(
                  datePicker.getValue(), temp.Time, temp.Description, selectedItem);
            }
          }
        });
  }
  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;
  }