123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- /*
- @VaadinApache2LicenseForJavaFiles@
- */
- package com.vaadin.terminal.gwt.client.ui;
-
- import java.util.List;
-
- 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.ComponentConnector;
- import com.vaadin.terminal.gwt.client.DirectionalManagedLayout;
- import com.vaadin.terminal.gwt.client.LayoutManager;
- import com.vaadin.terminal.gwt.client.Paintable;
- import com.vaadin.terminal.gwt.client.UIDL;
- import com.vaadin.terminal.gwt.client.Util;
- import com.vaadin.terminal.gwt.client.VCaption;
- import com.vaadin.terminal.gwt.client.ValueMap;
- import com.vaadin.terminal.gwt.client.communication.RpcProxy;
- import com.vaadin.terminal.gwt.client.communication.ServerRpc;
- import com.vaadin.terminal.gwt.client.ui.layout.ComponentConnectorLayoutSlot;
- import com.vaadin.terminal.gwt.client.ui.layout.VLayoutSlot;
-
- public abstract class AbstractOrderedLayoutConnector extends
- AbstractComponentContainerConnector implements Paintable,
- DirectionalManagedLayout {
-
- public interface AbstractOrderedLayoutServerRPC extends LayoutClickRPC,
- ServerRpc {
-
- }
-
- AbstractOrderedLayoutServerRPC rpc;
-
- private LayoutClickEventHandler clickEventHandler = new LayoutClickEventHandler(
- this) {
-
- @Override
- protected ComponentConnector getChildComponent(Element element) {
- return Util.getConnectorForElement(getConnection(), getWidget(),
- element);
- }
-
- @Override
- protected LayoutClickRPC getLayoutClickRPC() {
- return rpc;
- };
-
- };
-
- @Override
- public void init() {
- rpc = RpcProxy.create(AbstractOrderedLayoutServerRPC.class, this);
- getLayoutManager().registerDependency(this,
- getWidget().spacingMeasureElement);
- }
-
- public void updateCaption(ComponentConnector component) {
- VMeasuringOrderedLayout layout = getWidget();
- if (VCaption.isNeeded(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();
- } else {
- layout.setCaption(component.getWidget(), null);
- getLayoutManager().setNeedsUpdate(this);
- }
- }
-
- @Override
- public VMeasuringOrderedLayout getWidget() {
- return (VMeasuringOrderedLayout) super.getWidget();
- }
-
- public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
- if (!isRealUpdate(uidl)) {
- return;
- }
- clickEventHandler.handleEventHandlerRegistration();
-
- VMeasuringOrderedLayout layout = getWidget();
-
- ValueMap expandRatios = uidl.getMapAttribute("expandRatios");
- ValueMap alignments = uidl.getMapAttribute("alignments");
-
- for (ComponentConnector child : getChildren()) {
- VLayoutSlot slot = layout.getSlotForChild(child.getWidget());
- String pid = child.getConnectorId();
-
- 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);
- }
-
- 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 getState().getHeight();
- } else {
- return getState().getWidth();
- }
- }
-
- 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();
- }
- }
-
- @Override
- public void onConnectorHierarchyChange(
- com.vaadin.terminal.gwt.client.ConnectorHierarchyChangeEvent event) {
- super.onConnectorHierarchyChange(event);
- List<ComponentConnector> previousChildren = event.getOldChildren();
- int currentIndex = 0;
- VMeasuringOrderedLayout layout = getWidget();
-
- for (ComponentConnector child : getChildren()) {
- Widget childWidget = child.getWidget();
- VLayoutSlot slot = layout.getSlotForChild(childWidget);
-
- if (childWidget.getParent() != layout) {
- // If the child widget was previously attached to another
- // AbstractOrderedLayout a slot might be found that belongs to
- // another AbstractOrderedLayout. In this case we discard it and
- // create a new slot.
- slot = new ComponentConnectorLayoutSlot(getWidget()
- .getStylePrimaryName(), child, this);
- }
- layout.addOrMove(slot, currentIndex++);
- }
-
- for (ComponentConnector child : previousChildren) {
- if (child.getParent() != this) {
- // Remove slot if the connector is no longer a child of this
- // layout
- layout.removeSlotForWidget(child.getWidget());
- }
- }
-
- };
-
- }
|