fi.jasoft.dragdroplayouts.client.ui.verticallayout.VDDVerticalLayout.java Source code

Java tutorial

Introduction

Here is the source code for fi.jasoft.dragdroplayouts.client.ui.verticallayout.VDDVerticalLayout.java

Source

/*
 * Copyright 2014 John Ahlroos
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package fi.jasoft.dragdroplayouts.client.ui.verticallayout;

import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.WidgetCollection;
import com.vaadin.client.MouseEventDetailsBuilder;
import com.vaadin.client.Util;
import com.vaadin.client.ui.VVerticalLayout;
import com.vaadin.client.ui.dd.VDragEvent;
import com.vaadin.client.ui.orderedlayout.Slot;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.shared.ui.dd.VerticalDropLocation;

import fi.jasoft.dragdroplayouts.DDVerticalLayout;
import fi.jasoft.dragdroplayouts.client.VDragFilter;
import fi.jasoft.dragdroplayouts.client.ui.Constants;
import fi.jasoft.dragdroplayouts.client.ui.LayoutDragMode;
import fi.jasoft.dragdroplayouts.client.ui.VDragDropUtil;
import fi.jasoft.dragdroplayouts.client.ui.VLayoutDragDropMouseHandler;
import fi.jasoft.dragdroplayouts.client.ui.VLayoutDragDropMouseHandler.DragStartListener;
import fi.jasoft.dragdroplayouts.client.ui.interfaces.VDDHasDropHandler;
import fi.jasoft.dragdroplayouts.client.ui.interfaces.VDragImageProvider;
import fi.jasoft.dragdroplayouts.client.ui.interfaces.VHasDragFilter;
import fi.jasoft.dragdroplayouts.client.ui.interfaces.VHasDragImageReferenceSupport;
import fi.jasoft.dragdroplayouts.client.ui.interfaces.VHasDragMode;
import fi.jasoft.dragdroplayouts.client.ui.interfaces.VHasIframeShims;
import fi.jasoft.dragdroplayouts.client.ui.util.IframeCoverUtility;

/**
 * Client side implementation for {@link DDVerticalLayout}
 * 
 * @author John Ahlroos / www.jasoft.fi
 * @since 0.4.0
 */
