-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
159 additions
and
490 deletions.
There are no files selected for viewing
118 changes: 118 additions & 0 deletions
118
.../src/main/java/ntnu/systemutvikling/team6/controller/AvailableOrganizationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package ntnu.systemutvikling.team6.controller; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import javafx.event.ActionEvent; | ||
| import javafx.fxml.FXML; | ||
| import javafx.fxml.FXMLLoader; | ||
| import javafx.scene.Parent; | ||
| import javafx.scene.control.TextField; | ||
| import javafx.scene.layout.FlowPane; | ||
| import ntnu.systemutvikling.team6.database.DatabaseManager; | ||
| import ntnu.systemutvikling.team6.models.Charity; | ||
| import ntnu.systemutvikling.team6.models.CharityRegistry; | ||
|
|
||
| public class AvailableOrganizationController { | ||
|
|
||
| @FXML private TextField searchField; | ||
| @FXML private FlowPane cardsContainer; | ||
|
|
||
| private Charity charity; | ||
| private List<Charity> allCharities; | ||
|
|
||
| @FXML | ||
| public void initialize() { | ||
| DatabaseManager db = new DatabaseManager(); | ||
| CharityRegistry charities = db.getCharitiesFromDB(); | ||
| allCharities = charities.getAllCharities(); | ||
|
|
||
| // Start empty and show matching cards when the user types in the search field. | ||
| cardsContainer.getChildren().clear(); | ||
|
|
||
| searchField | ||
| .textProperty() | ||
| .addListener( | ||
| (observable, oldValue, newValue) -> displayCharities(filterCharities(newValue))); | ||
| } | ||
|
|
||
| private List<Charity> filterCharities(String query) { | ||
| List<Charity> matches = new ArrayList<>(); | ||
|
|
||
| query = query.toLowerCase().trim(); | ||
|
|
||
| if (query.isEmpty()) { | ||
| return matches; | ||
| } | ||
|
|
||
| for (Charity charity : allCharities) { | ||
| String name = charity.getName().toLowerCase(); | ||
| String description = charity.getDescription().toLowerCase(); | ||
|
|
||
| if (name.contains(query) || description.contains(query)) { | ||
| matches.add(charity); | ||
| } | ||
| } | ||
| return matches; | ||
| } | ||
|
|
||
| private void displayCharities(List<Charity> charities) { | ||
| cardsContainer.getChildren().clear(); | ||
|
|
||
| for (Charity charity : charities) { | ||
| try { | ||
| FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/organizationCard.fxml")); | ||
| Parent card = loader.load(); | ||
|
|
||
| OrganizationCardController cardController = loader.getController(); | ||
| cardController.setOrganization(charity); | ||
|
|
||
| cardsContainer.getChildren().add(card); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException("Could not load organization card.", e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The method initialize the search in searchbar. | ||
| * @param query | ||
| */ | ||
| @FXML | ||
| public void setInitialSearch(String query) { | ||
| if (query == null || query.isBlank()) { | ||
| return; | ||
| } | ||
|
|
||
| searchField.setText(query); | ||
| } | ||
|
|
||
| /** | ||
| * This method is used to switch to the front page. | ||
| * | ||
| * @param event action event from button click | ||
| */ | ||
| @FXML | ||
| public void switchToFrontPage(ActionEvent event) { | ||
| LoaderScene.LoadScene("FrontPage", event, charity); | ||
| } | ||
|
|
||
| /** | ||
| * This method is used to switch to the charity page for the selected charity. | ||
| * | ||
| * @param event action event from button click | ||
| */ | ||
| public void switchToCharityPage(ActionEvent event) { | ||
| LoaderScene.LoadScene("CharityPage", event, charity); | ||
| } | ||
|
|
||
| /** | ||
| * This method is used to switch to the donation page. | ||
| * | ||
| * @param event action event from button click | ||
| */ | ||
| @FXML | ||
| public void switchToDonationPage(ActionEvent event) { | ||
| LoaderScene.LoadScene("DonationPage", event, charity); | ||
| } | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.