Skip to content

Commit

Permalink
fixes issue vaadin#4035: allows Flash embed to expand if percentage d…
Browse files Browse the repository at this point in the history
…imensions given
  • Loading branch information
OlliTietavainenVaadin committed Feb 13, 2017
1 parent 8dc5d8d commit 3e4ea29
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 9 deletions.
77 changes: 70 additions & 7 deletions client/src/main/java/com/vaadin/client/ui/VFlash.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -103,8 +106,6 @@ public void rebuildIfNeeded() {

@Override
public void setWidth(String width) {
// super.setWidth(height);

if (this.width != width) {
this.width = width;
needsRebuild = true;
Expand All @@ -113,8 +114,6 @@ public void setWidth(String width) {

@Override
public void setHeight(String height) {
// super.setHeight(height);

if (this.height != height) {
this.height = height;
needsRebuild = true;
Expand All @@ -136,13 +135,22 @@ public void setEmbedParams(Map<String, String> params) {
}
}

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
Expand Down Expand Up @@ -223,8 +231,19 @@ protected String createFlashEmbed() {
// 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
Expand All @@ -250,4 +269,48 @@ protected String createFlashEmbed() {
return html.toString();
}

protected 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());
}

}

protected String getRelativePixelWidth() {
double percent = Double
.parseDouble(width.substring(0, width.indexOf('%')));
int widthInPixels = (int) (percent / 100) * slotOffsetWidth;
return widthInPixels + "px";
}

protected String getRelativePixelHeight() {
double percent = Double
.parseDouble(height.substring(0, height.indexOf('%')));
int heightInPixels = (int) (percent / 100) * slotOffsetHeight;
return heightInPixels + "px";
}

private boolean hasPercentageHeight() {
return ((height != null) && (height.indexOf('%') > 0));
}

private boolean hasPercentageWidth() {
return ((width != null) && (width.indexOf('%') > 0));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 Vaadin Ltd.
* 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
Expand All @@ -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;
Expand All @@ -39,7 +42,6 @@ public FlashState getState() {
public void onStateChanged(StateChangeEvent stateChangeEvent) {

super.onStateChanged(stateChangeEvent);

getWidget().setSource(
getResourceUrl(AbstractEmbeddedState.SOURCE_RESOURCE));
getWidget().setArchive(getState().archive);
Expand All @@ -52,4 +54,26 @@ public void onStateChanged(StateChangeEvent stateChangeEvent) {

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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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.Button.ClickEvent;
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");
button.addClickListener(new Button.ClickListener() {

@Override
public void buttonClick(ClickEvent event) {
player.setSizeFull();

}
});
addComponent(button);
}

@Override
protected String getDescription() {
return "Flash object should expand according to percentile sizes";
}

@Override
protected Integer getTicketNumber() {
// TODO Auto-generated method stub
return null;
}

}

0 comments on commit 3e4ea29

Please sign in to comment.