From eff01d328c85dd5cdcbc7c46f2f5f8dad2cb3fd5 Mon Sep 17 00:00:00 2001 From: jensp Date: Wed, 4 Oct 2017 16:57:44 +0000 Subject: [PATCH] CCM NG: A reusable ConfirmDialog for the Vaadin prototype git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5022 8810af33-2d31-482b-a856-94f89814c4df --- .../java/org/libreccm/ui/ConfirmDialog.java | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 ccm-core/src/main/java/org/libreccm/ui/ConfirmDialog.java diff --git a/ccm-core/src/main/java/org/libreccm/ui/ConfirmDialog.java b/ccm-core/src/main/java/org/libreccm/ui/ConfirmDialog.java new file mode 100644 index 000000000..1b3ac5535 --- /dev/null +++ b/ccm-core/src/main/java/org/libreccm/ui/ConfirmDialog.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2017 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.ui; + +import com.vaadin.ui.Button; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.VerticalLayout; +import com.vaadin.ui.Window; +import org.libreccm.core.UnexpectedErrorException; + +import java.util.concurrent.Callable; + +/** + * + * @author Jens Pelzetter + */ +public class ConfirmDialog extends Window { + + private static final long serialVersionUID = -3953818045293474190L; + + private final Callable confirmedAction; + private final Callable cancelAction; + + private Label messageLabel; + private Button confirmButton; + private Button cancelButton; + + public ConfirmDialog(final Callable confirmedAction) { + this.confirmedAction = confirmedAction; + this.cancelAction = () -> { + close(); + return null; + }; + addWidgets(); + } + + public ConfirmDialog(final Callable confirmedAction, + final Callable cancelAction) { + this.confirmedAction = confirmedAction; + this.cancelAction = cancelAction; + addWidgets(); + } + + private void addWidgets() { + + messageLabel = new Label(""); + + confirmButton = new Button("OK"); + confirmButton.addClickListener(event -> { + try { + confirmedAction.call(); + } catch (Exception ex) { + throw new UnexpectedErrorException(ex); + } + }); + + cancelButton = new Button("Cancel"); + cancelButton.addClickListener(event -> { + try { + cancelAction.call(); + } catch (Exception ex) { + throw new UnexpectedErrorException(ex); + } + }); + + final HorizontalLayout buttonsLayout = new HorizontalLayout( + confirmButton, cancelButton); + final VerticalLayout layout = new VerticalLayout(messageLabel, + buttonsLayout); + setContent(layout); + } + + public String getMessage() { + return messageLabel.getValue(); + } + + public void setMessage(final String message) { + messageLabel.setValue(message); + } + + public Button getConfirmButton() { + return confirmButton; + } + + public Button getCancelButton() { + return cancelButton; + } + +}