JAL-3048 amendFeatures as dialog runner - repaint after amend not always reliable
authorJim Procter <jprocter@issues.jalview.org>
Fri, 6 Jul 2018 13:32:59 +0000 (14:32 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Fri, 6 Jul 2018 13:32:59 +0000 (14:32 +0100)
src/jalview/gui/FeatureRenderer.java
src/jalview/gui/Finder.java
src/jalview/gui/PopupMenu.java
src/jalview/gui/SeqPanel.java

index fc3dcc6..f987b60 100644 (file)
@@ -30,6 +30,7 @@ import jalview.io.FeaturesFile;
 import jalview.schemes.FeatureColour;
 import jalview.util.ColorUtils;
 import jalview.util.MessageManager;
+import jalview.util.dialogrunner.RunResponse;
 
 import java.awt.BorderLayout;
 import java.awt.Color;
@@ -123,11 +124,12 @@ public class FeatureRenderer
    * @param create
    *          true to create features, false to amend or delete
    * @param alignPanel
-   * @return
+   * @param responseHandler
+   *          boolean true RunResponse is run if features are created
    */
   protected boolean amendFeatures(final List<SequenceI> sequences,
           final List<SequenceFeature> features, boolean create,
-          final AlignmentPanel alignPanel)
+          final AlignmentPanel alignPanel, final Runnable responseHandler)
   {
     featureIndex = 0;
 
@@ -282,8 +284,8 @@ public class FeatureRenderer
             highlight.addResult(sequences.get(0), sf.getBegin(),
                     sf.getEnd());
 
-            alignPanel.getSeqPanel().seqCanvas.highlightSearchResults(
-                    highlight, false);
+            alignPanel.getSeqPanel().seqCanvas
+                    .highlightSearchResults(highlight, false);
           }
           FeatureColourI col = getFeatureStyle(name.getText());
           if (col == null)
@@ -388,121 +390,125 @@ public class FeatureRenderer
                     { sequences.get(0).getName() });
 
     /*
-     * show the dialog
+     * register responses and show the dialog
      */
