1 package siouxsie.mvc.impl;
2
3 import java.awt.BorderLayout;
4 import java.util.ArrayList;
5 import java.util.Collection;
6
7 import javax.swing.JComponent;
8 import javax.swing.JPanel;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 import siouxsie.mvc.IScreen;
14 import siouxsie.mvc.IScreenHandler;
15 import siouxsie.mvc.IValueStackAware;
16 import siouxsie.mvc.Message;
17 import siouxsie.mvc.Severity;
18
19 import com.opensymphony.xwork2.ActionInvocation;
20 import com.opensymphony.xwork2.ObjectFactory;
21 import com.opensymphony.xwork2.ValidationAware;
22
23 /**
24 * Panel-based {@link IScreenHandler}.
25 * Tightly-coupled with {@link ScreenResult}.
26 * Install the {@link IScreen} in a the center of
27 * {@link BorderLayout} panel.
28 * @author Arnaud Cogoluegnes
29 * @version $Id$
30 */
31 public class PanelScreenHandler implements IScreenHandler {
32
33 private static final Log LOG = LogFactory.getLog(PanelScreenHandler.class);
34
35 private JPanel panel = new JPanel();
36
37 private IScreen installedScreen;
38
39 public PanelScreenHandler() {
40 this.panel = new JPanel(new BorderLayout());
41 }
42
43
44
45
46
47 @SuppressWarnings("unchecked")
48 public void displayScreen(ActionInvocation actionInvocation) {
49 try {
50
51 ScreenResult res = (ScreenResult) actionInvocation.getResult();
52
53
54
55
56
57 IScreen screen = (IScreen) actionInvocation.getInvocationContext().getContainer().getInstance(ObjectFactory.class).buildBean(
58 res.getLocation(), null
59 );
60
61
62
63
64 if(screen instanceof IValueStackAware) {
65 ((IValueStackAware) screen).setValueStack(actionInvocation.getStack());
66 }
67
68 screen.buildGUI();
69
70 screen.initGUI();
71
72
73
74
75
76 if(actionInvocation.getAction() instanceof ValidationAware) {
77 ValidationAware va = (ValidationAware) actionInvocation.getAction();
78 if(va.hasErrors() || va.hasActionMessages()) {
79
80 Collection<Message> messages = new ArrayList<Message>();
81 if(va.hasActionErrors()) {
82 for(Object error : va.getActionErrors()) {
83 Message message = new Message(error.toString(),Severity.ERROR);
84 messages.add(message);
85 }
86 }
87
88 if(va.hasActionMessages()) {
89 for(Object msg : va.getActionMessages()) {
90 Message message = new Message(msg.toString(),Severity.INFO);
91 messages.add(message);
92 }
93 }
94
95 screen.displayMessages(messages);
96 }
97 }
98
99 panel.removeAll();
100
101
102 if(screen.getComponent() == null) {
103
104 throw new IllegalStateException(
105 "Component null for screen "+screen.getClass().getName()+
106 ". Did you override the IScreen.getComponent method correctly?"
107 );
108 }
109
110 panel.add(screen.getComponent(),BorderLayout.CENTER);
111 panel.validate();
112
113 this.installedScreen = screen;
114
115
116 } catch (Exception e) {
117 LOG.error("Error while handling view",e);
118
119 throw new RuntimeException(e);
120 }
121 }
122
123
124
125
126
127 public JComponent getComponent() {
128 return panel;
129 }
130
131
132
133
134
135 public void displayMessages(Collection<Message> messages) {
136 if(installedScreen != null) {
137 installedScreen.displayMessages(messages);
138 }
139 }
140
141 public IScreen getInstalledScreen() {
142 return installedScreen;
143 }
144
145 }