View Javadoc

1   /**
2    * 
3    */
4   package org.starobjects.wicket.ui.selector;
5   
6   import java.util.List;
7   
8   import org.apache.wicket.Component;
9   import org.apache.wicket.MarkupContainer;
10  import org.apache.wicket.markup.html.form.DropDownChoice;
11  import org.apache.wicket.markup.html.form.IChoiceRenderer;
12  import org.apache.wicket.model.IModel;
13  import org.apache.wicket.model.Model;
14  import org.starobjects.wicket.ui.ComponentFactory;
15  import org.starobjects.wicket.ui.util.Components;
16  
17  /**
18   * {@link ComponentFactory} for rendering a selection of {@link ComponentFactory}s.
19   * 
20   * <p>
21   * Used by {@link SelectorPanelAbstract}.
22   */
23  public class DropDownChoiceComponentFactory extends
24  		DropDownChoice<ComponentFactory> {
25  
26  	private static final long serialVersionUID = 1L;
27  	
28  	private final IModel<?> underlyingModel;
29  	private final String underlyingId;
30  	private MarkupContainer container;
31  
32  	private static final class ComponentFactoryChoiceRenderer implements
33  	IChoiceRenderer<ComponentFactory> {
34  		private static final long serialVersionUID = 1L;
35  	
36  		@Override
37  		public Object getDisplayValue(ComponentFactory object) {
38  			return object.getName();
39  		}
40  	
41  		@Override
42  		public String getIdValue(ComponentFactory object, int index) {
43  			return Integer.toString(index);
44  		}
45  	}
46  
47  	/**
48  	 * @param id - id to use for the drop down
49  	 * @param selectedComponentFactoryModel - currently selected in the drop-down
50  	 * @param componentFactories - list of {@link ComponentFactory}s to show in drop-down
51  	 * @param container - the container that should contain the {@link Component} created by the selected {@link ComponentFactory} 
52  	 * @param underlyingId - the id of the {@link Component} created
53  	 * @param underlyingModel - the model for the {@link Component}, ie as passed to {@link Components#findComponentFactories(org.starobjects.wicket.viewer.components.ComponentType, IModel)}
54  	 */
55  	public DropDownChoiceComponentFactory(String id,
56  			Model<ComponentFactory> selectedComponentFactoryModel,
57  			List<? extends ComponentFactory> componentFactories,
58  			MarkupContainer container, 
59  			String underlyingId, 
60  			IModel<?> underlyingModel) {
61  		super(id, selectedComponentFactoryModel, componentFactories, new ComponentFactoryChoiceRenderer());
62  		this.underlyingId = underlyingId;
63  		this.underlyingModel = underlyingModel;
64  		this.container = container;
65  	}
66  
67  	@Override
68  	protected boolean wantOnSelectionChangedNotifications() {
69  		return true;
70  	}
71  
72  	@Override
73  	protected void onSelectionChanged(ComponentFactory newSelection) {
74  		ComponentFactory componentFactory = getModel().getObject();
75  		if (componentFactory != null)
76  			container.addOrReplace(componentFactory
77  					.createComponent(underlyingId, underlyingModel));
78  	}
79  }