(no commit message)
[simdecs.git] / src / java / org / ufcspa / simdecs / diagram / mb / EditNode.java
CommitLineData
d076ae96 1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package org.ufcspa.simdecs.diagram.mb;
6
7import java.io.IOException;
8import java.lang.reflect.Constructor;
9import java.lang.reflect.Field;
10import java.lang.reflect.InvocationTargetException;
11import java.lang.reflect.Method;
12import java.util.ArrayList;
13import java.util.LinkedList;
14import java.util.List;
15import java.util.Locale;
16import javax.el.MethodExpression;
17import javax.faces.event.ActionEvent;
18import javax.faces.event.MethodExpressionActionListener;
19import org.primefaces.component.menu.Menu;
20import org.primefaces.component.menuitem.MenuItem;
21import org.primefaces.model.DefaultMenuModel;
22import org.primefaces.model.MenuModel;
1c6214fc 23import org.ufcspa.simdecs.diagram.DiagramManager;
d076ae96 24import org.ufcspa.simdecs.diagram.elements.Node;
25import org.ufcspa.simdecs.diagram.elements.NodeElement;
26import org.ufcspa.simdecs.diagram.elements.NodeTextProperty;
27import org.ufcspa.simdecs.diagram.elements.SwimLane;
28import org.ufcspa.simdecs.diagram.mb.beans.PropertyEditorBean;
29import org.ufcspa.simdecs.diagram.util.ReflectionUtils;
30
31/**
32 *
33 * @author maroni
34 */
35public class EditNode extends DefaultManagedBean {
36
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;
43
44 public EditNode() {
45 menuActionList = new DefaultMenuModel();
46 properties = new ArrayList<PropertyEditorBean>();
47 }
48
49 public EditDiagram getEditDiagram() {
50 return editDiagram;
51 }
52
53 public void setEditDiagram(EditDiagram editDiagram) {
54 this.editDiagram = editDiagram;
55 }
56
57 public MenuModel getMenuActionList() {
58 return menuActionList;
59 }
60
61 public void setMenuActionList(MenuModel menuActionList) {
62 this.menuActionList = menuActionList;
63 }
64
65 public void prepareActionMenu() throws ClassNotFoundException, IOException, NoSuchMethodException, IllegalAccessException, InstantiationException, IllegalArgumentException, InvocationTargetException {
66 String pActorID = getTextParameter("pActorID");
67 currentSwimLane = editDiagram.getDiagramManager().getSwimLane(pActorID);
68
69 menuActionList = new DefaultMenuModel();
70
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());
75
76 if (currentSwimLane.getNodes().isEmpty() && !node.isFirst())
77 continue;
78
79 if (!node.isGrantedOnThisSwimLane(currentSwimLane))
80 continue;
81 LinkedList nodes = currentSwimLane.getNodes();
82 if (nodes != null && !nodes.isEmpty() && !node.isGrantedAfterThisNode((Node)nodes.getLast()))
83 continue;
84
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");
91
92 MethodExpression actionExpression = getFacesContext().getApplication().getExpressionFactory().createMethodExpression(getFacesContext().getELContext(),
93 "#{editNode.processAddNode}",
94 null,
95 new Class[]{ActionEvent.class});
96
97 item.addActionListener(new MethodExpressionActionListener(actionExpression));
98 menuActionList.addMenuItem(item);
99 }
100 }
101
102 Menu menu = (Menu) getComponentById("fAddNodeMenu:dinamycAddNodeMenu");
103 menu.getChildren().clear();
104 menu.clearInitialState();
105 menu.setModel(menuActionList);
106 menu.buildMenuFromModel();
107 }
108
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);
113
114 properties = new ArrayList<PropertyEditorBean>();
115 for (Field field : currentNodeClass.getDeclaredFields()) {
116 NodeTextProperty textAnnotation = field.getAnnotation(NodeTextProperty.class);
117
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);
126 }
127 }
128
129 Menu menu = (Menu) getComponentById("fAddNodeMenu:dinamycAddNodeMenu");
130 }
131
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());
135
136 for (PropertyEditorBean property : properties) {
137 Method acessor = ReflectionUtils.getSetterMethod(currentNodeClass, property.getReflectionField());
138 acessor.invoke(node, property.getValue());
139 }
140
1c6214fc 141 getEditDiagram().getDiagramManager().addNode(currentSwimLane, node);
d076ae96 142
143 }
144
145 public List<PropertyEditorBean> getProperties() {
146 return properties;
147 }
148
149 public void setProperties(List<PropertyEditorBean> properties) {
150 this.properties = properties;
151 }
152
153
154}