List of usage examples for com.google.gwt.user.client.ui Widget getParent
public Widget getParent()
From source file:com.vaadin.client.widgets.Grid.java
License:Apache License
private boolean isElementInChildWidget(Element e) { Widget w = WidgetUtil.findWidget(e, null); if (w == this) { return false; }//from w ww . j a va2s. c om /* * If e is directly inside this grid, but the grid is wrapped in a * Composite, findWidget is not going to find this, only the wrapper. * Thus we need to check its parents to see if we encounter this; if we * don't, the found widget is actually a parent of this, so we should * return false. */ while (w != null && w != this) { w = w.getParent(); } return w != null; }
From source file:com.vaadin.client.widgets.Grid.java
License:Apache License
private void attachWidget(Widget w, Element parent) { assert w.getParent() == null; parent.appendChild(w.getElement());//from w ww . j a v a2 s . c o m setParent(w, this); }
From source file:com.vaadin.client.widgets.Grid.java
License:Apache License
private void detachWidget(Widget w) { assert w.getParent() == this; setParent(w, null);/* w w w . ja v a 2 s . c o m*/ w.getElement().removeFromParent(); }
From source file:com.vaadin.client.WidgetUtil.java
License:Apache License
/** * Helper method to find first instance of given Widget type found by * traversing DOM upwards from given element. * <p>/* ww w . j av a 2 s. c o m*/ * <strong>Note:</strong> If {@code element} is inside some widget {@code W} * , <em>and</em> {@code W} in turn is wrapped in a {@link Composite} * {@code C}, this method will not find {@code W}. It returns either * {@code C} or null, depending on whether the class parameter matches. This * may also be the case with other Composite-like classes that hijack the * event handling of their child widget(s). * * @param element * the element where to start seeking of Widget * @param class1 * the Widget type to seek for */ @SuppressWarnings("unchecked") public static <T> T findWidget(Element element, Class<? extends Widget> class1) { if (element != null) { /* First seek for the first EventListener (~Widget) from dom */ EventListener eventListener = null; while (eventListener == null && element != null) { eventListener = Event.getEventListener(element); if (eventListener == null) { element = element.getParentElement(); } } if (eventListener instanceof Widget) { /* * Then find the first widget of type class1 from widget * hierarchy */ Widget w = (Widget) eventListener; while (w != null) { if (class1 == null || w.getClass() == class1) { return (T) w; } w = w.getParent(); } } } return null; }
From source file:com.vaadin.client.WidgetUtil.java
License:Apache License
private static boolean checkVisibilityRecursively(Widget widget) { if (widget.isVisible()) { Widget parent = widget.getParent(); if (parent == null) { return true; // root panel } else {/* ww w. j a v a2 s . c o m*/ return checkVisibilityRecursively(parent); } } else { return false; } }
From source file:com.vaadin.terminal.gwt.client.ComponentLocator.java
License:Open Source License
/** * Generates a String locator which uniquely identifies the target element. * The {@link #getElementByPath(String)} method can be used for the inverse * operation, i.e. locating an element based on the return value from this * method.//ww w . j a va2 s. c o m * <p> * Note that getElementByPath(getPathForElement(element)) == element is not * always true as {@link #getPathForElement(Element)} can return a path to * another element if the widget determines an action on the other element * will give the same result as the action on the target element. * </p> * * @since 5.4 * @param targetElement * The element to generate a path for. * @return A String locator that identifies the target element or null if a * String locator could not be created. */ public String getPathForElement(Element targetElement) { String pid = null; Element e = targetElement; while (true) { pid = client.getPid(e); if (pid != null) { break; } e = DOM.getParent(e); if (e == null) { break; } } Widget w = null; if (pid != null) { // If we found a Paintable then we use that as reference. We should // find the Paintable for all but very special cases (like // overlays). w = (Widget) client.getPaintable(pid); /* * Still if the Paintable contains a widget that implements * SubPartAware, we want to use that as a reference */ Widget targetParent = findParentWidget(targetElement, w); while (targetParent != w && targetParent != null) { if (targetParent instanceof SubPartAware) { /* * The targetParent widget is a child of the Paintable and * the first parent (of the targetElement) that implements * SubPartAware */ w = targetParent; break; } targetParent = targetParent.getParent(); } } if (w == null) { // Check if the element is part of a widget that is attached // directly to the root panel RootPanel rootPanel = RootPanel.get(); int rootWidgetCount = rootPanel.getWidgetCount(); for (int i = 0; i < rootWidgetCount; i++) { Widget rootWidget = rootPanel.getWidget(i); if (rootWidget.getElement().isOrHasChild(targetElement)) { // The target element is contained by this root widget w = findParentWidget(targetElement, rootWidget); break; } } if (w != null) { // We found a widget but we should still see if we find a // SubPartAware implementor (we cannot find the Paintable as // there is no link from VOverlay to its paintable/owner). Widget subPartAwareWidget = findSubPartAwareParentWidget(w); if (subPartAwareWidget != null) { w = subPartAwareWidget; } } } if (w == null) { // Containing widget not found return null; } // Determine the path for the target widget String path = getPathForWidget(w); if (path == null) { /* * No path could be determined for the target widget. Cannot create * a locator string. */ return null; } if (w.getElement() == targetElement) { /* * We are done if the target element is the root of the target * widget. */ return path; } else if (w instanceof SubPartAware) { /* * If the widget can provide an identifier for the targetElement we * let it do that */ String elementLocator = ((SubPartAware) w).getSubPartName(targetElement); if (elementLocator != null) { return path + SUBPART_SEPARATOR + elementLocator; } } /* * If everything else fails we use the DOM path to identify the target * element */ return path + getDOMPathForElement(targetElement, w.getElement()); }
From source file:com.vaadin.terminal.gwt.client.ComponentLocator.java
License:Open Source License
/** * Creates a locator String for the given widget. The path can be used to * locate the widget using {@link #getWidgetFromPath(String)}. * /* w w w . j av a2s. co m*/ * Returns null if no path can be determined for the widget or if the widget * is null. * * @param w * The target widget * @return A String locator for the widget */ private String getPathForWidget(Widget w) { if (w == null) { return null; } String pid = client.getPid(w.getElement()); if (isStaticPid(pid)) { return pid; } if (w instanceof VView) { return ""; } else if (w instanceof VWindow) { VWindow win = (VWindow) w; ArrayList<VWindow> subWindowList = client.getView().getSubWindowList(); int indexOfSubWindow = subWindowList.indexOf(win); return PARENTCHILD_SEPARATOR + "VWindow[" + indexOfSubWindow + "]"; } else if (w instanceof RootPanel) { return ROOT_ID; } Widget parent = w.getParent(); String basePath = getPathForWidget(parent); if (basePath == null) { return null; } String simpleName = Util.getSimpleName(w); /* * Check if the parent implements Iterable. At least VPopupView does not * implement HasWdgets so we cannot check for that. */ if (!(parent instanceof Iterable<?>)) { // Parent does not implement Iterable so we cannot find out which // child this is return null; } Iterator<?> i = ((Iterable<?>) parent).iterator(); int pos = 0; while (i.hasNext()) { Object child = i.next(); if (child == w) { return basePath + PARENTCHILD_SEPARATOR + simpleName + "[" + pos + "]"; } String simpleName2 = Util.getSimpleName(child); if (simpleName.equals(simpleName2)) { pos++; } } return null; }
From source file:com.vaadin.terminal.gwt.client.ui.dd.VDragAndDropManager.java
License:Open Source License
/** * First seeks the widget from this element, then iterates widgets until one * implement HasDropHandler. Returns DropHandler from that. * //from w w w.j a v a 2s . c om * @param element * @return */ private VDropHandler findDragTarget(Element element) { try { Widget w = Util.findWidget((com.google.gwt.user.client.Element) element, null); if (w == null) { return null; } while (!(w instanceof VHasDropHandler)) { w = w.getParent(); if (w == null) { break; } } if (w == null) { return null; } else { VDropHandler dh = ((VHasDropHandler) w).getDropHandler(); return dh; } } catch (Exception e) { // ApplicationConnection.getConsole().log( // "FIXME: Exception when detecting drop handler"); // e.printStackTrace(); return null; } }
From source file:com.vaadin.terminal.gwt.client.ui.dd.VTargetInSubtree.java
License:Open Source License
@Override protected boolean accept(VDragEvent drag, UIDL configuration) { VTree tree = (VTree) VDragAndDropManager.get().getCurrentDropHandler().getPaintable(); TreeNode treeNode = tree.getNodeByKey((String) drag.getDropDetails().get("itemIdOver")); if (treeNode != null) { Widget parent2 = treeNode; int depth = configuration.getIntAttribute("depth"); if (depth < 0) { depth = Integer.MAX_VALUE; }//w w w. ja v a 2s . c o m final String searchedKey = configuration.getStringAttribute("key"); for (int i = 0; i <= depth && parent2 instanceof TreeNode; i++) { if (searchedKey.equals(((TreeNode) parent2).key)) { return true; } // panel -> next level node parent2 = parent2.getParent().getParent(); } } return false; }
From source file:com.vaadin.terminal.gwt.client.ui.ShortcutActionHandler.java
License:Open Source License
private void fireAction(final Event event, final ShortcutAction a, Paintable target) { final Element et = DOM.eventGetTarget(event); if (target == null) { Widget w = Util.findWidget(et, null); while (w != null && !(w instanceof Paintable)) { w = w.getParent(); }//from w w w .j av a2 s . c o m target = (Paintable) w; } final Paintable finalTarget = target; event.preventDefault(); /* * The target component might have unpublished changes, try to * synchronize them before firing shortcut action. */ if (finalTarget instanceof BeforeShortcutActionListener) { ((BeforeShortcutActionListener) finalTarget).onBeforeShortcutAction(event); } else { shakeTarget(et); Scheduler.get().scheduleDeferred(new Command() { public void execute() { shakeTarget(et); } }); } Scheduler.get().scheduleDeferred(new Command() { public void execute() { if (finalTarget != null) { client.updateVariable(paintableId, "actiontarget", finalTarget, false); } client.updateVariable(paintableId, "action", a.getKey(), true); } }); }