Find an object inside a complex object

159 views Asked by At

I am using Justinmind's API for developing a plugin. What I need is basically finding an element by ID. However, the API does not seem to have a method like findById(), as I searched the whole documentation here. I assume I have to create a method. Here I am explaining details of the problem and general structure in Justinmind, though the problem is not specific to Justinmind

Assume that I have the following page layout:

enter image description here

I would design this page in Justinmind as in the following hieararchy:

Screen
----Image
----Group
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
--------Button
------------Image
------------Label
----Text
----Text
----Text

In Justinmind API, a screen is an ICanvas object. I store the screen in myScreen variable. Then, getApiRoot() returns the topmost component of the page. After that, every component is a child, and can be accessed via getApiChildren() method.

List<IComponent> components = myScreen.getApiRoot().getApiChildren();

The above code returns Image, Group and Texts, i.e. the first order hiearchy components. For example, I can access the first button's label (OVERVIEW) as follows:

IComponent group = components.get(1); //Returns the Group
IComponent button1 = group.getApiChildren().get(0); //Returns the first button group, which is the first child of group
IComponent label1 = button1.getApiChildren().get(1); //Returns the label, which is the second child of the first button group

As you see, I always access a component by getApiChildren() method. And this is not very feasible in large and dynamic pages. Because the plugin I am writing, for example, will give an alert when the a label is missing in a button group. In order to check the label's existence, I have to traverse all the screen's components with getApiChildren() method.

I can represent it via a tree:

enter image description here

However, this structure will always change. Should I build a tree, or a hashmap? What is the best approach to find an object inside a very complex object?

1

There are 1 answers

1
Mr_Thorynque On BEST ANSWER

I don't know justinmind. But if you have a graph of complex object in java you can try this library http://commons.apache.org/proper/commons-jxpath/ and use XPATH syntaxe to specify your query.