/** Handle the KeyEvent when DOWN is pressed in TextInput */
 private void handleDown() {
   if (cmdHistory.getPointer() == cmdHistory.getSize() - 1) {
     return;
   } else {
     cmdHistory.incrementPointer();
     textInput.setText(cmdHistory.getCommand());
     textInput.positionCaret(Integer.MAX_VALUE);
   }
 }
Ejemplo n.º 2
0
 @FXML
 protected void handleYearChanged(KeyEvent event) {
   String value = year.getText();
   if (value.matches("^[0-9]*$")) details.year = Integer.parseInt(value);
   else {
     int selectionStart = year.getSelection().getStart();
     year.setText(Integer.toString(details.year));
     year.positionCaret(selectionStart);
   }
 }
  /** Handle the KeyEvent when UP is pressed in TextInput */
  private void handleUp() {

    if (cmdHistory.getPointer() == 0) {
      return;
    } else {
      cmdHistory.decrementPointer();
      textInput.setText(cmdHistory.getCommand());
      textInput.positionCaret(MAX_INT);
    }
  }
Ejemplo n.º 4
0
 // set number to text field
 public void numberButtonClicked(ActionEvent e) {
   if (start) {
     text.setText("0");
     start = false;
   }
   String a;
   a = ((Button) e.getSource()).getText();
   text.setText(text.getText() + a);
   text.positionCaret(text.getText().length());
 }
Ejemplo n.º 5
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);
                  }
                });
  }