JAL-3878 Introduce IProgressIndicator#addProgressBar
authorMateusz Warowny <mmzwarowny@dundee.ac.uk>
Tue, 5 Apr 2022 16:27:43 +0000 (18:27 +0200)
committerMateusz Warowny <mmzwarowny@dundee.ac.uk>
Tue, 5 Apr 2022 16:27:43 +0000 (18:27 +0200)
addProgressBar adds a bar to the panel if it doesn't exist
ensuring that the bar is present after the call contrary
to setProgressBar which toggles the progress bar on and off.

src/jalview/gui/AlignFrame.java
src/jalview/gui/Desktop.java
src/jalview/gui/IProgressIndicator.java
src/jalview/gui/PCAPanel.java
src/jalview/gui/ProgressBar.java
src/jalview/gui/StructureChooser.java
src/jalview/gui/WebserviceInfo.java

index 94d6b6b..7011d4d 100644 (file)
@@ -1073,6 +1073,12 @@ public class AlignFrame extends GAlignFrame
   }
   
   @Override
+  public void addProgressBar(long id, String message)
+  {
+    progressBar.addProgressBar(id, message);
+  }
+
+  @Override
   public void removeProgressBar(long id)
   {
     progressBar.removeProgressBar(id);
index 0fa74d6..4fab4ed 100644 (file)
@@ -2584,6 +2584,13 @@ public class Desktop extends GDesktop
   }
   
   @Override
+  public void addProgressBar(long id, String message)
+  {
+    // TODO
+    throw new UnsupportedOperationException("not implemented");
+  }
+
+  @Override
   public void removeProgressBar(long id)
   {
     //TODO
index c250aca..d13338e 100644 (file)
@@ -57,10 +57,24 @@ public interface IProgressIndicator
   boolean operationInProgress();
 
   /**
-   * Remove progress bar with a given id from the panel.
+   * Adds a progress bar for the given id if it doesn't exist displaying the
+   * provided message. Subsequent calls do nothing.
    * 
    * @param id
+   *          progress bar identifier
+   * @param message
+   *          displayed message
+   */
+  void addProgressBar(long id, String message);
+
+  /**
+   * Removes a progress bar for the given id if it exists. Subsequent calls do
+   * nothing.
+   * 
+   * @param id
+   *          id of the progress bar to be removed
    */
   void removeProgressBar(long id);
 
+
 }
index c0d57a6..65bfe1c 100644 (file)
@@ -609,6 +609,12 @@ public class PCAPanel extends GPCAPanel
   }
   
   @Override
+  public void addProgressBar(long id, String message)
+  {
+    progressBar.addProgressBar(id, message);
+  }
+
+  @Override
   public void removeProgressBar(long id)
   {
     progressBar.removeProgressBar(id);
index abf096f..7c98398 100644 (file)
@@ -83,7 +83,7 @@ public class ProgressBar implements IProgressIndicator
       throw new NullPointerException();
     }
     if (!GridLayout.class
-            .isAssignableFrom(container.getLayout().getClass()))
+        .isAssignableFrom(container.getLayout().getClass()))
     {
       throw new IllegalArgumentException("Container must have GridLayout");
     }
@@ -126,62 +126,81 @@ public class ProgressBar implements IProgressIndicator
       @Override
       public void run()
       {
-        JPanel progressPanel = progressBars.get(id);
-        if (progressPanel != null)
+        if (progressBars.containsKey(id))
         {
           /*
            * Progress bar is displayed for this id - remove it now, and any handler
            */
-          progressBars.remove(id);
+          removeProgressBar(id);
           if (message != null && statusBar != null)
           {
             statusBar.setText(message);
           }
-          if (progressBarHandlers.containsKey(id))
-          {
-            progressBarHandlers.remove(id);
-          }
-          removeRow(progressPanel);
         }
         else
         {
           /*
            * No progress bar for this id - add one now
            */
-          progressPanel = new JPanel(new BorderLayout(10, 5));
-
-          JProgressBar progressBar = new JProgressBar();
-          progressBar.setIndeterminate(true);
-
-          progressPanel.add(new JLabel(message), BorderLayout.WEST);
-          progressPanel.add(progressBar, BorderLayout.CENTER);
-
-          addRow(progressPanel);
-
-          progressBars.put(id, progressPanel);
+          addProgressBar(id, message);
         }
-
-        refreshLayout();
       }
     });
 
   }
