f297142d38c53b83fbb22b1bff3b0e2d8e6b0883
[simdecs_seam.git] / Diagrama / src / java / org / ufcspa / simdecs / diagram / mb / EditNode.java
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5 package org.ufcspa.simdecs.diagram.mb;
6
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;
27
28 /**
29  *
30  * @author maroni
31  */
32 public class EditNode extends DefaultManagedBean {
33
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;
40
41     public EditNode() {
42         menuActionList = new DefaultMenuModel();
43         properties = new ArrayList<PropertyEditorBean>();
44     }
45     
46     public EditDiagram getEditDiagram() {
47         return editDiagram;
48     }
49
50     public void setEditDiagram(EditDiagram editDiagram) {
51         this.editDiagram = editDiagram;
52     }
53
54     public MenuModel getMenuActionList() {
55         return menuActionList;
56     }
57
58     public void setMenuActionList(MenuModel menuActionList) {
59         this.menuActionList = menuActionList;
60     }
61
62     public void prepareActionMenu() {
63         String pActorID = getTextParameter("pActorID");
64         currentSwimLane = editDiagram.getDiagramManager().getSwimLane(pActorID);
65
66         menuActionList = new DefaultMenuModel();
67
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");
76
77             MethodExpression actionExpression = getFacesContext().getApplication().getExpressionFactory().createMethodExpression(getFacesContext().getELContext(),
78                     "#{editNode.processAddNode}",
79                     null,
80                     new Class[]{ActionEvent.class});
81
82             item.addActionListener(new MethodExpressionActionListener(actionExpression));
83             menuActionList.addMenuItem(item);
84         }
85
86         Menu menu = (Menu) getComponentById("fAddNodeMenu:dinamycAddNodeMenu");
87         menu.getChildren().clear();
88         menu.clearInitialState();
89         menu.setModel(menuActionList);
90         menu.buildMenuFromModel();
91     }
92
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);
97
98         properties = new ArrayList<PropertyEditorBean>();
99         for (Field field : currentNodeClass.getDeclaredFields()) {
100             NodeTextProperty textAnnotation = field.getAnnotation(NodeTextProperty.class);
101
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);
111             }
112         }
113
114         Menu menu = (Menu) getComponentById("fAddNodeMenu:dinamycAddNodeMenu");
115     }
116  
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());
120         
121         for (PropertyEditorBean property : properties) {
122             Method acessor = ReflectionUtils.getSetterMethod(currentNodeClass, property.getReflectionField());
123             acessor.invoke(node, property.getValue());
124         }
125         
126         currentSwimLane.addNode(node);
127         
128     }
129
130     public List<PropertyEditorBean> getProperties() {
131         return properties;
132     }
133
134     public void setProperties(List<PropertyEditorBean> properties) {
135         this.properties = properties;
136     }
137
138
139 }