(no commit message)
[simdecs.git] / 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.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;
30
31 /**
32  *
33  * @author maroni
34  */
35 public 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         
141         getEditDiagram().getDiagramManager().addNode(currentSwimLane, node);
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 }