List of usage examples for com.vaadin.client WidgetUtil findWidget
@SuppressWarnings("unchecked") public static <T> T findWidget(Element element, Class<? extends Widget> class1)
From source file:com.haulmont.cuba.web.widgets.client.addons.dragdroplayouts.ui.panel.VDDPanel.java
License:Apache License
/** * Updates the drop details while dragging. This is needed to ensure client * side criterias can validate the drop location. * /* w w w . j a va 2 s . c o m*/ * @param event * The drag event */ protected void updateDragDetails(VDragEvent event) { Element over = event.getElementOver(); Widget content = WidgetUtil.findWidget(over, null); if (content != null && content != this) { event.getDropDetails().put(Constants.DROP_DETAIL_OVER_CLASS, content.getClass().getName()); } else { event.getDropDetails().put(Constants.DROP_DETAIL_OVER_CLASS, this.getClass().getName()); } // Add mouse event details MouseEventDetails details = MouseEventDetailsBuilder.buildMouseEventDetails(event.getCurrentGwtEvent(), getElement()); event.getDropDetails().put(Constants.DROP_DETAIL_MOUSE_EVENT, details.serialize()); }
From source file:com.haulmont.cuba.web.widgets.client.addons.dragdroplayouts.ui.VDragDropUtil.java
License:Apache License
/** * Creates a transferable from a mouse down event. Returns null if creation * was not successful./*from ww w. j ava 2 s . c om*/ * * @param event * The mouse down event * @param root * The root layout from where the component is dragged * @return A transferable or NULL if something failed */ public static VTransferable createLayoutTransferableFromMouseDown(NativeEvent event, Widget root, Widget target) { // NPE check if (target == null) { VConsole.error("Could not find widget"); return null; } VConsole.log("Creating transferable for root:" + root.getElement() + "\t target:" + target.getElement()); // Special treatment for Tabsheet if (root instanceof VDDTabSheet) { VDDTabSheet tabsheet = (VDDTabSheet) root; TabCaption tab = WidgetUtil.findWidget(target.getElement(), TabCaption.class); if (tab != null && tabsheet.getElement().isOrHasChild(tab.getElement())) { return createTabsheetTransferableFromMouseDown(tabsheet, tab, event); } else { // Not a tab VConsole.error("Not on tab"); return null; } } // Special treatment for Accordion if (root instanceof VDDAccordion) { VDDAccordion accordion = (VDDAccordion) root; StackItem tab = WidgetUtil.findWidget(target.getElement(), StackItem.class); if (tab != null && accordion.getElement().isOrHasChild(tab.getElement())) { return createAccordionTransferableFromMouseDown(accordion, tab, event); } else { // Not on tab VConsole.error("Not on tab"); return null; } } // Ensure we have the right widget target = getTransferableWidget(target); // Find the containing layout of the component ComponentConnector widgetConnector = Util.findConnectorFor(target); if (widgetConnector == null) { VConsole.error("No connector found for " + target); return null; } // Iterate until parent either is the root or a layout with drag and // drop enabled ComponentConnector layoutConnector = (ComponentConnector) widgetConnector.getParent(); Widget layout = layoutConnector.getWidget(); while (layout != root && layout != null && layoutConnector != null) { if (isDraggingEnabled(layoutConnector, target)) { // Found parent layout with support for drag and drop break; } target = layout; widgetConnector = layoutConnector; layoutConnector = (ComponentConnector) layoutConnector.getParent(); if (layoutConnector == null) { break; } layout = layoutConnector.getWidget(); } // Consistency check if (target == null) { VConsole.error("Target was null"); return null; } if (root == target) { /* * Dispatch event again so parent layout can handle the drag of the * root */ target.getElement().dispatchEvent(createMouseDownEvent(event)); return null; } if (layoutConnector == null) { VConsole.error("No layout connector was found"); return null; } return createTransferable(layoutConnector, widgetConnector, event); }
From source file:com.haulmont.cuba.web.widgets.client.addons.dragdroplayouts.ui.verticallayout.VDDVerticalLayoutDropHandler.java
License:Apache License
@Override protected Slot getSlot(Element e, NativeEvent event) { Slot slot = null;//from w w w . j a v a 2s . co m if (getLayout().getElement() == e) { // Most likely between components, use the closest one in that case slot = findSlotVertically(12, event); } else { slot = WidgetUtil.findWidget(e, Slot.class); if (slot == null) { return null; } VAbstractOrderedLayout layout = VDragDropUtil.getSlotLayout(slot); while (layout != getLayout() && getLayout().getElement().isOrHasChild(e.getParentElement())) { e = e.getParentElement(); slot = WidgetUtil.findWidget(e, Slot.class); if (slot == null) { return null; } layout = VDragDropUtil.getSlotLayout(slot); } } return slot; }
From source file:com.haulmont.cuba.web.widgets.client.addons.dragdroplayouts.ui.VLayoutDragDropMouseHandler.java
License:Apache License
/** * Initiates the drag only on the first move event * * @param originalEvent//from w w w . j a v a2 s . c o m * the original Mouse Down event. Only events on elements are * passed in here (Element.as() is safe without check here) */ protected void initiateDragOnMove(final NativeEvent originalEvent) { EventTarget eventTarget = originalEvent.getEventTarget(); boolean stopEventPropagation = false; Element targetElement = Element.as(eventTarget); Widget target = WidgetUtil.findWidget(targetElement, null); Widget targetParent = target.getParent(); // Stop event propagation and prevent default behaviour if // - target is *not* a VTabsheet.TabCaption or // - drag mode is caption mode and widget is caption boolean isTabCaption = targetParent instanceof VTabsheet.TabCaption; boolean isCaption = VDragDropUtil.isCaptionOrCaptionless(targetParent); if (dragMode == LayoutDragMode.CLONE && isTabCaption == false) { stopEventPropagation = true; // overwrite stopEventPropagation flag again if // - root implements VHasDragFilter but // - target is not part of its drag filter and // - target is not a GWT Label based widget if (root instanceof VHasDragFilter) { if (((VHasDragFilter) root).getDragFilter().isDraggable(target) == false && (target instanceof LabelBase) == false) { stopEventPropagation = false; } } if (root instanceof VHasGrabFilter) { VGrabFilter grabFilter = ((VHasGrabFilter) root).getGrabFilter(); if (grabFilter != null && !grabFilter.canBeGrabbed(root, target)) { return; } } } if (dragMode == LayoutDragMode.CAPTION && isCaption) { stopEventPropagation = true; } if (isElementNotDraggable(targetElement)) { stopEventPropagation = false; } if (stopEventPropagation) { originalEvent.stopPropagation(); originalEvent.preventDefault(); // Manually focus as preventDefault() will also cancel focus targetElement.focus(); } mouseDownHandlerReg = Event.addNativePreviewHandler(new NativePreviewHandler() { @Override public void onPreviewNativeEvent(NativePreviewEvent event) { int type = event.getTypeInt(); if (type == Event.ONMOUSEUP || type == Event.ONTOUCHCANCEL || type == Event.ONTOUCHEND) { mouseDownHandlerReg.removeHandler(); mouseDownHandlerReg = null; } else if (type == Event.ONMOUSEMOVE || type == Event.ONTOUCHMOVE) { mouseDownHandlerReg.removeHandler(); mouseDownHandlerReg = null; initiateDrag(originalEvent); } } }); }
From source file:com.haulmont.cuba.web.widgets.client.addons.dragdroplayouts.ui.VLayoutDragDropMouseHandler.java
License:Apache License
/** * Called when the dragging a component should be initiated by both a mouse * down event as well as a touch start event * * FIXME This method is a BIG hack to circumvent Vaadin's very poor client * side API's. This will break often. Refactor once Vaadin gets a grip. * * @param event//from www . j av a2s .com */ protected void initiateDrag(NativeEvent event) { // Check that dragging is enabled if (dragMode == LayoutDragMode.NONE) { return; } // Dragging can only be done with left mouse button and no modifier keys if (!isMouseDragEvent(event) && !Util.isTouchEvent(event)) { return; } // Get target widget EventTarget eventTarget = event.getEventTarget(); Element targetElement = Element.as(eventTarget); Widget target = WidgetUtil.findWidget(targetElement, null); if (isEventOnScrollBar(event)) { return; } // do not drag close button of TabSheet tab if (isElementNotDraggable(targetElement)) { VDragAndDropManager.get().interruptDrag(); return; } // Abort if drag mode is caption mode and widget is not a caption boolean isPanelCaption = target instanceof VPanel && targetElement.getParentElement().getClassName().contains("v-panel-caption"); boolean isCaption = isPanelCaption || VDragDropUtil.isCaptionOrCaptionless(target); if (dragMode == LayoutDragMode.CAPTION && !isCaption) { /* * Ensure target is a caption in caption mode */ return; } if (dragMode == LayoutDragMode.CAPTION && isCaption) { /* * Ensure that captions in nested layouts don't get accepted if in * caption mode */ Widget w = VDragDropUtil.getTransferableWidget(target); ComponentConnector c = Util.findConnectorFor(w); ComponentConnector parent = (ComponentConnector) c.getParent(); if (parent.getWidget() != root) { return; } } // Create the transfarable VTransferable transferable = VDragDropUtil.createLayoutTransferableFromMouseDown(event, root, target); // Are we trying to drag the root layout if (transferable == null) { VConsole.log("Creating transferable on mouse down returned null"); return; } // Resolve the component final Widget w; ComponentConnector c = null, parent = null; if (target instanceof TabCaption) { TabCaption tabCaption = (TabCaption) target; Tab tab = tabCaption.getTab(); int tabIndex = ((ComplexPanel) tab.getParent()).getWidgetIndex(tab); VTabsheet tabsheet = tab.getTabsheet(); w = tab; c = tabsheet.getTab(tabIndex); parent = Util.findConnectorFor(tabsheet); } else if (root instanceof VDDAccordion) { w = target; parent = Util.findConnectorFor(root); StackItem tab = WidgetUtil.findWidget(targetElement, StackItem.class); if (tab != null && root.getElement().isOrHasChild(tab.getElement())) { c = ((VDDAccordion) root).getTab(((VDDAccordion) root).getTabPosition(tab)); } } else if (transferable.getData(Constants.TRANSFERABLE_DETAIL_COMPONENT) != null) { ComponentConnector connector = (ComponentConnector) transferable .getData(Constants.TRANSFERABLE_DETAIL_COMPONENT); w = connector.getWidget(); c = Util.findConnectorFor(w); parent = (ComponentConnector) c.getParent(); } else { // Failsafe if no widget was found w = root; c = Util.findConnectorFor(w); parent = (ComponentConnector) c.getParent(); VConsole.log("Could not resolve component, using root as component"); } VConsole.log("Dragging widget: " + w); VConsole.log(" in parent: " + parent); // Ensure component is draggable if (!VDragDropUtil.isDraggingEnabled(parent, w)) { VConsole.log("Dragging disabled for " + w.getClass().getName() + " in " + parent.getWidget().getClass().getName()); VDragAndDropManager.get().interruptDrag(); return; } // Announce drag start to listeners for (DragStartListener dl : dragStartListeners) { if (!dl.dragStart(w, dragMode)) { VDragAndDropManager.get().interruptDrag(); return; } } currentDraggedWidget = w; // Announce to handler that we are starting a drag operation VDragEvent currentDragEvent = VDragAndDropManager.get().startDrag(transferable, event, true); /* * Create the drag image */ boolean hasDragCaption = false; com.google.gwt.dom.client.Element dragImageElement = null; if (root instanceof VHasDragCaptionProvider) { VDragCaptionProvider dragCaptionProvider = ((VHasDragCaptionProvider) root).getDragCaptionProvider(); if (dragCaptionProvider != null) { hasDragCaption = true; dragImageElement = dragCaptionProvider.getDragCaptionElement(currentDraggedWidget); } } if (!hasDragCaption && dragImageProvider != null) { dragImageElement = dragImageProvider.getDragImageElement(w); } if (dragImageElement != null) { // Set stylename to proxy component as well if (hasDragCaption) { dragImageElement.addClassName(ACTIVE_DRAG_CUSTOM_IMAGE_STYLENAME); } else { dragImageElement.addClassName(ACTIVE_DRAG_SOURCE_STYLENAME); } } else if (root instanceof VCssLayout) { /* * CSS Layout does not have an enclosing div so we just use the * component div */ dragImageElement = w.getElement(); } else if (root instanceof VTabsheet) { /* * Tabsheet should use the dragged tab as a drag image */ dragImageElement = targetElement; } else if (root instanceof VAccordion) { /* * Accordion should use the dragged tab as a drag image */ dragImageElement = targetElement; } else if (root instanceof VFormLayout) { /* * Dragging a component in a form layout should include the caption * and error indicator as well */ Element rowElement = (Element) VDDFormLayout .getRowFromChildElement((com.google.gwt.dom.client.Element) w.getElement().cast(), (com.google.gwt.dom.client.Element) root.getElement().cast()) .cast(); dragImageElement = rowElement; } else { /* * For other layouts we just use the target element; */ dragImageElement = w.getElement(); } Element clone; if (hasDragCaption) { currentDragEvent.setDragImage(dragImageElement); clone = dragImageElement; } else { currentDragEvent.createDragImage(dragImageElement, true); clone = currentDragEvent.getDragImage(); } assert (clone != null); // Lock drag image dimensions if (!hasDragCaption) { clone.getStyle().setWidth(dragImageElement.getOffsetWidth(), Style.Unit.PX); clone.getStyle().setHeight(dragImageElement.getOffsetHeight(), Style.Unit.PX); } if (c != null && c.delegateCaptionHandling() && !(root instanceof VTabsheet) && !(root instanceof VAccordion)) { /* * Captions are not being dragged with the widget since they are * separate. Manually add a clone of the caption to the drag image. */ if (target instanceof VCaption) { clone.insertFirst(targetElement.cloneNode(true)); } } if (BrowserInfo.get().isIE()) { // Fix IE not aligning the drag image correctly when dragging // layouts clone.getStyle().setPosition(Position.ABSOLUTE); } currentDraggedWidget.addStyleName(ACTIVE_DRAG_SOURCE_STYLENAME); // Listen to mouse up for cleanup mouseUpHandlerReg = Event.addNativePreviewHandler(new Event.NativePreviewHandler() { @Override public void onPreviewNativeEvent(NativePreviewEvent event) { if (event.getTypeInt() == Event.ONMOUSEUP || event.getTypeInt() == Event.ONTOUCHEND || event.getTypeInt() == Event.ONTOUCHCANCEL) { if (mouseUpHandlerReg != null) { mouseUpHandlerReg.removeHandler(); if (currentDraggedWidget != null) { currentDraggedWidget.removeStyleName(ACTIVE_DRAG_SOURCE_STYLENAME); if (dragImageProvider != null) { com.google.gwt.dom.client.Element dragImageElement = dragImageProvider .getDragImageElement(currentDraggedWidget); if (dragImageElement != null) { dragImageElement.removeClassName(ACTIVE_DRAG_SOURCE_STYLENAME); } } currentDraggedWidget = null; } } // Ensure capturing is turned off at mouse up Event.releaseCapture(RootPanel.getBodyElement()); } } }); }
From source file:fi.jasoft.dragdroplayouts.client.ui.VLayoutDragDropMouseHandler.java
License:Apache License
/** * Initiates the drag only on the first move event * /*from w w w .ja v a2s . c o m*/ * @param originalEvent * the original Mouse Down event. Only events on elements are * passed in here (Element.as() is safe without check here) */ protected void initiateDragOnMove(final NativeEvent originalEvent) { EventTarget eventTarget = originalEvent.getEventTarget(); boolean stopEventPropagation = false; Element targetElement = Element.as(eventTarget); Widget target = WidgetUtil.findWidget(targetElement, null); Widget targetParent = target.getParent(); // Stop event propagation and prevent default behaviour if // - target is *not* a VTabsheet.TabCaption or // - drag mode is caption mode and widget is caption boolean isTabCaption = targetParent instanceof VTabsheet.TabCaption; boolean isCaption = VDragDropUtil.isCaptionOrCaptionless(targetParent); if (dragMode == LayoutDragMode.CLONE && isTabCaption == false) { stopEventPropagation = true; // overwrite stopEventPropagation flag again if // - root implements VHasDragFilter but // - target is not part of its drag filter and // - target is not a GWT Label based widget if (root instanceof VHasDragFilter) { if (((VHasDragFilter) root).getDragFilter().isDraggable(target) == false && (target instanceof LabelBase) == false) { stopEventPropagation = false; } } } if (dragMode == LayoutDragMode.CAPTION && isCaption) { stopEventPropagation = true; } if (isElementNotDraggable(targetElement)) { stopEventPropagation = false; } if (stopEventPropagation) { originalEvent.stopPropagation(); originalEvent.preventDefault(); // Manually focus as preventDefault() will also cancel focus targetElement.focus(); } mouseDownHandlerReg = Event.addNativePreviewHandler(new NativePreviewHandler() { @Override public void onPreviewNativeEvent(NativePreviewEvent event) { int type = event.getTypeInt(); if (type == Event.ONMOUSEUP || type == Event.ONTOUCHCANCEL || type == Event.ONTOUCHEND) { mouseDownHandlerReg.removeHandler(); mouseDownHandlerReg = null; } else if (type == Event.ONMOUSEMOVE || type == Event.ONTOUCHMOVE) { mouseDownHandlerReg.removeHandler(); mouseDownHandlerReg = null; initiateDrag(originalEvent); } } }); }
From source file:fi.jasoft.dragdroplayouts.client.ui.VLayoutDragDropMouseHandler.java
License:Apache License
/** * Called when the dragging a component should be initiated by both a mouse * down event as well as a touch start event * // w w w. j ava2s .c o m * FIXME This method is a BIG hack to circumvent Vaadin's very poor client * side API's. This will break often. Refactor once Vaadin gets a grip. * * @param event */ protected void initiateDrag(NativeEvent event) { // Check that dragging is enabled if (dragMode == LayoutDragMode.NONE) { return; } // Dragging can only be done with left mouse button and no modifier keys if (!isMouseDragEvent(event) && !Util.isTouchEvent(event)) { return; } // Get target widget EventTarget eventTarget = event.getEventTarget(); Element targetElement = Element.as(eventTarget); Widget target = WidgetUtil.findWidget(targetElement, null); if (isEventOnScrollBar(event)) { return; } // do not drag close button of TabSheet tab if (isElementNotDraggable(targetElement)) { VDragAndDropManager.get().interruptDrag(); return; } // Abort if drag mode is caption mode and widget is not a caption boolean isPanelCaption = target instanceof VPanel && targetElement.getParentElement().getClassName().contains("v-panel-caption"); boolean isCaption = isPanelCaption || VDragDropUtil.isCaptionOrCaptionless(target); if (dragMode == LayoutDragMode.CAPTION && !isCaption) { /* * Ensure target is a caption in caption mode */ return; } if (dragMode == LayoutDragMode.CAPTION && isCaption) { /* * Ensure that captions in nested layouts don't get accepted if in * caption mode */ Widget w = VDragDropUtil.getTransferableWidget(target); ComponentConnector c = Util.findConnectorFor(w); ComponentConnector parent = (ComponentConnector) c.getParent(); if (parent.getWidget() != root) { return; } } // Create the transfarable VTransferable transferable = VDragDropUtil.createLayoutTransferableFromMouseDown(event, root, target); // Are we trying to drag the root layout if (transferable == null) { VConsole.log("Creating transferable on mouse down returned null"); return; } // Resolve the component final Widget w; ComponentConnector c = null, parent = null; if (target instanceof TabCaption) { TabCaption tabCaption = (TabCaption) target; Tab tab = tabCaption.getTab(); int tabIndex = ((ComplexPanel) tab.getParent()).getWidgetIndex(tab); VTabsheet tabsheet = tab.getTabsheet(); w = tab; c = tabsheet.getTab(tabIndex); parent = Util.findConnectorFor(tabsheet); } else if (root instanceof VDDAccordion) { w = target; parent = Util.findConnectorFor(root); StackItem tab = WidgetUtil.findWidget(targetElement, StackItem.class); if (tab != null && root.getElement().isOrHasChild(tab.getElement())) { c = ((VDDAccordion) root).getTab(((VDDAccordion) root).getTabPosition(tab)); } } else if (transferable.getData(Constants.TRANSFERABLE_DETAIL_COMPONENT) != null) { ComponentConnector connector = (ComponentConnector) transferable .getData(Constants.TRANSFERABLE_DETAIL_COMPONENT); w = connector.getWidget(); c = Util.findConnectorFor(w); parent = (ComponentConnector) c.getParent(); } else { // Failsafe if no widget was found w = root; c = Util.findConnectorFor(w); parent = (ComponentConnector) c.getParent(); VConsole.log("Could not resolve component, using root as component"); } VConsole.log("Dragging widget: " + w); VConsole.log(" in parent: " + parent); // Ensure component is draggable if (!VDragDropUtil.isDraggingEnabled(parent, w)) { VConsole.log("Dragging disabled for " + w.getClass().getName() + " in " + parent.getWidget().getClass().getName()); VDragAndDropManager.get().interruptDrag(); return; } // Announce drag start to listeners for (DragStartListener dl : dragStartListeners) { if (!dl.dragStart(w, dragMode)) { VDragAndDropManager.get().interruptDrag(); return; } } currentDraggedWidget = w; // Announce to handler that we are starting a drag operation VDragEvent currentDragEvent = VDragAndDropManager.get().startDrag(transferable, event, true); /* * Create the drag image */ com.google.gwt.dom.client.Element dragImageElement = dragImageProvider == null ? null : dragImageProvider.getDragImageElement(w); if (dragImageElement != null) { // Set stylename to proxy component as well dragImageElement.addClassName(ACTIVE_DRAG_SOURCE_STYLENAME); } else if (root instanceof VCssLayout) { /* * CSS Layout does not have an enclosing div so we just use the * component div */ dragImageElement = w.getElement(); } else if (root instanceof VTabsheet) { /* * Tabsheet should use the dragged tab as a drag image */ dragImageElement = targetElement; } else if (root instanceof VAccordion) { /* * Accordion should use the dragged tab as a drag image */ dragImageElement = targetElement; } else if (root instanceof VFormLayout) { /* * Dragging a component in a form layout should include the caption * and error indicator as well */ Element rowElement = (Element) VDDFormLayout .getRowFromChildElement((com.google.gwt.dom.client.Element) w.getElement().cast(), (com.google.gwt.dom.client.Element) root.getElement().cast()) .cast(); dragImageElement = rowElement; } else { /* * For other layouts we just use the target element; */ dragImageElement = w.getElement(); } currentDragEvent.createDragImage(dragImageElement, true); Element clone = currentDragEvent.getDragImage(); assert (clone != null); // Lock drag image dimensions clone.getStyle().setWidth(dragImageElement.getOffsetWidth(), Unit.PX); clone.getStyle().setHeight(dragImageElement.getOffsetHeight(), Unit.PX); if (c != null && c.delegateCaptionHandling() && !(root instanceof VTabsheet) && !(root instanceof VAccordion)) { /* * Captions are not being dragged with the widget since they are * separate. Manually add a clone of the caption to the drag image. */ if (target instanceof VCaption) { clone.insertFirst(targetElement.cloneNode(true)); } } if (BrowserInfo.get().isIE()) { // Fix IE not aligning the drag image correctly when dragging // layouts clone.getStyle().setPosition(Position.ABSOLUTE); } currentDraggedWidget.addStyleName(ACTIVE_DRAG_SOURCE_STYLENAME); // Listen to mouse up for cleanup mouseUpHandlerReg = Event.addNativePreviewHandler(new Event.NativePreviewHandler() { @Override public void onPreviewNativeEvent(NativePreviewEvent event) { if (event.getTypeInt() == Event.ONMOUSEUP || event.getTypeInt() == Event.ONTOUCHEND || event.getTypeInt() == Event.ONTOUCHCANCEL) { if (mouseUpHandlerReg != null) { mouseUpHandlerReg.removeHandler(); if (currentDraggedWidget != null) { currentDraggedWidget.removeStyleName(ACTIVE_DRAG_SOURCE_STYLENAME); if (dragImageProvider != null) { com.google.gwt.dom.client.Element dragImageElement = dragImageProvider .getDragImageElement(currentDraggedWidget); if (dragImageElement != null) { dragImageElement.removeClassName(ACTIVE_DRAG_SOURCE_STYLENAME); } } currentDraggedWidget = null; } } // Ensure capturing is turned off at mouse up Event.releaseCapture(RootPanel.getBodyElement()); } } }); }