| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Florian Pirchner <florian.pirchner@gmail.com> - Initial implementation |
| */ |
| package org.eclipse.osbp.vaadin.addons.absolutelayout.client; |
| |
| import com.google.gwt.dom.client.Element; |
| import com.google.gwt.dom.client.Style.Unit; |
| import com.google.gwt.user.client.Timer; |
| import com.google.gwt.user.client.ui.Widget; |
| import com.vaadin.client.ui.VOverlay; |
| |
| // TODO: Auto-generated Javadoc |
| /** |
| * The Class OAlignmentOverlay. |
| */ |
| public class OAlignmentOverlay extends VOverlay { |
| |
| /** The Constant THICKNESS. */ |
| private static final int THICKNESS = 1; |
| |
| /** The Constant CLASSNAME. */ |
| private static final String CLASSNAME = "o-alignmentLine"; |
| |
| /** The alignment location. */ |
| private final AlignmentsLocation alignmentLocation; |
| |
| /** The drag element. */ |
| private final Element dragElement; |
| |
| /** The snap timer. */ |
| private Timer snapTimer; |
| |
| /** |
| * Shows alignment lines for drag and resize operations. |
| * |
| * @param owner the owner |
| * @param dragElement the drag element |
| * @param location the location |
| * @param snap the snap |
| * @param resize the resize |
| * @param autoSnap the auto snap |
| */ |
| public OAlignmentOverlay(Widget owner, final Element dragElement, AlignmentsLocation location, |
| final OAlignmentSnapInfo snap, final boolean resize, boolean autoSnap) { |
| super(false, false); |
| |
| super.setOwner(owner); |
| this.dragElement = dragElement; |
| this.alignmentLocation = location; |
| |
| setStyleName(CLASSNAME); |
| |
| setZIndex(VOverlay.Z_INDEX + 1); |
| |
| if (autoSnap && (snap.getX() != 0 || snap.getY() != 0)) { |
| snapTimer = new Timer() { |
| @Override |
| public void run() { |
| if (resize) { |
| snapResizeImagePosition(dragElement, snap); |
| } else { |
| snapDragImagePosition(dragElement, snap); |
| } |
| } |
| }; |
| } |
| } |
| |
| /* (non-Javadoc) |
| * @see com.vaadin.client.ui.VOverlay#show() |
| */ |
| @Override |
| public void show() { |
| int left = 0; |
| int top = 0; |
| int width = 0; |
| int height = 0; |
| |
| if (alignmentLocation == null) { |
| return; |
| } |
| |
| switch (alignmentLocation) { |
| case TOP_EDGE_TOLEFT: |
| width = getAlignmentLineX(); |
| left = getOwner().getOffsetWidth() - width; |
| height = THICKNESS; |
| break; |
| case TOP_EDGE_TORIGHT: |
| width = getAlignmentLineX(); |
| left = 0; |
| height = THICKNESS; |
| break; |
| case BOTTOM_EDGE_TOLEFT: |
| top = getOwner().getOffsetHeight(); |
| width = getAlignmentLineX(); |
| left = getOwner().getOffsetWidth() - width; |
| height = THICKNESS; |
| break; |
| case BOTTOM_EDGE_TORIGHT: |
| width = getAlignmentLineX(); |
| left = 0; |
| top = getOwner().getOffsetHeight(); |
| height = THICKNESS; |
| break; |
| case LEFT_EDGE_TOBOTTOM: |
| top = 0; |
| width = THICKNESS; |
| height = getAlignmentLineY(); |
| break; |
| case LEFT_EDGE_TOTOP: |
| top = getOwner().getOffsetHeight() - getAlignmentLineY(); |
| width = THICKNESS; |
| height = getAlignmentLineY(); |
| break; |
| case RIGHT_EDGE_TOBOTTOM: |
| top = 0; |
| left = getOwner().getOffsetWidth(); |
| width = THICKNESS; |
| height = getAlignmentLineY(); |
| break; |
| case RIGHT_EDGE_TOTOP: |
| top = getOwner().getOffsetHeight() - getAlignmentLineY(); |
| left = getOwner().getOffsetWidth(); |
| width = THICKNESS; |
| height = getAlignmentLineY(); |
| break; |
| } |
| |
| // set adjustment size |
| Element element = getContainerElement(); |
| element.getStyle().setWidth(width, Unit.PX); |
| element.getStyle().setHeight(height, Unit.PX); |
| String bg = element.getStyle().getBackgroundColor(); |
| if (bg == null || bg.equals("")) { |
| element.getStyle().setBackgroundColor("red"); |
| } |
| |
| // set the popup position |
| int absoluteLeft = getOwner().getAbsoluteLeft() + left; |
| int absoluteTop = getOwner().getAbsoluteTop() + top; |
| setPopupPosition(absoluteLeft, absoluteTop); |
| |
| super.show(); |
| |
| if (snapTimer != null) { |
| snapTimer.schedule(300); |
| } |
| } |
| |
| /** |
| * Calculates the horizontal alignment line. |
| * |
| * @return the alignment line x |
| */ |
| private int getAlignmentLineX() { |
| if (isOwnerLeftOfImage()) { |
| return dragElement.getAbsoluteLeft() - getOwner().getAbsoluteLeft() + +dragElement.getOffsetWidth(); |
| } else { |
| return getOwner().getAbsoluteLeft() - dragElement.getAbsoluteLeft() + getOwner().getOffsetWidth(); |
| } |
| } |
| |
| /** |
| * Checks if is owner left of image. |
| * |
| * @return true, if is owner left of image |
| */ |
| private boolean isOwnerLeftOfImage() { |
| return getOwner().getAbsoluteLeft() < dragElement.getAbsoluteLeft(); |
| } |
| |
| /** |
| * Calculates the vertical alignment line. |
| * |
| * @return the alignment line y |
| */ |
| private int getAlignmentLineY() { |
| if (isOwnerTopOfImage()) { |
| return dragElement.getAbsoluteTop() - getOwner().getAbsoluteTop() + dragElement.getOffsetHeight(); |
| } else { |
| return getOwner().getAbsoluteTop() - dragElement.getAbsoluteTop() + getOwner().getOffsetHeight(); |
| } |
| } |
| |
| /** |
| * Checks if is owner top of image. |
| * |
| * @return true, if is owner top of image |
| */ |
| private boolean isOwnerTopOfImage() { |
| return getOwner().getAbsoluteTop() < dragElement.getAbsoluteTop(); |
| } |
| |
| /* (non-Javadoc) |
| * @see com.vaadin.client.ui.VOverlay#hide() |
| */ |
| @Override |
| public void hide() { |
| if (snapTimer != null) { |
| snapTimer.cancel(); |
| } |
| |
| super.hide(); |
| } |
| |
| /** |
| * Snap drag image position. |
| * |
| * @param dragImage the drag image |
| * @param snap the snap |
| */ |
| private void snapDragImagePosition(final Element dragImage, final OAlignmentSnapInfo snap) { |
| int offsetTop = 0; |
| try { |
| String offsetTopStr = dragImage.getStyle().getMarginTop(); |
| offsetTop = Integer.parseInt(offsetTopStr.substring(0, offsetTopStr.length() - 2)); |
| } catch (NumberFormatException e) { |
| // nothing to do |
| } |
| dragImage.getStyle().setTop(dragImage.getAbsoluteTop() - offsetTop - snap.getY(), Unit.PX); |
| |
| int offsetLeft = 0; |
| try { |
| String offsetLeftStr = dragImage.getStyle().getMarginLeft(); |
| offsetLeft = Integer.parseInt(offsetLeftStr.substring(0, offsetLeftStr.length() - 2)); |
| } catch (NumberFormatException e) { |
| // nothing to do |
| } |
| dragImage.getStyle().setLeft(dragImage.getAbsoluteLeft() - offsetLeft - snap.getX(), Unit.PX); |
| } |
| |
| /** |
| * Snap resize image position. |
| * |
| * @param dragImage the drag image |
| * @param delta the delta |
| */ |
| private void snapResizeImagePosition(final Element dragImage, final OAlignmentSnapInfo delta) { |
| |
| OResizeLocation location = OAlignmentManager.get().getResizeLocation(); |
| int oldWidth = dragImage.getOffsetWidth(); |
| int oldHeigth = dragImage.getOffsetHeight(); |
| |
| int newWidth = 0; |
| int newHeight = 0; |
| |
| switch (location) { |
| case TOP: |
| case TOP_LEFT: |
| case LEFT: |
| newHeight = oldHeigth + delta.getY(); |
| newWidth = oldWidth + delta.getX(); |
| break; |
| case BOTTOM: |
| case BOTTOM_RIGHT: |
| case RIGHT: |
| newHeight = oldHeigth - delta.getY(); |
| newWidth = oldWidth - delta.getX(); |
| break; |
| case TOP_RIGHT: |
| newHeight = oldHeigth + delta.getY(); |
| newWidth = oldWidth - delta.getX(); |
| break; |
| case BOTTOM_LEFT: |
| newHeight = oldHeigth - delta.getY(); |
| newWidth = oldWidth + delta.getX(); |
| break; |
| } |
| |
| dragImage.getStyle().setWidth(newWidth, Unit.PX); |
| dragImage.getStyle().setHeight(newHeight, Unit.PX); |
| } |
| |
| /** |
| * Indicates where the alignments should be painted. |
| */ |
| public enum AlignmentsLocation { |
| |
| /** The top edge toleft. */ |
| TOP_EDGE_TOLEFT, |
| /** The top edge toright. */ |
| TOP_EDGE_TORIGHT, |
| /** The left edge totop. */ |
| LEFT_EDGE_TOTOP, |
| /** The left edge tobottom. */ |
| LEFT_EDGE_TOBOTTOM, |
| /** The bottom edge toleft. */ |
| BOTTOM_EDGE_TOLEFT, |
| /** The bottom edge toright. */ |
| BOTTOM_EDGE_TORIGHT, |
| /** The right edge totop. */ |
| RIGHT_EDGE_TOTOP, |
| /** The right edge tobottom. */ |
| RIGHT_EDGE_TOBOTTOM |
| } |
| } |