From dc3389f5bfac0575795faa1c172f7d9fe97e3a9b Mon Sep 17 00:00:00 2001
From: tobias <thinkdifferent055@gmail.com>
Date: Fri, 30 Dec 2016 19:18:31 +0100
Subject: [PATCH] Add search handler for pads

---
 .../DesktopMenuToolbarViewController.java     |  3 +-
 .../desktop/DesktopSearchController.java      | 73 ++++++++++++-------
 .../main/MainViewController.java              |  3 +-
 .../de/tobias/playpad/project/Project.java    | 27 ++++++-
 .../main/IMainViewController.java             |  3 +-
 5 files changed, 75 insertions(+), 34 deletions(-)

diff --git a/PlayWall/src/de/tobias/playpad/layout/desktop/DesktopMenuToolbarViewController.java b/PlayWall/src/de/tobias/playpad/layout/desktop/DesktopMenuToolbarViewController.java
index 453d49f8..e33f2211 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 eb061618..92acef47 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 4081a872..a25c1b9c 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 6ed273ba..100a296d 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 5451c138..6cdbcf12 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.
-- 
GitLab