DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Using GWT widgets in Vaadin 7 - Part 1

Using GWT widgets in Vaadin 7 - Part 1

Nicolas Fränkel user avatar by
Nicolas Fränkel
CORE ·
Jun. 05, 12 · Interview
Like (1)
Save
Tweet
Share
24.28K Views

Join the DZone community and get the full member experience.

Join For Free

Even if Vaadin provides you with plenty of components out-of-the-box, chances are sooner or later, you'll want to use that special GWT widget you just saw the last day. In that case, be happy because Vaadin let you do that. Then, you'll be able to package the component in a simple JAR archive and reuse it in all projects you want, just like Vaadin components themselves. In this 3 parts article series, we'll see how to do just that with Vaadin 7:

  • This first part is about just wrapping GWT widgets in Vaadin components
  • In the second part, we'll see how to configure the client widget from the server component
  • The final part will detail how to communicate from client to server

 The first step it to choose which widget to wrap. As an illustration, we'll use GWT-Bootstrap, a GWT implementation of Twitter Bootstrap and most specifically the com.github.gwtbootstrap.client.ui.Button. Of course, lessons learned here can easily be transposed for other GWT widgets.

Wrapping a GWT widget in Vaadin code requires a couple of classes:

  • The first class to create is the Vaadin component itself. Such components should extends com.vaadin.ui.AbstractComponent (or com.vaadin.ui.AbstractComponentContainer for components that contains other components, which is not the case for buttons):
    public class BootstrapButton extends AbstractComponent {
    
        ...
    }
  • The second class to create is the client widget itself. It's very simple: just extend the wanted GWT widget (the Bootstrap button in our case). Note that it's mandatory to locate it in a ui.client subpackage relative to the above component:
    public class VBootstrapButton extends Button {
    
        public VBootstrapButton() {
    
            addStyleName("v-button-bootstrap");
        }
    }
  • The final class will bind the component and the widget. In Vaadin semantics, it's called a connector. Connectors have to extend com.vaadin.terminal.gwt.client.ui.AbstractComponentConnector (or com.vaadin.terminal.gwt.client.ui.AbstractComponentContainerConnector for those widgets that contain other widgets). Connecting is achieved by implement the createWidget() that should return the previously implemented client class and by annotating the connector with the @Connect that takes the server class as the value.
    @Connect(com.morevaadin.vaadin7.custom.BootstrapButton.class)
    public class BootstrapButtonConnector extends AbstractComponentConnector {
    
        @Override
        protected Widget createWidget() {
    
            return GWT.create(VBootstrapButton.class);
        }
    }
    It also has to be located in the ui.client subpackage.

GWT reads Java code and renders HTML and JavaScript. When using standard Vaadin components, the code is precompiled and available in the Vaadin JAR, but when adding third-party widgets, we need to compile both Vaadin and the other widgets. In order to achieve this, two things are necessary:

  • First, we have to create a GWT widgetset gwt.xml file, referencing both the Vaadin and the Bootstrap widgetset:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.7.0//EN" "http://google-web-toolkit.googlecode.com/svn/tags/1.7.0/distro-source/core/src/gwt-module.dtd">
    <module>
        <inherits name="com.vaadin.terminal.gwt.DefaultWidgetSet" />
        <inherits name="com.github.gwtbootstrap.Bootstrap" />
        <!-- Reduces compilation time in development mode -->
        <!--
            <set-property name="user.agent" value="safari,gecko1_8" />
        -->
    </module>
  • Then, we have to handle the compilation itself. Either uses the Eclipse Vaadin plugin compilation feature or insert the following snippet in the Maven POM:
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>gwt-maven-plugin</artifactId>
                <version>2.4.0</version>
                <configuration>
                    <webappDirectory>${project.build.directory}/${project.build.finalName}/VAADIN/widgetsets</webappDirectory>
                    <extraJvmArgs>-Xmx512M -Xss1024k</extraJvmArgs>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>resources</goal>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-maven-plugin</artifactId>
                <version>1.0.2</version>
                <executions>
                    <execution>
                        <configuration>
                        </configuration>
                        <goals>
                            <goal>update-widgetset</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

 Sources for this article can be found on GitHub. In the next part, we'll detail how to let developers customize widgets though components on the server side.

Vaadin

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Upgrade Guide To Spring Data Elasticsearch 5.0
  • An Introduction to Data Mesh
  • Bye-Bye, Regular Dev [Comic]
  • 5 Factors When Selecting a Database

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: