Automatically generate IDs on SWT-Widgets

梦想与她 提交于 2019-12-19 21:23:22

问题


Is there a way to automatically generate IDs on SWT-Widgets so UI-Tests can reference them? I know i can manually set an id using seData but I want to implement this feature for an existing application in a somewhat generic fashion.


回答1:


You can recursively assign IDs for all your shells in your application using Display.getCurrent().getShells(); and Widget.setData();.

Setting the IDs

Shell []shells = Display.getCurrent().getShells();

for(Shell obj : shells) {
    setIds(obj);
}

You have access to all the active (not disposed) Shells in your application with the method Display.getCurrent().getShells(); . You can loop through all children of each Shell and assign an ID to each Control with the method Widget.setData();.

private Integer count = 0;

private void setIds(Composite c) {
    Control[] children = c.getChildren();
    for(int j = 0 ; j < children.length; j++) {
        if(children[j] instanceof Composite) {
            setIds((Composite) children[j]);
        } else {
            children[j].setData(count);
            System.out.println(children[j].toString());
            System.out.println(" '-> ID: " + children[j].getData());
            ++count;
        }
    }
}

If the Control is a Composite it may have controls inside the composite, that's the reason I have used a recursive solution in my example.


Finding Controls by ID

Now, if you like to find a Control in one of your shells I would suggest a similar, recursive, approach:

public Control findControlById(Integer id) {
    Shell[] shells = Display.getCurrent().getShells();

    for(Shell e : shells) {
        Control foundControl = findControl(e, id);
        if(foundControl != null) {
            return foundControl;
        }
    }
    return null;
}

private Control findControl(Composite c, Integer id) {
    Control[] children = c.getChildren();
    for(Control e : children) {
        if(e instanceof Composite) {
            Control found = findControl((Composite) e, id);
            if(found != null) {
                return found;
            }
        } else {
            int value = id.intValue();
            int objValue = ((Integer)e.getData()).intValue();

            if(value == objValue)
                return e;
        }
    }
    return null;
}

With the method findControlById() you can easily find a Control by it's ID.

    Control foundControl = findControlById(12);
    System.out.println(foundControl.toString());

Links

  • SWT API: Widget
  • SWT API: Display


来源:https://stackoverflow.com/questions/13721105/automatically-generate-ids-on-swt-widgets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!