added a progress bar

This commit is contained in:
hneemann 2019-04-02 22:42:29 +02:00
parent a8ac936e92
commit b3ba34fe9c
3 changed files with 78 additions and 5 deletions

View File

@ -33,6 +33,7 @@ public class ExpressionCreator {
private static final int COMPLEX_VAR_SIZE = 8;
private final TruthTable theTable;
private ProgressListener progressListener;
/**
* Creates a new instance
@ -98,6 +99,8 @@ public class ExpressionCreator {
listener.close();
time = System.currentTimeMillis() - time;
LOGGER.debug("time: " + time / 1000.0 + " sec");
if (progressListener!=null)
progressListener.complete();
}
private Job simplify(ExpressionListener listener, List<Variable> vars, String resultName, BoolTable boolTable) throws AnalyseException, ExpressionException {
@ -127,6 +130,26 @@ public class ExpressionCreator {
}
}
public ExpressionCreator setProgressListener(ProgressListener progressListener) {
this.progressListener = progressListener;
return this;
}
/**
* Listener used to monitor the progress
*/
public interface ProgressListener {
/**
* Called if a equation is calculated
*/
void oneCompleted();
/**
* Called if all equations are calculated
*/
void complete();
}
private final class Job {
private final List<Variable> localVars;
private final BoolTable boolTable;
@ -146,6 +169,8 @@ public class ExpressionCreator {
long time = System.currentTimeMillis();
getMinimizer(localVars.size()).minimize(localVars, boolTable, resultName, listener);
LOGGER.info("finished job with complexity " + getComplexity() + ": " + (System.currentTimeMillis() - time) / 1000 + "sec");
if (progressListener!=null)
progressListener.oneCompleted();
}
private int getComplexity() {

View File

@ -0,0 +1,47 @@
/*
* Copyright (c) 2019 Helmut Neemann.
* Use of this source code is governed by the GPL v3 license
* that can be found in the LICENSE file.
*/
package de.neemann.digital.gui.components.table;
import de.neemann.digital.lang.Lang;
import javax.swing.*;
import java.awt.*;
public class ProgressDialog extends JDialog implements ExpressionCreator.ProgressListener {
private final JProgressBar bar;
private int prog;
/**
* Create a new instance
*
* @param tableDialog the table dialog
*/
public ProgressDialog(TableDialog tableDialog) {
super(tableDialog,false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
bar = new JProgressBar(0, tableDialog.getModel().getTable().getResultCount());
bar.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
final JLabel label = new JLabel(Lang.get("msg_optimizationInProgress"));
label.setBorder(BorderFactory.createEmptyBorder(5,5,0,5));
getContentPane().add(label, BorderLayout.NORTH);
getContentPane().add(bar, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(tableDialog);
SwingUtilities.invokeLater(() -> setVisible(true));
}
@Override
public void oneCompleted() {
SwingUtilities.invokeLater(() -> bar.setValue(++prog));
}
@Override
public void complete() {
SwingUtilities.invokeLater(this::dispose);
}
}

View File

@ -599,15 +599,16 @@ public class TableDialog extends JDialog {
expressionListener = new ExpressionListenerJK(expressionListener);
final TruthTable table = model.getTable();
if (table.getResultCount() > 4 && table.getVars().size() > 8) {
if (!allSolutionsDialog.isVisible())
allSolutionsDialog.setVisible(true);
allSolutionsDialog.setText(Lang.get("msg_optimizationInProgress"));
if (table.getVars().size() >= 8) {
ExpressionCreator.ProgressListener progress =
table.getResultCount() == 1 ? null :
new ProgressDialog(this);
ExpressionListener finalExpressionListener = expressionListener;
new Thread(() -> {
ExpressionListenerStore storage = new ExpressionListenerStore(null);
try {
new ExpressionCreator(table).create(storage);
new ExpressionCreator(table).setProgressListener(progress).create(storage);
} catch (ExpressionException | FormatterException | AnalyseException e) {
SwingUtilities.invokeLater(() -> {
allSolutionsDialog.setVisible(false);