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.lang.reflect.Constructor;
8 import java.lang.reflect.Field;
9 import java.lang.reflect.InvocationTargetException;
10 import java.lang.reflect.Method;
11 import java.util.ArrayList;
12 import java.util.List;
13 import java.util.Locale;
14 import javax.el.MethodExpression;
15 import javax.faces.event.ActionEvent;
16 import javax.faces.event.MethodExpressionActionListener;
17 import org.primefaces.component.menu.Menu;
18 import org.primefaces.component.menuitem.MenuItem;
19 import org.primefaces.model.DefaultMenuModel;
20 import org.primefaces.model.MenuModel;
21 import org.ufcspa.simdecs.diagram.elements.Begin;
22 import org.ufcspa.simdecs.diagram.elements.Node;
23 import org.ufcspa.simdecs.diagram.elements.NodeTextProperty;
24 import org.ufcspa.simdecs.diagram.elements.SwimLane;
25 import org.ufcspa.simdecs.diagram.mb.beans.PropertyEditorBean;
26 import org.ufcspa.simdecs.diagram.util.ReflectionUtils;
32 public class EditNode extends DefaultManagedBean {
34 private final String BASE_ELEMENT_PACKAGE = "org.ufcspa.simdecs.diagram.elements";
35 private MenuModel menuActionList;
36 private EditDiagram editDiagram;
37 private Class currentNodeClass;
38 private List<PropertyEditorBean> properties;
39 private SwimLane currentSwimLane;
42 menuActionList = new DefaultMenuModel();
43 properties = new ArrayList<PropertyEditorBean>();
46 public EditDiagram getEditDiagram() {
50 public void setEditDiagram(EditDiagram editDiagram) {
51 this.editDiagram = editDiagram;
54 public MenuModel getMenuActionList() {
55 return menuActionList;
58 public void setMenuActionList(MenuModel menuActionList) {
59 this.menuActionList = menuActionList;
62 public void prepareActionMenu() {
63 String pActorID = getTextParameter("pActorID");
64 currentSwimLane = editDiagram.getDiagramManager().getSwimLane(pActorID);
66 menuActionList = new DefaultMenuModel();
68 if (currentSwimLane.getNodes().isEmpty()) {
69 MenuItem item = new MenuItem();
70 Begin begin = new Begin(getMessageBundle(), getLocale());
71 item.setValue(begin.getName());
72 item.setId("menuItem-" + begin.getId());
73 item.setOnstart("dialogAddNodeMenu.hide();");
74 item.setOncomplete("dialogEditNodeMenu.show();");
75 item.setUpdate("fEditNodeMenu");
77 MethodExpression actionExpression = getFacesContext().getApplication().getExpressionFactory().createMethodExpression(getFacesContext().getELContext(),
78 "#{editNode.processAddNode}",
80 new Class[]{ActionEvent.class});
82 item.addActionListener(new MethodExpressionActionListener(actionExpression));
83 menuActionList.addMenuItem(item);
86 Menu menu = (Menu) getComponentById("fAddNodeMenu:dinamycAddNodeMenu");
87 menu.getChildren().clear();
88 menu.clearInitialState();
89 menu.setModel(menuActionList);
90 menu.buildMenuFromModel();
93 public void processAddNode(ActionEvent event) throws ClassNotFoundException {
94 String callerID = event.getComponent().getClientId();
95 String callerClassStrName = callerID.substring(callerID.indexOf("-") + 1, callerID.indexOf("-", callerID.indexOf("-") + 1));
96 currentNodeClass = Class.forName(BASE_ELEMENT_PACKAGE + '.' + callerClassStrName);
98 properties = new ArrayList<PropertyEditorBean>();
99 for (Field field : currentNodeClass.getDeclaredFields()) {
100 NodeTextProperty textAnnotation = field.getAnnotation(NodeTextProperty.class);
102 if (textAnnotation != null) {
103 System.out.println("annoted");
104 PropertyEditorBean property = new PropertyEditorBean();
105 property.setReflectionField(field);
106 property.setName(getResourceMessage(textAnnotation.namei18n()));
107 property.setMinLength(textAnnotation.minLength());
108 property.setMaxLength(textAnnotation.maxLength());
109 property.setRequired(textAnnotation.required());
110 properties.add(property);
114 Menu menu = (Menu) getComponentById("fAddNodeMenu:dinamycAddNodeMenu");
117 public void addNode() throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
118 Constructor nodeConstructor = currentNodeClass.getConstructor(new Class[]{String.class, Locale.class});
119 Node node = (Node) nodeConstructor.newInstance(getMessageBundle(), getLocale());
121 for (PropertyEditorBean property : properties) {
122 Method acessor = ReflectionUtils.getSetterMethod(currentNodeClass, property.getReflectionField());
123 acessor.invoke(node, property.getValue());
126 currentSwimLane.addNode(node);
130 public List<PropertyEditorBean> getProperties() {
134 public void setProperties(List<PropertyEditorBean> properties) {
135 this.properties = properties;