JAL-4392 Fixed failed test cases
[jalview.git] / src / jalview / gui / AlignFrame.java
index 9aa8733..766b35e 100644 (file)
@@ -56,9 +56,11 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Deque;
 import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 import java.util.Vector;
 
 import javax.swing.ButtonGroup;
@@ -150,6 +152,7 @@ import jalview.schemes.ColourSchemeI;
 import jalview.schemes.ColourSchemes;
 import jalview.schemes.ResidueColourScheme;
 import jalview.schemes.TCoffeeColourScheme;
+import jalview.util.Constants;
 import jalview.util.HttpUtils;
 import jalview.util.ImageMaker.TYPE;
 import jalview.util.MessageManager;
@@ -802,6 +805,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
       }
       ap.av.updateConservation(ap);
       ap.av.updateConsensus(ap);
+      ap.av.updateSecondaryStructureConsensus(ap);
       ap.av.updateStrucConsensus(ap);
     }
   }
@@ -5670,6 +5674,141 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     viewport.setShowUnconserved(showNonconservedMenuItem.getState());
     alignPanel.paintAlignment(false, false);
   }
+  
+  @Override
+  protected void updateShowSecondaryStructureMenu(JMenu showSS, ButtonGroup ssButtonGroup){
+    
+    List<String> ssSources = new ArrayList<String>();
+    AlignmentAnnotation[] anns = alignPanel.getAlignment()
+            .getAlignmentAnnotation();
+    Map<String, JCheckBoxMenuItem> checkboxMap = getCheckboxesInMenu(showSS);
+    
+    ssSources = AlignmentUtils.extractSSSourceInAlignmentAnnotation(anns);
+    
+    if(ssSources == null) {
+      showSS.removeAll();
+      ssButtonGroup.clearSelection();
+      return;
+    }
+    
+    List<String> selectedCheckBoxes = getSelectedOptions(checkboxMap);
+    
+    // Add checkboxes for categories
+    for (String ssSource : ssSources) {
+      
+      if(checkboxMap.get(ssSource)== null) {
+        JCheckBoxMenuItem checkBox = new JCheckBoxMenuItem(ssSource);
+        checkBox.setSelected(false);
+                
+        checkBox.addItemListener(e -> {
+            if (e.getStateChange() == ItemEvent.SELECTED) {
+                
+              showOrHideSecondaryStructureForSource(ssSource, true);
+              
+            } else {                
+
+              showOrHideSecondaryStructureForSource(ssSource, false);
+              
+            }
+        });
+        showSS.add(checkBox);
+      }
+    }
+    // Iterate over the keys of checkboxMap
+    for (String key : checkboxMap.keySet()) {
+        // Check if the key is not in ssSources
+        if (!ssSources.contains(key)) {
+            showSS.remove(checkboxMap.get(key));            
+            checkboxMap.remove(key);
+            selectedCheckBoxes.remove(key);
+        }
+        if(selectedCheckBoxes.contains(key)){
+          checkboxMap.get(key).setSelected(true);
+        }
+        else {
+          checkboxMap.get(key).setSelected(false);
+        }        
+
+        ssButtonGroup.clearSelection();
+    }
+
+  }
+  
+  private List<String> getSelectedOptions(Map<String, JCheckBoxMenuItem> checkboxMap) {
+    List<String> selectedOptions = new ArrayList<>();
+    for (String key : checkboxMap.keySet()) {
+        JCheckBoxMenuItem checkbox = checkboxMap.get(key);
+        if (checkbox.isSelected()) {
+            selectedOptions.add(key);
+        }
+    }
+    return selectedOptions;
+  }
+  
+  private Map<String, JCheckBoxMenuItem> getCheckboxesInMenu(JMenu menu) {
+    Map<String, JCheckBoxMenuItem> checkboxMap = new HashMap<>();
+    for (Component component : menu.getMenuComponents()) {
+        if (component instanceof JCheckBoxMenuItem) {
+            JCheckBoxMenuItem checkbox = (JCheckBoxMenuItem) component;
+            checkboxMap.put(checkbox.getText(), checkbox);
+        }
+    }
+    return checkboxMap;
+}
+  
+  @Override
+  protected void showOrHideSecondaryStructureForSource(String ssSourceSelection, boolean visible){
+    
+    String noneOption = MessageManager.getString("option.ss_providers_none");
+    String allOption = MessageManager.getString("option.ss_providers_all");
+    
+    AlignmentAnnotation[] annotations = alignPanel.getAlignment()
+            .getAlignmentAnnotation();
+    
+    for (AlignmentAnnotation aa: annotations) {
+      
+      boolean isSSConsensus = aa.label.startsWith(MessageManager.getString("label.ssconsensus_label"));
+      boolean matchesSSSourceSelection = aa.description.startsWith(ssSourceSelection);
+      
+      if(isSSConsensus && (matchesSSSourceSelection || ssSourceSelection.equals(noneOption))) {
+        
+        if (ssSourceSelection.equals(allOption)) {
+          aa.visible  = true;
+          break;
+        }
+        
+        if(!aa.description.startsWith(allOption))
+          aa.visible  = visible;
+
+      }
+      
+      for (String label : Constants.SECONDARY_STRUCTURE_LABELS.keySet()) {
+        
+        if (label.equals(aa.label)) { 
+
+            String ssSource = AlignmentUtils.extractSSSourceFromAnnotationDescription(aa);
+
+            if(ssSource != null && (ssSource.equals(ssSourceSelection) || ssSourceSelection.equals(noneOption))) {
+              aa.visible = visible;
+            }           
+        }
+      }
+      
+    }
+    
+      PaintRefresher.Refresh(this, viewport.getSequenceSetId());
+      alignPanel.updateAnnotation();
+      alignPanel.paintAlignment(true, true);
+    
+  }
+  
+  protected void showSSConsensus_actionPerformed(ActionEvent e)
+  {
+    viewport.setShowSSConsensus(showSSConsensus.getState());
+    alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
+    
+  }
+
 
   /*
    * (non-Javadoc)
@@ -5686,6 +5825,13 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
 
   }
 
+  @Override
+  protected void showGroupSSConsensus_actionPerformed(ActionEvent e)
+  {
+    viewport.setShowGroupSSConsensus(showGroupSSConsensus.getState());
+    alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
+
+  }
   /*
    * (non-Javadoc)
    * 
@@ -5713,7 +5859,7 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     viewport.setShowConsensusHistogram(showConsensusHistogram.getState());
     alignPanel.updateAnnotation(applyAutoAnnotationSettings.getState());
   }
-
+  
   /*
    * (non-Javadoc)
    *