From 84e28e2794399c713db58c3766c136801d8b9062 Mon Sep 17 00:00:00 2001
From: apvrille <ludovic.apvrille@eurecom.fr>
Date: Fri, 1 Mar 2019 17:22:58 +0100
Subject: [PATCH] Adding in search box of TTool help elements

---
 src/main/java/help/HelpEntry.java   | 50 +++++++++++++++++++++++++++++
 src/main/java/help/HelpManager.java | 24 +++++++++++++-
 src/main/java/ui/MainGUI.java       |  2 ++
 3 files changed, 75 insertions(+), 1 deletion(-)

diff --git a/src/main/java/help/HelpEntry.java b/src/main/java/help/HelpEntry.java
index bbb09a79eb..efdd84c0a3 100644
--- a/src/main/java/help/HelpEntry.java
+++ b/src/main/java/help/HelpEntry.java
@@ -249,5 +249,55 @@ public class HelpEntry implements GenericTree {
        return getIndexOfKid((HelpEntry)child);
     }
 
+    public HelpEntry getFromMasterKeyword(String search) {
+        if (masterKeyword != null) {
+            if (masterKeyword.compareTo(search) == 0) {
+                return this;
+            }
+        }
+        if (entries != null) {
+            HelpEntry ret;
+            for(HelpEntry he: entries) {
+                ret = he.getFromMasterKeyword(search);
+                if (ret != null) {
+                    return ret;
+                }
+            }
+        }
+        return null;
+    }
+
+    public void addEntries(Vector<HelpEntry> list) {
+        list.add(this);
+        if (entries != null) {
+            for (HelpEntry he : entries) {
+                he.addEntries(list);
+            }
+        }
+    }
+
+    public int hasSimilarWords(String[] words) {
+        int result = 0;
+        for(String s:words) {
+            if (hasKeyword(s)) {
+                result ++;
+            }
+        }
+        return result;
+    }
+
+    public boolean hasKeyword(String word) {
+        for(String s: keywords) {
+            if (s.compareTo(word) == 0) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+
+
+
+
 
 }
diff --git a/src/main/java/help/HelpManager.java b/src/main/java/help/HelpManager.java
index 42fa5b0584..d994e763a3 100644
--- a/src/main/java/help/HelpManager.java
+++ b/src/main/java/help/HelpManager.java
@@ -45,6 +45,7 @@ import java.io.BufferedReader;
 import java.io.File;
 import java.io.InputStreamReader;
 import java.net.URL;
+import java.util.Vector;
 
 
 /**
@@ -61,6 +62,8 @@ public class HelpManager extends HelpEntry {
 
     private boolean helpLoaded = false;
 
+    private Vector<HelpEntry> allEntries;
+
 
     public HelpManager() {
         linkToParent = null;
@@ -123,7 +126,7 @@ public class HelpManager extends HelpEntry {
             return false;
         }
 
-
+        computeAllEntries();
         helpLoaded = true;
 
         return true;
@@ -243,4 +246,23 @@ public class HelpManager extends HelpEntry {
         return top;
     }
 
+    private void computeAllEntries() {
+        allEntries = new Vector<>();
+        addEntries(allEntries);
+
+    }
+
+    public Vector<HelpEntry> getEntriesWithKeyword(String[] words) {
+        Vector<HelpEntry> result = new Vector<>();
+        for(HelpEntry he: allEntries) {
+            int nb = he.hasSimilarWords(words);
+            if (nb > 0) {
+                result.add(he);
+            }
+        }
+        return result;
+    }
+
+
+
 }
diff --git a/src/main/java/ui/MainGUI.java b/src/main/java/ui/MainGUI.java
index 91e7248549..43e36797ba 100644
--- a/src/main/java/ui/MainGUI.java
+++ b/src/main/java/ui/MainGUI.java
@@ -758,6 +758,8 @@ public class MainGUI implements ActionListener, WindowListener, KeyListener, Per
             panel.searchForText(text.toLowerCase(), elements);
         }
 
+        elements.addAll(helpManager.getEntriesWithKeyword(text.split( " ")));
+
         gtm.setElementsOfSearchTree(elements);
         //TraceManager.addDev("Found " + elements.size() + " elements");
         dtree.forceUpdate();
-- 
GitLab