d076ae96 |
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.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.elements.Node; |
24 | import org.ufcspa.simdecs.diagram.elements.NodeElement; |
25 | import org.ufcspa.simdecs.diagram.elements.NodeTextProperty; |
26 | import org.ufcspa.simdecs.diagram.elements.SwimLane; |
27 | import org.ufcspa.simdecs.diagram.mb.beans.PropertyEditorBean; |
28 | import org.ufcspa.simdecs.diagram.util.ReflectionUtils; |
29 | |
30 | /** |
31 | * |
32 | * @author maroni |
33 | */ |
34 | public class EditNode extends DefaultManagedBean { |
35 | |
36 | private final String BASE_ELEMENT_PACKAGE = "org.ufcspa.simdecs.diagram.elements"; |
37 | private MenuModel menuActionList; |
38 | private EditDiagram editDiagram; |
39 | private Class currentNodeClass; |
40 | private List<PropertyEditorBean> properties; |
41 | private SwimLane currentSwimLane; |
42 | |
43 | public EditNode() { |
44 | menuActionList = new DefaultMenuModel(); |
45 | properties = new ArrayList<PropertyEditorBean>(); |
46 | } |
47 | |
48 | public EditDiagram getEditDiagram() { |
49 | return editDiagram; |
50 | } |
51 | |
52 | public void setEditDiagram(EditDiagram editDiagram) { |
53 | this.editDiagram = editDiagram; |
54 | } |
55 | |
56 | public MenuModel getMenuActionList() { |
57 | return menuActionList; |
58 | } |
59 | |
60 | public void setMenuActionList(MenuModel menuActionList) { |
61 | this.menuActionList = menuActionList; |
62 | } |
63 | |
64 | public void prepareActionMenu() throws ClassNotFoundException, IOException, NoSuchMethodException, IllegalAccessException, InstantiationException, IllegalArgumentException, InvocationTargetException { |
65 | String pActorID = getTextParameter("pActorID"); |
66 | currentSwimLane = editDiagram.getDiagramManager().getSwimLane(pActorID); |
67 | |
68 | menuActionList = new DefaultMenuModel(); |
69 | |
70 | for(Class cclass: ReflectionUtils.getClasses(BASE_ELEMENT_PACKAGE)) { |
71 | if (cclass.getAnnotation(NodeElement.class) != null) { |
72 | Constructor nodeConstructor = cclass.getConstructor(new Class[]{String.class, Locale.class}); |
73 | Node node = (Node) nodeConstructor.newInstance(getMessageBundle(), getLocale()); |
74 | |
75 | if (currentSwimLane.getNodes().isEmpty() && !node.isFirst()) |
76 | continue; |
77 | |
78 | if (!node.isGrantedOnThisSwimLane(currentSwimLane)) |
79 | continue; |
80 | LinkedList nodes = currentSwimLane.getNodes(); |
81 | if (nodes != null && !nodes.isEmpty() && !node.isGrantedAfterThisNode((Node)nodes.getLast())) |
82 | continue; |
83 | |
84 | MenuItem item = new MenuItem(); |
85 | item.setValue(node.getName()); |
86 | item.setId("menuItem-" + node.getId()); |
87 | item.setOnstart("dialogAddNodeMenu.hide();"); |
88 | item.setOncomplete("dialogEditNodeMenu.show();"); |
89 | item.setUpdate("fEditNodeMenu"); |
90 | |
91 | MethodExpression actionExpression = getFacesContext().getApplication().getExpressionFactory().createMethodExpression(getFacesContext().getELContext(), |
92 | "#{editNode.processAddNode}", |
93 | null, |
94 | new Class[]{ActionEvent.class}); |
95 | |
96 | item.addActionListener(new MethodExpressionActionListener(actionExpression)); |
97 | menuActionList.addMenuItem(item); |
98 | } |
99 | } |
100 | |
101 | Menu menu = (Menu) getComponentById("fAddNodeMenu:dinamycAddNodeMenu"); |
102 | menu.getChildren().clear(); |
103 | menu.clearInitialState(); |
104 | menu.setModel(menuActionList); |
105 | menu.buildMenuFromModel(); |
106 | } |
107 | |
108 | public void processAddNode(ActionEvent event) throws ClassNotFoundException { |
109 | String callerID = event.getComponent().getClientId(); |
110 | String callerClassStrName = callerID.substring(callerID.indexOf("-") + 1, callerID.indexOf("-", callerID.indexOf("-") + 1)); |
111 | currentNodeClass = Class.forName(BASE_ELEMENT_PACKAGE + '.' + callerClassStrName); |
112 | |
113 | properties = new ArrayList<PropertyEditorBean>(); |
114 | for (Field field : currentNodeClass.getDeclaredFields()) { |
115 | NodeTextProperty textAnnotation = field.getAnnotation(NodeTextProperty.class); |
116 | |
117 | if (textAnnotation != null) { |
118 | PropertyEditorBean property = new PropertyEditorBean(); |
119 | property.setReflectionField(field); |
120 | property.setName(getResourceMessage(textAnnotation.namei18n())); |
121 | property.setMinLength(textAnnotation.minLength()); |
122 | property.setMaxLength(textAnnotation.maxLength()); |
123 | property.setRequired(textAnnotation.required()); |
124 | properties.add(property); |
125 | } |
126 | } |
127 | |
128 | Menu menu = (Menu) getComponentById("fAddNodeMenu:dinamycAddNodeMenu"); |
129 | } |
130 | |
131 | public void addNode() throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { |
132 | Constructor nodeConstructor = currentNodeClass.getConstructor(new Class[]{String.class, Locale.class}); |
133 | Node node = (Node) nodeConstructor.newInstance(getMessageBundle(), getLocale()); |
134 | |
135 | for (PropertyEditorBean property : properties) { |
136 | Method acessor = ReflectionUtils.getSetterMethod(currentNodeClass, property.getReflectionField()); |
137 | acessor.invoke(node, property.getValue()); |
138 | } |
139 | |
140 | currentSwimLane.addNode(node); |
141 | |
142 | } |
143 | |
144 | public List<PropertyEditorBean> getProperties() { |
145 | return properties; |
146 | } |
147 | |
148 | public void setProperties(List<PropertyEditorBean> properties) { |
149 | this.properties = properties; |
150 | } |
151 | |
152 | |
153 | } |