How to get the widget's parent Object?

WindowTester allows you to easily create and run unit tests for every GUI they build. It can also be used to generate system level tests.

Moderators: gnebling, Eric Clayberg, Dan Rubel, keertip, Phil Quitslund

How to get the widget's parent Object?

Postby jyotisaxena » Thu Nov 18, 2010 4:24 am

Hi,
I need to fetch the parent for a particular swt.widget object. I know of two methods: WidgetLocatorService.getParent and SWTHierarchyHelper(Display).getParent. Both seem to belong to restricted access. Is there a better way?
jyotisaxena
 
Posts: 12
Joined: Mon Nov 08, 2010 11:04 am

Re: How to get the widget's parent Object?

Postby Phil Quitslund » Fri Nov 19, 2010 9:31 am

Hmmmm.... Ideally, our locators should protect you from needing to traverse the widget graph yourself. What are you trying to do? Why do you need access to the parent?
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Re: How to get the widget's parent Object?

Postby jyotisaxena » Fri Nov 19, 2010 10:05 am

Some of our widgets are not directly labelled, they are encapsulated. Like ComboBox, it is a childWidget of our specified AxCombo type. I have overridden the matcher function where i traverse through the hierarchy, find the immediate sibling label of AxCombo matching the specified widget label. Similarly We have our own matchers for links and text fields too due to encapsulation.
jyotisaxena
 
Posts: 12
Joined: Mon Nov 08, 2010 11:04 am

Re: How to get the widget's parent Object?

Postby Phil Quitslund » Mon Nov 22, 2010 9:35 am

Interesting. Thanks for the context. I would recommend maintaining your own version of such a utility method. Here's some code to get you started. (Note: this could be cleaned up and optimized but you get the idea!)
Code: Select all
// Copyright 2010 Google Inc. All Rights Reserved.

import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.widgets.Caret;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.CoolItem;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Tracker;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.swt.widgets.Widget;



public class WidgetUtils {

  public synchronized Widget getParent(final Widget widget) {

    if (widget instanceof Shell)
      return null; //this preserves some WT finder behavior but may not be what you want...

    if (widget == null || widget.isDisposed())
      return null;

    final Widget[] parent = new Widget[1];

    widget.getDisplay().syncExec(new Runnable() {
      public void run() {
        if (widget instanceof Control)
          parent[0] = ((Control) widget).getParent();
        if (widget instanceof Caret)
          parent[0] = ((Caret) widget).getParent();
        if (widget instanceof Menu)
          parent[0] = ((Menu) widget).getParent();
        if (widget instanceof ScrollBar)
          parent[0] = ((ScrollBar) widget).getParent();
        if (widget instanceof CoolItem)
          parent[0] = ((CoolItem) widget).getParent();
        if (widget instanceof MenuItem)
          parent[0] = ((MenuItem) widget).getParent();
        if (widget instanceof TabItem)
          parent[0] = ((TabItem) widget).getParent();
        if (widget instanceof TableColumn)
          parent[0] = ((TableColumn) widget).getParent();
        if (widget instanceof TableItem)
          parent[0] = ((TableItem) widget).getParent();
        if (widget instanceof ToolItem)
          parent[0] = ((ToolItem) widget).getParent();
        if (widget instanceof TreeItem)
          parent[0] = ((TreeItem) widget).getParent();
        if (widget instanceof DragSource)
          parent[0] = ((DragSource) widget).getControl().getParent();
        if (widget instanceof DropTarget)
          parent[0] = ((DropTarget) widget).getControl().getParent();
        if (widget instanceof Tracker)
          debug("requested the parent of a Tracker- UNFINDABLE");
      }
    });
    return parent[0];
  }

  private static void debug(String msg) {
    System.out.println(msg);
  }

}


Hope this helps!
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Re: How to get the widget's parent Object?

Postby jyotisaxena » Tue Nov 23, 2010 3:48 pm

Yes, it very much helped. Thanks very much. Happy thanksgiving.
jyotisaxena
 
Posts: 12
Joined: Mon Nov 08, 2010 11:04 am

Re: How to get the widget's parent Object?

Postby keertip » Wed Nov 24, 2010 8:33 am

Good to know that worked.
keertip
Moderator
 
Posts: 221
Joined: Thu Mar 15, 2007 10:26 am


Return to Window Tester

Who is online

Users browsing this forum: No registered users and 1 guest

cron