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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.ui;
  5. import java.util.Hashtable;
  6. import java.util.Iterator;
  7. import com.vaadin.terminal.PaintException;
  8. import com.vaadin.terminal.PaintTarget;
  9. import com.vaadin.terminal.Resource;
  10. import com.vaadin.terminal.gwt.client.ui.VEmbedded;
  11. /**
  12. * Component for embedding external objects.
  13. *
  14. * @author IT Mill Ltd.
  15. * @version
  16. * @VERSION@
  17. * @since 3.0
  18. */
  19. @SuppressWarnings("serial")
  20. @ClientWidget(VEmbedded.class)
  21. public class Embedded extends AbstractComponent {
  22. /**
  23. * General object type.
  24. */
  25. public static final int TYPE_OBJECT = 0;
  26. /**
  27. * Image types.
  28. */
  29. public static final int TYPE_IMAGE = 1;
  30. /**
  31. * Browser ("iframe") type.
  32. */
  33. public static final int TYPE_BROWSER = 2;
  34. /**
  35. * Type of the object.
  36. */
  37. private int type = TYPE_OBJECT;
  38. /**
  39. * Source of the embedded object.
  40. */
  41. private Resource source = null;
  42. /**
  43. * Generic object attributes.
  44. */
  45. private String mimeType = null;
  46. private String standby = null;
  47. /**
  48. * Hash of object parameteres.
  49. */
  50. private final Hashtable parameters = new Hashtable();
  51. /**
  52. * Applet or other client side runnable properties.
  53. */
  54. private String codebase = null;
  55. private String codetype = null;
  56. private String classId = null;
  57. private String archive = null;
  58. /**
  59. * Creates a new empty Embedded object.
  60. */
  61. public Embedded() {
  62. }
  63. /**
  64. * Creates a new empty Embedded object with caption.
  65. *
  66. * @param caption
  67. */
  68. public Embedded(String caption) {
  69. setCaption(caption);
  70. }
  71. /**
  72. * Creates a new Embedded object whose contents is loaded from given
  73. * resource. The dimensions are assumed if possible. The type is guessed
  74. * from resource.
  75. *
  76. * @param caption
  77. * @param source
  78. * the Source of the embedded object.
  79. */
  80. public Embedded(String caption, Resource source) {
  81. setCaption(caption);
  82. setSource(source);
  83. }
  84. /**
  85. * Gets the component UIDL tag.
  86. *
  87. * @return the Component UIDL tag as string.
  88. */
  89. @Override
  90. public String getTag() {
  91. return "embedded";
  92. }
  93. /**
  94. * Invoked when the component state should be painted.
  95. */
  96. @Override
  97. public void paintContent(PaintTarget target) throws PaintException {
  98. switch (type) {
  99. case TYPE_IMAGE:
  100. target.addAttribute("type", "image");
  101. break;
  102. case TYPE_BROWSER:
  103. target.addAttribute("type", "browser");
  104. break;
  105. default:
  106. break;
  107. }
  108. if (getSource() != null) {
  109. target.addAttribute("src", getSource());
  110. }
  111. if (mimeType != null && !"".equals(mimeType)) {
  112. target.addAttribute("mimetype", mimeType);
  113. }
  114. if (classId != null && !"".equals(classId)) {
  115. target.addAttribute("classid", classId);
  116. }
  117. if (codebase != null && !"".equals(codebase)) {
  118. target.addAttribute("codebase", codebase);
  119. }
  120. if (codetype != null && !"".equals(codetype)) {
  121. target.addAttribute("codetype", codetype);
  122. }
  123. if (standby != null && !"".equals(standby)) {
  124. target.addAttribute("standby", standby);
  125. }
  126. if (archive != null && !"".equals(archive)) {
  127. target.addAttribute("archive", archive);
  128. }
  129. // Params
  130. for (final Iterator i = getParameterNames(); i.hasNext();) {
  131. target.startTag("embeddedparam");
  132. final String key = (String) i.next();
  133. target.addAttribute("name", key);
  134. target.addAttribute("value", getParameter(key));
  135. target.endTag("embeddedparam");
  136. }
  137. }
  138. /**
  139. * Sets an object parameter. Parameters are optional information, and they
  140. * are passed to the instantiated object. Parameters are are stored as name
  141. * value pairs. This overrides the previous value assigned to this
  142. * parameter.
  143. *
  144. * @param name
  145. * the name of the parameter.
  146. * @param value
  147. * the value of the parameter.
  148. */
  149. public void setParameter(String name, String value) {
  150. parameters.put(name, value);
  151. requestRepaint();
  152. }
  153. /**
  154. * Gets the value of an object parameter. Parameters are optional
  155. * information, and they are passed to the instantiated object. Parameters
  156. * are are stored as name value pairs.
  157. *
  158. * @return the Value of parameter or null if not found.
  159. */
  160. public String getParameter(String name) {
  161. return (String) parameters.get(name);
  162. }
  163. /**
  164. * Removes an object parameter from the list.
  165. *
  166. * @param name
  167. * the name of the parameter to remove.
  168. */
  169. public void removeParameter(String name) {
  170. parameters.remove(name);
  171. requestRepaint();
  172. }
  173. /**
  174. * Gets the embedded object parameter names.
  175. *
  176. * @return the Iterator of parameters names.
  177. */
  178. public Iterator getParameterNames() {
  179. return parameters.keySet().iterator();
  180. }
  181. /**
  182. * Gets the codebase, the root-path used to access resources with relative
  183. * paths.
  184. *
  185. * @return the code base.
  186. */
  187. public String getCodebase() {
  188. return codebase;
  189. }
  190. /**
  191. * Gets the MIME-Type of the code.
  192. *
  193. * @return the MIME-Type of the code.
  194. */
  195. public String getCodetype() {
  196. return codetype;
  197. }
  198. /**
  199. * Gets the MIME-Type of the object.
  200. *
  201. * @return the MIME-Type of the object.
  202. */
  203. public String getMimeType() {
  204. return mimeType;
  205. }
  206. /**
  207. * Gets the standby text displayed when the object is loading.
  208. *
  209. * @return the standby text.
  210. */
  211. public String getStandby() {
  212. return standby;
  213. }
  214. /**
  215. * Sets the codebase, the root-path used to access resources with relative
  216. * paths.
  217. *
  218. * @param codebase
  219. * the codebase to set.
  220. */
  221. public void setCodebase(String codebase) {
  222. if (codebase != this.codebase
  223. || (codebase != null && !codebase.equals(this.codebase))) {
  224. this.codebase = codebase;
  225. requestRepaint();
  226. }
  227. }
  228. /**
  229. * Sets the codetype, the MIME-Type of the code.
  230. *
  231. * @param codetype
  232. * the codetype to set.
  233. */
  234. public void setCodetype(String codetype) {
  235. if (codetype != this.codetype
  236. || (codetype != null && !codetype.equals(this.codetype))) {
  237. this.codetype = codetype;
  238. requestRepaint();
  239. }
  240. }
  241. /**
  242. * Sets the mimeType, the MIME-Type of the object.
  243. *
  244. * @param mimeType
  245. * the mimeType to set.
  246. */
  247. public void setMimeType(String mimeType) {
  248. if (mimeType != this.mimeType
  249. || (mimeType != null && !mimeType.equals(this.mimeType))) {
  250. this.mimeType = mimeType;
  251. requestRepaint();
  252. }
  253. }
  254. /**
  255. * Sets the standby, the text to display while loading the object.
  256. *
  257. * @param standby
  258. * the standby to set.
  259. */
  260. public void setStandby(String standby) {
  261. if (standby != this.standby
  262. || (standby != null && !standby.equals(this.standby))) {
  263. this.standby = standby;
  264. requestRepaint();
  265. }
  266. }
  267. /**
  268. * Gets the classId attribute.
  269. *
  270. * @return the class id.
  271. */
  272. public String getClassId() {
  273. return classId;
  274. }
  275. /**
  276. * Sets the classId attribute.
  277. *
  278. * @param classId
  279. * the classId to set.
  280. */
  281. public void setClassId(String classId) {
  282. if (classId != this.classId
  283. || (classId != null && !classId.equals(classId))) {
  284. this.classId = classId;
  285. requestRepaint();
  286. }
  287. }
  288. /**
  289. * Gets the resource contained in the embedded object.
  290. *
  291. * @return the Resource
  292. */
  293. public Resource getSource() {
  294. return source;
  295. }
  296. /**
  297. * Gets the type of the embedded object.
  298. * <p>
  299. * This can be one of the following:
  300. * <ul>
  301. * <li>TYPE_OBJECT <i>(This is the default)</i>
  302. * <li>TYPE_IMAGE
  303. * </ul>
  304. * </p>
  305. *
  306. * @return the type.
  307. */
  308. public int getType() {
  309. return type;
  310. }
  311. /**
  312. * Sets the object source resource. The dimensions are assumed if possible.
  313. * The type is guessed from resource.
  314. *
  315. * @param source
  316. * the source to set.
  317. */
  318. public void setSource(Resource source) {
  319. if (source != null && !source.equals(this.source)) {
  320. this.source = source;
  321. final String mt = source.getMIMEType();
  322. if (mimeType == null) {
  323. mimeType = mt;
  324. }
  325. if (mt.equals("image/svg+xml")) {
  326. type = TYPE_OBJECT;
  327. } else if ((mt.substring(0, mt.indexOf("/"))
  328. .equalsIgnoreCase("image"))) {
  329. type = TYPE_IMAGE;
  330. } else {
  331. // Keep previous type
  332. }
  333. requestRepaint();
  334. }
  335. }
  336. /**
  337. * Sets the object type.
  338. * <p>
  339. * This can be one of the following:
  340. * <ul>
  341. * <li>TYPE_OBJECT <i>(This is the default)</i>
  342. * <li>TYPE_IMAGE
  343. * </ul>
  344. * </p>
  345. *
  346. * @param type
  347. * the type to set.
  348. */
  349. public void setType(int type) {
  350. if (type != TYPE_OBJECT && type != TYPE_IMAGE && type != TYPE_BROWSER) {
  351. throw new IllegalArgumentException("Unsupported type");
  352. }
  353. if (type != this.type) {
  354. this.type = type;
  355. requestRepaint();
  356. }
  357. }
  358. /**
  359. * Gets the archive attribute.
  360. *
  361. * @return the archive attribute.
  362. */
  363. public String getArchive() {
  364. return archive;
  365. }
  366. /**
  367. * Sets the archive attribute.
  368. *
  369. * @param archive
  370. * the archive string to set.
  371. */
  372. public void setArchive(String archive) {
  373. if (archive != this.archive
  374. || (archive != null && !archive.equals(this.archive))) {
  375. this.archive = archive;
  376. requestRepaint();
  377. }
  378. }
  379. }