View Javadoc

1   /*
2   Copyright 2010 James Pether Sörling Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
3   	$Id
4   */
5   package org.directdemocracyportal.democracy.web.actionlistener;
6   
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   import org.directdemocracyportal.democracy.web.ApplicationMessageHolder;
10  import org.directdemocracyportal.democracy.web.ImageConstants;
11  import org.directdemocracyportal.democracy.web.ApplicationMessageHolder.MessageConstans;
12  import org.directdemocracyportal.democracy.web.action.ControllerAction;
13  import org.directdemocracyportal.democracy.web.controller.Controller;
14  
15  import thinwire.ui.Button;
16  import thinwire.ui.Frame;
17  import thinwire.ui.Hyperlink;
18  import thinwire.ui.Menu;
19  import thinwire.ui.MessageBox;
20  import thinwire.ui.GridBox.Range;
21  import thinwire.ui.event.ActionEvent;
22  import thinwire.ui.event.ActionListener;
23  
24  /***
25   * The listener interface for receiving applicationAction events.
26   * The class that is interested in processing a applicationAction
27   * event implements this interface, and the object created
28   * with that class is registered with a component using the
29   * component's <code>addApplicationActionListener<code> method. When
30   * the applicationAction event occurs, that object's appropriate
31   * method is invoked.
32   *
33   * @see ApplicationActionEvent
34   */
35  public class ApplicationActionListener implements ActionListener
36  {
37  
38      /*** The log. */
39      private static Log log = LogFactory.getLog(ApplicationActionListener.class);
40  
41      /*** The controller. */
42      private final Controller controller;
43  
44      /***
45       * Instantiates a new application action listener.
46       *
47       * @param controller the controller
48       */
49      public ApplicationActionListener(Controller controller) {
50          super();
51          this.controller = controller;
52      }
53  
54      /*
55       * (non-Javadoc)
56       *
57       * @see thinwire.ui.event.ActionListener#actionPerformed(thinwire.ui.event.ActionEvent)
58       */
59      public void actionPerformed(ActionEvent ev) {
60          Object source = ev.getSource();
61          Object userObject = null;
62  
63          if (source instanceof Button) {
64              userObject = ((Button) source).getUserObject();
65          } else if (source instanceof Hyperlink) {
66              userObject = ((Hyperlink) source).getUserObject();
67          } else if (source instanceof Range) {
68              userObject = ((Range)source).getRow().getUserObject();
69          } else if (source instanceof Menu.Item) {
70              if (((Menu.Item) source).isEnabled()) {
71                  userObject = ((Menu.Item) source).getUserObject();
72              }
73          } else if (source instanceof Frame) {
74              userObject = ((Frame)source).getUserObject();
75          }
76  
77          if (userObject != null) {
78              try {
79                  controller.handleAction((ControllerAction) userObject);
80              } catch (Exception e) {
81                  log.error("Should have done rollback, unexpected error", e);
82                  MessageBox.confirm(ImageConstants.ACTION_ERROR, ApplicationMessageHolder
83                          .getMessage(MessageConstans.ERROR_MESSAGE),
84                          ApplicationMessageHolder
85                          .getMessage(MessageConstans.ERROR_MESSAGE) + " " + e.getMessage());
86              }
87          }
88      }
89  }