1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.ui.vaadin.dialog;
35
36 import info.magnolia.ui.api.view.View;
37 import info.magnolia.ui.vaadin.editorlike.DialogActionListener;
38
39 import com.vaadin.event.ShortcutAction.KeyCode;
40 import com.vaadin.ui.Component;
41 import com.vaadin.ui.Label;
42
43
44
45
46 public class ConfirmationDialog extends LightDialog {
47
48 public static final String CONFIRM_ACTION_NAME = "confirm";
49
50 private String message;
51
52 public ConfirmationDialog(final String message, boolean cancelIsDefault) {
53 setMessage(message);
54 init(cancelIsDefault);
55 }
56
57 public ConfirmationDialog(final View contents, boolean cancelIsDefault) {
58 message = "";
59 setContent(contents.asVaadinComponent());
60 init(cancelIsDefault);
61 }
62
63 public void init(boolean cancelIsDefault) {
64
65 if (cancelIsDefault) {
66 this.setDefaultAction(CANCEL_ACTION_NAME);
67 } else {
68 this.setDefaultAction(CONFIRM_ACTION_NAME);
69 }
70
71 addAction(CONFIRM_ACTION_NAME, "OK", new DialogActionListener() {
72 @Override
73 public void onActionExecuted(String actionName) {
74 fireEvent(new ConfirmationEvent(ConfirmationDialog.this, true));
75 }
76 });
77
78 addShortcut(CONFIRM_ACTION_NAME, KeyCode.ENTER);
79
80 addAction(CANCEL_ACTION_NAME, "Cancel", new DialogActionListener() {
81 @Override
82 public void onActionExecuted(String actionName) {
83 fireEvent(new ConfirmationEvent(ConfirmationDialog.this, false));
84 }
85 });
86 }
87
88
89 public void setConfirmActionLabel(final String label) {
90 addAction(CONFIRM_ACTION_NAME, label);
91 }
92
93 public void setRejectActionLabel(final String label) {
94 addAction(CANCEL_ACTION_NAME, label);
95 }
96
97 public void setMessage(String message) {
98 this.message = message;
99 if (getContent() != null && getContent() instanceof Label) {
100 ((Label) getContent()).setValue(message);
101 }
102 }
103
104 public String getMessage() {
105 return message;
106 }
107
108 @Override
109 public void setContent(Component content) {
110 super.setContent(content);
111 }
112
113 @Override
114 protected Component createDefaultContent() {
115 return new Label();
116 }
117
118 public void addConfirmationHandler(ConfirmationEvent.Handler handler) {
119 addListener("confirmation_event", ConfirmationEvent.class, handler, ConfirmationEvent.ON_CONFIRMATION);
120 }
121
122 public void removeConfirmationHandler(ConfirmationEvent.Handler handler) {
123 removeListener("confirmation_event", ConfirmationEvent.class, handler);
124 }
125
126
127
128
129 public static class ConfirmationEvent extends Component.Event {
130
131
132
133
134 public interface Handler {
135 void onConfirmation(ConfirmationEvent event);
136 }
137
138 public static final java.lang.reflect.Method ON_CONFIRMATION;
139
140 static {
141 try {
142 ON_CONFIRMATION = ConfirmationEvent.Handler.class.getDeclaredMethod("onConfirmation", new Class[]{ConfirmationEvent.class});
143 } catch (final java.lang.NoSuchMethodException e) {
144 throw new java.lang.RuntimeException(e);
145 }
146 }
147
148 private final boolean isConfirmed;
149
150 public ConfirmationEvent(Component source, boolean isConfirmed) {
151 super(source);
152 this.isConfirmed = isConfirmed;
153 }
154
155 public boolean isConfirmed() {
156 return isConfirmed;
157 }
158 }
159 }