Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit 9481312

Browse files
lgallgal
authored andcommitted
SORMAS-Foundation#1613 added cancel button on the unsaved changes dialog
1 parent ac8c020 commit 9481312

5 files changed

Lines changed: 102 additions & 36 deletions

File tree

sormas-api/src/main/java/de/symeda/sormas/api/i18n/Strings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,7 @@ public interface Strings {
757757
String text = "text";
758758
String toCase = "toCase";
759759
String total = "total";
760+
String unsavedChanges_cancel = "unsavedChanges.cancel";
760761
String unsavedChanges_discard = "unsavedChanges.discard";
761762
String unsavedChanges_save = "unsavedChanges.save";
762763
String unsavedChanges_warningMessage = "unsavedChanges.warningMessage";

sormas-api/src/main/resources/strings.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@ unsavedChanges.warningTitle = Confirm navigation
807807
unsavedChanges.warningMessage = You have unsaved changes on this form.
808808
unsavedChanges.discard = Discard changes
809809
unsavedChanges.save = Save changes
810+
unsavedChanges.cancel = Cancel navigation
810811

811812
headingNetworkDiagramTooManyContacts = Too many contacts
812813
warningNetworkDiagramTooManyContacts = There are %d contacts and it is possible that your browser will freeze while displaying the diagram.<br/>Please choose a smaller time window.

sormas-ui/src/main/java/de/symeda/sormas/ui/utils/AbstractDetailView.java

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.vaadin.ui.Label;
88
import com.vaadin.ui.UI;
99
import com.vaadin.ui.Window;
10+
import com.vaadin.ui.themes.ValoTheme;
1011

1112
import de.symeda.sormas.api.ReferenceDto;
1213
import de.symeda.sormas.api.i18n.I18nProperties;
@@ -70,28 +71,56 @@ protected boolean findReferenceByParams(String params) {
7071
@Override
7172
public void beforeLeave(ViewBeforeLeaveEvent event) {
7273
if (subComponent.isDirty()) {
73-
Window warningPopup = VaadinUiUtil.showConfirmationPopup(
74-
I18nProperties.getString(Strings.unsavedChanges_warningTitle),
75-
new Label(I18nProperties.getString(Strings.unsavedChanges_warningMessage)),
76-
I18nProperties.getString(Strings.unsavedChanges_save),
77-
I18nProperties.getString(Strings.unsavedChanges_discard),
78-
400,
79-
(confirmed) -> {
80-
if (confirmed) {
81-
subComponent.commitAndHandle();
82-
} else {
83-
subComponent.discard();
84-
}
85-
86-
event.navigate();
87-
});
88-
89-
warningPopup.setClosable(true);
74+
showNavigationConfirmPopup(event);
9075
} else {
9176
event.navigate();
9277
}
9378
}
9479

80+
private void showNavigationConfirmPopup(ViewBeforeLeaveEvent event) {
81+
Window warningPopup = VaadinUiUtil.showConfirmationPopup(
82+
I18nProperties.getString(Strings.unsavedChanges_warningTitle),
83+
new Label(I18nProperties.getString(Strings.unsavedChanges_warningMessage)),
84+
popupWindow -> {
85+
ConfirmationComponent confirmationComponent = new ConfirmationComponent(false, null) {
86+
87+
private static final long serialVersionUID = 3664636750443474734L;
88+
89+
@Override
90+
protected void onConfirm() {
91+
subComponent.commitAndHandle();
92+
popupWindow.close();
93+
event.navigate();
94+
}
95+
96+
@Override
97+
protected void onCancel() {
98+
subComponent.discard();
99+
popupWindow.close();
100+
event.navigate();
101+
}
102+
};
103+
104+
confirmationComponent.getConfirmButton().setCaption(I18nProperties.getString(Strings.unsavedChanges_save));
105+
confirmationComponent.getCancelButton().setCaption(I18nProperties.getString(Strings.unsavedChanges_discard));
106+
107+
confirmationComponent.addExtraButton(
108+
ButtonHelper.createButtonWithCaption(
109+
Strings.unsavedChanges_cancel,
110+
I18nProperties.getString(Strings.unsavedChanges_cancel),
111+
null,
112+
ValoTheme.BUTTON_LINK),
113+
buttonEvent -> {
114+
popupWindow.close();
115+
});
116+
117+
return confirmationComponent;
118+
},
119+
600);
120+
121+
warningPopup.setClosable(true);
122+
}
123+
95124
/**
96125
* @return The root object displayed on this View.
97126
*/

sormas-ui/src/main/java/de/symeda/sormas/ui/utils/ConfirmationComponent.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,23 @@ public abstract class ConfirmationComponent extends HorizontalLayout {
3737

3838
private transient List<DoneListener> doneListeners = new ArrayList<DoneListener>();
3939

40+
private final boolean inverseOrder;
4041
private Button confirmButton;
4142
private Button cancelButton;
43+
private final String cancelButtonStyle;
4244

4345
public ConfirmationComponent() {
4446
this(false);
4547
}
4648

4749
public ConfirmationComponent(boolean inverseOrder) {
50+
this(inverseOrder, ValoTheme.BUTTON_PRIMARY);
51+
}
52+
53+
public ConfirmationComponent(boolean inverseOrder, String cancelButtonStyle) {
54+
this.inverseOrder = inverseOrder;
55+
this.cancelButtonStyle = cancelButtonStyle;
56+
4857
setSpacing(true);
4958
setSizeUndefined();
5059

@@ -59,6 +68,15 @@ public ConfirmationComponent(boolean inverseOrder) {
5968
addComponent(discardButton);
6069
}
6170

71+
public void addExtraButton(Button button, Button.ClickListener handler) {
72+
button.addClickListener(e -> {
73+
handler.buttonClick(e);
74+
onDone();
75+
});
76+
77+
addComponent(button, inverseOrder ? getComponentCount() - 1 : 0);
78+
}
79+
6280
public Button getConfirmButton() {
6381
if (confirmButton == null) {
6482
confirmButton = ButtonHelper.createButton(Captions.actionConfirm, event -> {
@@ -75,7 +93,7 @@ public Button getCancelButton() {
7593
cancelButton = ButtonHelper.createButton(Captions.actionCancel, event -> {
7694
onCancel();
7795
onDone();
78-
}, ValoTheme.BUTTON_LINK);
96+
}, cancelButtonStyle);
7997
}
8098

8199
return cancelButton;

sormas-ui/src/main/java/de/symeda/sormas/ui/utils/VaadinUiUtil.java

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package de.symeda.sormas.ui.utils;
1919

2020
import java.util.function.Consumer;
21+
import java.util.function.Function;
2122

2223
import com.vaadin.icons.VaadinIcons;
2324
import com.vaadin.server.Sizeable.Unit;
@@ -153,6 +154,37 @@ public static Window showConfirmationPopup(
153154
Integer width,
154155
Consumer<Boolean> resultConsumer) {
155156

157+
return showConfirmationPopup(caption, content, popupWindow -> {
158+
ConfirmationComponent confirmationComponent = new ConfirmationComponent(false) {
159+
160+
private static final long serialVersionUID = 1L;
161+
162+
@Override
163+
protected void onConfirm() {
164+
resultConsumer.accept(true);
165+
popupWindow.close();
166+
}
167+
168+
@Override
169+
protected void onCancel() {
170+
resultConsumer.accept(false);
171+
popupWindow.close();
172+
}
173+
};
174+
175+
confirmationComponent.getConfirmButton().setCaption(confirmCaption);
176+
confirmationComponent.getCancelButton().setCaption(cancelCaption);
177+
178+
return confirmationComponent;
179+
}, width);
180+
}
181+
182+
public static Window showConfirmationPopup(
183+
String caption,
184+
Component content,
185+
Function<Window, ConfirmationComponent> confirmationComponentProvider,
186+
Integer width) {
187+
156188
Window popupWindow = VaadinUiUtil.createPopupWindow();
157189
if (width != null) {
158190
popupWindow.setWidth(width, Unit.PIXELS);
@@ -166,33 +198,18 @@ public static Window showConfirmationPopup(
166198
content.setWidth(100, Unit.PERCENTAGE);
167199
layout.addComponent(content);
168200

169-
ConfirmationComponent confirmationComponent = new ConfirmationComponent(false) {
170-
171-
private static final long serialVersionUID = 1L;
172-
173-
@Override
174-
protected void onConfirm() {
175-
resultConsumer.accept(true);
176-
popupWindow.close();
177-
}
178-
179-
@Override
180-
protected void onCancel() {
181-
resultConsumer.accept(false);
182-
popupWindow.close();
183-
}
184-
};
185-
confirmationComponent.getConfirmButton().setCaption(confirmCaption);
186-
confirmationComponent.getCancelButton().setCaption(cancelCaption);
201+
ConfirmationComponent confirmationComponent = confirmationComponentProvider.apply(popupWindow);
187202

188203
layout.addComponent(confirmationComponent);
189204
layout.setComponentAlignment(confirmationComponent, Alignment.BOTTOM_RIGHT);
190205
layout.setWidth(100, Unit.PERCENTAGE);
191206
layout.setSpacing(true);
207+
192208
popupWindow.setContent(layout);
193209
popupWindow.setClosable(false);
194210

195211
UI.getCurrent().addWindow(popupWindow);
212+
196213
return popupWindow;
197214
}
198215

0 commit comments

Comments
 (0)