Vos solutions informatiques sur mesure
SOCIETE DE SERVICES EN INGENIERIE INFORMATIQUE
Due to the ajax=true attribute, every graph change on the client side is sent to the server.
Ajax behaviors have been set and Ajax requests are fired on selection or unselection events.
After every Ajax Request the textArea at the bottom of the graph is updated with a dump of the server side model.
<p:growl id="growl" /> <h:form id="mainForm"> <div style="height:40%;width:49%; border:solid 1px; padding:2px; float:left"> <div id="dijGraphContainer" style="height:90%;"> <p:graphvis id="graph" value="#{demoAjaxBehaviorsBean.graphModel}" widgetVar="myGraph" ajax="true" update ="mainForm:txtModelDescription"> <p:ajax event="select" listener="#{demoAjaxBehaviorsBean.onSelectItems}" update=":growl" /> <p:ajax event="unselect" listener="#{demoAjaxBehaviorsBean.onUnselectItems}" update=":growl" /> <p:ajax event="nodeSelect" listener="#{demoAjaxBehaviorsBean.onSelectNodes}" update=":growl" /> <p:ajax event="nodeUnselect" listener="#{demoAjaxBehaviorsBean.onUnselectNodes}" update=":growl" /> <p:ajax event="edgeSelect" listener="#{demoAjaxBehaviorsBean.onSelectEdges}" update=":growl" /> <p:ajax event="edgeUnselect" listener="#{demoAjaxBehaviorsBean.onUnselectEdges}" update=":growl" /> </p:graphvis> </div> </div> <div id="stylePanel" style="margin-left:50%; width:30%;"> <p:fieldset > <p:commandButton value="generate new Graph" style="width:100%" action="#{demoAjaxBehaviorsBean.fillGraphModel()}" onsuccess="myGraph.synchronize(true)"/> <p:commandButton type="button" value="Remove selected nodes" onclick="removeSelectedNodes()" style="width:100%"/> <p:commandButton type="button" value="Create edge between selected nodes" onclick="createEdges()" style="width:100%"/> <br/><br/><b><h:outputText value="Style:"/></b> <h:panelGrid columns = "2" style="font-size:10px"> <h:outputText value="Layout:"/> <p:selectOneMenu label="Shape:" widgetVar="layoutSelector" onchange="myGraph.doLayout(layoutSelector.value);" > <f:selectItem itemLabel="ForceDirected" itemValue="ForceDirected" /> <f:selectItem itemLabel="Circle" itemValue="Circle" /> <f:selectItem itemLabel="Radial" itemValue="Radial" /> <f:selectItem itemLabel="Tree" itemValue="Tree" /> </p:selectOneMenu> <h:outputText value="Shape:"/> <p:selectOneMenu label="Shape:" widgetVar="shapeSelector" onchange="addLogMessage(shapeSelector.value);" > <f:selectItem itemLabel="ELLIPSE" itemValue="ELLIPSE" /> <f:selectItem itemLabel="RECTANGLE" itemValue="RECTANGLE" /> <f:selectItem itemLabel="TRIANGLE" itemValue="TRIANGLE" /> <f:selectItem itemLabel="DIAMOND" itemValue="DIAMOND" /> <f:selectItem itemLabel="HEXAGON" itemValue="HEXAGON" /> <f:selectItem itemLabel="OCTAGON" itemValue="OCTAGON" /> <f:selectItem itemLabel="PARALLELOGRAM" itemValue="PARALLELOGRAM" /> </p:selectOneMenu> <h:outputText value="Color:"/> <p:colorPicker id="colorPicker" widgetVar="colorPicker" style="vertical-align:top"/> <h:outputText value="Size:"/> <p:spinner widgetVar="spinSize" value="10" min="10" max="50" size="2" style="vertical-align:top"/> </h:panelGrid> <p:commandButton type="button" onclick="applyStyleOnSelection()" value="Apply style on selection" style="width:100%"/> </p:fieldset> </div> <p:inputTextarea id="txtModelDescription" value ="#{demoAjaxBehaviorsBean.modelDescription}" style="clear:left;width:100%; height:25%;overflow:auto" /> </h:form> <script type="text/javascript"> $(document).ready(function() { $(colorPicker.jqId + '_livePreview').css('background-color', getSelectedColor()); }); function removeSelectedNodes(){ myGraph.getSelectedNodes().map(function(node){myGraph.removeNode(node.getId())}); myGraph.redraw(); } function applyStyleOnSelection(){ var color = getSelectedColor(); var size = spinSize.value; var shape = shapeSelector.value; myGraph.getSelectedNodes().map( function(node){ node.setColor(color); node.setSize(size); node.setShape(shape); } ); myGraph.getSelectedEdges().map( function(edge){ edge.setColor(color); edge.setWidth(size); edge.setLabel("" + size) } ); } function createEdges(){ var color = getSelectedColor(); var size = spinSize.value; var sourceNode = null; myGraph.getSelectedNodes().map( function(node){ if(sourceNode){ var id = "E_" + new Date().getTime(); myGraph.addEdge(id, id, sourceNode.getId(), node.getId(), true, size, color) } sourceNode = node; } ); myGraph.redraw(); } function getSelectedColor(){ //color from the colorpicker widget return $('.ui-colorpicker_hex > :input').val(); } </script>
package graphvis.demo.ajax; import graphvis.demo.common.GraphModelGenerator; import java.util.Date; import java.util.List; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import org.primefaces.event.SelectEvent; import org.primefaces.event.UnselectEvent; import org.primefaces.model.graphvis.GraphvisEdge; import org.primefaces.model.graphvis.GraphvisModel; import org.primefaces.model.graphvis.GraphvisModelElement; import org.primefaces.model.graphvis.GraphvisNode; import org.primefaces.model.graphvis.impl.GraphvisModelImpl; @ManagedBean @SessionScoped public class DemoAjaxBehaviorsBean { protected GraphvisModel graphModel; public GraphvisModel getGraphModel() { if(graphModel == null){ graphModel = new GraphvisModelImpl(); fillGraphModel(); } return graphModel; } public void setGraphModel(GraphvisModel graphModel) { this.graphModel = graphModel; } public void fillGraphModel() { GraphModelGenerator.fillGraphModel(graphModel, 5, 10, 20); graphModel.setLayout("ForceDirected"); } public void onSelectItems(SelectEvent event){ System.out.println("SELECTION:" + event.getObject()); FacesContext context = FacesContext.getCurrentInstance(); context.addMessage(null, new FacesMessage("Selection:" + dumpElementList((List<GraphvisModelElement>) event.getObject()))); } public void onUnselectItems(UnselectEvent event){ System.out.println("UNSELECTION:" + event.getObject()); FacesContext context = FacesContext.getCurrentInstance(); context.addMessage(null, new FacesMessage("Unselection: " + dumpElementList((List<GraphvisModelElement>) event.getObject()))); } public void onSelectNodes(SelectEvent event){ System.out.println("NODES SELECTION:" + event.getObject()); FacesContext context = FacesContext.getCurrentInstance(); context.addMessage(null, new FacesMessage("Nodes Selection:" + dumpElementList((List<GraphvisModelElement>) event.getObject()))); } public void onUnselectNodes(UnselectEvent event){ System.out.println("NODES UNSELECTION:" + event.getObject()); FacesContext context = FacesContext.getCurrentInstance(); context.addMessage(null, new FacesMessage("Nodes Unselection: " + dumpElementList((List<GraphvisModelElement>) event.getObject()))); } public void onSelectEdges(SelectEvent event){ System.out.println("EDGES SELECTION:" + event.getObject()); FacesContext context = FacesContext.getCurrentInstance(); context.addMessage(null, new FacesMessage("Edges Selection:" + dumpElementList((List<GraphvisModelElement>) event.getObject()))); } public void onUnselectEdges(UnselectEvent event){ System.out.println("EDGES UNSELECTION:" + event.getObject()); FacesContext context = FacesContext.getCurrentInstance(); context.addMessage(null, new FacesMessage("Edges Unselection: " + dumpElementList((List<GraphvisModelElement>) event.getObject()))); } protected String dumpElementList(List<GraphvisModelElement> lElements){ StringBuffer sb = new StringBuffer(); for(GraphvisModelElement element : lElements){ sb.append(element.getId() + " " + element.getLabel() + "\n"); } return sb.toString(); } public void setModelDescription(String modelDescription){ } public String getModelDescription(){ StringBuffer sb = new StringBuffer(); getGraphModel(); sb.append("NODES:\n"); for(GraphvisNode node : graphModel.getNodes()){ sb.append("Node " + node.getId() + ": " + node.getLabel() + " X:" + node.getX() + " Y:" + node.getY() + " " + node.getShape() + "\n"); } sb.append("EDGES:\n"); for(GraphvisEdge node : graphModel.getEdges()){ sb.append("Node " + node.getId() + ": " + node.getLabel() + " From " + node.getSourceNode().getId() + " to " + node.getTargetNode().getId() + "\n"); } sb.append(new Date().toString()); return sb.toString(); } }
package conf; import org.primefaces.model.graphvis.GraphvisEdge; import org.primefaces.model.graphvis.GraphvisModel; import org.primefaces.model.graphvis.GraphvisNode; import org.primefaces.model.graphvis.impl.GraphvisModelImpl; public class GraphModelGenerator { /** * Create a new graph model and fill it randomly * * @param minNodes : minimum node number * @param maxNodes : maximum node number * @param maxEdges : maximum edge number * * @return a new GraphvisModel filled */ public static GraphvisModel generateGraphModel(int minNodes, int maxNodes, int maxEdges){ GraphvisModel graphModel = new GraphvisModelImpl(); fillGraphModel(graphModel, minNodes, maxNodes, maxEdges); return graphModel; } /** * fill randomly a graph model. * * @param graphModel : the graphModel * @param minNodes : minimum node number * @param maxNodes : maximum node number * @param maxEdges : maximum edge number */ public static GraphvisModel fillGraphModel(GraphvisModel graphModel, int minNodes, int maxNodes, int maxEdges){ int maxWeight = 10; //remove all previous nodes for(GraphvisNode node : graphModel.getNodes()){ graphModel.removeNode(node); } long nodesNumber = minNodes + Math.round(Math.random() * (maxNodes - minNodes) ); long edgesNumber = nodesNumber * 3; if(edgesNumber > maxEdges){ edgesNumber = maxEdges; } System.out.println("Nodes number:" + nodesNumber + " Edeges number:" + edgesNumber); for(int i = 1; i <= nodesNumber ; i++){ graphModel.addNode("N" + i, "N" + i); } for(int i = 1; i < edgesNumber; i++){ GraphvisNode sourceNode = graphModel.getNode("N" + (Math.round(Math.random() * (nodesNumber - 1)) + 1)); GraphvisNode targetNode = graphModel.getNode("N" + (Math.round(Math.random() * (nodesNumber - 1)) + 1)); GraphvisEdge edge = graphModel.addEdge("" + i + "_" + sourceNode.getId() + "_" + targetNode.getId() , sourceNode, targetNode); edge.setWidth((int) (Math.round(Math.random() * maxWeight) + 1)); edge.setLabel("" + edge.getWidth()); edge.setDirected(true); } return graphModel; } }