1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.xulux.guilayer.swing.listeners;
17
18 import java.awt.event.ActionEvent;
19 import java.awt.event.ActionListener;
20 import java.io.InputStream;
21 import java.util.List;
22
23 import javax.swing.event.ListSelectionEvent;
24 import javax.swing.event.ListSelectionListener;
25
26 import org.xulux.api.dataprovider.IField;
27 import org.xulux.api.dataprovider.IMapping;
28 import org.xulux.api.gui.IContentWidget;
29 import org.xulux.api.gui.IWidget;
30 import org.xulux.core.ApplicationPart;
31 import org.xulux.core.XuluxContext;
32 import org.xulux.gui.XuluxListener;
33 import org.xulux.gui.utils.XuluxEventQueue;
34 import org.xulux.guidriver.XuluxGuiDriver;
35 import org.xulux.guilayer.swing.widgets.Table;
36 import org.xulux.utils.ClassLoaderUtils;
37
38
39
40
41
42
43
44
45 public class UpdateButtonsListener extends XuluxListener implements ActionListener, ListSelectionListener {
46
47
48
49
50 protected IWidget parent;
51
52
53
54 protected IWidget source;
55
56
57
58
59 protected Table table;
60
61
62
63
64 public UpdateButtonsListener(Table table) {
65 this.table = table;
66 }
67
68
69
70
71
72
73 public UpdateButtonsListener(IWidget parent, IWidget source) {
74 super(source);
75 this.parent = parent;
76 this.source = source;
77 }
78
79
80
81
82 public void actionPerformed(ActionEvent e) {
83 XuluxEventQueue.getInstance().holdEvents(false);
84 String actionType = source.getProperty("action.type");
85 if (actionType == null) {
86 return;
87 }
88 Object partBean = parent.getGuiValue();
89 if (actionType.equals("add")) {
90 completed(true);
91 Object value = partBean;
92 if (value == null) {
93
94 if (parent instanceof IContentWidget) {
95 Object objContent = ((IContentWidget) parent).getContent();
96
97 if (objContent instanceof List || objContent == null) {
98 List content = (List) objContent;
99 String classType = parent.getProperty("classType");
100 if (classType != null) {
101 partBean = ClassLoaderUtils.getObjectFromClassString(classType);
102 } else if (content != null && content.size() > 0) {
103 partBean = ClassLoaderUtils.getObjectFromClass(content.get(0).getClass());
104 }
105 if (partBean == null) {
106 System.out.println(
107 "Cannot determine type to create for widget "
108 + parent.getName()
109 + " please create a rule to call"
110 + " the updateform or add the classType property to the table");
111
112 return;
113 }
114 }
115 }
116 } else {
117 partBean = ClassLoaderUtils.getObjectFromClass(value.getClass());
118 }
119 setLocatorValue(partBean);
120 } else if (actionType.equals("delete")) {
121
122 completed(true);
123 System.out.println("Deleting");
124 } else if (actionType.equals("update")) {
125 completed(true);
126 System.out.println("Updating");
127 } else {
128 completed(true);
129 return;
130 }
131 String xml = source.getProperty("action");
132 if (xml == null || "".equals(xml)) {
133
134 System.out.println("No action to preform on widget " + source.getName());
135 return;
136 }
137 XuluxGuiDriver handler = new XuluxGuiDriver();
138 InputStream stream = getClass().getClassLoader().getResourceAsStream(xml);
139 ApplicationPart part = handler.read(stream, partBean);
140 part.getSession().setValue("nyx.callerwidget", parent);
141 part.setParentPart(parent.getPart());
142 part.activate();
143 }
144
145
146
147
148
149
150 private void setLocatorValue(Object bean) {
151 String locator = parent.getProperty("locator");
152 if (bean == null) {
153 return;
154 }
155 if (locator != null) {
156 String locatorField = parent.getProperty("locator.field");
157 IWidget widget = parent.getPart().getWidget(locator);
158 if (widget != null && locatorField != null) {
159 Object value = widget.getValue();
160 String field = widget.getField();
161 if (field != null && value != null) {
162 IMapping mapping = XuluxContext.getDictionary().getDefaultProvider().getMapping(bean);
163 IField f = mapping.getField(locatorField);
164 f.setValue(bean, value);
165 }
166 }
167 }
168
169 }
170
171
172
173 public void valueChanged(ListSelectionEvent e) {
174 if (table != null) {
175 table.refreshUpdateButtons();
176 }
177 }
178
179 }