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 com.vaadin.ui.Alignment;
37 import com.vaadin.ui.Button;
38 import com.vaadin.ui.Button.ClickEvent;
39 import com.vaadin.ui.Button.ClickListener;
40 import com.vaadin.ui.Component;
41 import com.vaadin.ui.HorizontalLayout;
42 import com.vaadin.ui.Label;
43
44
45
46
47 public class ConfirmationDialog extends LightDialog {
48
49 public static final String CONFIRM_ACTION_NAME = "confirm";
50
51 private String message;
52
53 private Button confirmButton;
54
55 private Button cancelButton;
56
57 public ConfirmationDialog(final String message, String confirmLabel, String cancelLabel, boolean cancelIsDefault) {
58 setMessage(message);
59 init(confirmLabel, cancelLabel, cancelIsDefault);
60 }
61
62 public ConfirmationDialog(final Component contents, String confirmLabel, String cancelLabel, boolean cancelIsDefault) {
63 message = "";
64 setContent(contents);
65 init(confirmLabel, cancelLabel, cancelIsDefault);
66 }
67
68 public void init(String confirmLabel, String cancelLabel, boolean cancelIsDefault) {
69 HorizontalLayout footer = new HorizontalLayout();
70
71 confirmButton = new Button(confirmLabel, new ClickListener() {
72 @Override
73 public void buttonClick(ClickEvent event) {
74 fireEvent(new ConfirmationEvent(ConfirmationDialog.this, true));
75 }
76 });
77
78 cancelButton = new Button(cancelLabel, new ClickListener() {
79 @Override
80 public void buttonClick(ClickEvent event) {
81 fireEvent(new ConfirmationEvent(ConfirmationDialog.this, false));
82 }
83 });
84
85 footer.addComponent(confirmButton);
86 footer.addComponent(cancelButton);
87
88 cancelButton.addStyleName("btn-dialog");
89 cancelButton.addStyleName("cancel");
90 confirmButton.addStyleName("btn-dialog");
91 confirmButton.addStyleName("confirm");
92
93 footer.setComponentAlignment(confirmButton, Alignment.MIDDLE_RIGHT);
94 footer.setComponentAlignment(cancelButton, Alignment.MIDDLE_LEFT);
95 footer.setSpacing(true);
96 setFooterToolbar(footer);
97
98
99 if (cancelIsDefault) {
100 cancelButton.addStyleName("default");
101 } else {
102 confirmButton.addStyleName("default");
103 }
104 }
105
106 public void setMessage(String message) {
107 this.message = message;
108 if (getContent() != null && getContent() instanceof Label) {
109 ((Label) getContent()).setValue(message);
110 }
111 }
112
113 public String getMessage() {
114 return message;
115 }
116
117 @Override
118 public void setContent(Component content) {
119 super.setContent(content);
120 }
121
122 @Override
123 protected Component createDefaultContent() {
124 return new Label();
125 }
126
127 public void addConfirmationHandler(ConfirmationEvent.Handler handler) {
128 addListener("confirmation_event", ConfirmationEvent.class, handler, ConfirmationEvent.ON_CONFIRMATION);
129 }
130
131 public void removeConfirmationHandler(ConfirmationEvent.Handler handler) {
132 removeListener("confirmation_event", ConfirmationEvent.class, handler);
133 }
134
135
136
137
138 public static class ConfirmationEvent extends Component.Event {
139
140
141
142
143 public interface Handler {
144 void onConfirmation(ConfirmationEvent event);
145 }
146
147 public static final java.lang.reflect.Method ON_CONFIRMATION;
148
149 static {
150 try {
151 ON_CONFIRMATION = ConfirmationEvent.Handler.class.getDeclaredMethod("onConfirmation", new Class[]{ConfirmationEvent.class});
152 } catch (final java.lang.NoSuchMethodException e) {
153 throw new java.lang.RuntimeException(e);
154 }
155 }
156
157 private final boolean isConfirmed;
158
159 public ConfirmationEvent(Component source, boolean isConfirmed) {
160 super(source);
161 this.isConfirmed = isConfirmed;
162 }
163
164 public boolean isConfirmed() {
165 return isConfirmed;
166 }
167 }
168 }