





















































The Portlet class extends ContentPanel to provide a special type of panel that can be repositioned in the Viewport by the user with a Portal container. It may appear similar to a window in a desktop application. Creating a Portlet is similar to creating other containers. This code:
Portlet portlet = new Portlet();
portlet.setHeight(150);
portlet.setHeading("Example Portlet");
creates a Portlet like this:
A Portlet can be excluded from being repositioned by pinning it using:
portal.setPinned(true);
Apart from that, a Portlet inherits all the features of a standard ContentPanel.
A Portal is a special container for Portlet components. In fact, it is a Container containing a collection of LayoutContainer components arranged using ColumnLayout. Each of those LayoutContainer components in turn is able to contain Portlet components, arranged using a RowLayout.
Portal also supports dragging and dropping of Portlet components, both in terms of changing the row it is in within a column and the column within the Portal.
When creating a Portal, we need to set the number of columns the Portal should create in the constructor. We also need to set the widths of each column before using the setColumnWidth method of the Portal.
So to create a Portal with two columns, (one using 30 percent of the width and the second 70 percent) we would define it as follows:
Portal portal = new Portal(2);
portal.setColumnWidth(0, 0.3);
portal.setColumnWidth(1, 0.7);
We can then add a Portlet to each column like this:
Portlet portlet1 = new Portlet();
portlet1.setHeight(150);
portlet1.setHeading("Example Portlet 1");
portal.add(portlet1, 0);
Portlet portlet2 = new Portlet();
portlet2.setHeight(150);
portlet2.setHeading("Example Portlet 2");
portal.add(portlet2, 1);
This will produce the following output:
Both Portlet components can be dragged and dropped into different positions. The Portlet turns into a blue box while being dragged as shown in the following screenshot:
A Portlet will automatically resize and ft into the column in which it is dropped, as seen in the next screenshot:
Like ContentPanel that Portlet extends, we can add ToolButton components to the header. These can be very useful for making a Portlet look and behave even more like windows in a desktop application.
portlet.getHeader().addTool(new ToolButton("x-tool-minimize"));
portlet.getHeader().addTool(new ToolButton("x-tool-maximize"));
portlet.getHeader().addTool(new ToolButton("x-tool-close"));
The output can be seen as shown in the following screenshot:
At the moment, we are using ContentPanel components in our example application and laying them out using a BorderLayout. We shall now see that it does not take much to change the ContentPanel components into Portlet components and manage them using a Portal.
Portlet components are ideally suited to being independent, self-contained user interface elements that respond to the data passed to them. Rather than tying them into a Portal directly, we can use the MVC components to cause the Portal to respond to the creation of a new Portlet to preserve that independence.
public static final EventType NewPortletCreated = new EventType();
public class PortalController extends Controller {
public class PortalView extends View {
public PortalView(PortalController portalController) {
super(portalController);
}
private PortalView portalView;
@Override
public void initialize() {
super.initialize();
portalView = new PortalView(this);
}
public PortalController() {
registerEventTypes(AppEvents.NewPortletCreated );
registerEventTypes(AppEvents.Error);
}
@Override
public void handleEvent(AppEvent event) {
EventType eventType = event.getType();
if (eventType.equals(AppEvents.error)) {
GWT.log("Error", (Throwable) event.getData());
} else {
forwardToView(portalView, event);
}
}
private final Portal portal = new Portal(2);
@Override
protected void initialize() {
portal.setColumnWidth(0, 0.3);
portal.setColumnWidth(1, 0.7);
}
@Override
protected void initialize() {
portal.setColumnWidth(0, 0.3);
portal.setColumnWidth(1, 0.7);
final Viewport viewport = new Viewport();
viewport.setLayout(new FitLayout());
viewport.add(portal);
RootPanel.get().add(viewport);
}
@Override
protected void handleEvent(AppEvent event) {
EventType eventType = event.getType();
if (eventType.equals(AppEvents.NewPortletCreated )) {
}
}
public void onModuleLoad() {
final FeedServiceAsync feedService =
GWT.create(FeedService.class);
Registry.register(RSSReaderConstants.FEED_SERVICE, feedService);
Dispatcher dispatcher = Dispatcher.get();
dispatcher.addController(new PortalController());
}
We created the basic framework for a Portal layout of our application. However, if we started it now, we would just get a blank screen. What we need to do is add Portlet components.
The actual Portlet components are not too complicated. They will just act as wrappers.