-  
+
+  /**
+   * Add a progress bar for the given id if it doesn't exist displaying the
+   * provided message. Subsequent calls do nothing.
+   * 
+   * @param id
+   *          progress bar identifier
+   * @param message
+   *          displayed message
+   */
+  @Override
+  public void addProgressBar(final long id, final String message)
+  {
+    if (progressBars.containsKey(id))
+      return;
+    JPanel progressPanel = new JPanel(new BorderLayout(10, 5));
+    progressBars.put(id, progressPanel);
+    Runnable r = () -> {
+      JProgressBar progressBar = new JProgressBar();
+      progressBar.setIndeterminate(true);
+      progressPanel.add(new JLabel(message), BorderLayout.WEST);
+      progressPanel.add(progressBar, BorderLayout.CENTER);
+      addRow(progressPanel);
+      refreshLayout();
+    };
+    if (SwingUtilities.isEventDispatchThread())
+      r.run();
+    else
+      SwingUtilities.invokeLater(r);
+  }
+
+  /**
+   * Remove a progress bar for the given id if it exists. Subsequent calls do
+   * nothing.
+   * 
+   * @param id
+   *          id of the progress bar to be removed
+   */
   @Override
   public void removeProgressBar(final long id)
   {
-    SwingUtilities.invokeLater(() -> {
-      JPanel progressPanel = progressBars.get(id);
-      if (progressPanel != null)
-      {
-        progressBars.remove(id);
-        if (progressBarHandlers.containsKey(id))
-        {
-          progressBarHandlers.remove(id);
-        }
-        removeRow(progressPanel);
-      }
-    });
+    JPanel progressPanel = progressBars.remove(id);
+    if (progressPanel == null)
+      return;
+    progressBarHandlers.remove(id);
+    Runnable r = () -> {
+      removeRow(progressPanel);
+      refreshLayout();
+    };
+    if (SwingUtilities.isEventDispatchThread())
+      r.run();
+    else
+      SwingUtilities.invokeLater(r);
   }
 
   /**
@@ -236,7 +255,7 @@ public class ProgressBar implements IProgressIndicator
    */
   @Override
   public void registerHandler(final long id,
-          final IProgressIndicatorHandler handler)
+      final IProgressIndicatorHandler handler)
   {
     final IProgressIndicator us = this;
 
@@ -249,7 +268,7 @@ public class ProgressBar implements IProgressIndicator
         if (progressPanel == null)
         {
           System.err.println(
-                  "call setProgressBar before registering the progress bar's handler.");
+              "call setProgressBar before registering the progress bar's handler.");
           return;
         }
 
@@ -263,7 +282,7 @@ public class ProgressBar implements IProgressIndicator
 
         progressBarHandlers.put(id, handler);
         JButton cancel = new JButton(
-                MessageManager.getString("action.cancel"));
+            MessageManager.getString("action.cancel"));
         cancel.addActionListener(new ActionListener()
         {
 
@@ -272,9 +291,9 @@ public class ProgressBar implements IProgressIndicator
           {
             handler.cancelActivity(id);
             us.setProgressBar(MessageManager
-                    .formatMessage("label.cancelled_params", new Object[]
-                    { ((JLabel) progressPanel.getComponent(0)).getText() }),
-                    id);
+                .formatMessage("label.cancelled_params", new Object[]
+                { ((JLabel) progressPanel.getComponent(0)).getText() }),
+                id);
           }
         });
         progressPanel.add(cancel, BorderLayout.EAST);
index 97b0cfb..509ca43 100644 (file)
@@ -1285,6 +1285,12 @@ public class StructureChooser extends GStructureChooser
   }
   
   @Override
+  public void addProgressBar(long id, String message)
+  {
+    progressBar.addProgressBar(id, message);
+  }
+
+  @Override
   public void removeProgressBar(long id)
   {
     progressBar.removeProgressBar(id);
index b9b42dc..15d0055 100644 (file)
@@ -935,6 +935,12 @@ public void hyperlinkUpdate(HyperlinkEvent e)
   }
   
   @Override
+  public void addProgressBar(long id, String message)
+  {
+    progressBar.addProgressBar(id, message);
+  }
+
+  @Override
   public void removeProgressBar(long id)
   {
     progressBar.removeProgressBar(id);