-    int reply = JvOptionPane.showInternalOptionDialog(Desktop.desktop,
-            mainPanel, title, JvOptionPane.YES_NO_CANCEL_OPTION,
-            JvOptionPane.QUESTION_MESSAGE, null, options,
-            MessageManager.getString("action.ok"));
-
-    FeaturesFile ffile = new FeaturesFile();
-
-    final String enteredType = name.getText().trim();
-    final String enteredGroup = group.getText().trim();
-    final String enteredDescription = description.getText().replaceAll("\n", " ");
-
-    if (reply == JvOptionPane.OK_OPTION && enteredType.length() > 0)
-    {
-      /*
-       * update default values only if creating using default values
-       */
-      if (useLastDefaults)
-      {
-        lastFeatureAdded = enteredType;
-        lastFeatureGroupAdded = enteredGroup;
-        // TODO: determine if the null feature group is valid
-        if (lastFeatureGroupAdded.length() < 1)
-        {
-          lastFeatureGroupAdded = null;
-        }
-      }
-    }
-
-    if (!create)
-    {
-      SequenceFeature sf = features.get(featureIndex);
-
-      if (reply == JvOptionPane.NO_OPTION)
-      {
-        /*
-         * NO_OPTION corresponds to the Delete button
-         */
-        sequences.get(0).getDatasetSequence().deleteFeature(sf);
-        // update Feature Settings for removal of feature / group
-        featuresAdded();
-      }
-      else if (reply == JvOptionPane.YES_OPTION)
-      {
-        /*
-         * YES_OPTION corresponds to the Amend button
-         * need to refresh Feature Settings if type, group or colour changed;
-         * note we don't force the feature to be visible - the user has been
-         * warned if a hidden feature type or group was entered
-         */
-        boolean refreshSettings = (!featureType.equals(enteredType) || !featureGroup
-                .equals(enteredGroup));
-        refreshSettings |= (fcol != oldcol);
-        setColour(enteredType, fcol);
-        int newBegin = sf.begin;
-        int newEnd = sf.end;
-        try
-        {
-          newBegin = ((Integer) start.getValue()).intValue();
-          newEnd = ((Integer) end.getValue()).intValue();
-        } catch (NumberFormatException ex)
-        {
-          // JSpinner doesn't accept invalid format data :-)
-        }
-
-        /*
-         * replace the feature by deleting it and adding a new one
-         * (to ensure integrity of SequenceFeatures data store)
-         */
-        sequences.get(0).deleteFeature(sf);
-        SequenceFeature newSf = new SequenceFeature(sf, enteredType,
-                newBegin, newEnd, enteredGroup, sf.getScore());
-        newSf.setDescription(enteredDescription);
-        ffile.parseDescriptionHTML(newSf, false);
-        // amend features dialog only updates one sequence at a time
-        sequences.get(0).addSequenceFeature(newSf);
-
-        if (refreshSettings)
-        {
-          featuresAdded();
-        }
-      }
-    }
-    else
-    // NEW FEATURES ADDED
-    {
-      if (reply == JvOptionPane.OK_OPTION && enteredType.length() > 0)
-      {
-        for (int i = 0; i < sequences.size(); i++)
-        {
-          SequenceFeature sf = features.get(i);
-          SequenceFeature sf2 = new SequenceFeature(enteredType,
-                  enteredDescription, sf.getBegin(), sf.getEnd(),
-                  enteredGroup);
-          ffile.parseDescriptionHTML(sf2, false);
-          sequences.get(i).addSequenceFeature(sf2);
-        }
-
-        setColour(enteredType, fcol);
-
-        featuresAdded();
-
-        alignPanel.paintAlignment(true, true);
-
-        return true;
-      }
-      else
-      {
-        return false;
-      }
-    }
-
-    alignPanel.paintAlignment(true, true);
+    JvOptionPane.newOptionDialog(Desktop.desktop).response(
 
+            new RunResponse(JvOptionPane.OK_OPTION)
+            {
+              public void run()
+              {
+                final String enteredType = name.getText().trim();
+                final String enteredGroup = group.getText().trim();
+                final String enteredDescription = description.getText()
+                        .replaceAll("\n", " ");
+                if (enteredType.length() > 0)
+
+                {
+                  /*
+                   * update default values only if creating using default values
+                   */
+                  if (useLastDefaults)
+                  {
+                    lastFeatureAdded = enteredType;
+                    lastFeatureGroupAdded = enteredGroup;
+                    // TODO: determine if the null feature group is valid
+                    if (lastFeatureGroupAdded.length() < 1)
+                    {
+                      lastFeatureGroupAdded = null;
+                    }
+                  }
+                }
+
+                if (create)
+                {
+                  // NEW FEATURES ADDED
+                  if (enteredType.length() > 0)
+                  {
+                    for (int i = 0; i < sequences.size(); i++)
+                    {
+                      SequenceFeature sf = features.get(i);
+                      SequenceFeature sf2 = new SequenceFeature(enteredType,
+                              enteredDescription, sf.getBegin(),
+                              sf.getEnd(), enteredGroup);
+                      new FeaturesFile().parseDescriptionHTML(sf2, false);
+                      sequences.get(i).addSequenceFeature(sf2);
+                    }
+
+                    setColour(enteredType, fcol);
+
+                    featuresAdded();
+
+                    responseHandler.run();
+                  }
+                } else {
+                  SequenceFeature sf = features.get(featureIndex);
+                  /*
+                   * Feature amended - YES_OPTION corresponds to the Amend button
+                   * need to refresh Feature Settings if type, group or colour changed;
+                   * note we don't force the feature to be visible - the user has been
+                   * warned if a hidden feature type or group was entered
+                   */
+                  boolean refreshSettings = (!featureType.equals(enteredType)
+                          || !featureGroup.equals(enteredGroup));
+                  refreshSettings |= (fcol != oldcol);
+                  setColour(enteredType, fcol);
+                  int newBegin = sf.begin;
+                  int newEnd = sf.end;
+                  try
+                  {
+                    newBegin = ((Integer) start.getValue()).intValue();
+                    newEnd = ((Integer) end.getValue()).intValue();
+                  } catch (NumberFormatException ex)
+                  {
+                    // JSpinner doesn't accept invalid format data :-)
+                  }
+
+                  /*
+                   * replace the feature by deleting it and adding a new one
+                   * (to ensure integrity of SequenceFeatures data store)
+                   */
+                  sequences.get(0).deleteFeature(sf);
+                  SequenceFeature newSf = new SequenceFeature(sf, enteredType,
+                          newBegin, newEnd, enteredGroup, sf.getScore());
+                  newSf.setDescription(enteredDescription);
+                  new FeaturesFile().parseDescriptionHTML(newSf, false);
+                  // amend features dialog only updates one sequence at a time
+                  sequences.get(0).addSequenceFeature(newSf);
+
+                  if (refreshSettings)
+                  {
+                    featuresAdded();
+                  }
+                }
+                alignPanel.getSeqPanel().seqCanvas.highlightSearchResults(null, false);
+                alignPanel.paintAlignment(true, true);
+              }
+            }).response(new RunResponse(JvOptionPane.NO_OPTION)
+            {
+              public void run()
+              {
+                SequenceFeature sf = features.get(featureIndex);
+                /*
+                 * NO_OPTION corresponds to the Delete button
+                 */
+                sequences.get(0).getDatasetSequence().deleteFeature(sf);
+                // update Feature Settings for removal of feature / group
+                featuresAdded();
+                alignPanel.getSeqPanel().seqCanvas.highlightSearchResults(null, false);
+                alignPanel.paintAlignment(true, true);
+              }
+            }).defaultResponse(new Runnable()
+            {
+              public void run()
+              {
+                alignPanel.getSeqPanel().seqCanvas.highlightSearchResults(null, false);
+                alignPanel.paintAlignment(true, true);
+              }
+            }).showInternalDialog(mainPanel, title,
+                    JvOptionPane.YES_NO_CANCEL_OPTION,
+                    JvOptionPane.QUESTION_MESSAGE, null, options,
+                    MessageManager.getString("action.ok"));
     return true;
   }
 
