View Javadoc

1   package org.starobjects.wicket.ui.pages;
2   
3   import java.util.Arrays;
4   import java.util.Collections;
5   import java.util.List;
6   
7   import org.apache.wicket.PageParameters;
8   import org.apache.wicket.markup.MarkupStream;
9   import org.apache.wicket.markup.html.WebPage;
10  import org.apache.wicket.markup.html.internal.HtmlHeaderContainer;
11  import org.apache.wicket.model.IModel;
12  import org.nakedobjects.metamodel.adapter.oid.stringable.OidStringifier;
13  import org.nakedobjects.metamodel.services.ServicesInjector;
14  import org.nakedobjects.metamodel.specloader.SpecificationLoader;
15  import org.nakedobjects.runtime.context.NakedObjectsContext;
16  import org.nakedobjects.runtime.persistence.PersistenceSession;
17  import org.starobjects.wicket.model.models.ApplicationActionsModel;
18  import org.starobjects.wicket.ui.ComponentFactory;
19  import org.starobjects.wicket.ui.ComponentType;
20  import org.starobjects.wicket.ui.app.cssrenderer.ApplicationCssRenderer;
21  import org.starobjects.wicket.ui.app.registry.ComponentFactoryRegistry;
22  import org.starobjects.wicket.ui.app.registry.ComponentFactoryRegistryAccessor;
23  
24  /**
25   * Convenience adapter for {@link WebPage}s built up using {@link ComponentType}s.
26   */
27  public abstract class PageAbstract extends WebPage {
28  
29  	public static final String ID_MENU_LINK = "menuLink";
30  	
31  	private List<ComponentType> childComponentIds;
32  	private PageParameters pageParameters;
33  
34  	public PageAbstract(PageParameters pageParameters, final ComponentType... childComponentIds) {
35  		addApplicationActionsComponent();
36  		this.childComponentIds = Collections.unmodifiableList(Arrays.asList(childComponentIds));
37  		this.pageParameters = pageParameters;
38  	}
39  	
40  	
41  	/**
42  	 * As provided in the {@link #PageAbstract(ComponentType) constructor}.
43  	 * 
44  	 * <p>
45  	 * This superclass doesn't do anything with this property directly, but requiring it to
46  	 * be provided enforces standardization of the implementation of the subclasses.
47  	 */
48  	public List<ComponentType> getChildModelTypes() {
49  		return childComponentIds;
50  	}
51  	
52  
53  	@Override
54  	public PageParameters getPageParameters() {
55  		return pageParameters;
56  	}
57  	
58  
59  	private void addApplicationActionsComponent() {
60  		ApplicationActionsModel model = new ApplicationActionsModel();
61  		addComponent(ComponentType.APPLICATION_ACTIONS, model);
62  	}
63  
64  	/**
65  	 * For subclasses to call.
66  	 * 
67  	 * <p>
68  	 * Should be called in the subclass' constructor.
69  	 * 
70  	 * @param model - used to find the best matching {@link ComponentFactory} to render the model.
71  	 */
72  	protected void addChildComponents(IModel<?> model) {
73  		for (ComponentType componentType : getChildModelTypes()) {
74  			addComponent(componentType, model);
75  		}
76  	}
77  
78  	private void addComponent(ComponentType componentType, IModel<?> model) {
79  		getComponentFactoryRegistry().addOrReplaceComponent(this, componentType, model);
80  	}
81  
82  	
83  	@Override
84  	protected void onRender(MarkupStream markupStream) {
85  		super.onRender(markupStream);
86  	}
87  	
88  	/**
89  	 * Renders the application-supplied CSS, if any.
90  	 */
91  	@Override
92  	public void renderHead(HtmlHeaderContainer container) {
93  		super.renderHead(container);
94  		final ApplicationCssRenderer applicationCssRenderer = getApplicationCssRenderer();
95          applicationCssRenderer.renderApplicationCss(container);
96  	}
97  
98  
99  	/////////////////////////////////////////////////////////////////////
100 	// Convenience
101 	/////////////////////////////////////////////////////////////////////
102 
103 	protected ComponentFactoryRegistry getComponentFactoryRegistry() {
104 		final ComponentFactoryRegistryAccessor cfra = (ComponentFactoryRegistryAccessor) getApplication();
105         return cfra.getComponentFactoryRegistry();
106 	}
107 
108     protected ApplicationCssRenderer getApplicationCssRenderer() {
109         return (ApplicationCssRenderer) getApplication();
110     }
111     
112 
113 	/////////////////////////////////////////////////////
114 	// System components
115 	/////////////////////////////////////////////////////
116 
117 	protected ServicesInjector getServicesInjector() {
118 		return getPersistenceSession().getServicesInjector();
119 	}
120 	
121 	protected PersistenceSession getPersistenceSession() {
122 		return NakedObjectsContext.getPersistenceSession();
123 	}
124 
125 	protected OidStringifier getOidStringifier() {
126 		return getPersistenceSession().getOidGenerator().getOidStringifier();
127 	}
128 	
129 	protected SpecificationLoader getSpecificationLoader() {
130 		return NakedObjectsContext.getSpecificationLoader();
131 	}
132 }