/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.ufcspa.simdecs.diagram.mb; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Locale; import javax.el.MethodExpression; import javax.faces.event.ActionEvent; import javax.faces.event.MethodExpressionActionListener; import org.primefaces.component.menu.Menu; import org.primefaces.component.menuitem.MenuItem; import org.primefaces.model.DefaultMenuModel; import org.primefaces.model.MenuModel; import org.ufcspa.simdecs.diagram.elements.Node; import org.ufcspa.simdecs.diagram.elements.NodeElement; import org.ufcspa.simdecs.diagram.elements.NodeTextProperty; import org.ufcspa.simdecs.diagram.elements.SwimLane; import org.ufcspa.simdecs.diagram.mb.beans.PropertyEditorBean; import org.ufcspa.simdecs.diagram.util.ReflectionUtils; /** * * @author maroni */ public class EditNode extends DefaultManagedBean { private final String BASE_ELEMENT_PACKAGE = "org.ufcspa.simdecs.diagram.elements"; private MenuModel menuActionList; private EditDiagram editDiagram; private Class currentNodeClass; private List properties; private SwimLane currentSwimLane; public EditNode() { menuActionList = new DefaultMenuModel(); properties = new ArrayList(); } public EditDiagram getEditDiagram() { return editDiagram; } public void setEditDiagram(EditDiagram editDiagram) { this.editDiagram = editDiagram; } public MenuModel getMenuActionList() { return menuActionList; } public void setMenuActionList(MenuModel menuActionList) { this.menuActionList = menuActionList; } public void prepareActionMenu() throws ClassNotFoundException, IOException, NoSuchMethodException, IllegalAccessException, InstantiationException, IllegalArgumentException, InvocationTargetException { String pActorID = getTextParameter("pActorID"); currentSwimLane = editDiagram.getDiagramManager().getSwimLane(pActorID); menuActionList = new DefaultMenuModel(); for(Class cclass: ReflectionUtils.getClasses(BASE_ELEMENT_PACKAGE)) { if (cclass.getAnnotation(NodeElement.class) != null) { Constructor nodeConstructor = cclass.getConstructor(new Class[]{String.class, Locale.class}); Node node = (Node) nodeConstructor.newInstance(getMessageBundle(), getLocale()); if (currentSwimLane.getNodes().isEmpty() && !node.isFirst()) continue; if (!node.isGrantedOnThisSwimLane(currentSwimLane)) continue; LinkedList nodes = currentSwimLane.getNodes(); if (nodes != null && !nodes.isEmpty() && !node.isGrantedAfterThisNode((Node)nodes.getLast())) continue; MenuItem item = new MenuItem(); item.setValue(node.getName()); item.setId("menuItem-" + node.getId()); item.setOnstart("dialogAddNodeMenu.hide();"); item.setOncomplete("dialogEditNodeMenu.show();"); item.setUpdate("fEditNodeMenu"); MethodExpression actionExpression = getFacesContext().getApplication().getExpressionFactory().createMethodExpression(getFacesContext().getELContext(), "#{editNode.processAddNode}", null, new Class[]{ActionEvent.class}); item.addActionListener(new MethodExpressionActionListener(actionExpression)); menuActionList.addMenuItem(item); } } Menu menu = (Menu) getComponentById("fAddNodeMenu:dinamycAddNodeMenu"); menu.getChildren().clear(); menu.clearInitialState(); menu.setModel(menuActionList); menu.buildMenuFromModel(); } public void processAddNode(ActionEvent event) throws ClassNotFoundException { String callerID = event.getComponent().getClientId(); String callerClassStrName = callerID.substring(callerID.indexOf("-") + 1, callerID.indexOf("-", callerID.indexOf("-") + 1)); currentNodeClass = Class.forName(BASE_ELEMENT_PACKAGE + '.' + callerClassStrName); properties = new ArrayList(); for (Field field : currentNodeClass.getDeclaredFields()) { NodeTextProperty textAnnotation = field.getAnnotation(NodeTextProperty.class); if (textAnnotation != null) { PropertyEditorBean property = new PropertyEditorBean(); property.setReflectionField(field); property.setName(getResourceMessage(textAnnotation.namei18n())); property.setMinLength(textAnnotation.minLength()); property.setMaxLength(textAnnotation.maxLength()); property.setRequired(textAnnotation.required()); properties.add(property); } } Menu menu = (Menu) getComponentById("fAddNodeMenu:dinamycAddNodeMenu"); } public void addNode() throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Constructor nodeConstructor = currentNodeClass.getConstructor(new Class[]{String.class, Locale.class}); Node node = (Node) nodeConstructor.newInstance(getMessageBundle(), getLocale()); for (PropertyEditorBean property : properties) { Method acessor = ReflectionUtils.getSetterMethod(currentNodeClass, property.getReflectionField()); acessor.invoke(node, property.getValue()); } currentSwimLane.addNode(node); } public List getProperties() { return properties; } public void setProperties(List properties) { this.properties = properties; } }