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.

IntegratingAnExistingGWTWidget.asciidoc 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. ---
  2. title: Integrating An Existing GWT Widget
  3. order: 33
  4. layout: page
  5. ---
  6. [[integrating-an-existing-gwt-widget]]
  7. = Integrating an existing GWT widget
  8. Integrating an existing, third party GWT widget usually just involves
  9. creating a regular Vaadin component with a client-side connector that
  10. uses the third-party widget, probably using a shared state, and possibly
  11. also with RPC - just as described in separate articles on these topics.
  12. Usually, the only addition is the need to modify your widgetset
  13. declaration to inherit the third-party widget's GWT module.
  14. In the following, we'll integrate the SimplePlot widget from the
  15. http://code.google.com/p/gflot/[GFlot library] (which in turn is a GWT
  16. adaptation of the pure JavaScript plotting library Flot) to create a
  17. simple line plot component. We'll start with modifying our widgetset's
  18. `gwt.xml` to inherit the GFlot GWT module, so if you're familiar with the
  19. rest of the process, you can basically stop once that is done.
  20. But first a note on package structure: this particular example uses the
  21. `com.example` package domain, and is set up to be a add-on project, with
  22. the actual component in the `addon` package, and the demo that uses the
  23. component in the `vaadingflot` package. The `addon` package contains the
  24. `widgetset gwt.xml` definition, the server-side component (LinePlot), as
  25. well as all the code related to the client-side in the `client.ui`
  26. subpackage.
  27. Once you have a working project, go ahead and
  28. http://code.google.com/p/gflot/downloads/list[download the GFlot jar],
  29. and add it to `WEB-INF/lib`, then update the widgetset `gwt.xml` as follows:
  30. [source,xml]
  31. ....
  32. <?xml version="1.0" encoding="UTF-8"?>
  33. <!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.7.0//EN"
  34. "http://google-web-toolkit.googlecode.com/svn/tags/1.7.0/distro-source/core/src/gwt-module.dtd">
  35. <module>
  36. <inherits name="com.vaadin.DefaultWidgetSet" />
  37. <inherits name="ca.nanometrics.gflot.GFlot" />
  38. </module>
  39. ....
  40. It inherits the default Vaadin widgetset as well as the GFlot GWT
  41. module.
  42. Now we're ready to integrate the SimplePlot widget from the GFlot
  43. package. Since we're familiar with the GFlot API, we know we want to add
  44. 'series' to the plot - we'll create a shared state for this purpose, and
  45. a `DataSeries` class to represent the actual series within the state:
  46. [source,java]
  47. ....
  48. package com.example.addon.client.ui;
  49. import java.util.ArrayList;
  50. import java.util.List;
  51. import com.vaadin.client.ComponentState;
  52. public class LinePlotState extends AbstractComponentState {
  53. public List<DataSeries> series = new ArrayList<DataSeries>();
  54. public static class DataSeries {
  55. public String label;
  56. public String color;
  57. public List<Float> data;
  58. }
  59. }
  60. ....
  61. Lets make the server-side component next:
  62. [source,java]
  63. ....
  64. package com.example.addon;
  65. import java.util.Arrays;
  66. import com.example.addon.client.ui.LinePlotState;
  67. import com.example.addon.client.ui.LinePlotState.DataSeries;
  68. import com.vaadin.ui.AbstractComponent;
  69. public class LinePlot extends AbstractComponent {
  70. public LinePlot() {
  71. }
  72. @Override
  73. public LinePlotState getState() {
  74. return (LinePlotState) super.getState();
  75. }
  76. public void addSeries(String label, String color, Float[] fs) {
  77. DataSeries ds = new DataSeries();
  78. ds.label = label;
  79. ds.color = color;
  80. ds.data = Arrays.asList(fs);
  81. getState().series.add(ds);
  82. }
  83. }
  84. ....
  85. We override `getState()` in order to narrow the return type to our own
  86. `LinePlotState`, and then implement a simple `addSeries()` that creates a
  87. `DataSeries` instance and adds it to the state. The state will be
  88. automatically transmitted to the client when needed, so the plots will
  89. remain intact over browser reloads for instance.The API for our
  90. component could obviously be expanded, but lets leave it like this for
  91. this example.
  92. Since the GWT widget we're going to use is already made for us (in the
  93. GFlot library), the only thing left for us to do is implement the
  94. client-side connector:
  95. [source,java]
  96. ....
  97. package com.example.addon.client.ui;
  98. import ca.nanometrics.gflot.client.DataPoint;
  99. import ca.nanometrics.gflot.client.SeriesHandler;
  100. import ca.nanometrics.gflot.client.SimplePlot;
  101. import com.example.addon.LinePlot;
  102. import com.example.addon.client.ui.LinePlotState.DataSeries;
  103. import com.google.gwt.core.client.GWT;
  104. import com.google.gwt.user.client.ui.Widget;
  105. import com.vaadin.client.communication.StateChangeEvent;
  106. import com.vaadin.client.ui.AbstractComponentConnector;
  107. import com.vaadin.client.ui.Connect;
  108. @Connect(LinePlot.class)
  109. public class LinePlotConnector extends AbstractComponentConnector {
  110. @Override
  111. public LinePlotState getState() {
  112. return (LinePlotState) super.getState();
  113. }
  114. @Override
  115. public SimplePlot getWidget() {
  116. return (SimplePlot) super.getWidget();
  117. }
  118. @Override
  119. protected Widget createWidget() {
  120. return GWT.create(SimplePlot.class);
  121. }
  122. @Override
  123. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  124. super.onStateChanged(stateChangeEvent);
  125. getWidget().getModel().clear();
  126. for (DataSeries ds : getState().series) {
  127. SeriesHandler s = getWidget().getModel().addSeries(ds.label,
  128. ds.color);
  129. for (int i = 0; i < ds.data.size(); i++) {
  130. s.add(new DataPoint(i, ds.data.get(i)));
  131. }
  132. }
  133. getWidget().redraw();
  134. }
  135. }
  136. ....
  137. We override both `getState()` and `getWidget()` to narrow the return type to
  138. our liking, then make `createWidget()` return an instance of the GFlot
  139. widget we're going to use, `SimplePlot`.
  140. Last, we override `onStateChange()` which is called whenever the shared
  141. state has been changed. Here we make use of the `SimplePlot` API to add
  142. the series contained in the shared state (for simplicity, we clear the
  143. `SimplePlot` first, then add all the series in our state).
  144. That's it! The full source is available as an attachment to this
  145. article.
  146. link:img/vaadingflot.zip[Attachment vaadingflot.zip]