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   
6   package com.hack23.cia.web.controller.application;
7   
8   import org.apache.commons.logging.Log;
9   import org.apache.commons.logging.LogFactory;
10  
11  import thinwire.ui.Button;
12  import thinwire.ui.Component;
13  import thinwire.ui.Frame;
14  import thinwire.ui.Hyperlink;
15  import thinwire.ui.Menu;
16  import thinwire.ui.Tree;
17  import thinwire.ui.GridBox.Range;
18  import thinwire.ui.event.ActionEvent;
19  import thinwire.ui.event.KeyPressEvent;
20  
21  import com.hack23.cia.model.application.impl.user.ApplicationActionEvent;
22  import com.hack23.cia.web.action.common.ApplicationErrorAction;
23  import com.hack23.cia.web.action.common.ControllerAction;
24  import com.hack23.cia.web.common.ControllerActionListener;
25  
26  /***
27   * The listener interface for receiving applicationAction events.
28   * The class that is interested in processing a applicationAction
29   * event implements this interface, and the object created
30   * with that class is registered with a component using the
31   * component's <code>addApplicationActionListener<code> method. When
32   * the applicationAction event occurs, that object's appropriate
33   * method is invoked.
34   *
35   * @see ApplicationActionEvent
36   */
37  public class ApplicationActionListener implements ControllerActionListener {
38  
39      /*** The Constant LOGGER. */
40      private static final Log LOGGER = LogFactory
41              .getLog(ApplicationActionListener.class);
42  
43  	/*** The Constant serialVersionUID. */
44  	private static final long serialVersionUID = 1L;
45  
46      /*** The controller. */
47      private final Controller controller;
48  
49      /***
50       * Instantiates a new application action listener.
51       *
52       * @param controller the controller
53       */
54      public ApplicationActionListener(final Controller controller) {
55          super();
56          this.controller = controller;
57      }
58  
59      /*
60       * (non-Javadoc)
61       * 
62       * @seethinwire.ui.event.ActionListener#actionPerformed(thinwire.ui.event.
63       * ActionEvent)
64       */
65      @Override
66  	public final void actionPerformed(final ActionEvent ev) {
67          final Object source = ev.getSource();
68          Object userObject = null;
69  
70          if (source instanceof Button) {
71              userObject = ((Button) source).getUserObject();
72          } else if (source instanceof Hyperlink) {
73              userObject = ((Hyperlink) source).getUserObject();
74          } else if (source instanceof Range) {
75              userObject = ((Range) source).getRow().getUserObject();
76          } else if (source instanceof Menu.Item) {
77              if (((Menu.Item) source).isEnabled()) {
78                  userObject = ((Menu.Item) source).getUserObject();
79              }
80          } else if (source instanceof Frame) {
81              userObject = ((Frame) source).getUserObject();
82          } else if (source instanceof Tree) {
83              if (((Tree) source).getSelectedItem() != null) {
84                  userObject = ((Tree) source).getSelectedItem().getUserObject();
85              } else {
86                  userObject = ((Tree) source).getUserObject();
87              }
88          } else if (source instanceof Tree.Item) {
89              userObject = ((Tree.Item) source).getUserObject();
90          }
91  
92          if (userObject != null) {
93          	handleUserAction(userObject);
94          }
95      }
96  
97      /* (non-Javadoc)
98       * @see com.hack23.cia.web.action.common.ControllerActionListener#handleUserAction(java.lang.Object)
99       */
100     @Override
101 	public final void handleUserAction(final Object userObject) {
102         if (userObject != null) {
103             try {
104                 controller.handleAction((ControllerAction) userObject);
105             } catch (final Exception e) {
106                 LOGGER.error("Should have done rollback, unexpected error:", e); //$NON-NLS-1$
107 
108                 final ApplicationErrorAction applicationErrorAction = new ApplicationErrorAction(e.getStackTrace(),"Should have done rollback, unexpected error");
109                 try {
110                     controller.handleAction(applicationErrorAction);
111                 } catch (final Exception reallyScrewedUpException) {
112                     LOGGER
113                             .fatal(
114                                     "Something really bad, failed to log application error",
115                                     reallyScrewedUpException);
116                 }
117             }
118         }
119     }
120 
121 	/*
122      * (non-Javadoc)
123      * 
124      * @see
125      * thinwire.ui.event.KeyPressListener#keyPress(thinwire.ui.event.KeyPressEvent
126      * )
127      */
128     @Override
129     public final void keyPress(final KeyPressEvent ev) {
130         final Object source = ev.getSource();
131         Object userObject = null;
132 
133         if (source instanceof Component) {
134             userObject = ((Component) source).getUserObject();
135         }
136 
137         if (userObject != null) {
138         	handleUserAction(userObject);
139         }
140     }
141 }