From e71f6cfd817ffddb7c9cc4a7855d248a859d837f Mon Sep 17 00:00:00 2001 From: Henri Sara Date: Wed, 16 Sep 2015 17:12:48 +0300 Subject: [PATCH] Make ordered layout Slot customizable (#18703) Change-Id: If425d7196e9393f87659462d5b4488549988ee0d --- .../vaadin/client/ui/orderedlayout/Slot.java | 35 +++++++++++++++-- .../orderedlayout/VAbstractOrderedLayout.java | 39 ++++++++++++++++++- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/client/src/com/vaadin/client/ui/orderedlayout/Slot.java b/client/src/com/vaadin/client/ui/orderedlayout/Slot.java index b97cf73989..5ecda3dca4 100644 --- a/client/src/com/vaadin/client/ui/orderedlayout/Slot.java +++ b/client/src/com/vaadin/client/ui/orderedlayout/Slot.java @@ -41,11 +41,12 @@ import com.vaadin.shared.ui.AlignmentInfo; /** * Represents a slot which contains the actual widget in the layout. */ -public final class Slot extends SimplePanel { +public class Slot extends SimplePanel { private static final String ALIGN_CLASS_PREFIX = "v-align-"; - private final VAbstractOrderedLayout layout; + // this must be set at construction time and not changed afterwards + private VAbstractOrderedLayout layout; public static final String SLOT_CLASSNAME = "v-slot"; @@ -87,6 +88,31 @@ public final class Slot extends SimplePanel { private double expandRatio = -1; + /** + * Constructs a slot. + * + * When using this constructor, the layout and widget must be set before any + * other operations are performed on the slot. + */ + public Slot() { + setStyleName(SLOT_CLASSNAME); + } + + /** + * Set the layout in which this slot is. This method must be called exactly + * once at slot construction time when using the default constructor. + * + * The method should normally only be called by + * {@link VAbstractOrderedLayout#createSlot(Widget)}. + * + * @since + * @param layout + * the layout containing the slot + */ + public void setLayout(VAbstractOrderedLayout layout) { + this.layout = layout; + } + /** * Constructs a slot. * @@ -94,9 +120,12 @@ public final class Slot extends SimplePanel { * The layout to which this slot belongs * @param widget * The widget to put in the slot + * @deprecated use {@link GWT#create(Class)}, {@link #setWidget(Widget)} and + * {@link #setLayout(VAbstractOrderedLayout)} instead */ + @Deprecated public Slot(VAbstractOrderedLayout layout, Widget widget) { - this.layout = layout; + setLayout(layout); setStyleName(SLOT_CLASSNAME); setWidget(widget); } diff --git a/client/src/com/vaadin/client/ui/orderedlayout/VAbstractOrderedLayout.java b/client/src/com/vaadin/client/ui/orderedlayout/VAbstractOrderedLayout.java index 2e6d4cf5c8..53ba9ddb28 100644 --- a/client/src/com/vaadin/client/ui/orderedlayout/VAbstractOrderedLayout.java +++ b/client/src/com/vaadin/client/ui/orderedlayout/VAbstractOrderedLayout.java @@ -18,6 +18,7 @@ package com.vaadin.client.ui.orderedlayout; import java.util.HashMap; import java.util.Map; +import com.google.gwt.core.client.GWT; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.Node; import com.google.gwt.dom.client.Style; @@ -209,8 +210,23 @@ public class VAbstractOrderedLayout extends FlowPanel { */ public void removeWidget(Widget widget) { Slot slot = widgetToSlot.get(widget); + removeSlot(slot); + } + + /** + * Remove a slot from the layout. + * + * This method is called automatically by {@link #removeWidget(Widget)} and + * should not be called directly by the user. When overridden, the super + * method must be called. + * + * @since + * @param Slot + * to remove + */ + protected void removeSlot(Slot slot) { remove(slot); - widgetToSlot.remove(widget); + widgetToSlot.remove(slot.getWidget()); } /** @@ -225,12 +241,31 @@ public class VAbstractOrderedLayout extends FlowPanel { public Slot getSlot(Widget widget) { Slot slot = widgetToSlot.get(widget); if (slot == null) { - slot = new Slot(this, widget); + slot = createSlot(widget); widgetToSlot.put(widget, slot); } return slot; } + /** + * Create a slot to be added to the layout. + * + * This method is called automatically by {@link #getSlot(Widget)} when a + * new slot is needed. It should not be called directly by the user, but can + * be overridden to customize slot creation. + * + * @since + * @param widget + * the widget for which a slot is being created + * @return created Slot + */ + protected Slot createSlot(Widget widget) { + Slot slot = GWT.create(Slot.class); + slot.setLayout(this); + slot.setWidget(widget); + return slot; + } + /** * Gets a slot based on the widget element. If no slot is found then null is * returned. -- 2.39.5