Java slowness warning

1

How to check that a method is taking a long time to be finalized and to display a warning to the user.

There is a method in the application that can take a while ... I made a test code to detect the slowness of a method and to show a warning to the user if it happens, so that it does not get lost, since the screen stays with the same thing for several seconds.

The thread with the delayed code will execute and then it will take 2s until the checkSlowness checks to see if it is taking too long. If passed 2s means that it is and that it should show the warning. The problem now is how to "pause" the worker to do nothing while waiting for user confirmation to proceed or not, as the worker continues to run and while the warning is shown the task may have ended successfully.

Any ideas?

package view;

import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingWorker;

public class Panel extends JPanel {

private final JButton btn;
private final JDialog dialog;

private SwingWorker<Void, Void> worker;

public Panel() {
    super(new GridBagLayout());
    btn = new JButton("Botão");
    dialog = new JDialog(null, "Teste", Dialog.ModalityType.APPLICATION_MODAL);
    init();
}

private final short secondsToBuildPDF = 2;

private void checkSlowness() {
    try {
        System.out.println("vai esperar " + secondsToBuildPDF + "s para verificar lentidão");
        Thread.sleep(secondsToBuildPDF * 1000);

        if (!pdfCreated) {

            int confirm = JOptionPane.showConfirmDialog(dialog,
                    "<html>A construção do PDF está demorando muito. <br><br>Deseja esperar?</html>",
                    "Alerta", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);

            System.out.println("confirmacao");
            if (confirm == JOptionPane.YES_OPTION) {
                checkSlowness();
            } else {
                worker.cancel(true);
            }
        }
    } catch (final InterruptedException ex) {
        System.out.println("exception: " + ex);
    }
}

private void init() {
    worker = new SwingWorker<Void, Void>() {
        @Override
        protected Void doInBackground() throws Exception {
            System.out.println("vai executar metodo");
            pdfCreated = false;
            Thread.sleep(20 * 1000);
            pdfCreated = true;
            return null;
        }
    };

    btn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent evt) {
            System.out.println("botao clicado, vai executar metodo demorado");

            worker.execute();
            checkSlowness();
        }
    });

    GridBagConstraints cons = new GridBagConstraints();
    cons.gridx = 0;
    cons.gridy = 0;
    cons.ipadx = 171;
    cons.anchor = GridBagConstraints.NORTHWEST;
    cons.insets = new Insets(91, 54, 149, 62);
    add(btn, cons);

    setSize(new Dimension(400, 200));

    dialog.setSize(this.getSize());
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

    dialog.add(this);
}

private void showAsDialog() {
    dialog.setVisible(true);
}

public static void main(final String[] args) {
    Panel panel = new Panel();
    panel.showAsDialog();
}

private boolean pdfCreated;

}
    
asked by anonymous 08.08.2018 / 21:47

0 answers