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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui;
  17. import com.vaadin.client.WidgetUtil;
  18. import com.vaadin.client.communication.StateChangeEvent;
  19. import com.vaadin.shared.communication.URLReference;
  20. import com.vaadin.shared.ui.AbstractMediaState;
  21. import com.vaadin.shared.ui.MediaControl;
  22. import com.vaadin.shared.ui.PreloadMode;
  23. public abstract class MediaBaseConnector extends AbstractComponentConnector {
  24. @Override
  25. protected void init() {
  26. super.init();
  27. registerRpc(MediaControl.class, new MediaControl() {
  28. @Override
  29. public void play() {
  30. getWidget().play();
  31. }
  32. @Override
  33. public void pause() {
  34. getWidget().pause();
  35. }
  36. });
  37. }
  38. @Override
  39. public AbstractMediaState getState() {
  40. return (AbstractMediaState) super.getState();
  41. }
  42. @Override
  43. public void onStateChanged(StateChangeEvent event) {
  44. super.onStateChanged(event);
  45. final VMediaBase widget = getWidget();
  46. final AbstractMediaState state = getState();
  47. setAltText(state.altText); // must do before loading sources
  48. setPreload(state.preload);
  49. widget.setLoop(state.loop);
  50. widget.setAutoplay(state.autoplay);
  51. widget.setMuted(state.muted);
  52. widget.setControls(state.showControls);
  53. if (event.hasPropertyChanged("sources")) {
  54. widget.removeAllSources();
  55. for (int i = 0; i < state.sources.size(); i++) {
  56. URLReference source = state.sources.get(i);
  57. String sourceType = state.sourceTypes.get(i);
  58. widget.addSource(source.getURL(), sourceType);
  59. }
  60. widget.load();
  61. }
  62. }
  63. @Override
  64. public VMediaBase getWidget() {
  65. return (VMediaBase) super.getWidget();
  66. }
  67. private void setAltText(String altText) {
  68. if (altText == null || "".equals(altText)) {
  69. altText = getDefaultAltHtml();
  70. } else if (!getState().htmlContentAllowed) {
  71. altText = WidgetUtil.escapeHTML(altText);
  72. }
  73. getWidget().setAltText(altText);
  74. }
  75. private void setPreload(final PreloadMode preload) {
  76. if (preload != null) {
  77. getWidget().setPreload(preload.getValue());
  78. }
  79. }
  80. /**
  81. * @return the default HTML to show users with browsers that do not support
  82. * HTML5 media markup.
  83. */
  84. protected abstract String getDefaultAltHtml();
  85. }