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?
Moderators: gnebling, Eric Clayberg, Dan Rubel, keertip, Phil Quitslund
// 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);
}
}
Users browsing this forum: No registered users and 1 guest