Integrating an existing GWT widget

Integrating an existing, third party GWT widget usually just involves creating a regular Vaadin component with a client-side connector that uses the third-party widget, probably using a shared state, and possibly also with RPC - just as described in separate articles on these topics. Usually, the only addition is the need to modify your widgetset declaration to inherit the third-party widget’s GWT module.

In the following, we’ll integrate the SimplePlot widget from the GFlot library (which in turn is a GWT adaptation of the pure JavaScript plotting library Flot) to create a simple line plot component. We’ll start with modifying our widgetset’s gwt.xml to inherit the GFlot GWT module, so if you’re familiar with the rest of the process, you can basically stop once that is done.

But first a note on package structure: this particular example uses the com.example package domain, and is set up to be a add-on project, with the actual component in the addon package, and the demo that uses the component in the vaadingflot package. The addon package contains the widgetset gwt.xml definition, the server-side component (LinePlot), as well as all the code related to the client-side in the client.ui subpackage.

Once you have a working project, go ahead and download the GFlot jar, and add it to WEB-INF/lib, then update the widgetset gwt.xml as follows:

XML

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.7.0//EN"
  3. "http://google-web-toolkit.googlecode.com/svn/tags/1.7.0/distro-source/core/src/gwt-module.dtd">
  4. <module>
  5. <inherits name="com.vaadin.DefaultWidgetSet" />
  6. <inherits name="ca.nanometrics.gflot.GFlot" />
  7. </module>

It inherits the default Vaadin widgetset as well as the GFlot GWT module.

Now we’re ready to integrate the SimplePlot widget from the GFlot package. Since we’re familiar with the GFlot API, we know we want to add ‘series’ to the plot - we’ll create a shared state for this purpose, and a DataSeries class to represent the actual series within the state:

Java

  1. package com.example.addon.client.ui;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.vaadin.client.ComponentState;
  5. public class LinePlotState extends AbstractComponentState {
  6. public List<DataSeries> series = new ArrayList<DataSeries>();
  7. public static class DataSeries {
  8. public String label;
  9. public String color;
  10. public List<Float> data;
  11. }
  12. }

Lets make the server-side component next:

Java

  1. package com.example.addon;
  2. import java.util.Arrays;
  3. import com.example.addon.client.ui.LinePlotState;
  4. import com.example.addon.client.ui.LinePlotState.DataSeries;
  5. import com.vaadin.ui.AbstractComponent;
  6. public class LinePlot extends AbstractComponent {
  7. public LinePlot() {
  8. }
  9. @Override
  10. public LinePlotState getState() {
  11. return (LinePlotState) super.getState();
  12. }
  13. public void addSeries(String label, String color, Float[] fs) {
  14. DataSeries ds = new DataSeries();
  15. ds.label = label;
  16. ds.color = color;
  17. ds.data = Arrays.asList(fs);
  18. getState().series.add(ds);
  19. }
  20. }

We override getState() in order to narrow the return type to our own LinePlotState, and then implement a simple addSeries() that creates a DataSeries instance and adds it to the state. The state will be automatically transmitted to the client when needed, so the plots will remain intact over browser reloads for instance.The API for our component could obviously be expanded, but lets leave it like this for this example.

Since the GWT widget we’re going to use is already made for us (in the GFlot library), the only thing left for us to do is implement the client-side connector:

Java

  1. package com.example.addon.client.ui;
  2. import ca.nanometrics.gflot.client.DataPoint;
  3. import ca.nanometrics.gflot.client.SeriesHandler;
  4. import ca.nanometrics.gflot.client.SimplePlot;
  5. import com.example.addon.LinePlot;
  6. import com.example.addon.client.ui.LinePlotState.DataSeries;
  7. import com.google.gwt.core.client.GWT;
  8. import com.google.gwt.user.client.ui.Widget;
  9. import com.vaadin.client.communication.StateChangeEvent;
  10. import com.vaadin.client.ui.AbstractComponentConnector;
  11. import com.vaadin.client.ui.Connect;
  12. @Connect(LinePlot.class)
  13. public class LinePlotConnector extends AbstractComponentConnector {
  14. @Override
  15. public LinePlotState getState() {
  16. return (LinePlotState) super.getState();
  17. }
  18. @Override
  19. public SimplePlot getWidget() {
  20. return (SimplePlot) super.getWidget();
  21. }
  22. @Override
  23. protected Widget createWidget() {
  24. return GWT.create(SimplePlot.class);
  25. }
  26. @Override
  27. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  28. super.onStateChanged(stateChangeEvent);
  29. getWidget().getModel().clear();
  30. for (DataSeries ds : getState().series) {
  31. SeriesHandler s = getWidget().getModel().addSeries(ds.label,
  32. ds.color);
  33. for (int i = 0; i < ds.data.size(); i++) {
  34. s.add(new DataPoint(i, ds.data.get(i)));
  35. }
  36. }
  37. getWidget().redraw();
  38. }
  39. }

We override both getState() and getWidget() to narrow the return type to our liking, then make createWidget() return an instance of the GFlot widget we’re going to use, SimplePlot.

Last, we override onStateChange() which is called whenever the shared state has been changed. Here we make use of the SimplePlot API to add the series contained in the shared state (for simplicity, we clear the SimplePlot first, then add all the series in our state).

That’s it! The full source is available as an attachment to this article.

Attachment vaadingflot.zip