Creating a simple component container

Components in Vaadin can be roughly split into two groups, Components and ComponentContainers. ComponentContainers are Components in themselves which can also contain other components. If you are about to implement a component that contains other components, then you’ll get a headstart by extending Vaadin’s ComponentContainer. The biggest feature is in transferring the list of server side components from your component to the client. Here’s how you do it.

Server Side

To start of we implement our server side component. For this we extend the ready made abstract implementation AbstractComponentContainer. This requires us to implement addComponent(Component), removeComponent(Component), replaceComponent(Component, Component), getComponentCount and getComponentIterator().

Java

  1. package com.example.widgetcontainer;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import com.vaadin.ui.AbstractComponentContainer;
  6. import com.vaadin.ui.Component;
  7. public class WidgetContainer extends AbstractComponentContainer {
  8. List<Component> children = new ArrayList<Component>();
  9. @Override
  10. public void addComponent(Component c) {
  11. children.add(c);
  12. super.addComponent(c);
  13. markAsDirty();
  14. }
  15. @Override
  16. public void removeComponent(Component c) {
  17. children.remove(c);
  18. super.removeComponent(c);
  19. markAsDirty();
  20. }
  21. public void replaceComponent(Component oldComponent, Component newComponent) {
  22. int index = children.indexOf(oldComponent);
  23. if (index != -1) {
  24. children.remove(index);
  25. children.add(index, newComponent);
  26. fireComponentDetachEvent(oldComponent);
  27. fireComponentAttachEvent(newComponent);
  28. markAsDirty();
  29. }
  30. }
  31. public int getComponentCount() {
  32. return children.size();
  33. }
  34. public Iterator<Component> iterator() {
  35. return children.iterator();
  36. }
  37. }

Add, remove and replace are quite straight forward. In the class we upkeep a list of children internally, and these three methods modify them. Add and remove have ready made methods in the super class for notifying all event handlers that the children have changed and because of that we should make calls to the super methods after we have updated the list. In replaceComponent we have to call fireComponentDetachEvent(Component) and fireComponentAttachEvent(Component) to manually trigger these events. In all three methods we should also call markAsDirty as a last step to notify the client side that the children have changed.

The methods getComponentCount() and iterator() takes care of providing the required information that we need to the client side. Here they are simple delegate methods to the List’s size() and iterator().

Client Side

Next up, we want to set up a standard GWT widget which will be our component container’s client side widget. GWT in itself has a bunch of component containers in it. In GWT, these are called Panels. For this case I will start with a VerticalPanel. It is roughly the same as VerticalLayout in Vaadin. Down the road you want to edit this file to add features or even extend Widget to create a complete custom widget. For now extending VerticalPanel is enough and we’ll use that as-is.

Java

  1. package com.example.widgetcontainer.client.ui;
  2. import com.google.gwt.user.client.ui.VerticalPanel;
  3. public class VWidgetContainer extends VerticalPanel {
  4. public static final String CLASSNAME = "v-widgetcontainer";
  5. public VWidgetContainer() {
  6. setStyleName(CLASSNAME);
  7. }
  8. }

Connector

Your widget’s Connector will transfer the components from the server side as child widgets to our widget. The connector will feed the children to the panel trough it’s standard API, namely add(Widget), remove(Widget) and clear();

Instead of going the standard route of extending AbstractComponentConnector as your connector, here we can take use of Vaadin’s internal features and extend AbstractComponentContainerConnector. Additionally to implementing the getWidget() -method from AbstractComponentConnector, we also have to supply the class with an implementation to a method called updateCaption(ComponentConnector). This method is there if we want the container to take care of the captions for all the components. We don’t need to take care of these captions in this example so we can leave the implementation empty.

The real benefit of extending AbstractComponentContainerConnector is that we can now extend a method called onConnectorHierarchyChange(ConnectorHierarchyChangeEvent). This method will be called every time that the server side calls markAsDirty() if the component hierarchy has been changed. From within it, we can call on getChildComponents to get a list of all the child components, and populate our widget with those.

Java

  1. package com.example.widgetcontainer.client.ui;
  2. import java.util.List;
  3. import com.google.gwt.core.client.GWT;
  4. import com.google.gwt.user.client.ui.Widget;
  5. import com.example.widgetcontainer.WidgetContainer;
  6. import com.vaadin.client.ComponentConnector;
  7. import com.vaadin.client.ConnectorHierarchyChangeEvent;
  8. import com.vaadin.client.ui.AbstractComponentContainerConnector;
  9. import com.vaadin.client.ui.Connect;
  10. @Connect(WidgetContainer.class)
  11. public class WidgetContainerConnector extends
  12. AbstractComponentContainerConnector {
  13. @Override
  14. public void onConnectorHierarchyChange(ConnectorHierarchyChangeEvent event) {
  15. List<ComponentConnector> children = getChildComponents();
  16. VWidgetContainer widget = getWidget();
  17. widget.clear();
  18. for (ComponentConnector connector : children) {
  19. widget.add(connector.getWidget());
  20. }
  21. }
  22. @Override
  23. public VWidgetContainer getWidget() {
  24. return (VWidgetContainer) super.getWidget();
  25. }
  26. public void updateCaption(ComponentConnector connector) {
  27. }
  28. }

This implementation removes all the component’s in the widget and adds all that are returned from getChildComponents. An obvious optimization to these is to compare what is already in the widget and only add/remove/move those widgets that have changed.

Example Usage

Nothing left but to use the component! Compile the widgetset and check that the widgetset is in use in your web.xml. Here is a little stand-alone application that uses this component:

Java

  1. package com.example.widgetcontainer;
  2. import java.util.Random;
  3. import com.vaadin.terminal.WrappedRequest;
  4. import com.vaadin.ui.Button;
  5. import com.vaadin.ui.Button.ClickEvent;
  6. import com.vaadin.ui.Button.ClickListener;
  7. import com.vaadin.ui.CheckBox;
  8. import com.vaadin.ui.Component;
  9. import com.vaadin.ui.Label;
  10. import com.vaadin.ui.UI;
  11. public class WidgetcontainerUI extends UI {
  12. @Override
  13. public void init(VaadinRequest request) {
  14. VerticalLayout layout = new VerticalLayout();
  15. layout.setMargin(true);
  16. setContent(layout);
  17. Label label = new Label("Hello Vaadin user");
  18. layout.addComponent(label);
  19. final WidgetContainer widgetContainer = new WidgetContainer();
  20. layout.addComponent(widgetContainer);
  21. widgetContainer.addComponent(new Label(
  22. "Click the button to add components to the WidgetContainer."));
  23. Button button = new Button("Add more components", new ClickListener() {
  24. @Override
  25. public void buttonClick(ClickEvent event) {
  26. Random randomGenerator = new Random();
  27. int random = randomGenerator.nextInt(3);
  28. Component component;
  29. if (random % 3 == 0) {
  30. component = new Label("A new label");
  31. } else if (random % 3 == 1) {
  32. component = new Button("A button!");
  33. } else {
  34. component = new CheckBox("A textfield");
  35. }
  36. widgetContainer.addComponent(component);
  37. }
  38. });
  39. layout.addComponent(button);
  40. }
  41. }