From 6e45fb5e7a5abd66db27c67ac72ff7b0061530be Mon Sep 17 00:00:00 2001 From: Nils Christian Ehmke <nie@informatik.uni-kiel.de> Date: Tue, 1 May 2012 10:34:59 +0200 Subject: [PATCH] Started with the observer patterns --- .../session/SelectedMainProjectBean.java | 13 +++- .../beans/session/SelectedPluginBean.java | 72 +++++++++++++++++-- Kieker.WebGUI/src/main/webapp/main.xhtml | 3 +- 3 files changed, 79 insertions(+), 9 deletions(-) diff --git a/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/SelectedMainProjectBean.java b/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/SelectedMainProjectBean.java index 5368438f..869a43d9 100644 --- a/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/SelectedMainProjectBean.java +++ b/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/SelectedMainProjectBean.java @@ -28,6 +28,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Observable; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; @@ -65,7 +66,7 @@ import org.eclipse.emf.common.util.EList; */ @ManagedBean @SessionScoped -public class SelectedMainProjectBean { +public class SelectedMainProjectBean extends Observable { /** * The logger within this class. */ @@ -95,6 +96,10 @@ public class SelectedMainProjectBean { */ private List<Connection> connections; + public enum UPDATE_MSG { + SELECTION_CHANGED + } + /** * Creates a new instance of this class. */ @@ -121,7 +126,11 @@ public class SelectedMainProjectBean { this.mainProject = mainProject; this.connections = this.getConnectionsFromProject(); FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "", "New main project: " + mainProject.getName())); - } + + // Inform the observers + setChanged(); + notifyObservers(UPDATE_MSG.SELECTION_CHANGED); + } /** * This method delivers the available reader-plugins for the current main project. The delivered plugins are never abstract. diff --git a/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/SelectedPluginBean.java b/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/SelectedPluginBean.java index 4e6a045b..94d60148 100644 --- a/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/SelectedPluginBean.java +++ b/Kieker.WebGUI/src/main/java/kieker/webgui/beans/session/SelectedPluginBean.java @@ -22,20 +22,40 @@ package kieker.webgui.beans.session; import java.util.ArrayList; import java.util.List; +import java.util.Observable; +import java.util.Observer; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; +import javax.faces.context.FacesContext; import kieker.analysis.model.analysisMetaModel.MIPlugin; +import org.primefaces.context.RequestContext; + /** - * This bean can be used to save the currently selected plugin of the user. + * This bean can be used to save the currently selected plugin of the user. This bean registers to the user's instance of {@link SelectedMainProjectBean} as an + * observer. If the main project changes, the content of this bean changes as well. This bean also updates the properties form if the selection of the plugin + * changes. * * @author Nils Christian Ehmke * @version 1.0 */ @ManagedBean @SessionScoped -public final class SelectedPluginBean { +public final class SelectedPluginBean implements Observer { + + /** + * This is the name of the target element to be updated when the plugin selection changes. + */ + private static final String UPDATE_TARGET = "propertiesForm"; + + /** + * This is the main project bean where this instance registers as an observer. + */ + private final SelectedMainProjectBean observableBean; /** * The plugin which is stored by this container. @@ -46,7 +66,29 @@ public final class SelectedPluginBean { * Creates a new instance of this class. */ public SelectedPluginBean() { - /* No code necessary. */ + // Try to get the main project bean. + final FacesContext context = FacesContext.getCurrentInstance(); + this.observableBean = context.getApplication().evaluateExpressionGet(context, "#{selectedMainProjectBean}", SelectedMainProjectBean.class); + } + + /** + * This method is used to initialize the object after creation. + */ + @PostConstruct + @SuppressWarnings("FinalPrivateMethod") + private final void postConstruct() { + // Register this instance as an observer + this.observableBean.addObserver(this); + } + + /** + * This method is used to finalize the object before destruction. + */ + @PreDestroy + @SuppressWarnings("FinalPrivateMethod") + private final void preDestroy() { + // Unregister this instance as an observer + this.observableBean.deleteObserver(this); } /** @@ -59,21 +101,39 @@ public final class SelectedPluginBean { } /** - * Sets the plugin stored by this bean. + * Sets the plugin stored by this bean and updates (if possible) the corresponding element in the current context. * * @param plugin * The new bean to be stored within this bean. */ public void setPlugin(final MIPlugin plugin) { this.plugin = plugin; + // Update the element + final RequestContext requestContext = RequestContext.getCurrentInstance(); + requestContext.addPartialUpdateTarget(SelectedPluginBean.UPDATE_TARGET); } + /** + * This method delivers a list containing all properties of the current plugin, but adds the name as a string to it. + * + * @return A list with all properties plus the name (as a string). If no plugin is selected, this method returns an empty list, never null. + */ public List<Object> getAdvancedProperties() { final List<Object> resultList = new ArrayList<Object>(); - resultList.add(plugin.getName()); - resultList.addAll(plugin.getProperties()); + if (this.plugin != null) { + resultList.add(this.plugin.getName()); + resultList.addAll(this.plugin.getProperties()); + } return resultList; } + + @Override + public void update(final Observable o, final Object arg) { + // If the main project has been newly selected, we have to set the current plugin to null + if (arg == SelectedMainProjectBean.UPDATE_MSG.SELECTION_CHANGED) { + this.setPlugin(null); + } + } } diff --git a/Kieker.WebGUI/src/main/webapp/main.xhtml b/Kieker.WebGUI/src/main/webapp/main.xhtml index 5ca6a0c7..dd046438 100644 --- a/Kieker.WebGUI/src/main/webapp/main.xhtml +++ b/Kieker.WebGUI/src/main/webapp/main.xhtml @@ -34,6 +34,7 @@ <p:menubar> <p:submenu label="File"> <p:menuitem value="New Project" onclick="newProjectDialog.show()" ajax="true" /> + <p:menuitem value="Import Project" ajax="true" /> <p:menuitem value="Manage Dependencies" ajax="false" url="/Kieker.WebGUI/manageDependencies" /> <p:separator /> <p:menuitem value="Settings" onclick="settingsDialog.show()" ajax="true" /> @@ -114,7 +115,7 @@ <h:form id="centerForm" style="height: 100%"> <div class="canvas" id="mainCanvas" style="width : #{currentWorkspaceSizeBean.sizeX}px;height: #{currentWorkspaceSizeBean.sizeY}px"> <c:forEach items="#{selectedMainProjectBean.mainProject.plugins}" var="plugin" varStatus="counter"> - <p:remoteCommand name="setPlugin#{counter.index}" action="#{selectedPluginBean.setPlugin(plugin)}" update=":propertiesForm"/> + <p:remoteCommand name="setPlugin#{counter.index}" action="#{selectedPluginBean.setPlugin(plugin)}"/> <!-- Netbeans reports an error here, but the code does still work though... --> <div onclick="setPlugin#{counter.index}();" class="ui-panel ui-widget ui-widget-content ui-corner-all block draggable" id="#{stringToIDBean.stringToID(plugin)}" > <div class="ui-panel-titlebar ui-widget-header ui-corner-all"> -- GitLab