您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VBrowserFrame.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2000-2018 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.google.gwt.dom.client.Document;
  18. import com.google.gwt.dom.client.Element;
  19. import com.google.gwt.dom.client.IFrameElement;
  20. import com.google.gwt.user.client.ui.Widget;
  21. import com.vaadin.client.BrowserInfo;
  22. public class VBrowserFrame extends Widget {
  23. protected IFrameElement iframe;
  24. protected Element altElement;
  25. protected String altText;
  26. public static final String CLASSNAME = "v-browserframe";
  27. public VBrowserFrame() {
  28. Element root = Document.get().createDivElement();
  29. setElement(root);
  30. setStyleName(CLASSNAME);
  31. createAltTextElement();
  32. }
  33. /**
  34. * Always creates new iframe inside widget. Will replace previous iframe.
  35. *
  36. * @return
  37. */
  38. protected IFrameElement createIFrameElement(String src) {
  39. String name = null;
  40. // Remove alt text
  41. if (altElement != null) {
  42. getElement().removeChild(altElement);
  43. altElement = null;
  44. }
  45. // Remove old iframe
  46. if (iframe != null) {
  47. name = iframe.getAttribute("name");
  48. getElement().removeChild(iframe);
  49. iframe = null;
  50. }
  51. iframe = Document.get().createIFrameElement();
  52. iframe.setSrc(src);
  53. iframe.setFrameBorder(0);
  54. iframe.setAttribute("width", "100%");
  55. iframe.setAttribute("height", "100%");
  56. iframe.setAttribute("allowTransparency", "true");
  57. getElement().appendChild(iframe);
  58. // Reset old attributes (except src)
  59. if (name != null) {
  60. iframe.setName(name);
  61. }
  62. return iframe;
  63. }
  64. protected void createAltTextElement() {
  65. if (iframe != null) {
  66. return;
  67. }
  68. if (altElement == null) {
  69. altElement = Document.get().createSpanElement();
  70. getElement().appendChild(altElement);
  71. }
  72. if (altText != null) {
  73. altElement.setInnerText(altText);
  74. } else {
  75. altElement.setInnerText("");
  76. }
  77. }
  78. public void setAlternateText(String altText) {
  79. if (this.altText != altText) {
  80. this.altText = altText;
  81. if (altElement != null) {
  82. if (altText != null) {
  83. altElement.setInnerText(altText);
  84. } else {
  85. altElement.setInnerText("");
  86. }
  87. }
  88. }
  89. }
  90. /**
  91. * Set the source (the "src" attribute) of iframe. Will replace old iframe
  92. * with new.
  93. *
  94. * @param source
  95. * Source of iframe.
  96. */
  97. public void setSource(String source) {
  98. if (source == null) {
  99. if (iframe != null) {
  100. getElement().removeChild(iframe);
  101. iframe = null;
  102. }
  103. createAltTextElement();
  104. setAlternateText(altText);
  105. return;
  106. }
  107. if (iframe == null || iframe.getSrc() != source) {
  108. createIFrameElement(source);
  109. }
  110. }
  111. public void setName(String name) {
  112. if (iframe != null) {
  113. iframe.setName(name);
  114. }
  115. }
  116. @Override
  117. protected void onDetach() {
  118. if (BrowserInfo.get().isIE()) {
  119. // Force browser to fire unload event when component is detached
  120. // from the view (IE doesn't do this automatically)
  121. if (iframe != null) {
  122. /*
  123. * src was previously set to javascript:false, but this was not
  124. * enough to overcome a bug when detaching an iframe with a pdf
  125. * loaded in IE9. about:blank seems to cause the adobe reader
  126. * plugin to unload properly before the iframe is removed. See
  127. * #7855
  128. */
  129. iframe.setSrc("about:blank");
  130. }
  131. }
  132. super.onDetach();
  133. }
  134. }