2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
5 package org.ufcspa.simdecs.diagram.mb;
7 import java.io.IOException;
8 import java.lang.reflect.Constructor;
9 import java.lang.reflect.Field;
10 import java.lang.reflect.InvocationTargetException;
11 import java.lang.reflect.Method;
12 import java.util.ArrayList;
13 import java.util.LinkedList;
14 import java.util.List;
15 import java.util.Locale;
16 import javax.el.MethodExpression;
17 import javax.faces.event.ActionEvent;
18 import javax.faces.event.MethodExpressionActionListener;
19 import org.primefaces.component.menu.Menu;
20 import org.primefaces.component.menuitem.MenuItem;
21 import org.primefaces.model.DefaultMenuModel;
22 import org.primefaces.model.MenuModel;
23 import org.ufcspa.simdecs.diagram.DiagramManager;
24 import org.ufcspa.simdecs.diagram.elements.Node;
25 import org.ufcspa.simdecs.diagram.elements.NodeElement;
26 import org.ufcspa.simdecs.diagram.elements.NodeTextProperty;
27 import org.ufcspa.simdecs.diagram.elements.SwimLane;
28 import org.ufcspa.simdecs.diagram.mb.beans.PropertyEditorBean;
29 import org.ufcspa.simdecs.diagram.util.ReflectionUtils;
35 public class EditNode extends DefaultManagedBean {
37 private final String BASE_ELEMENT_PACKAGE = "org.ufcspa.simdecs.diagram.elements";
38 private MenuModel menuActionList;
39 private EditDiagram editDiagram;
40 private Class currentNodeClass;
41 private List<PropertyEditorBean> properties;
42 private SwimLane currentSwimLane;
45 menuActionList = new DefaultMenuModel();
46 properties = new ArrayList<PropertyEditorBean>();
49 public EditDiagram getEditDiagram() {
53 public void setEditDiagram(EditDiagram editDiagram) {
54 this.editDiagram = editDiagram;
57 public MenuModel getMenuActionList() {
58 return menuActionList;
61 public void setMenuActionList(MenuModel menuActionList) {
62 this.menuActionList = menuActionList;
65 public void prepareActionMenu() throws ClassNotFoundException, IOException, NoSuchMethodException, IllegalAccessException, InstantiationException, IllegalArgumentException, InvocationTargetException {
66 String pActorID = getTextParameter("pActorID");
67 currentSwimLane = editDiagram.getDiagramManager().getSwimLane(pActorID);
69 menuActionList = new DefaultMenuModel();
71 for(Class cclass: ReflectionUtils.getClasses(BASE_ELEMENT_PACKAGE)) {
72 if (cclass.getAnnotation(NodeElement.class) != null) {
73 Constructor nodeConstructor = cclass.getConstructor(new Class[]{String.class, Locale.class});
74 Node node = (Node) nodeConstructor.newInstance(getMessageBundle(), getLocale());
76 if (currentSwimLane.getNodes().isEmpty() && !node.isFirst())
79 if (!node.isGrantedOnThisSwimLane(currentSwimLane))
81 LinkedList nodes = currentSwimLane.getNodes();
82 if (nodes != null && !nodes.isEmpty() && !node.isGrantedAfterThisNode((Node)nodes.getLast()))
85 MenuItem item = new MenuItem();
86 item.setValue(node.getName());
87 item.setId("menuItem-" + node.getId());
88 item.setOnstart("dialogAddNodeMenu.hide();");
89 item.setOncomplete("dialogEditNodeMenu.show();");
90 item.setUpdate("fEditNodeMenu");
92 MethodExpression actionExpression = getFacesContext().getApplication().getExpressionFactory().createMethodExpression(getFacesContext().getELContext(),
93 "#{editNode.processAddNode}",
95 new Class[]{ActionEvent.class});
97 item.addActionListener(new MethodExpressionActionListener(actionExpression));
98 menuActionList.addMenuItem(item);
102 Menu menu = (Menu) getComponentById("fAddNodeMenu:dinamycAddNodeMenu");
103 menu.getChildren().clear();
104 menu.clearInitialState();
105 menu.setModel(menuActionList);
106 menu.buildMenuFromModel();
109 public void processAddNode(ActionEvent event) throws ClassNotFoundException {
110 String callerID = event.getComponent().getClientId();
111 String callerClassStrName = callerID.substring(callerID.indexOf("-") + 1, callerID.indexOf("-", callerID.indexOf("-") + 1));
112 currentNodeClass = Class.forName(BASE_ELEMENT_PACKAGE + '.' + callerClassStrName);
114 properties = new ArrayList<PropertyEditorBean>();
115 for (Field field : currentNodeClass.getDeclaredFields()) {
116 NodeTextProperty textAnnotation = field.getAnnotation(NodeTextProperty.class);
118 if (textAnnotation != null) {
119 PropertyEditorBean property = new PropertyEditorBean();
120 property.setReflectionField(field);
121 property.setName(getResourceMessage(textAnnotation.namei18n()));
122 property.setMinLength(textAnnotation.minLength());
123 property.setMaxLength(textAnnotation.maxLength());
124 property.setRequired(textAnnotation.required());
125 properties.add(property);
129 Menu menu = (Menu) getComponentById("fAddNodeMenu:dinamycAddNodeMenu");
132 public void addNode() throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
133 Constructor nodeConstructor = currentNodeClass.getConstructor(new Class[]{String.class, Locale.class});
134 Node node = (Node) nodeConstructor.newInstance(getMessageBundle(), getLocale());
136 for (PropertyEditorBean property : properties) {
137 Method acessor = ReflectionUtils.getSetterMethod(currentNodeClass, property.getReflectionField());
138 acessor.invoke(node, property.getValue());
141 getEditDiagram().getDiagramManager().addNode(currentSwimLane, node);
145 public List<PropertyEditorBean> getProperties() {
149 public void setProperties(List<PropertyEditorBean> properties) {
150 this.properties = properties;