| /** |
| * |
| * 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.designer.overlay; |
| |
| import org.eclipse.osbp.vaadin.addons.designer.overlay.client.widgets.AlignmentInfo; |
| |
| import com.vaadin.ui.Alignment; |
| import com.vaadin.ui.Component; |
| import com.vaadin.ui.GridLayout; |
| import com.vaadin.ui.HorizontalLayout; |
| import com.vaadin.ui.VerticalLayout; |
| |
| // TODO: Auto-generated Javadoc |
| /** |
| * Util to deal with {@link AlignmentInfo}. |
| */ |
| public class AlignmentInfoUtil { |
| |
| /** |
| * Returns the horizontal width for the given alignment. |
| * |
| * @param alignment the alignment |
| * @return the horizonal width |
| */ |
| public static String getHorizonalWidth(AlignmentInfo alignment) { |
| if (alignment.isHorizontalFill()) { |
| return "100%"; |
| } else { |
| return "-1px"; |
| } |
| } |
| |
| /** |
| * Returns the vertical height for the given alignment. |
| * |
| * @param alignment the alignment |
| * @return the vertical height |
| */ |
| public static String getVerticalHeight(AlignmentInfo alignment) { |
| if (alignment.isVerticalFill()) { |
| return "100%"; |
| } else { |
| return "-1px"; |
| } |
| } |
| |
| /** |
| * Converts alignment info to vaadin alignment. |
| * |
| * @param alignment the alignment |
| * @return the alignment |
| */ |
| public static Alignment toVaadin(AlignmentInfo alignment) { |
| switch (alignment) { |
| case BOTTOM_CENTER: |
| return Alignment.BOTTOM_CENTER; |
| case BOTTOM_LEFT: |
| return Alignment.BOTTOM_LEFT; |
| case BOTTOM_RIGHT: |
| return Alignment.BOTTOM_RIGHT; |
| case MIDDLE_CENTER: |
| return Alignment.MIDDLE_CENTER; |
| case MIDDLE_LEFT: |
| return Alignment.MIDDLE_LEFT; |
| case MIDDLE_RIGHT: |
| return Alignment.MIDDLE_RIGHT; |
| case TOP_LEFT: |
| return Alignment.TOP_LEFT; |
| case TOP_RIGHT: |
| return Alignment.TOP_RIGHT; |
| case TOP_CENTER: |
| return Alignment.TOP_CENTER; |
| // fill here |
| case BOTTOM_FILL: |
| // fill the bottom line -> position at origin |
| return Alignment.BOTTOM_LEFT; |
| case MIDDLE_FILL: |
| // fill the middle line -> position at origin |
| return Alignment.MIDDLE_LEFT; |
| case TOP_FILL: |
| // fill the top line -> position at origin |
| return Alignment.TOP_LEFT; |
| case FILL_CENTER: |
| // fill the center row -> position at origin |
| return Alignment.TOP_CENTER; |
| case FILL_FILL: |
| // fill the all -> position at origin |
| return Alignment.TOP_LEFT; |
| case FILL_LEFT: |
| // fill the left row -> position at origin |
| return Alignment.TOP_LEFT; |
| case FILL_RIGHT: |
| // fill the right row -> position at origin |
| return Alignment.TOP_RIGHT; |
| } |
| |
| return Alignment.TOP_LEFT; |
| } |
| |
| /** |
| * Applies the alignment to the given layout and child. |
| * |
| * @param layout the layout |
| * @param child the child |
| * @param alignment the alignment |
| */ |
| public static void apply(GridLayout layout, Component child, AlignmentInfo alignment) { |
| layout.setComponentAlignment(child, AlignmentInfoUtil.toVaadin(alignment)); |
| |
| child.setWidth(AlignmentInfoUtil.getHorizonalWidth(alignment)); |
| child.setHeight(AlignmentInfoUtil.getVerticalHeight(alignment)); |
| |
| if (alignment.isHorizontalFill()) { |
| layout.setColumnExpandRatio(layout.getComponentArea(child).getColumn1(), 1.0f); |
| } else { |
| layout.setColumnExpandRatio(layout.getComponentArea(child).getColumn1(), 0f); |
| } |
| |
| if (alignment.isVerticalFill()) { |
| layout.setRowExpandRatio(layout.getComponentArea(child).getRow1(), 1.0f); |
| } else { |
| layout.setRowExpandRatio(layout.getComponentArea(child).getRow1(), 0f); |
| } |
| } |
| |
| /** |
| * Applies the alignment to the given layout and child. |
| * |
| * @param layout the layout |
| * @param child the child |
| * @param alignment the alignment |
| */ |
| public static void apply(VerticalLayout layout, Component child, AlignmentInfo alignment) { |
| layout.setComponentAlignment(child, AlignmentInfoUtil.toVaadin(alignment)); |
| |
| child.setWidth(AlignmentInfoUtil.getHorizonalWidth(alignment)); |
| child.setHeight(AlignmentInfoUtil.getVerticalHeight(alignment)); |
| |
| if (alignment.isVerticalFill()) { |
| layout.setExpandRatio(child, 1.0f); |
| } else { |
| layout.setExpandRatio(child, 0f); |
| } |
| } |
| |
| /** |
| * Applies the alignment to the given layout and child. |
| * |
| * @param layout the layout |
| * @param child the child |
| * @param alignment the alignment |
| */ |
| public static void apply(HorizontalLayout layout, Component child, AlignmentInfo alignment) { |
| layout.setComponentAlignment(child, AlignmentInfoUtil.toVaadin(alignment)); |
| |
| child.setWidth(AlignmentInfoUtil.getHorizonalWidth(alignment)); |
| child.setHeight(AlignmentInfoUtil.getVerticalHeight(alignment)); |
| |
| if (alignment.isHorizontalFill()) { |
| layout.setExpandRatio(child, 1.0f); |
| } else { |
| layout.setExpandRatio(child, 0f); |
| } |
| } |
| } |