index 84540f4..8b5bbda 100755 (executable)
@@ -223,22 +223,26 @@ public class Finder extends GFinder
     for (SearchResultMatchI match : searchResults.getResults())
     {
       seqs.add(match.getSequence().getDatasetSequence());
-      features.add(new SequenceFeature(searchString, desc,
-              match
-              .getStart(), match.getEnd(), desc));
+      features.add(new SequenceFeature(searchString, desc, match.getStart(),
+              match.getEnd(), desc));
     }
 
-    if (ap.getSeqPanel().seqCanvas.getFeatureRenderer().amendFeatures(seqs,
-            features, true, ap))
-    {
-      /*
-       * ensure feature display is turned on to show the new features,
-       * and remove them as highlighted regions
-       */
-      ap.alignFrame.showSeqFeatures.setSelected(true);
-      av.setShowSequenceFeatures(true);
-      ap.highlightSearchResults(null);
-    }
+    ap.getSeqPanel().seqCanvas.getFeatureRenderer().amendFeatures(seqs,
+            features, true, ap, new Runnable()
+            {
+              @Override
+              public void run()
+              {
+
+                /*
+                 * ensure feature display is turned on to show the new features,
+                 * and remove them as highlighted regions
+                 */
+                ap.alignFrame.showSeqFeatures.setSelected(true);
+                av.setShowSequenceFeatures(true);
+                ap.highlightSearchResults(null);
+              }
+            });
   }
 
   /**
index 8f082c1..0a5224c 100644 (file)
@@ -2041,13 +2041,13 @@ public class PopupMenu extends JPopupMenu implements ColourChangeListener
      */
     if (!seqs.isEmpty())
     {
-      if (ap.getSeqPanel().seqCanvas.getFeatureRenderer()
-              .amendFeatures(seqs, features, true, ap))
-      {
+      ap.getSeqPanel().seqCanvas.getFeatureRenderer()
+              .amendFeatures(seqs, features, true, ap,new Runnable() {
+                public void run() {
         ap.alignFrame.setShowSeqFeatures(true);
         ap.av.setSearchResults(null); // clear highlighting
         ap.repaint(); // draw new/amended features
-      }
+      }});
     }
   }
 
index 18c6acc..8f8e9a9 100644 (file)
@@ -1681,9 +1681,12 @@ public class SeqPanel extends JPanel
          */
         List<SequenceI> seqs = Collections.singletonList(sequence);
         seqCanvas.getFeatureRenderer().amendFeatures(seqs, features, false,
-                ap);
+                ap, new Runnable() {@Override
+                public void run()
+                {
         av.setSearchResults(null); // clear highlighting
         seqCanvas.repaint(); // draw new/amended features
+                }});
       }
     }
   }