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.

Embedded.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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.ui;
  17. import java.util.Iterator;
  18. import com.vaadin.event.MouseEvents.ClickEvent;
  19. import com.vaadin.event.MouseEvents.ClickListener;
  20. import com.vaadin.server.Resource;
  21. import com.vaadin.shared.EventId;
  22. import com.vaadin.shared.Registration;
  23. import com.vaadin.shared.ui.embedded.EmbeddedServerRpc;
  24. import com.vaadin.shared.ui.embedded.EmbeddedState;
  25. /**
  26. * A component for embedding external objects.
  27. * <p>
  28. * The {@code Embedded} component is used to display various types of multimedia
  29. * content using the HTML {@code <object>} element. This includes PDF documents,
  30. * Java applets, and QuickTime videos. Installing a browser plug-in is usually
  31. * required to actually view the embedded content.
  32. * <p>
  33. * Note that before Vaadin 7, {@code Embedded} was also used to display images,
  34. * Adobe Flash objects, and embedded web pages. This use of the component is
  35. * deprecated in Vaadin 7; the {@link Image}, {@link Flash}, and
  36. * {@link BrowserFrame} components should be used instead, respectively.
  37. *
  38. * @see Video
  39. * @see Audio
  40. *
  41. * @author Vaadin Ltd.
  42. * @since 3.0
  43. */
  44. @SuppressWarnings("serial")
  45. public class Embedded extends AbstractComponent {
  46. /**
  47. * General object type.
  48. */
  49. public static final int TYPE_OBJECT = 0;
  50. /**
  51. * Image types.
  52. *
  53. * @deprecated As of 7.0, use the {@link Image} component instead.
  54. */
  55. @Deprecated
  56. public static final int TYPE_IMAGE = 1;
  57. /**
  58. * Browser ("iframe") type.
  59. *
  60. * @deprecated As of 7.0, use the {@link BrowserFrame} component instead.
  61. */
  62. @Deprecated
  63. public static final int TYPE_BROWSER = 2;
  64. private EmbeddedServerRpc rpc = mouseDetails -> fireEvent(
  65. new ClickEvent(Embedded.this, mouseDetails));
  66. /**
  67. * Creates a new empty Embedded object.
  68. */
  69. public Embedded() {
  70. registerRpc(rpc);
  71. }
  72. /**
  73. * Creates a new empty Embedded object with caption.
  74. *
  75. * @param caption
  76. */
  77. public Embedded(String caption) {
  78. this();
  79. setCaption(caption);
  80. }
  81. /**
  82. * Creates a new Embedded object whose contents is loaded from given
  83. * resource. The dimensions are assumed if possible. The type is guessed
  84. * from resource.
  85. *
  86. * @param caption
  87. * @param source
  88. * the Source of the embedded object.
  89. */
  90. public Embedded(String caption, Resource source) {
  91. this(caption);
  92. setSource(source);
  93. }
  94. /**
  95. * Sets this component's "alt-text", that is, an alternate text that can be
  96. * presented instead of this component's normal content, for accessibility
  97. * purposes. Does not work when {@link #setType(int)} has been called with
  98. * {@link #TYPE_BROWSER}.
  99. *
  100. * @param altText
  101. * A short, human-readable description of this component's
  102. * content.
  103. * @since 6.8
  104. */
  105. public void setAlternateText(String altText) {
  106. String oldAltText = getAlternateText();
  107. if (altText != oldAltText
  108. || (altText != null && !altText.equals(oldAltText))) {
  109. getState().altText = altText;
  110. }
  111. }
  112. /**
  113. * Gets this component's "alt-text".
  114. *
  115. * @see #setAlternateText(String)
  116. */
  117. public String getAlternateText() {
  118. return getState(false).altText;
  119. }
  120. /**
  121. * Sets an object parameter. Parameters are optional information, and they
  122. * are passed to the instantiated object. Parameters are are stored as name
  123. * value pairs. This overrides the previous value assigned to this
  124. * parameter.
  125. *
  126. * @param name
  127. * the name of the parameter.
  128. * @param value
  129. * the value of the parameter.
  130. */
  131. public void setParameter(String name, String value) {
  132. getState().parameters.put(name, value);
  133. }
  134. /**
  135. * Gets the value of an object parameter. Parameters are optional
  136. * information, and they are passed to the instantiated object. Parameters
  137. * are are stored as name value pairs.
  138. *
  139. * @return the Value of parameter or null if not found.
  140. */
  141. public String getParameter(String name) {
  142. return getState(false).parameters.get(name);
  143. }
  144. /**
  145. * Removes an object parameter from the list.
  146. *
  147. * @param name
  148. * the name of the parameter to remove.
  149. */
  150. public void removeParameter(String name) {
  151. getState().parameters.remove(name);
  152. }
  153. /**
  154. * Gets the embedded object parameter names.
  155. *
  156. * @return the Iterator of parameters names.
  157. */
  158. public Iterator<String> getParameterNames() {
  159. return getState(false).parameters.keySet().iterator();
  160. }
  161. /**
  162. * This attribute specifies the base path used to resolve relative URIs
  163. * specified by the classid, data, and archive attributes. When absent, its
  164. * default value is the base URI of the current document.
  165. *
  166. * @return the code base.
  167. */
  168. public String getCodebase() {
  169. return getState(false).codebase;
  170. }
  171. /**
  172. * Gets the MIME-Type of the code.
  173. *
  174. * @return the MIME-Type of the code.
  175. */
  176. public String getCodetype() {
  177. return getState(false).codetype;
  178. }
  179. /**
  180. * Gets the MIME-Type of the object.
  181. *
  182. * @return the MIME-Type of the object.
  183. */
  184. public String getMimeType() {
  185. return getState(false).mimeType;
  186. }
  187. /**
  188. * This attribute specifies a message that a user agent may render while
  189. * loading the object's implementation and data.
  190. *
  191. * @return The text displayed when loading
  192. */
  193. public String getStandby() {
  194. return getState(false).standby;
  195. }
  196. /**
  197. * This attribute specifies the base path used to resolve relative URIs
  198. * specified by the classid, data, and archive attributes. When absent, its
  199. * default value is the base URI of the current document.
  200. *
  201. * @param codebase
  202. * The base path
  203. */
  204. public void setCodebase(String codebase) {
  205. String oldCodebase = getCodebase();
  206. if (codebase != oldCodebase
  207. || (codebase != null && !codebase.equals(oldCodebase))) {
  208. getState().codebase = codebase;
  209. }
  210. }
  211. /**
  212. * This attribute specifies the content type of data expected when
  213. * downloading the object specified by classid. This attribute is optional
  214. * but recommended when classid is specified since it allows the user agent
  215. * to avoid loading information for unsupported content types. When absent,
  216. * it defaults to the value of the type attribute.
  217. *
  218. * @param codetype
  219. * the codetype to set.
  220. */
  221. public void setCodetype(String codetype) {
  222. String oldCodetype = getCodetype();
  223. if (codetype != oldCodetype
  224. || (codetype != null && !codetype.equals(oldCodetype))) {
  225. getState().codetype = codetype;
  226. }
  227. }
  228. /**
  229. * Sets the mimeType, the MIME-Type of the object.
  230. *
  231. * @param mimeType
  232. * the mimeType to set.
  233. */
  234. public void setMimeType(String mimeType) {
  235. String oldMimeType = getMimeType();
  236. if (mimeType != oldMimeType
  237. || (mimeType != null && !mimeType.equals(oldMimeType))) {
  238. getState().mimeType = mimeType;
  239. if ("application/x-shockwave-flash".equals(mimeType)) {
  240. /*
  241. * Automatically add wmode transparent as we use lots of
  242. * floating layers in Vaadin. If developers need better flash
  243. * performance, they can override this value programmatically
  244. * back to "window" (the default).
  245. */
  246. if (getParameter("wmode") == null) {
  247. setParameter("wmode", "transparent");
  248. }
  249. }
  250. }
  251. }
  252. /**
  253. * This attribute specifies a message that a user agent may render while
  254. * loading the object's implementation and data.
  255. *
  256. * @param standby
  257. * The text to display while loading
  258. */
  259. public void setStandby(String standby) {
  260. String oldStandby = getStandby();
  261. if (standby != oldStandby
  262. || (standby != null && !standby.equals(oldStandby))) {
  263. getState().standby = standby;
  264. }
  265. }
  266. /**
  267. * This attribute may be used to specify the location of an object's
  268. * implementation via a URI.
  269. *
  270. * @return the classid.
  271. */
  272. public String getClassId() {
  273. return getState(false).classId;
  274. }
  275. /**
  276. * This attribute may be used to specify the location of an object's
  277. * implementation via a URI.
  278. *
  279. * @param classId
  280. * the classId to set.
  281. */
  282. public void setClassId(String classId) {
  283. String oldClassId = getClassId();
  284. if (classId != oldClassId
  285. || (classId != null && !classId.equals(oldClassId))) {
  286. getState().classId = classId;
  287. }
  288. }
  289. /**
  290. * Gets the resource contained in the embedded object.
  291. *
  292. * @return the Resource
  293. */
  294. public Resource getSource() {
  295. return getResource("src");
  296. }
  297. /**
  298. * Gets the type of the embedded object.
  299. * <p>
  300. * This can be one of the following:
  301. * <ul>
  302. * <li>TYPE_OBJECT <i>(This is the default)</i>
  303. * <li>TYPE_IMAGE
  304. * </ul>
  305. * </p>
  306. *
  307. * @return the type.
  308. */
  309. public int getType() {
  310. return getState(false).type;
  311. }
  312. /**
  313. * Sets the object source resource. The dimensions are assumed if possible.
  314. * The type is guessed from resource.
  315. *
  316. * @param source
  317. * the source to set.
  318. */
  319. public void setSource(Resource source) {
  320. if (source != null && !source.equals(getSource())) {
  321. setResource("src", source);
  322. final String mt = source.getMIMEType();
  323. if (getMimeType() == null) {
  324. getState().mimeType = mt;
  325. }
  326. if (mt.equals("image/svg+xml")) {
  327. getState().type = TYPE_OBJECT;
  328. } else if ((mt.substring(0, mt.indexOf('/'))
  329. .equalsIgnoreCase("image"))) {
  330. getState().type = TYPE_IMAGE;
  331. } else {
  332. // Keep previous type
  333. }
  334. }
  335. }
  336. /**
  337. * Sets the object type.
  338. * <p>
  339. * This can be one of the following:
  340. * <ul>
  341. * <li>{@link #TYPE_OBJECT} <i>(This is the default)</i>
  342. * <li>{@link #TYPE_IMAGE} <i>(Deprecated)</i>
  343. * <li>{@link #TYPE_BROWSER} <i>(Deprecated)</i>
  344. * </ul>
  345. * </p>
  346. *
  347. * @param type
  348. * the type to set.
  349. */
  350. public void setType(int type) {
  351. if (type != TYPE_OBJECT && type != TYPE_IMAGE && type != TYPE_BROWSER) {
  352. throw new IllegalArgumentException("Unsupported type");
  353. }
  354. if (type != getType()) {
  355. getState().type = type;
  356. }
  357. }
  358. /**
  359. * This attribute may be used to specify a space-separated list of URIs for
  360. * archives containing resources relevant to the object, which may include
  361. * the resources specified by the classid and data attributes. Preloading
  362. * archives will generally result in reduced load times for objects.
  363. * Archives specified as relative URIs should be interpreted relative to the
  364. * codebase attribute.
  365. *
  366. * @return Space-separated list of URIs with resources relevant to the
  367. * object
  368. */
  369. public String getArchive() {
  370. return getState(false).archive;
  371. }
  372. /**
  373. * This attribute may be used to specify a space-separated list of URIs for
  374. * archives containing resources relevant to the object, which may include
  375. * the resources specified by the classid and data attributes. Preloading
  376. * archives will generally result in reduced load times for objects.
  377. * Archives specified as relative URIs should be interpreted relative to the
  378. * codebase attribute.
  379. *
  380. * @param archive
  381. * Space-separated list of URIs with resources relevant to the
  382. * object
  383. */
  384. public void setArchive(String archive) {
  385. String oldArchive = getArchive();
  386. if (archive != oldArchive
  387. || (archive != null && !archive.equals(oldArchive))) {
  388. getState().archive = archive;
  389. }
  390. }
  391. /**
  392. * Add a click listener to the component. The listener is called whenever
  393. * the user clicks inside the component. Depending on the content the event
  394. * may be blocked and in that case no event is fired.
  395. *
  396. * @see Registration
  397. *
  398. * @param listener
  399. * The listener to add
  400. * @return a registration object for removing the listener
  401. * @since 8.0
  402. */
  403. public Registration addClickListener(ClickListener listener) {
  404. return addListener(EventId.CLICK_EVENT_IDENTIFIER, ClickEvent.class,
  405. listener, ClickListener.clickMethod);
  406. }
  407. /**
  408. * Remove a click listener from the component. The listener should earlier
  409. * have been added using {@link #addClickListener(ClickListener)}.
  410. *
  411. * @param listener
  412. * The listener to remove
  413. *
  414. * @deprecated As of 8.0, replaced by {@link Registration#remove()} in the
  415. * registration object returned from
  416. * {@link #addClickListener(ClickListener)}.
  417. */
  418. @Deprecated
  419. public void removeClickListener(ClickListener listener) {
  420. removeListener(EventId.CLICK_EVENT_IDENTIFIER, ClickEvent.class,
  421. listener);
  422. }
  423. @Override
  424. protected EmbeddedState getState() {
  425. return (EmbeddedState) super.getState();
  426. }
  427. @Override
  428. protected EmbeddedState getState(boolean markAsDirty) {
  429. return (EmbeddedState) super.getState(markAsDirty);
  430. }
  431. }