All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bidib.wizard.script.client.view.ScriptActionDialog Maven / Gradle / Ivy

There is a newer version: 2.0.27
Show newest version
package org.bidib.wizard.script.client.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.util.function.Consumer;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.WindowConstants;

import org.bidib.jbidibc.messages.helpers.Context;
import org.bidib.wizard.api.locale.Resources;
import org.bidib.wizard.client.common.dialog.EscapeDialog;
import org.bidib.wizard.common.script.common.CommonScripting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jgoodies.forms.builder.ButtonBarBuilder;
import com.jgoodies.forms.builder.FormBuilder;
import com.jgoodies.forms.factories.Paddings;

public class ScriptActionDialog extends EscapeDialog {

    private static final long serialVersionUID = 1L;

    private static final Logger LOGGER = LoggerFactory.getLogger(ScriptActionDialog.class);

    private static final String ENCODED_DIALOG_COLUMN_SPECS = "pref, 10dlu, fill:50dlu:grow";

    private int result = JOptionPane.CANCEL_OPTION;

    private final Consumer okCallback;

    private final Consumer cancelCallback;

    private Timer actionCountDownTimer;

    private int remainingActionTime = 30;

    private JProgressBar progressBar;

    public ScriptActionDialog(final Component parent, final Context context, boolean modal, String dialogMessage,
        final Consumer cancelCallback) {

        this(parent, context, modal, dialogMessage, null, cancelCallback);
    }

    public ScriptActionDialog(final Component parent, final Context context, boolean modal, String dialogMessage,
        final Consumer okCallback, final Consumer cancelCallback) {

        super(JOptionPane.getFrameForComponent(parent), Resources.getString(ScriptActionDialog.class, "dialogTitle"),
            modal);
        this.okCallback = okCallback;
        this.cancelCallback = cancelCallback;

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        getContentPane().setLayout(new BorderLayout());

        LOGGER.info("Provided context: {}", context);

        final Integer pairingTimeout =
            context.get(CommonScripting.KEY_ACTION_TIMEOUT, Integer.class, Integer.valueOf(30));
        this.remainingActionTime = pairingTimeout.intValue();

        // final JLabel messageLabel = FormsSetup.getComponentFactoryDefault().createTitle(dialogMessage);
        final JLabel messageLabel = new JLabel();
        String message = dialogMessage.replaceAll("\\\\n", "
"); messageLabel.setText("" + message + ""); if (this.remainingActionTime > 0) { progressBar = new JProgressBar(0, this.remainingActionTime); this.actionCountDownTimer = new Timer(1000, evt -> { this.remainingActionTime--; progressBar.setValue(this.remainingActionTime); progressBar.setString(String.format("%ds", this.remainingActionTime)); if (this.remainingActionTime == 0) { try { LOGGER.info("The action timer has elapsed."); fireCancel(); } catch (Exception ex) { LOGGER.warn("Stop timer failed.", ex); } } }); this.actionCountDownTimer.setRepeats(true); } final String rowsSpec = (this.remainingActionTime > 0) ? "p, 10dlu, p, 3dlu, p, 10dlu, p" : "p, 10dlu, p"; final FormBuilder builder = FormBuilder.create().columns(ENCODED_DIALOG_COLUMN_SPECS).rows(rowsSpec); builder.border(Paddings.DIALOG); int row = 1; // add content builder.add(messageLabel).xyw(1, row, 3); if (this.progressBar != null) { row += 2; builder.add(Resources.getString(ScriptActionDialog.class, "remaining-time")).xyw(1, row, 3); row += 2; builder.add(progressBar).xyw(1, row, 3); progressBar.setStringPainted(true); progressBar.setValue(this.remainingActionTime); progressBar.setString(String.format("%ds", this.remainingActionTime)); } row += 2; JButton ok = new JButton(Resources.getString(ScriptActionDialog.class, "ok")); if (this.okCallback == null) { ok.setEnabled(false); ok.setVisible(false); } ok.addActionListener(evt -> fireOk()); JButton cancel = new JButton(Resources.getString(ScriptActionDialog.class, "cancel")); if (this.cancelCallback == null) { cancel.setEnabled(false); } cancel.addActionListener(evt -> fireCancel()); final JPanel buttons = new ButtonBarBuilder().addGlue().addButton(ok, cancel).build(); builder.add(buttons).xyw(1, row, 3); getContentPane().add(builder.build()); pack(); setLocationRelativeTo(parent); setMinimumSize(getSize()); } @Override public void setVisible(boolean visible) { LOGGER.info("Set the pairing dialog visible: {}", visible); if (visible == false) { if (this.actionCountDownTimer != null) { try { this.actionCountDownTimer.stop(); } catch (Exception ex) { LOGGER.warn("Stop action countdown timer failed.", ex); } this.actionCountDownTimer = null; } } else { if (this.actionCountDownTimer != null) { this.actionCountDownTimer.start(); } } super.setVisible(visible); } private void fireOk() { LOGGER.info("Close the script action process."); result = JOptionPane.OK_OPTION; if (this.okCallback != null) { try { this.okCallback.accept(Boolean.TRUE); } catch (Exception ex) { LOGGER.warn("Call okCallback failed.", ex); } } fireClose(); } private void fireCancel() { LOGGER.info("Cancel the script action process."); if (this.cancelCallback != null) { try { this.cancelCallback.accept(Boolean.FALSE); } catch (Exception ex) { LOGGER.warn("Call cancelCallback failed.", ex); } } fireClose(); } private void fireClose() { LOGGER.info("Close the dialog."); if (this.actionCountDownTimer != null) { try { LOGGER.info("Stop the action countdown timer."); this.actionCountDownTimer.stop(); } catch (Exception ex) { LOGGER.warn("Stop action countdown timer failed.", ex); } this.actionCountDownTimer = null; } if (SwingUtilities.isEventDispatchThread()) { setVisible(false); } else { SwingUtilities.invokeLater(() -> { setVisible(false); }); } } public int getResult() { return result; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy