JAL-4446 cancellable Pasimap - still need to make the actual pasimap CCA calculation...
authorJim Procter <jprocter@dundee.ac.uk>
Mon, 5 Aug 2024 17:40:18 +0000 (18:40 +0100)
committerJim Procter <jprocter@dundee.ac.uk>
Mon, 5 Aug 2024 17:40:18 +0000 (18:40 +0100)
src/jalview/gui/PaSiMapPanel.java
src/jalview/gui/PairwiseAlignPanel.java
src/jalview/viewmodel/PaSiMapModel.java

index 98a366e..edf7deb 100644 (file)
@@ -212,23 +212,41 @@ public class PaSiMapPanel extends GPaSiMapPanel
                 + MAX_PASIMAP_SEQ);
       }
 
-      PairwiseAlignPanel pap = new PairwiseAlignPanel(av, selGroup, true,
+      final PairwiseAlignPanel pap = new PairwiseAlignPanel(av, selGroup, true,
               GAP_OPEN_COST, GAP_EXTEND_COST, false, null);
       pap.setDiscardAlignments(true);
       pap.setQuiet(true);
 
       System.out.println(pap != null);
       setPairwiseAlignPanel(pap);
-      getPasimapModel().calculate(pap);
-
-      xCombobox.setSelectedIndex(0);
-      yCombobox.setSelectedIndex(1);
-      zCombobox.setSelectedIndex(2);
+      progressBar.registerHandler(progId, new IProgressIndicatorHandler()
+      {
+        
+        @Override
+        public boolean cancelActivity(long id)
+        {
+          getPasimapModel().cancel();
+          return true;
+        }
+        
+        @Override
+        public boolean canCancel()
+        {
+          return true;
+        }
+      });
 
-      getPasimapModel().updateRc(getRotatableCanvas());
-      // rc.invalidate();
-      setTop(getPasimapModel().getTop());
+      getPasimapModel().calculate(pap);
+      if (!getPasimapModel().isCancelled())
+      {
+        xCombobox.setSelectedIndex(0);
+        yCombobox.setSelectedIndex(1);
+        zCombobox.setSelectedIndex(2);
 
+        getPasimapModel().updateRc(getRotatableCanvas());
+        // rc.invalidate();
+        setTop(getPasimapModel().getTop());
+      }
     } catch (OutOfMemoryError er)
     {
       new OOMWarning("calculating PaSiMap", er);
@@ -236,11 +254,14 @@ public class PaSiMapPanel extends GPaSiMapPanel
       return;
     } finally
     {
-      progressBar.setProgressBar("", progId);
+      if (!getPasimapModel().isCancelled()) {
+        // finish up the progress bar if we weren't cancelled.
+        progressBar.setProgressBar("", progId);
+      }
     }
 
     repaint();
-    if (getParent() == null)
+    if (!getPasimapModel().isCancelled() && getParent() == null)
     {
       Desktop.addInternalFrame(this,
               MessageManager.formatMessage("label.calc_title", "PaSiMap",
index 4c3b30c..ca97aaf 100755 (executable)
@@ -70,6 +70,8 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel
 
   public static final String PROGRESS = "progress";
 
+  private volatile boolean cancelled;
+  
   private int total;
 
   private int progress;
@@ -151,7 +153,7 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel
 
   public void calculate(ScoreMatrix sm)
   {
-
+    cancelled=false;
     StringBuilder sb = new StringBuilder(1024);
 
     sequences = new Vector<SequenceI>();
@@ -206,6 +208,11 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel
       alignmentScores[i - 1][i - 1] = Float.NaN;
       for (int j = 0; j < i; j++)
       {
+        if (cancelled)
+        {
+          alignmentOutput = "Alignment was cancelled.";
+          return;
+        }
         AlignSeq as = new AlignSeq(seqs[i], seqStrings[i], seqs[j],
                 seqStrings[j], type, GAP_OPEN_COST, GAP_EXTEND_COST);
 
@@ -418,4 +425,9 @@ public class PairwiseAlignPanel extends GPairwiseAlignPanel
   {
     discardAlignments = discard;
   }
+
+  public boolean isCancelled()
+  {
+    return cancelled;
+  }
 }
index 8568af4..ac070e8 100644 (file)
@@ -86,6 +86,11 @@ public class PaSiMapModel
   {
     pasimap = new PaSiMap(inputData, scoreModel, pap);
     pasimap.run(); // executes in same thread, wait for completion
+    if (pasimap.isCancelled())
+    {
+      // no more work to do
+      return;
+    }
 
     // Now find the component coordinates
     int ii = 0;
@@ -262,4 +267,18 @@ public class PaSiMapModel
   {
     pasimap = data;
   }
+
+  public boolean isCancelled()
+  {
+    if (pasimap==null || pasimap.isCancelled())
+    {
+      return true;
+    }
+    return false;
+  }
+
+  public void cancel()
+  {
+    pasimap.cancel();    
+  }
 }