@param text Supplier of searchable text.
(Supplier<String> text)
| 38 | * Supplier of searchable text. |
| 39 | */ |
| 40 | public SearchBar(Supplier<String> text) { |
| 41 | setAlignment(Pos.CENTER_LEFT); |
| 42 | setHgap(7); |
| 43 | ColumnConstraints column1 = new ColumnConstraints(); |
| 44 | column1.setPercentWidth(75); |
| 45 | ColumnConstraints column2 = new ColumnConstraints(); |
| 46 | column2.setPercentWidth(25); |
| 47 | getColumnConstraints().addAll(column1, column2); |
| 48 | // TODO: Better search field: |
| 49 | // - Options |
| 50 | // - regex |
| 51 | this.text = text; |
| 52 | getStyleClass().add("context-menu"); |
| 53 | txtSearch.getStyleClass().add("search-field"); |
| 54 | txtSearch.setOnKeyPressed(e -> { |
| 55 | // Check if we've updated the search query |
| 56 | String searchText = e.getText(); |
| 57 | if(!searchText.equals(lastSearchText)) { |
| 58 | dirty = true; |
| 59 | } |
| 60 | lastSearchText = searchText; |
| 61 | // Handle operations |
| 62 | if(e.getCode().getName().equals(KeyCode.ESCAPE.getName())) { |
| 63 | // Escape the search bar |
| 64 | if(onEscape != null) |
| 65 | onEscape.run(); |
| 66 | } else if(e.getCode().getName().equals(KeyCode.ENTER.getName())) { |
| 67 | // Empty check |
| 68 | if (txtSearch.getText().isEmpty()) { |
| 69 | results = null; |
| 70 | return; |
| 71 | } |
| 72 | // Find next |
| 73 | // - Run search if necessary |
| 74 | if(dirty) { |
| 75 | results = search(); |
| 76 | dirty = false; |
| 77 | } |
| 78 | if(onSearch != null && results != null) |
| 79 | onSearch.accept(results); |
| 80 | } |
| 81 | }); |
| 82 | add(txtSearch, 0, 0); |
| 83 | add(lblResults, 1, 0); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @param onSearch |