diff options
author | Olli Tietäväinen <ollit@vaadin.com> | 2017-03-22 10:41:37 +0200 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-03-22 10:41:37 +0200 |
commit | d01a6ef0271f5238ffcd0f986a30fc0d68102f72 (patch) | |
tree | 14f9eca1e87fa62775cb1943826e22375cad4845 | |
parent | baa0baf8d6f06dc234c610a0ed558cd66c6c67b4 (diff) | |
download | vaadin-framework-d01a6ef0271f5238ffcd0f986a30fc0d68102f72.tar.gz vaadin-framework-d01a6ef0271f5238ffcd0f986a30fc0d68102f72.zip |
Fix Flash expanding with percentage sizing (#8898)
Allows Flash embed to expand if percentage dimensions given
Fixes #4035
4 files changed, 200 insertions, 8 deletions
diff --git a/client/src/main/java/com/vaadin/client/ui/VFlash.java b/client/src/main/java/com/vaadin/client/ui/VFlash.java index 650d4a05fc..6c45cf52ac 100644 --- a/client/src/main/java/com/vaadin/client/ui/VFlash.java +++ b/client/src/main/java/com/vaadin/client/ui/VFlash.java @@ -37,6 +37,9 @@ public class VFlash extends HTML { protected String width; protected String height; + private int slotOffsetHeight = -1; + private int slotOffsetWidth = -1; + public VFlash() { setStyleName(CLASSNAME); } @@ -103,8 +106,7 @@ public class VFlash extends HTML { @Override public void setWidth(String width) { - // super.setWidth(height); - + // explicitly not calling super here if (this.width != width) { this.width = width; needsRebuild = true; @@ -113,8 +115,7 @@ public class VFlash extends HTML { @Override public void setHeight(String height) { - // super.setHeight(height); - + // explicitly not calling super here if (this.height != height) { this.height = height; needsRebuild = true; @@ -136,13 +137,34 @@ public class VFlash extends HTML { } } + /** + * Set dimensions of the containing layout slot so that the size of the + * embed object can be calculated from percentages if needed. + * + * Triggers embed resizing if percentage sizes are in use. + * + * @since 7.7.8 + * @param slotOffsetHeight + * offset height of the layout slot + * @param slotOffsetWidth + * offset width of the layout slot + */ + public void setSlotHeightAndWidth(int slotOffsetHeight, + int slotOffsetWidth) { + this.slotOffsetHeight = slotOffsetHeight; + this.slotOffsetWidth = slotOffsetWidth; + if (hasPercentageHeight() || hasPercentageWidth()) { + resizeEmbedElement(); + } + + } + protected String createFlashEmbed() { /* * To ensure cross-browser compatibility we are using the twice-cooked * method to embed flash i.e. we add a OBJECT tag for IE ActiveX and * inside it a EMBED for all other browsers. */ - StringBuilder html = new StringBuilder(); // Start the object tag @@ -223,8 +245,19 @@ public class VFlash extends HTML { // Build inner EMBED tag html.append("<embed "); html.append("src=\"" + WidgetUtil.escapeAttribute(source) + "\" "); - html.append("width=\"" + WidgetUtil.escapeAttribute(width) + "\" "); - html.append("height=\"" + WidgetUtil.escapeAttribute(height) + "\" "); + if (hasPercentageWidth() && slotOffsetWidth >= 0) { + html.append("width=\"" + getRelativePixelWidth() + "\" "); + } else { + html.append("width=\"" + WidgetUtil.escapeAttribute(width) + "\" "); + } + + if (hasPercentageHeight() && slotOffsetHeight >= 0) { + html.append("height=\"" + getRelativePixelHeight() + "px\" "); + } else { + html.append( + "height=\"" + WidgetUtil.escapeAttribute(height) + "\" "); + } + html.append("type=\"application/x-shockwave-flash\" "); // Add the parameters to the Embed @@ -250,4 +283,46 @@ public class VFlash extends HTML { return html.toString(); } + private void resizeEmbedElement() { + // find <embed> element + com.google.gwt.dom.client.Element objectElem = getElement() + .getFirstChildElement(); + com.google.gwt.dom.client.Element objectChild = objectElem + .getFirstChildElement(); + while (!"EMBED".equalsIgnoreCase(objectChild.getTagName())) { + objectChild = objectChild.getNextSiblingElement(); + if (objectChild == null) { + return; + } + } + // update height & width from slot offset, if percentage size is given + if (hasPercentageHeight() && slotOffsetHeight >= 0) { + objectChild.setAttribute("height", getRelativePixelHeight()); + } + if (hasPercentageWidth() && slotOffsetWidth >= 0) { + objectChild.setAttribute("width", getRelativePixelWidth()); + } + + } + + private String getRelativePixelWidth() { + float relative = WidgetUtil.parseRelativeSize(width); + int widthInPixels = (int) (relative / 100) * slotOffsetWidth; + return widthInPixels + "px"; + } + + private String getRelativePixelHeight() { + float relative = WidgetUtil.parseRelativeSize(height); + int heightInPixels = (int) (relative / 100) * slotOffsetHeight; + return heightInPixels + "px"; + } + + private boolean hasPercentageHeight() { + return ((height != null) && (height.indexOf('%') > 0)); + } + + private boolean hasPercentageWidth() { + return ((width != null) && (width.indexOf('%') > 0)); + } + } diff --git a/client/src/main/java/com/vaadin/client/ui/flash/FlashConnector.java b/client/src/main/java/com/vaadin/client/ui/flash/FlashConnector.java index 7e221ee42d..fd03aec4b6 100644 --- a/client/src/main/java/com/vaadin/client/ui/flash/FlashConnector.java +++ b/client/src/main/java/com/vaadin/client/ui/flash/FlashConnector.java @@ -15,9 +15,12 @@ */ package com.vaadin.client.ui.flash; +import com.google.gwt.dom.client.Element; import com.vaadin.client.communication.StateChangeEvent; import com.vaadin.client.ui.AbstractComponentConnector; import com.vaadin.client.ui.VFlash; +import com.vaadin.client.ui.layout.ElementResizeEvent; +import com.vaadin.client.ui.layout.ElementResizeListener; import com.vaadin.shared.ui.AbstractEmbeddedState; import com.vaadin.shared.ui.Connect; import com.vaadin.shared.ui.flash.FlashState; @@ -39,7 +42,6 @@ public class FlashConnector extends AbstractComponentConnector { public void onStateChanged(StateChangeEvent stateChangeEvent) { super.onStateChanged(stateChangeEvent); - getWidget().setSource( getResourceUrl(AbstractEmbeddedState.SOURCE_RESOURCE)); getWidget().setArchive(getState().archive); @@ -52,4 +54,26 @@ public class FlashConnector extends AbstractComponentConnector { getWidget().rebuildIfNeeded(); } + + private final ElementResizeListener listener = new ElementResizeListener() { + public void onElementResize(ElementResizeEvent e) { + Element slot = e.getElement().getParentElement(); + getWidget().setSlotHeightAndWidth(slot.getOffsetHeight(), + slot.getOffsetWidth()); + } + }; + + @Override + protected void init() { + super.init(); + getLayoutManager().addElementResizeListener(getWidget().getElement(), + listener); + } + + @Override + public void onUnregister() { + getLayoutManager().removeElementResizeListener(getWidget().getElement(), + listener); + } + } diff --git a/uitest/src/main/java/com/vaadin/tests/components/flash/FlashExpansion.java b/uitest/src/main/java/com/vaadin/tests/components/flash/FlashExpansion.java new file mode 100755 index 0000000000..4dd9568682 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/flash/FlashExpansion.java @@ -0,0 +1,48 @@ +/* + * Copyright 2000-2017 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.components.flash; + +import com.vaadin.server.ClassResource; +import com.vaadin.tests.components.TestBase; +import com.vaadin.ui.Button; +import com.vaadin.ui.Flash; + +public class FlashExpansion extends TestBase { + + Flash player = new Flash(); + + @Override + protected void setup() { + + player.setWidth("400px"); + player.setHeight("300px"); + player.setSource(new ClassResource("simple.swf")); + addComponent(player); + Button button = new Button("click", e -> player.setSizeFull()); + addComponent(button); + } + + @Override + protected String getDescription() { + return "Flash object should expand according to percentile sizes"; + } + + @Override + protected Integer getTicketNumber() { + return 4035; + } + +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/flash/FlashExpansionTest.java b/uitest/src/test/java/com/vaadin/tests/components/flash/FlashExpansionTest.java new file mode 100755 index 0000000000..923ad2b779 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/flash/FlashExpansionTest.java @@ -0,0 +1,45 @@ +package com.vaadin.tests.components.flash; + +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.remote.DesiredCapabilities; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.FlashElement; +import com.vaadin.testbench.parallel.Browser; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class FlashExpansionTest extends MultiBrowserTest { + + @Override + public List<DesiredCapabilities> getBrowsersToTest() { + return getBrowsersSupportingFlash(); + } + + private final By locator = By.tagName("embed"); + + @Test + public void testFlashIsExpanded() throws Exception { + openTestURL(); + /* Allow the flash plugin to load */ + waitForElementPresent(locator); + WebElement embed = $(FlashElement.class).first().findElement(locator); + String width = embed.getAttribute("width"); + Assert.assertTrue("Width is not 400.0px initially", + "400.0px".equals(width)); + $(ButtonElement.class).first().click(); + String widthAfterExpansion = embed.getAttribute("width"); + Assert.assertFalse("Width is still 400.0px after expansion", + "400.0px".equals(widthAfterExpansion)); + } + + private List<DesiredCapabilities> getBrowsersSupportingFlash() { + // No Flash support in Chrome, FF, PhantomJS + return getBrowserCapabilities(Browser.IE8, Browser.IE9, Browser.IE10, + Browser.IE11); + } +} |