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.

ICoverflow.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.demo.coverflow.gwt.client.ui;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8. import com.google.gwt.core.client.GWT;
  9. import com.google.gwt.user.client.ui.Composite;
  10. import com.google.gwt.user.client.ui.HTML;
  11. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  12. import com.vaadin.terminal.gwt.client.Paintable;
  13. import com.vaadin.terminal.gwt.client.UIDL;
  14. public class ICoverflow extends Composite implements Paintable {
  15. private String uidlId;
  16. protected ApplicationConnection client;
  17. private ArrayList coverList = new ArrayList();
  18. private Object _selected;
  19. private boolean flashInited = false;
  20. private HTML flash;
  21. private boolean scrollbarVisibility = true;
  22. private String backgroundGradientStart;
  23. private String backgroundGradientEnd;
  24. private boolean colorChanged = false;
  25. private boolean sbVisibilityChanged = false;
  26. private HashMap keyMap = new HashMap();
  27. /**
  28. * Constructor
  29. */
  30. public ICoverflow() {
  31. flash = new HTML();
  32. initWidget(flash);
  33. }
  34. /**
  35. * This method accepts parses the uidl sent by the server
  36. *
  37. * @param UIDL
  38. * uidl
  39. * @param ApplicationConnection
  40. * client
  41. */
  42. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  43. // Store variables
  44. uidlId = uidl.getId();
  45. this.client = client;
  46. String tempColor;
  47. if (client.updateComponent(this, uidl, true)) {
  48. return;
  49. }
  50. // Has the scrollbar's visibility status changed?
  51. if (uidl.hasAttribute("scrollbarVisibility")) {
  52. boolean tempVis = uidl.getBooleanAttribute("scrollbarVisibility");
  53. if (scrollbarVisibility != tempVis) {
  54. scrollbarVisibility = tempVis;
  55. sbVisibilityChanged = true;
  56. }
  57. }
  58. // Has the start gradient changed?
  59. if (uidl.hasAttribute("backgroundGradientStart")) {
  60. tempColor = uidl.getStringAttribute("backgroundGradientStart")
  61. .toString();
  62. if (tempColor != backgroundGradientStart) {
  63. backgroundGradientStart = tempColor;
  64. colorChanged = true;
  65. }
  66. }
  67. // Has the end gradient changed?
  68. if (uidl.hasAttribute("backgroundGradientEnd")) {
  69. tempColor = uidl.getStringAttribute("backgroundGradientEnd")
  70. .toString();
  71. if (tempColor != backgroundGradientEnd) {
  72. backgroundGradientEnd = tempColor;
  73. colorChanged = true;
  74. }
  75. }
  76. final UIDL images = uidl.getChildUIDL(0);
  77. // Check which covers should be removed. This array list contains all
  78. // current
  79. // covers. We remove from this list all covers which are sent with the
  80. // repainted
  81. // uidl. All remaining covers in this list should be "old" ones and are
  82. // should
  83. // be deleted.
  84. ArrayList newList = new ArrayList();
  85. // Iterate through all option elements
  86. for (final Iterator i = images.getChildIterator(); i.hasNext();) {
  87. final UIDL imgUidl = (UIDL) i.next();
  88. // Make sure all required attributes exist
  89. if (imgUidl.hasAttribute("caption") && imgUidl.hasAttribute("key")
  90. && imgUidl.hasAttribute("icon")) {
  91. HashMap set = new HashMap();
  92. // Update the key map
  93. keyMap.put(imgUidl.getStringAttribute("caption"), imgUidl
  94. .getStringAttribute("key"));
  95. // Get information
  96. set.put("icon", client.translateToolkitUri(imgUidl
  97. .getStringAttribute("icon")));
  98. set.put("caption", imgUidl.getStringAttribute("caption"));
  99. newList.add(set);
  100. // Is the current cover selected?
  101. if (imgUidl.hasAttribute("selected")) {
  102. _selected = imgUidl.getStringAttribute("caption");
  103. }
  104. }
  105. }
  106. // Deleted items
  107. ArrayList intersectList = new ArrayList();
  108. intersectList.addAll(coverList);
  109. intersectList.removeAll(newList);
  110. if (flashInited) {
  111. for (int i = 0; i < intersectList.size(); i++) {
  112. HashMap cover = (HashMap) intersectList.get(i);
  113. removeCover(uidlId, cover.get("caption").toString());
  114. }
  115. }
  116. // Added items
  117. intersectList = new ArrayList();
  118. intersectList.addAll(newList);
  119. intersectList.removeAll(coverList);
  120. if (flashInited) {
  121. for (int i = 0; i < intersectList.size(); i++) {
  122. HashMap cover = (HashMap) intersectList.get(i);
  123. addCover(uidlId, cover.get("caption").toString(), cover.get(
  124. "icon").toString());
  125. }
  126. }
  127. coverList = newList;
  128. // Has the flash been initialized?
  129. if (!flashInited) {
  130. colorChanged = false;
  131. setFlash();
  132. initializeMethods(uidlId);
  133. }
  134. // Inform flash of the selected cover
  135. if (_selected != null && flashInited) {
  136. selectCover(uidlId, _selected.toString());
  137. }
  138. if (colorChanged && flashInited) {
  139. setBackgroundColor(uidlId, backgroundGradientStart,
  140. backgroundGradientEnd);
  141. colorChanged = false;
  142. }
  143. if (sbVisibilityChanged && flashInited) {
  144. toggleScrollbarVisibility(uidlId, scrollbarVisibility);
  145. sbVisibilityChanged = false;
  146. }
  147. }
  148. /**
  149. * Inform the server which cover is selected
  150. *
  151. * @param String
  152. * coverKey
  153. */
  154. public void setCover(String coverId) {
  155. if (uidlId == null || client == null) {
  156. return;
  157. }
  158. client.updateVariable(uidlId, "selected", new String[] { keyMap.get(
  159. coverId).toString() }, true);
  160. }
  161. /**
  162. * Initialize the native javascript functions needed for the flash <-> GWT
  163. * communication
  164. *
  165. * @param String
  166. * id
  167. */
  168. public native void initializeMethods(String id) /*-{
  169. var app = this;
  170. if($wnd.itmill.coverflow == null)
  171. var coverflow = [];
  172. else
  173. var coverflow = $wnd.itmill.coverflow;
  174. coverflow['getCovers_' + id] = function() {
  175. app.@com.vaadin.demo.coverflow.gwt.client.ui.ICoverflow::getCovers()();
  176. };
  177. coverflow['setCurrent_' + id] = function(selected) {
  178. app.@com.vaadin.demo.coverflow.gwt.client.ui.ICoverflow::setCover(Ljava/lang/String;)(selected);
  179. };
  180. $wnd.itmill.coverflow = coverflow;
  181. }-*/;
  182. /**
  183. * This function sends all covers to the flash. We cannot do this directly
  184. * in the updateFromUIDL method, because we cannot be sure if the flash has
  185. * been loaded into the browser. The flash will call for this method when
  186. * it's ready.
  187. */
  188. public void getCovers() {
  189. // Loop through all stored coves
  190. for (int i = 0; i < coverList.size(); i++) {
  191. HashMap set = (HashMap) coverList.get(i);
  192. try {
  193. // Add the cover
  194. addCover(uidlId, set.get("caption").toString(), set.get("icon")
  195. .toString());
  196. } catch (Exception e) {
  197. // Do not add covers lacking obligatory data
  198. }
  199. }
  200. // The flash calls for this method, therefore we can be sure that the
  201. // flash has been loaded
  202. // into the browser.
  203. flashInited = true;
  204. // Set selected cover
  205. if (_selected != null) {
  206. selectCover(uidlId, _selected.toString());
  207. }
  208. }
  209. /**
  210. * This function is a native javascript function which adds covers to the
  211. * actual flash. This method works as a bridge between GWT and flash.
  212. *
  213. * @param id
  214. * @param key
  215. * @param caption
  216. * @param icon
  217. */
  218. public native void addCover(String id, String caption, String icon) /*-{
  219. try {
  220. $doc['fxcoverflow' + id].addCover(caption.toString(), icon.toString());
  221. }
  222. catch(e) {
  223. $wnd.alert(e.message);
  224. }
  225. }-*/;
  226. /**
  227. * This function tells the flash which cover should be selected.
  228. *
  229. * @param id
  230. * @param key
  231. */
  232. public native void selectCover(String id, String key) /*-{
  233. $doc["fxcoverflow" + id].selectCover(key.toString());
  234. }-*/;
  235. public native void setBackgroundColor(String id, String startGradient,
  236. String endGradient) /*-{
  237. $doc["fxcoverflow" + id].setBackgroundColor("0x" + startGradient.toString(), "0x" + endGradient.toString());
  238. }-*/;
  239. public native void toggleScrollbarVisibility(String id, boolean visibility) /*-{
  240. $doc["fxcoverflow" + id].toggleScrollbarVisibility(visibility);
  241. }-*/;
  242. public native void removeCover(String id, String key) /*-{
  243. $doc["fxcoverflow" + id].removeCover(key);
  244. }-*/;
  245. /**
  246. * Set the HTML coding of the flash movie. This isn't done until the
  247. * updateFromUIDL method is called for the first time. The reason is that we
  248. * use an id from the UIDL to uniquely identify all instances of this
  249. * widget.
  250. */
  251. private void setFlash() {
  252. String html = "<object id=\"fxcoverflow"
  253. + uidlId
  254. + "\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" width=\"100%\""
  255. + " height=\"100%\" codebase=\"http://fpdownload.adobe.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\">"
  256. + "<param name=\"movie\" value=\""
  257. + GWT.getModuleBaseURL()
  258. + "coverflowflash.swf\">"
  259. + "<param name=\"quality\" value=\"high\">"
  260. + "<param name=\"flashVars\" value=\"pid="
  261. + uidlId
  262. + "&sbVis="
  263. + scrollbarVisibility
  264. + "&bgS=0x"
  265. + backgroundGradientStart
  266. + "&bgE=0x"
  267. + backgroundGradientEnd
  268. + "\" />"
  269. + "<embed name=\"fxcoverflow"
  270. + uidlId
  271. + "\" flashVars=\"pid="
  272. + uidlId
  273. + "&sbVis="
  274. + scrollbarVisibility
  275. + "&bgS=0x"
  276. + backgroundGradientStart
  277. + "&bgE=0x"
  278. + backgroundGradientEnd
  279. + "\" src=\""
  280. + GWT.getModuleBaseURL()
  281. + "coverflowflash.swf\" width=\"100%\" height=\"100%\" "
  282. + "quality=\"high\" "
  283. + "pluginspage=\"http://www.adobe.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">"
  284. + "</embed>" + "</object>";
  285. flash.setHTML(html);
  286. }
  287. }