View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
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   * A listener for update buttons. The prepostfieldlistener isn't
40   * very usefull for this purpose.
41   *
42   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
43   * @version $Id: UpdateButtonsListener.java,v 1.1 2005/12/18 12:58:21 mvdb Exp $
44   */
45  public class UpdateButtonsListener extends XuluxListener implements ActionListener, ListSelectionListener {
46  
47      /**
48       * The parent
49       */
50      protected IWidget parent;
51      /**
52       * The source
53       */
54      protected IWidget source;
55  
56      /**
57       * Hack to reduce mess in table.
58       */
59      protected Table table;
60  
61      /**
62       * @param table the table
63       */
64      public UpdateButtonsListener(Table table) {
65          this.table = table;
66      }
67  
68      /**
69       *
70       * @param parent the parent widget
71       * @param source the source widget
72       */
73      public UpdateButtonsListener(IWidget parent, IWidget source) {
74          super(source);
75          this.parent = parent;
76          this.source = source;
77      }
78  
79      /**
80       * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
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                  // figure out what object type to create
94                  if (parent instanceof IContentWidget) {
95                      Object objContent = ((IContentWidget) parent).getContent();
96                      // @todo Only supports content lists...
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                             // @todo the nyx magic stops here for now
112                             return;
113                         }
114                     }
115                 }
116             } else {
117                 partBean = ClassLoaderUtils.getObjectFromClass(value.getClass());
118             }
119             setLocatorValue(partBean);
120         } else if (actionType.equals("delete")) {
121             // process post rules first..
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             // @todo Do some rule magic here, since rules can be part of actions!
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      * Set the locator value if such a locator is present
147      * properties used for tables : locator and locator.field
148      * @param bean the bean
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      * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
172      */
173     public void valueChanged(ListSelectionEvent e) {
174         if (table != null) {
175             table.refreshUpdateButtons();
176         }
177     }
178 
179 }