You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MediaBaseConnector.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  6. import com.vaadin.terminal.gwt.client.Paintable;
  7. import com.vaadin.terminal.gwt.client.UIDL;
  8. import com.vaadin.terminal.gwt.client.Util;
  9. import com.vaadin.terminal.gwt.client.communication.ClientRpc;
  10. public abstract class MediaBaseConnector extends AbstractComponentConnector
  11. implements Paintable {
  12. public static final String TAG_SOURCE = "src";
  13. public static final String ATTR_MUTED = "muted";
  14. public static final String ATTR_CONTROLS = "ctrl";
  15. public static final String ATTR_AUTOPLAY = "auto";
  16. public static final String ATTR_RESOURCE = "res";
  17. public static final String ATTR_RESOURCE_TYPE = "type";
  18. public static final String ATTR_HTML = "html";
  19. public static final String ATTR_ALT_TEXT = "alt";
  20. /**
  21. * Server to client RPC interface for controlling playback of the media.
  22. *
  23. * @since 7.0
  24. */
  25. public static interface MediaControl extends ClientRpc {
  26. /**
  27. * Start playing the media.
  28. */
  29. public void play();
  30. /**
  31. * Pause playback of the media.
  32. */
  33. public void pause();
  34. }
  35. @Override
  36. protected void init() {
  37. super.init();
  38. registerRpc(MediaControl.class, new MediaControl() {
  39. public void play() {
  40. getWidget().play();
  41. }
  42. public void pause() {
  43. getWidget().pause();
  44. }
  45. });
  46. }
  47. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  48. if (!isRealUpdate(uidl)) {
  49. return;
  50. }
  51. getWidget().setControls(shouldShowControls(uidl));
  52. getWidget().setAutoplay(shouldAutoplay(uidl));
  53. getWidget().setMuted(isMediaMuted(uidl));
  54. // Add all sources
  55. for (int ix = 0; ix < uidl.getChildCount(); ix++) {
  56. UIDL child = uidl.getChildUIDL(ix);
  57. if (TAG_SOURCE.equals(child.getTag())) {
  58. getWidget()
  59. .addSource(getSourceUrl(child), getSourceType(child));
  60. }
  61. }
  62. setAltText(uidl);
  63. }
  64. protected boolean shouldShowControls(UIDL uidl) {
  65. return uidl.getBooleanAttribute(ATTR_CONTROLS);
  66. }
  67. private boolean shouldAutoplay(UIDL uidl) {
  68. return uidl.getBooleanAttribute(ATTR_AUTOPLAY);
  69. }
  70. private boolean isMediaMuted(UIDL uidl) {
  71. return uidl.getBooleanAttribute(ATTR_MUTED);
  72. }
  73. private boolean allowHtmlContent(UIDL uidl) {
  74. return uidl.getBooleanAttribute(ATTR_HTML);
  75. }
  76. @Override
  77. public VMediaBase getWidget() {
  78. return (VMediaBase) super.getWidget();
  79. }
  80. /**
  81. * @param uidl
  82. * @return the URL of a resource to be used as a source for the media
  83. */
  84. private String getSourceUrl(UIDL uidl) {
  85. String url = getConnection().translateVaadinUri(
  86. uidl.getStringAttribute(MediaBaseConnector.ATTR_RESOURCE));
  87. if (url == null) {
  88. return "";
  89. }
  90. return url;
  91. }
  92. /**
  93. * @param uidl
  94. * @return the mime type of the media
  95. */
  96. private String getSourceType(UIDL uidl) {
  97. return uidl.getStringAttribute(MediaBaseConnector.ATTR_RESOURCE_TYPE);
  98. }
  99. private void setAltText(UIDL uidl) {
  100. String alt = uidl.getStringAttribute(MediaBaseConnector.ATTR_ALT_TEXT);
  101. if (alt == null || "".equals(alt)) {
  102. alt = getWidget().getDefaultAltHtml();
  103. } else if (!allowHtmlContent(uidl)) {
  104. alt = Util.escapeHTML(alt);
  105. }
  106. getWidget().setAltText(alt);
  107. }
  108. }