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 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright 2000-2016 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. public abstract class MediaBaseConnector extends AbstractComponentConnector {
  23. @Override
  24. protected void init() {
  25. super.init();
  26. registerRpc(MediaControl.class, new MediaControl() {
  27. @Override
  28. public void play() {
  29. getWidget().play();
  30. }
  31. @Override
  32. public void pause() {
  33. getWidget().pause();
  34. }
  35. });
  36. }
  37. @Override
  38. public AbstractMediaState getState() {
  39. return (AbstractMediaState) super.getState();
  40. }
  41. @Override
  42. public void onStateChanged(StateChangeEvent event) {
  43. super.onStateChanged(event);
  44. final VMediaBase widget = getWidget();
  45. final AbstractMediaState state = getState();
  46. setAltText(state.altText); // must do before loading sources
  47. widget.setAutoplay(state.autoplay);
  48. widget.setMuted(state.muted);
  49. widget.setControls(state.showControls);
  50. if (event.hasPropertyChanged("sources")) {
  51. widget.removeAllSources();
  52. for (int i = 0; i < state.sources.size(); i++) {
  53. URLReference source = state.sources.get(i);
  54. String sourceType = state.sourceTypes.get(i);
  55. widget.addSource(source.getURL(), sourceType);
  56. }
  57. widget.load();
  58. }
  59. }
  60. @Override
  61. public VMediaBase getWidget() {
  62. return (VMediaBase) super.getWidget();
  63. }
  64. private void setAltText(String altText) {
  65. if (altText == null || "".equals(altText)) {
  66. altText = getDefaultAltHtml();
  67. } else if (!getState().htmlContentAllowed) {
  68. altText = WidgetUtil.escapeHTML(altText);
  69. }
  70. getWidget().setAltText(altText);
  71. }
  72. /**
  73. * @return the default HTML to show users with browsers that do not support
  74. * HTML5 media markup.
  75. */
  76. protected abstract String getDefaultAltHtml();
  77. }