我正在使用Justinmind的API开发插件。我需要的基本上是通过ID查找元素。但是,在我搜索整个文档时,API似乎没有像findById()这样的方法。
here。我假设我必须创建一个方法。我在这里解释问题的细节和Justinmind的一般结构,尽管该问题并非特定于Justinmind

假设我具有以下页面布局:

java - 在复杂对象中查找对象-LMLPHP

我将按照以下层次结构在Justinmind中设计此页面:

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


在Justinmind API中,屏幕是ICanvas对象。我将屏幕存储在myScreen变量中。然后,getApiRoot()返回页面的顶部组件。之后,每个组件都是一个子组件,可以通过getApiChildren()方法进行访问。

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


上面的代码返回图像,组和文本,即一阶层次结构组件。例如,我可以按以下方式访问第一个按钮的标签(概述):

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


如您所见,我总是通过getApiChildren()方法访问组件。在大型动态页面中,这不太可行。例如,因为我正在编写的插件会在按钮组中缺少标签时发出警报。为了检查标签的存在,我必须使用getApiChildren()方法遍历屏幕的所有组件。

我可以通过树来表示它:

java - 在复杂对象中查找对象-LMLPHP

但是,这种结构将始终发生变化。我应该建造一棵树还是哈希图?在非常复杂的对象中找到对象的最佳方法是什么?

最佳答案

我不知道Justinmind。但是,如果您在Java中有一个复杂对象图,则可以尝试使用该库http://commons.apache.org/proper/commons-jxpath/并使用XPATH语法指定查询。

09-25 22:14