|
| 1 | +package org.gui; |
| 2 | + |
| 3 | +import java.net.URL; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.List; |
| 7 | +import java.util.ResourceBundle; |
| 8 | +import java.util.stream.Collectors; |
| 9 | + |
| 10 | +import javafx.fxml.FXML; |
| 11 | +import javafx.fxml.Initializable; |
| 12 | +import javafx.scene.control.Button; |
| 13 | +import javafx.scene.control.ListView; |
| 14 | +import javafx.scene.control.TextField; |
| 15 | +import javafx.scene.input.KeyEvent; |
| 16 | + |
| 17 | +/** |
| 18 | + * Controller for the Template Set Management GUI |
| 19 | + */ |
| 20 | +public class Controller implements Initializable { |
| 21 | + |
| 22 | + ArrayList<String> templateSets = new ArrayList<>(Arrays.asList("templates-devon4j-tests", "templates-devon4j-utils", |
| 23 | + "crud-openapi-net", "crud-angular-client-app", "crud-ionic-client-app", "rest-documentation")); |
| 24 | + |
| 25 | + @FXML |
| 26 | + public Button clearSearchResultsButton; |
| 27 | + |
| 28 | + @FXML |
| 29 | + public TextField searchBar; |
| 30 | + |
| 31 | + // TODO: Transform to ListView<HBox> |
| 32 | + @FXML |
| 33 | + public ListView<String> searchResultsView; |
| 34 | + |
| 35 | + @FXML |
| 36 | + public void search(KeyEvent event) { |
| 37 | + |
| 38 | + this.searchResultsView.getItems().clear(); |
| 39 | + this.searchResultsView.getItems().addAll(searchTemplateSets(this.searchBar.getText(), this.templateSets)); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Called when clearSearchResultsButton is pressed |
| 44 | + */ |
| 45 | + @FXML |
| 46 | + public void clearSearchResults() { |
| 47 | + |
| 48 | + this.searchBar.clear(); |
| 49 | + this.searchResultsView.getItems().clear(); |
| 50 | + this.searchResultsView.getItems().addAll(this.templateSets); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Initial View |
| 55 | + */ |
| 56 | + @Override |
| 57 | + public void initialize(URL arg0, ResourceBundle arg1) { |
| 58 | + |
| 59 | + this.searchResultsView.getItems().addAll(this.templateSets); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @param text |
| 64 | + * @param templateSets2 |
| 65 | + * @return |
| 66 | + */ |
| 67 | + private List<String> searchTemplateSets(String searchWords, List<String> listOfStrings) { |
| 68 | + |
| 69 | + List<String> searchTemplateSetsArray = Arrays.asList(searchWords.trim().split(" ")); |
| 70 | + |
| 71 | + return listOfStrings.stream().filter(input -> { |
| 72 | + return searchTemplateSetsArray.stream().allMatch(word -> input.toLowerCase().contains(word.toLowerCase())); |
| 73 | + }).collect(Collectors.toList()); |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments