From 8da04dc6d4f3f5ae67a0db53079b9a9e898e6a1b Mon Sep 17 00:00:00 2001 From: "Peter A. Jonsson" Date: Fri, 2 Dec 2022 15:47:40 +0100 Subject: [PATCH] GUI: replace ProgressBar with ProgressMonitor --- java/org/contikios/cooja/Cooja.java | 6 ++ java/org/contikios/cooja/GUI.java | 77 ++++++++++-------------- java/org/contikios/cooja/Simulation.java | 5 +- 3 files changed, 43 insertions(+), 45 deletions(-) diff --git a/java/org/contikios/cooja/Cooja.java b/java/org/contikios/cooja/Cooja.java index 8849df9eae..732f2873b6 100644 --- a/java/org/contikios/cooja/Cooja.java +++ b/java/org/contikios/cooja/Cooja.java @@ -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. diff --git a/java/org/contikios/cooja/GUI.java b/java/org/contikios/cooja/GUI.java index 49cadd72b3..3b03caa184 100644 --- a/java/org/contikios/cooja/GUI.java +++ b/java/org/contikios/cooja/GUI.java @@ -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; @@ -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; @@ -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; @@ -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 PROGRESS_WARNINGS = new ArrayList<>(); final ArrayList> menuMotePluginClasses = new ArrayList<>(); @@ -1260,35 +1263,16 @@ public SwingWorker 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. @@ -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 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) { @@ -1337,6 +1333,10 @@ protected void process(List 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()) { @@ -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; } @@ -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); diff --git a/java/org/contikios/cooja/Simulation.java b/java/org/contikios/cooja/Simulation.java index 205b68b9ae..f7e7fe816b 100644 --- a/java/org/contikios/cooja/Simulation.java +++ b/java/org/contikios/cooja/Simulation.java @@ -392,7 +392,9 @@ private SimulationCreationException startPlugins(Element root, Cooja cooja) { var startedPlugins = new ArrayList(); // 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"); @@ -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) {