@FXML
 private void handleSendButton() {
   if (inputField.getLength() < 1) {
     return;
   }
   String message = inputField.getText();
   printMessageToChat(model.getClientUser().getUsername(), message);
   inputField.setText("");
   connectionManager.sendChatMessage(model.getClientUser().getUsername(), message);
 }
Example #2
0
  @Override
  public void initialize(URL url, ResourceBundle resourceBundle) {
    viewModel.queryProperty().bind(searchTextField.textProperty());
    searchResultsListView.setItems(viewModel.getResults());

    searchTextField.setOnKeyPressed(
        event -> {
          if (event.getCode() == KeyCode.UP || event.getCode() == KeyCode.DOWN) {
            searchResultsListView.requestFocus();
            searchResultsListView.fireEvent(event);
          } else if (event.getCode() == KeyCode.ESCAPE) {
            event.consume();
            viewModel.closeSearch();
          } else if (event.getCode() == KeyCode.ENTER) {
            if (viewModel.queryProperty().get().equals("times new roman")) {
              ObservableList<String> classes =
                  MainController.getPrimaryStage().getScene().getRoot().getStyleClass();
              if (classes.contains("gag")) {
                classes.remove("gag");
              } else {
                classes.add("gag");
              }
            }
            event.consume();
            viewModel.buttonAction(searchResultsListView.getSelectionModel().getSelectedItem());
          }
        });

    searchTextField.setOnKeyReleased(
        event -> {
          viewModel.getSearchCommand().execute();
          searchResultsListView.getSelectionModel().selectFirst();
        });

    searchResultsListView.setOnKeyPressed(
        event -> {
          if (event.getCode() == KeyCode.ESCAPE) {
            event.consume();
            viewModel.closeSearch();
          } else if (event.getCode() == KeyCode.ENTER || event.getCode() == KeyCode.SPACE) {
            event.consume();
            viewModel.buttonAction(searchResultsListView.getSelectionModel().getSelectedItem());
          } else if (!(event.getCode() == KeyCode.UP || event.getCode() == KeyCode.DOWN)) {
            searchTextField.requestFocus();
            searchTextField.positionCaret(searchTextField.getLength());
            searchTextField.fireEvent(event);
          }
        });

    searchResultsListView.setCellFactory(
        param ->
            new ListCell<SearchResult>() {
              @Override
              protected void updateItem(SearchResult item, boolean empty) {
                setPadding(new Insets(0));
                super.updateItem(item, empty);
                if (!empty) {
                  setGraphic(viewModel.generateSearchResultRow(item));
                } else {
                  setGraphic(null);
                  setText("");
                }
              }
            });

    searchResultsListView
        .visibleProperty()
        .bind(Bindings.size(viewModel.getResults()).greaterThan(0));
    searchResultsListView
        .managedProperty()
        .bind(Bindings.size(viewModel.getResults()).greaterThan(0));

    viewModel
        .getResults()
        .addListener(
            (ListChangeListener<? super SearchResult>)
                c -> {
                  if (viewModel.getResults().size() > 0) {
                    searchResultsListView.setPrefHeight(
                        Integer.min(MAX_RESULTS_BEFORE_SCROLLBAR, viewModel.getResults().size())
                                * 30
                            + 10);
                    searchResultsListView
                        .getScene()
                        .getWindow()
                        .setHeight(
                            Integer.min(MAX_RESULTS_BEFORE_SCROLLBAR, viewModel.getResults().size())
                                    * 30
                                + 63);
                  } else {
                    searchResultsListView.getScene().getWindow().setHeight(61);
                  }
                });
  }