diff --git a/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/DashboardBean.java b/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/DashboardBean.java index 4ba2fc9ae2e41bcb9a10d37d4f0daf8ff622462c..57c57af1c38f908f4bf110ffe2027423a84af6cd 100644 --- a/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/DashboardBean.java +++ b/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/DashboardBean.java @@ -22,57 +22,140 @@ package kieker.webgui.beans.session; import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import java.util.Random; +import javax.annotation.PostConstruct; +import javax.faces.application.Application; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; +import javax.faces.component.html.HtmlOutputText; import javax.faces.context.FacesContext; import kieker.webgui.beans.application.ProjectsBean; -import kieker.webgui.beans.request.StringToIDBean; +import kieker.webgui.beans.application.ProjectsBean.Widget; -import org.primefaces.model.DashboardColumn; +import org.primefaces.component.dashboard.Dashboard; +import org.primefaces.component.panel.Panel; +import org.primefaces.event.DashboardReorderEvent; import org.primefaces.model.DashboardModel; import org.primefaces.model.DefaultDashboardColumn; import org.primefaces.model.DefaultDashboardModel; /** + * This bean can be used to handle an instance of {@link Dashboard} including a list of {@code Widget}. It makes sure that new widget-instances can be added + * dynamically. * * @author Nils Christian Ehmke */ -@ManagedBean @SessionScoped +@ManagedBean public class DashboardBean implements Serializable { - private final DashboardModel model; - private final List<ProjectsBean.Widget> widgets; + /** + * This is the prefix used to the widgets within the component. + */ + private static final String WIDGET_ID_PREFIX = "widget_"; + /** + * This is the dashboard to be controlled by this instance. + */ + private Dashboard dashboard; + /** + * This is the model used to fill the dasboard. + */ + private DashboardModel dashboardModel; + /** + * This is the list containing all widgets. + */ + private final List<Widget> widgets = new ArrayList<ProjectsBean.Widget>(); + + /** + * Creates a new instance of this class. + */ public DashboardBean() { - this.model = new DefaultDashboardModel(); - this.widgets = new ArrayList<ProjectsBean.Widget>(); + // No code necessary. + } + + @PostConstruct + public void init() { + final FacesContext fc = FacesContext.getCurrentInstance(); + final Application application = fc.getApplication(); + + // Add three columns for the model + this.dashboardModel = new DefaultDashboardModel(); + this.dashboardModel.addColumn(new DefaultDashboardColumn()); + this.dashboardModel.addColumn(new DefaultDashboardColumn()); + this.dashboardModel.addColumn(new DefaultDashboardColumn()); + + this.dashboard = (Dashboard) application.createComponent(fc, "org.primefaces.component.Dashboard", "org.primefaces.component.DashboardRenderer"); + this.dashboard.setId("dashboard"); + this.dashboard.setModel(this.dashboardModel); + + this.addChildren(); + } + + /** + * This method can be used to update the UI children of the dashboard. + */ + private void addChildren() { + synchronized (this) { + // Clear the list with the children to avoid double IDs. + this.dashboard.getChildren().clear(); + + final FacesContext fc = FacesContext.getCurrentInstance(); + final Application application = fc.getApplication(); + + // Run through all widgets and add a panel for each of them. + for (int i = 0; i < this.widgets.size(); i++) { + final Panel panel = (Panel) application.createComponent(fc, "org.primefaces.component.Panel", "org.primefaces.component.PanelRenderer"); + panel.setId(DashboardBean.WIDGET_ID_PREFIX + i); + panel.setHeader(this.widgets.get(i).getName()); + panel.setClosable(true); + panel.setToggleable(true); - final DashboardColumn column1 = new DefaultDashboardColumn(); - final DashboardColumn column2 = new DefaultDashboardColumn(); - final DashboardColumn column3 = new DefaultDashboardColumn(); + this.dashboard.getChildren().add(panel); - this.model.addColumn(column1); - this.model.addColumn(column2); - this.model.addColumn(column3); + final HtmlOutputText text = new HtmlOutputText(); + text.setId("t" + DashboardBean.WIDGET_ID_PREFIX + i); + text.setValue(this.widgets.get(i).getType()); + panel.getChildren().add(text); + } + } + } + + /** + * Delivers the currently stored dashboard. + * + * @return The dashboard. + */ + public Dashboard getDashboard() { + this.addChildren(); + return this.dashboard; } - public DashboardModel getModel() { - return this.model; + public void setDashboard(final Dashboard dashboard) { + this.dashboard = dashboard; } - public void addWidget(final ProjectsBean.Widget widget) { - final FacesContext context = FacesContext.getCurrentInstance(); - final StringToIDBean idBean = context.getApplication().evaluateExpressionGet(context, "#{stringToIDBean}", StringToIDBean.class); - synchronized (this.widgets) { - this.widgets.add(widget); - this.model.getColumn(0).addWidget(idBean.stringToID(widget.toString())); + /** + * Adds a new widget to the list. + * + * @param widget + * The widget to be add to the bean. + */ + public void addWidget(final Widget widget) { + // Add the widget and add it to the model as well. + synchronized (this) { + this.widgets.add(widget); + this.dashboardModel.getColumn(new Random().nextInt(3)).addWidget(DashboardBean.WIDGET_ID_PREFIX + (this.widgets.size() - 1)); } + + // Update the UI + this.addChildren(); } - public List<ProjectsBean.Widget> getWidgets() { - return this.widgets; + public void handleReorder(final DashboardReorderEvent event) { + // Nothing to do yet + this.addChildren(); } } diff --git a/Kieker.WebGUI/src/main/webapp/handleAnalysis.xhtml b/Kieker.WebGUI/src/main/webapp/handleAnalysis.xhtml index 792a458b3e666c799ca791b5c0b10e0ca224e0aa..e180744ca194e1beaa54da83019d8989cb9c5934 100644 --- a/Kieker.WebGUI/src/main/webapp/handleAnalysis.xhtml +++ b/Kieker.WebGUI/src/main/webapp/handleAnalysis.xhtml @@ -47,16 +47,13 @@ <p:layoutUnit position="center"> <h:form id="centerForm"> - <ui:repeat value="#{dashboardBean.widgets}" var="widget"> - <p:panel id="widget" header="#{widget.name}"> - - </p:panel> - <p:poll interval="2" update="widget"/> - </ui:repeat> + <p:dashboard id="dashboard" binding="#{dashboardBean.dashboard}"> + + </p:dashboard> </h:form> </p:layoutUnit> - <p:layoutUnit style="overflow:visible;" position="west" size="200" resizable="true" collapsible="true" header="Plugins"> + <p:layoutUnit position="west" size="200" resizable="true" collapsible="true" header="Plugins"> <h:form id="pluginsForm"> <p:accordionPanel value="#{analysisControllerBean.pluginMap.keySet().toArray()}" var="plugin" multiple="true" activeIndex=""> <p:tab title="#{plugin.name}">