diff --git a/PlayWall/src/de/tobias/playpad/layout/desktop/DesktopMenuToolbarViewController.java b/PlayWall/src/de/tobias/playpad/layout/desktop/DesktopMenuToolbarViewController.java index 453d49f820e3171e35b4c2bedb2172ae25a8a5d6..e33f2211b23a30989c405b797c0caf5d18942f24 100644 --- a/PlayWall/src/de/tobias/playpad/layout/desktop/DesktopMenuToolbarViewController.java +++ b/PlayWall/src/de/tobias/playpad/layout/desktop/DesktopMenuToolbarViewController.java @@ -672,7 +672,8 @@ public class DesktopMenuToolbarViewController extends BasicMenuToolbarViewContro field.setPromptText(Localization.getString(Strings.Search_Placeholder)); Button button = new Button(Localization.getString(Strings.Search_Button)); - button.setOnAction(new DesktopSearchController(field, this)); + Project project = PlayPadMain.getProgramInstance().getCurrentProject(); + button.setOnAction(new DesktopSearchController(project, field, mainViewController)); HBox box = new HBox(14, field, button); box.setAlignment(Pos.CENTER_LEFT); diff --git a/PlayWall/src/de/tobias/playpad/layout/desktop/DesktopSearchController.java b/PlayWall/src/de/tobias/playpad/layout/desktop/DesktopSearchController.java index eb061618c9bf592c3b26977bfbc55e104701b7c0..92acef47e403c4ec10f92c03afa0087a9bc3f2c1 100644 --- a/PlayWall/src/de/tobias/playpad/layout/desktop/DesktopSearchController.java +++ b/PlayWall/src/de/tobias/playpad/layout/desktop/DesktopSearchController.java @@ -1,6 +1,13 @@ package de.tobias.playpad.layout.desktop; -import de.tobias.utils.ui.Alertable; +import java.util.List; + +import de.tobias.playpad.PlayPadMain; +import de.tobias.playpad.Strings; +import de.tobias.playpad.pad.Pad; +import de.tobias.playpad.project.Project; +import de.tobias.playpad.viewcontroller.main.IMainViewController; +import de.tobias.utils.util.Localization; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.control.TextField; @@ -8,40 +15,52 @@ import javafx.scene.control.TextField; // TODO Search Pads public class DesktopSearchController implements EventHandler<ActionEvent> { + private static final int HIGHLIGHT_DURATION = 3; + + private Project currentProject; + private TextField textField; - private Alertable alertable; + private IMainViewController mainView; + + public DesktopSearchController(Project project, TextField textField, IMainViewController mainView) { + this.currentProject = project; - public DesktopSearchController(TextField textField, Alertable alertable) { this.textField = textField; - this.alertable = alertable; + this.mainView = mainView; } + // Current Search + private String lastSearchTerm; private int currentIndex = 0; + private List<Pad> searchResult; @Override public void handle(ActionEvent event) { -// if (textField.getText().isEmpty()) { -// return; -// } -// -// Project currentProject = PlayPadMain.getProgramInstance().getCurrentProject(); -// main: for (int i = currentIndex; i < currentProject.getPadCount(); i++) { -// Pad pad = currentProject.getPad(i); -// if (pad.getStatus() != PadStatus.EMPTY) { -// if (pad.getName().startsWith(textField.getText())) { -// while (pad.getController() == null) { -// if (!PlayPadPlugin.getImplementation().getMainViewController() -// .showPage(PlayPadPlugin.getImplementation().getMainViewController().getPage() + 1)) { -// break main; -// } -// } -// pad.getController().getView().highlightView(3); -// currentIndex = i + 1; -// return; -// } -// } -// } -// alertable.showInfoMessage(Localization.getString(Strings.Search_Alert_NoMatches), PlayPadMain.stageIcon.orElse(null)); -// currentIndex = 0; + String currentSearchTerm = textField.getText(); + if (currentSearchTerm.isEmpty()) { + return; + } + + // New Search + if (!currentSearchTerm.equals(lastSearchTerm)) { + this.lastSearchTerm = currentSearchTerm; + searchResult = currentProject.findPads(currentSearchTerm); + currentIndex = 0; + } + + if (searchResult.isEmpty()) { + mainView.showInfoMessage(Localization.getString(Strings.Search_Alert_NoMatches), PlayPadMain.stageIcon.orElse(null)); + } + + if (currentIndex < searchResult.size()) { + Pad result = searchResult.get(currentIndex++); + mainView.showPage(result.getPage()); + if (result.getController() != null) { + result.getController().getView().highlightView(HIGHLIGHT_DURATION); + } + } else { + mainView.showInfoMessage(Localization.getString(Strings.Search_Alert_NoMatches), PlayPadMain.stageIcon.orElse(null)); + currentIndex = 0; + } } } diff --git a/PlayWall/src/de/tobias/playpad/viewcontroller/main/MainViewController.java b/PlayWall/src/de/tobias/playpad/viewcontroller/main/MainViewController.java index 4081a87251aa332111f1ba6b9a407fd86cbac16b..a25c1b9c63eaf9656c83d2f0e22b27cee3e9b599 100644 --- a/PlayWall/src/de/tobias/playpad/viewcontroller/main/MainViewController.java +++ b/PlayWall/src/de/tobias/playpad/viewcontroller/main/MainViewController.java @@ -36,7 +36,6 @@ import de.tobias.playpad.view.main.MainLayoutHandler; import de.tobias.playpad.viewcontroller.dialog.ErrorSummaryDialog; import de.tobias.playpad.viewcontroller.dialog.SaveDialog; import de.tobias.utils.ui.BasicControllerSettings; -import de.tobias.utils.ui.NotificationHandler; import de.tobias.utils.ui.ViewController; import de.tobias.utils.ui.scene.NotificationPane; import de.tobias.utils.util.Localization; @@ -69,7 +68,7 @@ import javafx.stage.Modality; import javafx.stage.Screen; import javafx.stage.Stage; -public class MainViewController extends ViewController implements IMainViewController, NotificationHandler, ProfileListener { +public class MainViewController extends ViewController implements IMainViewController, ProfileListener { private static final int FIRST_PAGE = 0; diff --git a/PlayWallCore/src/de/tobias/playpad/project/Project.java b/PlayWallCore/src/de/tobias/playpad/project/Project.java index 6ed273ba2ef1b6ced6be424cd3ebfc3606033cad..100a296d8ac438e3a7fd450570b7339be4a4afb2 100644 --- a/PlayWallCore/src/de/tobias/playpad/project/Project.java +++ b/PlayWallCore/src/de/tobias/playpad/project/Project.java @@ -110,9 +110,9 @@ public class Project { public void setPad(PadIndex index, Pad pad) { if (pad != null) { + // Remove Pad from old location if (pad.getPage() != index.getPage()) { Page oldPage = getPage(pad.getPage()); - // Nur Löschen, wenn auch noch das Pad an dieser Stelle ist, und nicht an andere Stelle if (oldPage.getPad(pad.getIndex()).equals(pad)) { oldPage.removePade(index.getId()); } @@ -322,10 +322,31 @@ public class Project { if (pages.size() == ProjectSettings.MAX_PAGES) { return false; } - int index = pages.size(); - page.setId(index); + + int newIndex = pages.size(); + + page.setId(newIndex); pages.add(page); + return true; + } + /** + * Find pads, which name starts with a given string + * + * @param name + * search key + * @return found pads in project + */ + public List<Pad> findPads(String name) { + List<Pad> result = new ArrayList<>(); + for (Pad pad : getPads()) { + if (pad.getStatus() != PadStatus.EMPTY) { + if (pad.getName().startsWith(name)) { + result.add(pad); + } + } + } + return result; } } diff --git a/PlayWallCore/src/de/tobias/playpad/viewcontroller/main/IMainViewController.java b/PlayWallCore/src/de/tobias/playpad/viewcontroller/main/IMainViewController.java index 5451c138c78e59a849766ce2ba5d41d700531c68..6cdbcf12808a8620c7f69d7b4e2c213faf6e7dd1 100644 --- a/PlayWallCore/src/de/tobias/playpad/viewcontroller/main/IMainViewController.java +++ b/PlayWallCore/src/de/tobias/playpad/viewcontroller/main/IMainViewController.java @@ -7,6 +7,7 @@ import de.tobias.playpad.pad.view.IPadView; import de.tobias.playpad.settings.keys.KeyCollection; import de.tobias.playpad.view.main.MainLayoutConnect; import de.tobias.playpad.view.main.MainLayoutHandler; +import de.tobias.utils.ui.Alertable; import de.tobias.utils.ui.NotificationHandler; import de.tobias.utils.ui.scene.NotificationPane; import javafx.event.Event; @@ -26,7 +27,7 @@ import javafx.stage.Stage; * @since 5.1.0 * */ -public interface IMainViewController extends NotificationHandler { +public interface IMainViewController extends NotificationHandler, Alertable { /** * Setzt die Grid Farbe.