public class VDDVerticalLayout extends VVerticalLayout
        implements VHasDragMode, VDDHasDropHandler<VDDVerticalLayoutDropHandler>, DragStartListener, VHasDragFilter,
        VHasIframeShims, VHasDragImageReferenceSupport {

    private Widget currentlyEmphasised;

    public static final String OVER = "v-ddorderedlayout-over";

    public static final String OVER_SPACED = OVER + "-spaced";

    private VDDVerticalLayoutDropHandler dropHandler;

    private VDragFilter dragFilter;

    private final IframeCoverUtility iframeCoverUtility = new IframeCoverUtility();

    private final VLayoutDragDropMouseHandler ddMouseHandler = new VLayoutDragDropMouseHandler(this,
            LayoutDragMode.NONE);

    // Value delegated from the state
    private float cellTopBottomDropRatio = DDVerticalLayoutState.DEFAULT_VERTICAL_DROP_RATIO;

    private LayoutDragMode mode = LayoutDragMode.NONE;

    private boolean iframeCovers = false;

    public VDDVerticalLayout() {
        super();
    }

    @Override
    protected void onLoad() {
        super.onLoad();
        ddMouseHandler.addDragStartListener(this);
        setDragMode(mode);
        iframeShimsEnabled(iframeCovers);
    }

    @Override
    protected void onUnload() {
        super.onUnload();
        ddMouseHandler.removeDragStartListener(this);
        ddMouseHandler.updateDragMode(LayoutDragMode.NONE);
        iframeCoverUtility.setIframeCoversEnabled(false, getElement(), LayoutDragMode.NONE);
    }

    /**
     * Removes any applies drag and drop style applied by emphasis()
     */
    protected void deEmphasis() {
        if (currentlyEmphasised != null) {
            // Universal over style
            UIObject.setStyleName(currentlyEmphasised.getElement(), OVER, false);
            UIObject.setStyleName(currentlyEmphasised.getElement(), OVER_SPACED, false);

            // Vertical styles
            UIObject.setStyleName(currentlyEmphasised.getElement(),
                    OVER + "-" + VerticalDropLocation.TOP.toString().toLowerCase(), false);
            UIObject.setStyleName(currentlyEmphasised.getElement(),
                    OVER + "-" + VerticalDropLocation.MIDDLE.toString().toLowerCase(), false);
            UIObject.setStyleName(currentlyEmphasised.getElement(),
                    OVER + "-" + VerticalDropLocation.BOTTOM.toString().toLowerCase(), false);

            currentlyEmphasised = null;
        }
    }

    /**
     * Returns the horizontal location within the cell when hovering over the cell. By default the
     * cell is divided into three parts: left,center,right with the ratios 10%,80%,10%;
     * 
     * @param container The widget container
     * @param event The drag event
     * @return The horizontal drop location
     */
    protected VerticalDropLocation getVerticalDropLocation(Widget container, VDragEvent event) {
        return VDragDropUtil.getVerticalDropLocation(container.getElement(),
                Util.getTouchOrMouseClientY(event.getCurrentGwtEvent()), cellTopBottomDropRatio);
    }

    /**
     * Updates the drop details while dragging. This is needed to ensure client side criterias can
     * validate the drop location.
     * 
     * @param widget The container which we are hovering over
     * @param event The drag event
     */
    protected void updateDropDetails(Widget widget, VDragEvent event) {
        if (widget == null) {
            return;
        }

        /*
         * The horizontal position within the cell{
         */
        event.getDropDetails().put(Constants.DROP_DETAIL_VERTICAL_DROP_LOCATION,
                getVerticalDropLocation(widget, event));

        /*
         * The index over which the drag is. Can be used by a client side criteria to verify that a drag
         * is over a certain index.
         */
        int index = -1;
        if (widget instanceof Slot) {
            WidgetCollection captionsAndSlots = getChildren();
            int realIndex = 0;
            for (int i = 0; i < captionsAndSlots.size(); i++) {
                Widget w = captionsAndSlots.get(i);
                if (w == widget) {
                    index = realIndex;
                    break;
                } else if (w instanceof Slot) {
                    realIndex++;
                }
            }
        }

        event.getDropDetails().put(Constants.DROP_DETAIL_TO, index);

        // Add mouse event details
        MouseEventDetails details = MouseEventDetailsBuilder.buildMouseEventDetails(event.getCurrentGwtEvent(),
                getElement());
        event.getDropDetails().put(Constants.DROP_DETAIL_MOUSE_EVENT, details.serialize());
    }

    /**
     * Empasises the drop location of the component when hovering over a hildComponentContainer.
     * Passing null as the container removes any previous emphasis.
     * 
     * @param container The container which we are hovering over
     * @param event The drag event
     */
    protected void emphasis(Widget container, VDragEvent event) {

        // Remove emphasis from previous hovers
        deEmphasis();

        // validate container
        if (container == null || !getElement().isOrHasChild(container.getElement())) {
            return;
        }

        currentlyEmphasised = container;

        UIObject.setStyleName(container.getElement(), OVER, true);

        // Add drop location specific style
        if (container != this) {
            UIObject.setStyleName(container.getElement(),
                    OVER + "-" + getVerticalDropLocation(container, event).toString().toLowerCase(), true);
        } else {
            UIObject.setStyleName(container.getElement(),
                    OVER + "-" + VerticalDropLocation.MIDDLE.toString().toLowerCase(), true);
        }
    }

    /**
     * Returns the current drag mode which determines how the drag is visualized
     */
    public LayoutDragMode getDragMode() {
        return ddMouseHandler.getDragMode();
    }

    /**
     * A hook for extended components to post process the the drop before it is sent to the server.
     * Useful if you don't want to override the whole drop handler.
     */
    protected boolean postDropHook(VDragEvent drag) {
        // Extended classes can add content here...
        return true;
    }

    /**
     * A hook for extended components to post process the the enter event. Useful if you don't want to
     * override the whole drophandler.
     */
    protected void postEnterHook(VDragEvent drag) {
        // Extended classes can add content here...
    }

    /**
     * A hook for extended components to post process the the leave event. Useful if you don't want to
     * override the whole drophandler.
     */
    protected void postLeaveHook(VDragEvent drag) {
        // Extended classes can add content here...
    }

    /**
     * A hook for extended components to post process the the over event. Useful if you don't want to
     * override the whole drophandler.
     */
    protected void postOverHook(VDragEvent drag) {
        // Extended classes can add content here...
    }

    /**
     * Can be used to listen to drag start events, must return true for the drag to commence. Return
     * false to interrupt the drag:
     */
    public boolean dragStart(Widget widget, LayoutDragMode mode) {
        return ddMouseHandler.getDragMode() != LayoutDragMode.NONE && dragFilter.isDraggable(widget);
    }

    /**
     * Get the drop handler attached to the Layout
     */
    public VDDVerticalLayoutDropHandler getDropHandler() {
        return dropHandler;
    }

    public void setDropHandler(VDDVerticalLayoutDropHandler dropHandler) {
        this.dropHandler = dropHandler;
    }

    /*
     * (non-Javadoc)
     * 
     * @see fi.jasoft.dragdroplayouts.client.ui.interfaces.VHasDragFilter#getDragFilter ()
     */
    public VDragFilter getDragFilter() {
        return dragFilter;
    }

    IframeCoverUtility getIframeCoverUtility() {
        return iframeCoverUtility;
    }

    VLayoutDragDropMouseHandler getMouseHandler() {
        return ddMouseHandler;
    }

    @Override
    public void setDragFilter(VDragFilter filter) {
        this.dragFilter = filter;
    }

    public void setCellTopBottomDropRatio(float cellTopBottomDropRatio) {
        this.cellTopBottomDropRatio = cellTopBottomDropRatio;
    }

    public float getCellTopBottomDropRatio() {
        return cellTopBottomDropRatio;
    }

    @Override
    public void iframeShimsEnabled(boolean enabled) {
        iframeCovers = enabled;
        iframeCoverUtility.setIframeCoversEnabled(enabled, getElement(), mode);
    }

    @Override
    public boolean isIframeShimsEnabled() {
        return iframeCovers;
    }

    @Override
    public void setDragMode(LayoutDragMode mode) {
        this.mode = mode;
        ddMouseHandler.updateDragMode(mode);
        iframeShimsEnabled(iframeCovers);
    }

    @Override
    public void setDragImageProvider(VDragImageProvider provider) {
        ddMouseHandler.setDragImageProvider(provider);
    }
}