1
2
3
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
61
62
63
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
98
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);
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
123
124
125
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 }