1 | package eu.clarin.cmdi.virtualcollectionregistry.gui.dialog; |
---|
2 | |
---|
3 | import org.apache.wicket.ajax.AjaxRequestTarget; |
---|
4 | import org.apache.wicket.ajax.markup.html.form.AjaxButton; |
---|
5 | import org.apache.wicket.behavior.AttributeAppender; |
---|
6 | import org.apache.wicket.markup.html.form.Form; |
---|
7 | import org.apache.wicket.markup.html.panel.FeedbackPanel; |
---|
8 | import org.apache.wicket.markup.html.panel.Panel; |
---|
9 | import org.apache.wicket.model.AbstractReadOnlyModel; |
---|
10 | import org.apache.wicket.model.CompoundPropertyModel; |
---|
11 | import org.apache.wicket.model.IModel; |
---|
12 | import org.apache.wicket.model.Model; |
---|
13 | |
---|
14 | @SuppressWarnings("serial") |
---|
15 | public abstract class ModalEditDialogBase<T> extends ModalDialogBase { |
---|
16 | public abstract class ContentPanel extends Panel { |
---|
17 | protected ContentPanel(String id) { |
---|
18 | super(id); |
---|
19 | } |
---|
20 | |
---|
21 | public abstract Form<T> getForm(); |
---|
22 | |
---|
23 | public abstract FeedbackPanel getFeedbackPanel(); |
---|
24 | } // class ModalEditDialogBase.ContentPanel |
---|
25 | |
---|
26 | private final class ButtonBar extends Panel { |
---|
27 | public ButtonBar(String id, Form<?> form) { |
---|
28 | super(id); |
---|
29 | addButton = new AjaxButton("addButton", |
---|
30 | new Model<String>("Add"), form) { |
---|
31 | @Override |
---|
32 | protected void onError(AjaxRequestTarget target, Form<?> form) { |
---|
33 | target.addComponent(contentPanel.getFeedbackPanel()); |
---|
34 | super.onError(target, form); |
---|
35 | } |
---|
36 | |
---|
37 | @SuppressWarnings("unchecked") |
---|
38 | @Override |
---|
39 | protected void onSubmit(AjaxRequestTarget target, Form<?> form) { |
---|
40 | ModalEditDialogBase.this.doSubmit(target, (Form<T>) form); |
---|
41 | } |
---|
42 | }; |
---|
43 | add(addButton); |
---|
44 | modifyButton = new AjaxButton("modifyButton", |
---|
45 | new Model<String>("Modify"), form) { |
---|
46 | @Override |
---|
47 | protected void onError(AjaxRequestTarget target, Form<?> form) { |
---|
48 | target.addComponent(contentPanel.getFeedbackPanel()); |
---|
49 | super.onError(target, form); |
---|
50 | } |
---|
51 | |
---|
52 | @SuppressWarnings("unchecked") |
---|
53 | @Override |
---|
54 | protected void onSubmit(AjaxRequestTarget target, Form<?> form) { |
---|
55 | ModalEditDialogBase.this.doSubmit(target, (Form<T>) form); |
---|
56 | } |
---|
57 | }; |
---|
58 | add(modifyButton); |
---|
59 | final AjaxButton cancelButton = |
---|
60 | new AjaxButton("cancelButton", |
---|
61 | new Model<String>("Cancel"), form) { |
---|
62 | @SuppressWarnings("unchecked") |
---|
63 | @Override |
---|
64 | protected void onSubmit(AjaxRequestTarget target, Form<?> form) { |
---|
65 | ModalEditDialogBase.this.doCancel(target, (Form<T>) form); |
---|
66 | } |
---|
67 | }; |
---|
68 | cancelButton.setDefaultFormProcessing(false); |
---|
69 | add(cancelButton); |
---|
70 | } |
---|
71 | } // class EditCreatorDialog.ButtonBar |
---|
72 | |
---|
73 | private ContentPanel contentPanel; |
---|
74 | private AjaxButton addButton; |
---|
75 | private AjaxButton modifyButton; |
---|
76 | |
---|
77 | protected ModalEditDialogBase(String id, IModel<String> title) { |
---|
78 | super(id, title); |
---|
79 | } |
---|
80 | |
---|
81 | @Override |
---|
82 | protected final Panel createContent(String id) { |
---|
83 | final IModel<T> model = newInstanceModel(); |
---|
84 | contentPanel = createContentPanel(id, model); |
---|
85 | contentPanel.add(new AttributeAppender("class", |
---|
86 | new AbstractReadOnlyModel<String>() { |
---|
87 | @Override |
---|
88 | public String getObject() { |
---|
89 | final String clazz = getCssClass(); |
---|
90 | if ((clazz != null) && !clazz.isEmpty()) { |
---|
91 | StringBuilder sb = new StringBuilder("editDialog"); |
---|
92 | sb.append(' '); |
---|
93 | sb.append(clazz); |
---|
94 | return sb.toString(); |
---|
95 | } else { |
---|
96 | return "editDialog"; |
---|
97 | } |
---|
98 | } |
---|
99 | }, " ")); |
---|
100 | contentPanel.getForm().removePersistentFormComponentValues(true); |
---|
101 | contentPanel.getFeedbackPanel().setOutputMarkupId(true); |
---|
102 | return contentPanel; |
---|
103 | } |
---|
104 | |
---|
105 | @Override |
---|
106 | protected final Panel createButtonBar(String id) { |
---|
107 | return new ButtonBar(id, contentPanel.getForm()); |
---|
108 | } |
---|
109 | |
---|
110 | @Override |
---|
111 | public final void show(AjaxRequestTarget target) { |
---|
112 | this.show(target, null); |
---|
113 | } |
---|
114 | |
---|
115 | public final void show(AjaxRequestTarget target, IModel<T> model) { |
---|
116 | if (model == null) { |
---|
117 | model = newInstanceModel(); |
---|
118 | addButton.setVisible(true); |
---|
119 | modifyButton.setVisible(false); |
---|
120 | } else { |
---|
121 | addButton.setVisible(false); |
---|
122 | modifyButton.setVisible(true); |
---|
123 | } |
---|
124 | contentPanel.getForm().setModel(new CompoundPropertyModel<T>(model)); |
---|
125 | target.appendJavascript("activeTooltips()"); |
---|
126 | super.show(target); |
---|
127 | } |
---|
128 | |
---|
129 | private final void doSubmit(AjaxRequestTarget target, Form<T> form) { |
---|
130 | close(target); |
---|
131 | final T object = form.getModelObject(); |
---|
132 | onSubmit(target, object); |
---|
133 | } |
---|
134 | |
---|
135 | private final void doCancel(AjaxRequestTarget target, Form<T> form) { |
---|
136 | close(target); |
---|
137 | final T object = form.getModelObject(); |
---|
138 | onCancel(target, object); |
---|
139 | } |
---|
140 | |
---|
141 | protected String getCssClass() { |
---|
142 | return null; |
---|
143 | } |
---|
144 | |
---|
145 | protected abstract IModel<T> newInstanceModel(); |
---|
146 | |
---|
147 | protected abstract IModel<T> createEmptyModel(); |
---|
148 | |
---|
149 | protected abstract ContentPanel createContentPanel(String id, |
---|
150 | IModel<T> model); |
---|
151 | |
---|
152 | public abstract void onSubmit(AjaxRequestTarget target, T object); |
---|
153 | |
---|
154 | public void onCancel(AjaxRequestTarget target, T object) { |
---|
155 | } |
---|
156 | |
---|
157 | } // class ModalEditDialog |
---|