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
19
20
21
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
49
50
51
52
53
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 }