1 | package eu.clarin.cmdi.virtualcollectionregistry.gui.wizard; |
---|
2 | |
---|
3 | import java.io.Serializable; |
---|
4 | import java.util.Arrays; |
---|
5 | |
---|
6 | import org.apache.wicket.ajax.AjaxRequestTarget; |
---|
7 | import org.apache.wicket.ajax.markup.html.form.AjaxButton; |
---|
8 | import org.apache.wicket.behavior.AttributeAppender; |
---|
9 | import org.apache.wicket.markup.html.form.AbstractTextComponent; |
---|
10 | import org.apache.wicket.markup.html.form.DropDownChoice; |
---|
11 | import org.apache.wicket.markup.html.form.EnumChoiceRenderer; |
---|
12 | import org.apache.wicket.markup.html.form.Form; |
---|
13 | import org.apache.wicket.markup.html.panel.FeedbackPanel; |
---|
14 | import org.apache.wicket.markup.html.panel.Panel; |
---|
15 | import org.apache.wicket.model.CompoundPropertyModel; |
---|
16 | import org.apache.wicket.model.Model; |
---|
17 | import org.apache.wicket.validation.IValidatable; |
---|
18 | import org.apache.wicket.validation.Validatable; |
---|
19 | import org.apache.wicket.validation.ValidationError; |
---|
20 | import org.apache.wicket.validation.validator.AbstractValidator; |
---|
21 | |
---|
22 | import eu.clarin.cmdi.virtualcollectionregistry.gui.dialog.ModalDialogBase; |
---|
23 | import eu.clarin.cmdi.virtualcollectionregistry.model.Resource; |
---|
24 | import eu.clarin.cmdi.virtualcollectionregistry.service.impl.ReferenceValidator; |
---|
25 | |
---|
26 | @SuppressWarnings("serial") |
---|
27 | public abstract class AddResourcesDialog extends ModalDialogBase { |
---|
28 | private static final class Data implements Serializable { |
---|
29 | private Resource.Type type; |
---|
30 | private String[] references; |
---|
31 | |
---|
32 | public Resource[] getResources() { |
---|
33 | Resource[] value = null; |
---|
34 | if ((type != null) && (references != null) && |
---|
35 | (references.length > 0)) { |
---|
36 | value = new Resource[references.length]; |
---|
37 | int i = 0; |
---|
38 | for (String ref : references) { |
---|
39 | value[i++] = new Resource(type, ref); |
---|
40 | } |
---|
41 | } |
---|
42 | return value; |
---|
43 | } |
---|
44 | } // class AddResourcesDialog.Data |
---|
45 | |
---|
46 | private static final class Content extends Panel { |
---|
47 | private final Form<Data> form; |
---|
48 | private final FeedbackPanel feedbackPanel; |
---|
49 | |
---|
50 | public Content(String id) { |
---|
51 | super(id); |
---|
52 | add(new AttributeAppender("class", |
---|
53 | new Model<String>("editDialog addResourcesDialog"), " ")); |
---|
54 | form = new Form<Data>("addResourcesForm", |
---|
55 | new CompoundPropertyModel<Data>(null)); |
---|
56 | final DropDownChoice<Resource.Type> typeChoice = |
---|
57 | new DropDownChoice<Resource.Type>("type", |
---|
58 | Arrays.asList(Resource.Type.values()), |
---|
59 | new EnumChoiceRenderer<Resource.Type>(this)); |
---|
60 | typeChoice.setRequired(true); |
---|
61 | form.add(typeChoice); |
---|
62 | final AbstractTextComponent<String[]> referencesArea = |
---|
63 | new AbstractTextComponent<String[]>("references") { |
---|
64 | @Override |
---|
65 | protected void convertInput() { |
---|
66 | final String input = getRawInput(); |
---|
67 | if (input != null) { |
---|
68 | final String[] refs = input.split("[,;\\s]+"); |
---|
69 | setConvertedInput(refs); |
---|
70 | } else { |
---|
71 | setConvertedInput(null); |
---|
72 | } |
---|
73 | } |
---|
74 | }; |
---|
75 | referencesArea.setRequired(true); |
---|
76 | referencesArea.add(new AbstractValidator<String[]>() { |
---|
77 | @Override |
---|
78 | protected void onValidate(IValidatable<String[]> input) { |
---|
79 | String[] refs = input.getValue(); |
---|
80 | if (refs != null) { |
---|
81 | ReferenceValidator v = new ReferenceValidator(); |
---|
82 | for (String ref : refs) { |
---|
83 | if (ref.length() > 255) { |
---|
84 | ValidationError ve = new ValidationError(); |
---|
85 | ve.setMessage("'" + ref + |
---|
86 | "' is larger than 255 characters"); |
---|
87 | input.error(ve); |
---|
88 | continue; |
---|
89 | } |
---|
90 | Validatable<String> w = |
---|
91 | new Validatable<String>(ref); |
---|
92 | v.validate(w); |
---|
93 | if (!w.isValid()) { |
---|
94 | ValidationError ve = new ValidationError(); |
---|
95 | ve.setMessage("'" + ref + |
---|
96 | "' is not valid uri"); |
---|
97 | input.error(ve); |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | }); |
---|
103 | form.add(referencesArea); |
---|
104 | feedbackPanel = new FeedbackPanel("feedback"); |
---|
105 | feedbackPanel.setOutputMarkupId(true); |
---|
106 | form.add(feedbackPanel); |
---|
107 | add(form); |
---|
108 | } |
---|
109 | |
---|
110 | public Form<Data> getForm() { |
---|
111 | return form; |
---|
112 | } |
---|
113 | |
---|
114 | public FeedbackPanel getFeedbackPanel() { |
---|
115 | return feedbackPanel; |
---|
116 | } |
---|
117 | } // class AddResourcesDialog.Content |
---|
118 | |
---|
119 | private final class ButtonBar extends Panel { |
---|
120 | public ButtonBar(String id, Form<?> form) { |
---|
121 | super(id); |
---|
122 | final AjaxButton addButton = new AjaxButton("addButton", |
---|
123 | new Model<String>("Add"), form) { |
---|
124 | @Override |
---|
125 | protected void onError(AjaxRequestTarget target, Form<?> form) { |
---|
126 | target.addComponent(contentPanel.getFeedbackPanel()); |
---|
127 | super.onError(target, form); |
---|
128 | } |
---|
129 | |
---|
130 | @Override |
---|
131 | protected void onSubmit(AjaxRequestTarget target, Form<?> form) { |
---|
132 | AddResourcesDialog.this.close(target); |
---|
133 | final Data data = (Data) form.getModelObject(); |
---|
134 | final Resource[] resources = data.getResources(); |
---|
135 | if (resources != null) { |
---|
136 | AddResourcesDialog.this.onSubmit(target, resources); |
---|
137 | } |
---|
138 | form.setModelObject(null); |
---|
139 | } |
---|
140 | }; |
---|
141 | add(addButton); |
---|
142 | final AjaxButton cancelButton = |
---|
143 | new AjaxButton("cancelButton", |
---|
144 | new Model<String>("Cancel"), form) { |
---|
145 | @Override |
---|
146 | protected void onSubmit(AjaxRequestTarget target, Form<?> form) { |
---|
147 | AddResourcesDialog.this.close(target); |
---|
148 | form.setModelObject(null); |
---|
149 | } |
---|
150 | }; |
---|
151 | cancelButton.setDefaultFormProcessing(false); |
---|
152 | add(cancelButton); |
---|
153 | } |
---|
154 | } // class EditCreatorDialog.ButtonBar |
---|
155 | |
---|
156 | private Content contentPanel; |
---|
157 | |
---|
158 | public AddResourcesDialog(final String id) { |
---|
159 | super(id, new Model<String>("Add Multiple Resources")); |
---|
160 | setOutputMarkupId(true); |
---|
161 | setInitialWidth(600); |
---|
162 | } |
---|
163 | |
---|
164 | @Override |
---|
165 | protected Panel createContent(String id) { |
---|
166 | contentPanel = new Content(id); |
---|
167 | contentPanel.getForm().removePersistentFormComponentValues(true); |
---|
168 | return contentPanel; |
---|
169 | } |
---|
170 | |
---|
171 | @Override |
---|
172 | protected Panel createButtonBar(String id) { |
---|
173 | return new ButtonBar(id, contentPanel.getForm()); |
---|
174 | } |
---|
175 | |
---|
176 | @Override |
---|
177 | public void show(AjaxRequestTarget target) { |
---|
178 | contentPanel.getForm().setModelObject(new Data()); |
---|
179 | super.show(target); |
---|
180 | } |
---|
181 | |
---|
182 | public abstract void onSubmit(AjaxRequestTarget target, |
---|
183 | Resource[] resources); |
---|
184 | |
---|
185 | } // AddResourcesDialog |
---|