View Javadoc

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  	 * (non-Javadoc)
45  	 * @see siouxsie.mvc.IScreenHandler#displayScreen(com.opensymphony.xwork2.ActionInvocation)
46  	 */
47  	@SuppressWarnings("unchecked")
48  	public void displayScreen(ActionInvocation actionInvocation) {
49  		try {
50  			// oops
51  			ScreenResult res = (ScreenResult) actionInvocation.getResult();
52  			
53  			// create the screen
54  			//Class<? extends IScreen> screenClass = (Class<? extends IScreen>) Class.forName(res.getLocation());
55  			
56  			// delegate the creation to XW controller
57  			IScreen screen = (IScreen) actionInvocation.getInvocationContext().getContainer().getInstance(ObjectFactory.class).buildBean(
58  					res.getLocation(), null
59  			);
60  			
61  			//IScreen screen = screenClass.newInstance();
62  			
63  			// set the value stack
64  			if(screen instanceof IValueStackAware) {
65  				((IValueStackAware) screen).setValueStack(actionInvocation.getStack());
66  			}
67  			
68  			screen.buildGUI();
69  			
70  			screen.initGUI();
71  			
72  			// inject the screen (at least the action trigger)
73  			//actionInvocation.getInvocationContext().getContainer().inject(screen);
74  		
75  			// once all the UI is created and initialized, handle info and error messages from action
76  			if(actionInvocation.getAction() instanceof ValidationAware) {
77  				ValidationAware va = (ValidationAware) actionInvocation.getAction();
78  				if(va.hasErrors() || va.hasActionMessages()) {
79  					// the errors
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  					// just messages
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  					// display the messages
95  					screen.displayMessages(messages);
96  				}				
97  			}
98  			
99  			panel.removeAll();
100 			
101 			// checks if the screen component is not null			
102 			if(screen.getComponent() == null) {
103 				// null screen component, we'll get a exception anyway
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 			// TODO: avoid launching runtime exception
119 			throw new RuntimeException(e);
120 		}		
121 	}
122 	
123 	/*
124 	 * (non-Javadoc)
125 	 * @see siouxsie.mvc.IScreenHandler#getComponent()
126 	 */
127 	public JComponent getComponent() {
128 		return panel;
129 	}
130 
131 	/*
132 	 * (non-Javadoc)
133 	 * @see siouxsie.mvc.IScreenHandler#displayMessages(java.util.Collection)
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 }