1 | package eu.clarin.cmdi.virtualcollectionregistry.gui.dialog; |
---|
2 | |
---|
3 | import org.apache.wicket.Component; |
---|
4 | import org.apache.wicket.ajax.AjaxRequestTarget; |
---|
5 | import org.apache.wicket.ajax.markup.html.form.AjaxButton; |
---|
6 | import org.apache.wicket.behavior.AttributeAppender; |
---|
7 | import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow; |
---|
8 | import org.apache.wicket.markup.html.basic.MultiLineLabel; |
---|
9 | import org.apache.wicket.markup.html.form.Form; |
---|
10 | import org.apache.wicket.markup.html.panel.Panel; |
---|
11 | import org.apache.wicket.model.IModel; |
---|
12 | import org.apache.wicket.model.Model; |
---|
13 | |
---|
14 | @SuppressWarnings("serial") |
---|
15 | public abstract class ConfirmationDialog extends ModalDialogBase { |
---|
16 | private final class ButtonBar extends Panel { |
---|
17 | public ButtonBar(String id) { |
---|
18 | super(id); |
---|
19 | final Form<Void> form = new Form<Void>("buttonsForm"); |
---|
20 | final AjaxButton yesButton = |
---|
21 | new AjaxButton("yesButton", new Model<String>("Yes"), form) { |
---|
22 | @Override |
---|
23 | protected void onSubmit(AjaxRequestTarget target, |
---|
24 | Form<?> form) { |
---|
25 | answer = true; |
---|
26 | ConfirmationDialog.this.close(target); |
---|
27 | } |
---|
28 | }; |
---|
29 | yesButton.setDefaultFormProcessing(false); |
---|
30 | form.add(yesButton); |
---|
31 | final AjaxButton noButton = |
---|
32 | new AjaxButton("noButton", new Model<String>("No"), form) { |
---|
33 | @Override |
---|
34 | protected void onSubmit(AjaxRequestTarget target, |
---|
35 | Form<?> form) { |
---|
36 | answer = false; |
---|
37 | ConfirmationDialog.this.close(target); |
---|
38 | } |
---|
39 | }; |
---|
40 | noButton.setDefaultFormProcessing(false); |
---|
41 | form.add(noButton); |
---|
42 | add(form); |
---|
43 | } |
---|
44 | } // class ConfirmationDialog.ButtonBar |
---|
45 | |
---|
46 | private final class Content extends Panel { |
---|
47 | private Content(String id) { |
---|
48 | super(id); |
---|
49 | messageLabel = new MultiLineLabel("message"); |
---|
50 | add(messageLabel); |
---|
51 | } |
---|
52 | } // class ConfirmationDialog.Content |
---|
53 | |
---|
54 | private boolean answer; |
---|
55 | private MultiLineLabel messageLabel; |
---|
56 | |
---|
57 | public ConfirmationDialog(String id, IModel<String> message, |
---|
58 | final Component updateComponent) { |
---|
59 | super(id, new Model<String>("Please confirm")); |
---|
60 | setWindowClosedCallback(new ModalWindow.WindowClosedCallback() { |
---|
61 | @Override |
---|
62 | public void onClose(AjaxRequestTarget target) { |
---|
63 | if (answer) { |
---|
64 | onConfirm(target); |
---|
65 | if (updateComponent != null) { |
---|
66 | target.addComponent(updateComponent); |
---|
67 | } |
---|
68 | } else { |
---|
69 | onCancel(target); |
---|
70 | } |
---|
71 | } |
---|
72 | }); |
---|
73 | if (message != null) { |
---|
74 | setMessage(message); |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | public ConfirmationDialog(String id, final Component updateComponent) { |
---|
79 | this(id, null, updateComponent); |
---|
80 | } |
---|
81 | |
---|
82 | public ConfirmationDialog(String id) { |
---|
83 | this(id, null, null); |
---|
84 | } |
---|
85 | |
---|
86 | @Override |
---|
87 | protected Panel createButtonBar(String id) { |
---|
88 | return new ButtonBar(id); |
---|
89 | } |
---|
90 | |
---|
91 | @Override |
---|
92 | protected Panel createContent(String id) { |
---|
93 | Content content = new Content(id); |
---|
94 | content.add(new AttributeAppender("class", getCssClass(), " ")); |
---|
95 | return content; |
---|
96 | } |
---|
97 | |
---|
98 | protected Model<String> getCssClass() { |
---|
99 | return Model.of("confirmationDialog"); |
---|
100 | } |
---|
101 | |
---|
102 | @Override |
---|
103 | public void show(AjaxRequestTarget target) { |
---|
104 | answer = false; /* set save default value */ |
---|
105 | super.show(target); |
---|
106 | } |
---|
107 | |
---|
108 | public void show(AjaxRequestTarget target, IModel<String> message) { |
---|
109 | setMessage(message); |
---|
110 | this.show(target); |
---|
111 | } |
---|
112 | |
---|
113 | public void setMessage(IModel<String> message) { |
---|
114 | if (message == null) { |
---|
115 | throw new NullPointerException("message == null"); |
---|
116 | } |
---|
117 | messageLabel.setDefaultModel(message); |
---|
118 | } |
---|
119 | |
---|
120 | public abstract void onConfirm(AjaxRequestTarget target); |
---|
121 | |
---|
122 | public void onCancel(AjaxRequestTarget target) { |
---|
123 | } |
---|
124 | |
---|
125 | } // abstract class ConfirmationDialog |
---|