Skip to content

Commit

Permalink
GUI: replace ProgressBar with ProgressMonitor
Browse files Browse the repository at this point in the history
  • Loading branch information
pjonsson committed Dec 2, 2022
1 parent 00c3305 commit 8da04dc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 45 deletions.
6 changes: 6 additions & 0 deletions java/org/contikios/cooja/Cooja.java
Original file line number Diff line number Diff line change
Expand Up @@ -1903,6 +1903,12 @@ public static void setProgressMessage(String msg, int type) {
}
}

public static void setRemainingProgress(int remaining) {
if (gui != null) {
gui.loadProgress = GUI.maxProgress - remaining;
}
}

/**
* Load quick help for given object or identifier. Note that this method does not
* show the quick help pane.
Expand Down
77 changes: 33 additions & 44 deletions java/org/contikios/cooja/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
Expand Down Expand Up @@ -78,7 +77,6 @@
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
Expand All @@ -87,6 +85,7 @@
import javax.swing.JToggleButton;
import javax.swing.JToolBar;
import javax.swing.KeyStroke;
import javax.swing.ProgressMonitor;
import javax.swing.RepaintManager;
import javax.swing.SwingWorker;
import javax.swing.Timer;
Expand Down Expand Up @@ -119,7 +118,11 @@ public class GUI {

static JFrame frame;
final JDesktopPane myDesktopPane;
private static JProgressBar PROGRESS_BAR = null;

/** Number of registered plugins + estimated max lines of compilation output (number from rpl-border-router). */
static final int maxProgress = 157;
private ProgressMonitor progressMonitor = null;
int loadProgress = 0;
private final ArrayList<String> PROGRESS_WARNINGS = new ArrayList<>();

final ArrayList<Class<? extends Plugin>> menuMotePluginClasses = new ArrayList<>();
Expand Down Expand Up @@ -1260,35 +1263,16 @@ public SwingWorker<Simulation, Cooja.SimulationCreationException> createLoadSimW
addToFileHistory(configFile);
}

final JPanel progressPanel;
final JDialog progressDialog;
if (quick) {
final String progressTitle = "Loading " + (configFile == null ? "" : configFile.getAbsolutePath());
progressDialog = new JDialog(frame, progressTitle, Dialog.ModalityType.APPLICATION_MODAL);

progressPanel = new JPanel(new BorderLayout());
var progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setIndeterminate(true);

PROGRESS_BAR = progressBar; // Allow various parts of Cooja to show messages.

var button = new JButton("Abort");

progressPanel.add(BorderLayout.CENTER, progressBar);
progressPanel.add(BorderLayout.SOUTH, button);
progressPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

progressDialog.getContentPane().add(progressPanel);
progressDialog.setSize(400, 200);

progressDialog.getRootPane().setDefaultButton(button);
progressDialog.setLocationRelativeTo(frame);
progressDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
var file = configFile == null ? cooja.getSimulation().getCfg().file() : configFile.getAbsolutePath();
final String progressTitle = "Loading " + (file == null ? "" : file);
progressMonitor = new ProgressMonitor(frame, progressTitle,"", 0, maxProgress);
progressMonitor.setMillisToDecideToPopup(0);
progressMonitor.setProgress(0);
} else {
progressPanel = null;
progressDialog = null;
progressMonitor = null;
}
loadProgress = 0;

// SwingWorker can pass information from worker to process() through publish().
// Communicate information the other way through this shared queue.
Expand Down Expand Up @@ -1322,10 +1306,22 @@ public Simulation doInBackground() {
return newSim;
}

private boolean closeProgressMonitor() {
if (progressMonitor != null) {
var cancelled = progressMonitor.isCanceled();
progressMonitor.close();
progressMonitor = null;
loadProgress = 0;
return cancelled;
}
return false;
}

@Override
protected void process(List<Cooja.SimulationCreationException> exs) {
var cancelled = closeProgressMonitor();
for (var e : exs) {
var retry = showErrorDialog("Simulation load error", e, true);
var retry = cancelled ? false : showErrorDialog("Simulation load error", e, true);
try {
channel.put(retry ? 1 : 0);
} catch (InterruptedException ex) {
Expand All @@ -1337,6 +1333,10 @@ protected void process(List<Cooja.SimulationCreationException> exs) {

@Override
protected void done() {
if (closeProgressMonitor()) {
cooja.doRemoveSimulation();
return;
}
// Simulation loaded, plugins started, now Z-order visualized plugins.
for (int z = 0; z < myDesktopPane.getAllFrames().length; z++) {
for (var plugin : myDesktopPane.getAllFrames()) {
Expand Down Expand Up @@ -1397,19 +1397,8 @@ public void actionPerformed(ActionEvent e) {
dialog.setVisible(true);
}
PROGRESS_WARNINGS.clear();
if (progressDialog != null && progressDialog.isDisplayable()) {
progressDialog.dispose();
}
}
};

if (progressDialog != null) {
java.awt.EventQueue.invokeLater(() -> {
progressPanel.setVisible(true);
progressDialog.getRootPane().getDefaultButton().addActionListener(e -> worker.cancel(true));
progressDialog.setVisible(true);
});
}
return worker;
}

Expand Down Expand Up @@ -1449,9 +1438,9 @@ private void updateDesktopSize() {
}

public void setProgressMessage(String msg, int type) {
if (PROGRESS_BAR != null && PROGRESS_BAR.isShowing()) {
PROGRESS_BAR.setString(msg);
PROGRESS_BAR.setStringPainted(true);
if (progressMonitor != null && !progressMonitor.isCanceled()) {
progressMonitor.setNote(msg);
progressMonitor.setProgress(++loadProgress);
}
if (type != MessageListUI.NORMAL) {
PROGRESS_WARNINGS.add(msg);
Expand Down
5 changes: 4 additions & 1 deletion java/org/contikios/cooja/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ private SimulationCreationException startPlugins(Element root, Cooja cooja) {
var startedPlugins = new ArrayList<Plugin>();
// Restart plugins from config
boolean hasController = false;
for (var pluginElement : root.getChildren("plugin")) {
var pluginConfigs = root.getChildren("plugin");
Cooja.setRemainingProgress(pluginConfigs.size() - 1);
for (var pluginElement : pluginConfigs) {
String pluginClassName = pluginElement.getText().trim();
if (pluginClassName.startsWith("se.sics")) {
pluginClassName = pluginClassName.replaceFirst("se\\.sics", "org.contikios");
Expand Down Expand Up @@ -430,6 +432,7 @@ private SimulationCreationException startPlugins(Element root, Cooja cooja) {
mote = getMote(moteNr);
}
}
Cooja.setProgressMessage("Starting " + pluginClassName);
try {
startedPlugins.add(cooja.startPlugin(pluginClass, this, mote, pluginElement));
} catch (PluginConstructionException ex) {
Expand Down

0 comments on commit 8da04dc

Please sign in to comment.