Simulating the portal environment with dashboard

A layout component, dashboard has the drag-and-drop ability to support reordering of the panels. In this recipe, we will create a dashboard with predefined panels and then add new ones.

How to do it…

A basic definition for a dashboard with six panels would be as follows:

<p:dashboard id="board" model="#{dashboardBean.model}">
  <p:ajax event="reorder" listener=
    "#{dashboardBean.handleReorder}" update="growl" />
  <p:panel id="calculator" header="Calculator">
    <h:outputText value="Content for Calculator" />
  </p:panel>
  <p:panel id="calendar" header="Calendar">
    <h:outputText value="Content for Calendar" />
  </p:panel>
  <p:panel id="contact" header="Contacts">
    <h:outputText value="Content for Contacts" />
  </p:panel>
  <p:panel id="dictionary" header="Dictionary">
    <h:outputText value="Weather Content for Dictionary" />
  </p:panel>
  <p:panel id="weather" header="Weather">
    <h:outputText value="Content for Weather" />
  </p:panel>
  <p:panel id="translation" header="Translation">
    <h:outputText value="Content for Translation" />
  </p:panel>
</p:dashboard>

A data model needs to be provided in order to preserve the panel order. The data model depends on org.primefaces.model.DefaultDashboardModel. The model for the given sample would be as follows:

DashboardColumn column1 = new DefaultDashboardColumn();
DashboardColumn column2 = new DefaultDashboardColumn();
DashboardColumn column3 = new DefaultDashboardColumn();

column1.addWidget("calculator");
column1.addWidget("calendar");
column1.addWidget("contact");
column2.addWidget("dictionary");
column3.addWidget("weather");
column3.addWidget("translation");

model.addColumn(column1);
model.addColumn(column2);
model.addColumn(column3);

The preceding definition of the dashboard contains three columns and six panels and will look like the following image:

How to do it…

Note

The dashboard component preserves its state whenever a widget is reordered. So, you can easily store the changes made by the user in a persistence layer, such as database management systems.

There's more…

The dashboard component supports the reordering of the AJAX behavior event that will be fired when a panel is dragged and dropped. The reorder event definition is given in the previous sample. The handleReorder listener method defined next receives org.primefaces.event.DashboardReorderEvent as its parameter:

public void handleReorder(DashboardReorderEvent event) {
  MessageUtil.addInfoMessageWithoutKey("Reordered: " +
    event.getWidgetId(), 
    "Item index: " + event.getItemIndex() + 
    ", Column index: " + event.getColumnIndex() + 
    ", Sender column index: " +
    event.getSenderColumnIndex());
}

As seen in the method, the information about the drag-and-drop action is communicated to the user by adding it as a PrimeFaces message. The order number of the panel can be retrieved from the event.getItemIndex() method. The number of the column from which the panel has been dragged can be retrieved by event.getColumnIndex(), and the number of the column that the panel will be dragged onto can be retrieved from event.getSenderColumnIndex().

Creating new widgets

To add new panels that reside outside of the dashboard, you can use the draggable component. The following definition creates a new panel component and attaches a draggable component that knows the panel and the dashboard, which the panel could be dragged to:

<p:panel id="newWidget" style="width: 200px">
  <h:outputText value="Drag me and create a new widget" />
</p:panel>
<p:draggable for="newWidget" helper="clone" dashboard="board" />

Having fixed-sized columns

By default, if the user moves panels from one column to another and gets one column empty, it cannot be filled again by dragging panels back. To have columns of a fixed size, the ui-dashboard-column style should be defined with the preferred width, as shown in the following code snippet:

.ui-dashboard-column {
  width:200px;
}

Tip

Since dashboard contains panel components as its widgets, these panels can be defined with the menu or can be stated as closable and toggle-able. See the first recipe, Grouping content with a standard panel, in this chapter for detailed usage.

PrimeFaces Cookbook Showcase application

This recipe is available in the demo web application on GitHub (https://github.com/ova2/primefaces-cookbook/tree/second-edition). Clone the project if you have not done it yet, explore the project structure, and build and deploy the WAR file on application servers compatible with Servlet 3.x, such as JBoss WildFly and Apache TomEE.

The showcase for the recipe is available at http://localhost:8080/pf-cookbook/views/chapter4/dashboard.jsf.

See also

For details about the MessageUtil class, see the Internationalization (i18n) and Localization (L10n) recipe in Chapter 1, Getting Started with PrimeFaces.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.246.223