From b4b9f830c922d8ec24b6720a90b6f8735160cc44 Mon Sep 17 00:00:00 2001
From: Nils Christian Ehmke <nie@informatik.uni-kiel.de>
Date: Sat, 12 May 2012 23:27:40 +0200
Subject: [PATCH] Modified the dashboard.

---
 .../webgui/beans/session/DashboardBean.java   | 129 ++++++++++++++----
 .../src/main/webapp/handleAnalysis.xhtml      |  11 +-
 2 files changed, 110 insertions(+), 30 deletions(-)

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 4ba2fc9a..57c57af1 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 792a458b..e180744c 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}">
-- 
GitLab