123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- /*
- @VaadinApache2LicenseForJavaFiles@
- */
- package com.vaadin.terminal.gwt.client.ui;
-
- import java.util.HashSet;
- import java.util.Iterator;
-
- import com.google.gwt.dom.client.Node;
- import com.google.gwt.dom.client.Style;
- import com.google.gwt.user.client.Element;
- import com.google.gwt.user.client.ui.Widget;
- import com.vaadin.terminal.gwt.client.ApplicationConnection;
- import com.vaadin.terminal.gwt.client.DirectionalManagedLayout;
- import com.vaadin.terminal.gwt.client.LayoutManager;
- import com.vaadin.terminal.gwt.client.UIDL;
- import com.vaadin.terminal.gwt.client.VCaption;
- import com.vaadin.terminal.gwt.client.ConnectorMap;
- import com.vaadin.terminal.gwt.client.ComponentConnector;
- import com.vaadin.terminal.gwt.client.ValueMap;
- import com.vaadin.terminal.gwt.client.ui.layout.VLayoutSlot;
- import com.vaadin.terminal.gwt.client.ui.layout.VPaintableLayoutSlot;
-
- public abstract class VMeasuringOrderedLayoutPaintable extends
- AbstractComponentContainerConnector implements DirectionalManagedLayout {
-
- @Override
- public void init() {
- getLayoutManager().registerDependency(this,
- getWidget().spacingMeasureElement);
- }
-
- public void updateCaption(ComponentConnector component, UIDL uidl) {
- VMeasuringOrderedLayout layout = getWidget();
- if (VCaption.isNeeded(uidl, component.getState())) {
- VLayoutSlot layoutSlot = layout.getSlotForChild(component
- .getWidget());
- VCaption caption = layoutSlot.getCaption();
- if (caption == null) {
- caption = new VCaption(component, getConnection());
-
- Widget widget = component.getWidget();
-
- layout.setCaption(widget, caption);
- }
- caption.updateCaption(uidl);
- } else {
- layout.setCaption(component.getWidget(), null);
- }
- }
-
- @Override
- public VMeasuringOrderedLayout getWidget() {
- return (VMeasuringOrderedLayout) super.getWidget();
- }
-
- @Override
- public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
- super.updateFromUIDL(uidl, client);
- if (!isRealUpdate(uidl)) {
- return;
- }
-
- HashSet<ComponentConnector> previousChildren = new HashSet<ComponentConnector>(
- getChildren());
-
- VMeasuringOrderedLayout layout = getWidget();
-
- ValueMap expandRatios = uidl.getMapAttribute("expandRatios");
- ValueMap alignments = uidl.getMapAttribute("alignments");
-
- int currentIndex = 0;
- // TODO Support reordering elements!
- for (final Iterator<Object> it = uidl.getChildIterator(); it.hasNext();) {
- final UIDL childUIDL = (UIDL) it.next();
- final ComponentConnector child = client.getPaintable(childUIDL);
- Widget widget = child.getWidget();
-
- VLayoutSlot slot = layout.getSlotForChild(widget);
-
- if (widget.getParent() != layout) {
- slot = new VPaintableLayoutSlot(getWidget()
- .getStylePrimaryName(), child);
- }
- layout.addOrMove(slot, currentIndex++);
-
- String pid = child.getId();
-
- AlignmentInfo alignment;
- if (alignments.containsKey(pid)) {
- alignment = new AlignmentInfo(alignments.getInt(pid));
- } else {
- alignment = AlignmentInfo.TOP_LEFT;
- }
- slot.setAlignment(alignment);
-
- double expandRatio;
- if (expandRatios.containsKey(pid)) {
- expandRatio = expandRatios.getRawNumber(pid);
- } else {
- expandRatio = 0;
- }
- slot.setExpandRatio(expandRatio);
-
- if (!childUIDL.getBooleanAttribute("cached")) {
- child.updateFromUIDL(childUIDL, client);
- }
-
- previousChildren.remove(child);
- }
-
- for (ComponentConnector child : previousChildren) {
- Widget widget = child.getWidget();
-
- // Don't remove and unregister if it has been moved to a different
- // parent. Slot element will be left behind, but that is taken care
- // of later
- if (widget.getParent() == getWidget()) {
- layout.removeSlot(layout.getSlotForChild(widget));
-
- ConnectorMap vPaintableMap = ConnectorMap.get(client);
- vPaintableMap.unregisterConnector(child);
- }
- }
-
- // Remove empty layout slots left behind after children have moved to
- // other paintables
- while (true) {
- int childCount = layout.getElement().getChildCount();
- if (childCount <= 1) {
- // Stop if no more slots (spacing element is always present)
- break;
- }
-
- Node lastSlot = layout.getElement().getChild(childCount - 2);
- if (lastSlot.getChildCount() == 0) {
- // Remove if empty
- lastSlot.removeFromParent();
- } else {
- // Stop searching when last slot is not empty
- break;
- }
- }
-
- int bitMask = uidl.getIntAttribute("margins");
- layout.updateMarginStyleNames(new VMarginInfo(bitMask));
-
- layout.updateSpacingStyleName(uidl.getBooleanAttribute("spacing"));
-
- getLayoutManager().setNeedsUpdate(this);
- }
-
- private int getSizeForInnerSize(int size, boolean isVertical) {
- LayoutManager layoutManager = getLayoutManager();
- Element element = getWidget().getElement();
- if (isVertical) {
- return size + layoutManager.getBorderHeight(element)
- + layoutManager.getPaddingHeight(element);
- } else {
- return size + layoutManager.getBorderWidth(element)
- + layoutManager.getPaddingWidth(element);
- }
- }
-
- private static String getSizeProperty(boolean isVertical) {
- return isVertical ? "height" : "width";
- }
-
- private boolean isUndefinedInDirection(boolean isVertical) {
- if (isVertical) {
- return isUndefinedHeight();
- } else {
- return isUndefinedWidth();
- }
- }
-
- private int getInnerSizeInDirection(boolean isVertical) {
- if (isVertical) {
- return getLayoutManager().getInnerHeight(
- getWidget().getElement());
- } else {
- return getLayoutManager().getInnerWidth(
- getWidget().getElement());
- }
- }
-
- private void layoutPrimaryDirection() {
- VMeasuringOrderedLayout layout = getWidget();
- boolean isVertical = layout.isVertical;
- boolean isUndefined = isUndefinedInDirection(isVertical);
-
- int startPadding = getStartPadding(isVertical);
- int spacingSize = getSpacingInDirection(isVertical);
- int allocatedSize;
-
- if (isUndefined) {
- allocatedSize = -1;
- } else {
- allocatedSize = getInnerSizeInDirection(isVertical);
- }
-
- allocatedSize = layout.layoutPrimaryDirection(spacingSize,
- allocatedSize, startPadding);
-
- Style ownStyle = getWidget().getElement().getStyle();
- if (isUndefined) {
- ownStyle.setPropertyPx(getSizeProperty(isVertical),
- getSizeForInnerSize(allocatedSize, isVertical));
- } else {
- ownStyle.setProperty(getSizeProperty(isVertical),
- getDefinedSize(isVertical));
- }
- }
-
- private int getSpacingInDirection(boolean isVertical) {
- if (isVertical) {
- return getLayoutManager().getOuterHeight(
- getWidget().spacingMeasureElement);
- } else {
- return getLayoutManager().getOuterWidth(
- getWidget().spacingMeasureElement);
- }
- }
-
- private void layoutSecondaryDirection() {
- VMeasuringOrderedLayout layout = getWidget();
- boolean isVertical = layout.isVertical;
- boolean isUndefined = isUndefinedInDirection(!isVertical);
-
- int startPadding = getStartPadding(!isVertical);
-
- int allocatedSize;
- if (isUndefined) {
- allocatedSize = -1;
- } else {
- allocatedSize = getInnerSizeInDirection(!isVertical);
- }
-
- allocatedSize = layout.layoutSecondaryDirection(allocatedSize,
- startPadding);
-
- Style ownStyle = getWidget().getElement().getStyle();
-
- if (isUndefined) {
- ownStyle.setPropertyPx(
- getSizeProperty(!getWidget().isVertical),
- getSizeForInnerSize(allocatedSize,
- !getWidget().isVertical));
- } else {
- ownStyle.setProperty(
- getSizeProperty(!getWidget().isVertical),
- getDefinedSize(!getWidget().isVertical));
- }
- }
-
- private String getDefinedSize(boolean isVertical) {
- if (isVertical) {
- return getDeclaredHeight();
- } else {
- return getDeclaredWidth();
- }
- }
-
- private int getStartPadding(boolean isVertical) {
- if (isVertical) {
- return getLayoutManager().getPaddingTop(
- getWidget().getElement());
- } else {
- return getLayoutManager().getPaddingLeft(
- getWidget().getElement());
- }
- }
-
- public void layoutHorizontally() {
- if (getWidget().isVertical) {
- layoutSecondaryDirection();
- } else {
- layoutPrimaryDirection();
- }
- }
-
- public void layoutVertically() {
- if (getWidget().isVertical) {
- layoutPrimaryDirection();
- } else {
- layoutSecondaryDirection();
- }
- }
-
- }
|