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