cancel
Showing results for 
Search instead for 
Did you mean: 

Sample code-Search fucntionality of a Tree Node element in a Tree Hierarchy

Former Member
0 Kudos

Hi All

We have populated Tree hierarchy structure with Tree UI element which was perfect.

I need sample code on implementing the search functionality in a Tree Structure , for example when i search with any keyword in a search field which contains as part of Tree element in a Tree hierarchy , that particular Tree element in a Tree hierarchy should be highlighted/selected when we click on action of button after entering the search field with related keyword.

If any body would provide the sample code on the same requirement then it would be great help to us.

This sample code of search functionality should work for any number of levels in a Tree hierarchy

Thanks in Advance

Regards

Kalki Reddy

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

I presume that you have created your tree ui element and bind it to a node that contains recursion node in itself.

Now you can create a method with two parameters:

a string param to search an attributes for and iwdnodeelement param wich will be passed recursivle:

something like this:

public boolean nodeSearch( com.electrabel.test.component.wdp.IPrivateTestComponentView.ITreeNodeElement loElement, java.lang.String lsText )

{

if (lsText.equalsIgnoreCase(loElement.getText())){

wdContext.nodeTreeNode().setTreeSelection(loElement);

return true;

}

for (int i = 0; i < loElement.nodeChild().size(); i ++){

if (nodeSearch(loElement.getChildElementAt(i), lsText)){

return true;

}

}

return false;

}

In my case my node's name is 'TreeNode' and it has an attribute named 'text'.

When I call the recursion it is with the top level node:

nodeSearch(wdContext.nodeTreeNode().getTreeNodeElementAt(0), "get this text from the search input field ui element");

Of course, you must check before doing this if there is something in this node, in other case it will throw a null pointer exception and... that is all , if there is a tree element with such name it will be selected, else nothing will happen.

Best regards,

Anton

Former Member
0 Kudos

Hi Anton

Thanks for your reply

Currentlly the code which you had shared was working fine but our requirement is slightly different.

Rathen using "equalsIgnoreCase()" we are using contains() method for searching with the given the word with respect to the entire hierarchy in the tree"

for example : if the entered text for search is "General" then on action of search button then the entered text will compare with all the node elements in the tree hiearchy with contains() method, suppose the entered text "general" contains three tree node elements then it is hightlighting only the last element in the Tree strucuture,

actually our requirement , after entering the "general" and click of action then it has to highlihht the first tree element which matched the key word "General" then next time again when we click on the search button it has to highlight the second tree element in the hiearchy and next time search button it has to highlight the tree element which matches the keyword "General" with contains comparision like that it will complete the search opeartion with given entered ketword till the end of the tree hierarchy and highlight the tree element on click of search button each time

.

Could you please provide the sample code on the same?? currently you had given the sample code with "equalsIgnoreCase" comparision but we are using comparision with " IsText.contains(TreeNodelement)" , when we are using the contains() method for comparision if it returns 3 Node elements but first time on click of the search button it has to highlight first tree element and next time on click of search button it has to highlight the second tree element in the Tree hiearchy until the end of tree hiearchy.

Could you please provide sample code on the same requirement??

Thanks in advance

Regards,

Kalki Reddy

Former Member
0 Kudos

Here is a sample example of how you can achieve this:

First of all you must create two variables (either in the context or between begin-end block of your custom code)

//@@begin others

private boolean startSearch = false;

private IPrivateTestComponentView.ITreeNodeElement moElement = null;

//@@end

And here is what the current recursion must look like:

public boolean nodeSearch(IPrivateTestComponentView.ITreeNodeElement loElement, java.lang.String lsText )

{

//@@begin nodeSearch()

boolean lookThisElement = true;

if (!startSearch) {

if (moElement == null) {

startSearch = true;

}

else {

if (loElement.equals(moElement)){

startSearch = true;

}

lookThisElement = false;

}

}

if (loElement.getText().toLowerCase().indexOf(lsText.toLowerCase()) >= 0 && lookThisElement){

wdContext.nodeTreeNode().setTreeSelection(loElement);

moElement = loElement;

startSearch = false;

return true;

}

for (int i = 0; i < loElement.nodeChild().size(); i ++){

if (nodeSearch(loElement.getChildElementAt(i), lsText)){

return true;

}

}

return false;

//@@end

}

In the treenodeelement variable you saves the current status (where was the last search), on the next button click you first find go to the last element you have found and then begin the search again.

on the button click method:

startSearch = false;

if (!nodeSearch(wdContext.nodeTreeNode().getTreeNodeElementAt(0), "<text from the input field>")){

moElement = null;

//Could put an Information dialog, displaying no text were found.

}

startsearch variable must always be initialized to false before calling the recursion, because it is initialized inside (when to strat looking for the value). moElement must be set to null if there is no result found in the main recursion, if you don't reset it it will continue to look from the last match.

Best regards,

Anton

Former Member
0 Kudos

Hi Anton

Thanks for your reply.

The code which you sent is being used but it didn't work exactly as per our requirement.

1) Currently your code is working like this way : After i incorporated your code i tested the application but it is working as follows. when i entered text called "GENERAL" in the search field and i clicked on the search button then on click of the search button it is highlighting only the first tree element which was having the text "GENERAL" but next time when i click on the search button then it is not highlighting the second Tree element which actually contains the text "GENERAL". but it has to highlight that Tree element also.

When i searched with keyword "GENERAL" , the entire Tree hierarchy contains 3 Tree elements with keyword "GENERAL".

but currently with your code i am highlighting only the first tree element for the first time on click of search button but it is not highlighting the second tree element which contains "GENERAL" on click of search button for the second time etc.

2) Actually Our requirement : When i search with any key word in the search field then on click of search button suppose with the same keyword if there are 3 Tree elements in the Tree hierarchy then first time on click of search button it has to highlight the First Tree element which contains the same keyword then again second time on click of search button then it has to highlight the Second Tree element which contains the same keyword then again third time on click of search button then it has to highlight the Third Tree element which contains the same keyword etc till end of the Tree hiearchy it has to highlight all the Tree elements one by one on click of search button each time.

Please provide the sample code on the same requirement.

Thanks for your reply

Regards

Kalki Reddy

Former Member
0 Kudos

Hi,

What I have write here actually works exactly as your requirement. I'm sure that you have forget something to write in your code. Maybe in your onClick method you forget to set

startSearch = false;

before calling the nodeSearch() method.

However, what I have show you is just an example code of what you can do with tree and recursion nodes. You can search through the tree, you can select the correct tree element, you can start searching the tree from a middle element. This example is not really optimized, if you have a very big tree, it is not the best solution to go through all nodes, again and again. But it is an example and you can start from it and adapt it to your requirements.

Regards,

Anton

Answers (0)