選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Embedded.java 11KB

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