JAL-4386 - Implementation of retrieving secondary structure
[jalview.git] / src / jalview / gui / AlignFrame.java
index 1bf2529..ccda002 100644 (file)
@@ -61,7 +61,9 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Vector;
 
+import javax.swing.AbstractButton;
 import javax.swing.ButtonGroup;
+import javax.swing.ButtonModel;
 import javax.swing.JCheckBoxMenuItem;
 import javax.swing.JComponent;
 import javax.swing.JEditorPane;
@@ -71,6 +73,7 @@ import javax.swing.JLayeredPane;
 import javax.swing.JMenu;
 import javax.swing.JMenuItem;
 import javax.swing.JPanel;
+import javax.swing.JRadioButtonMenuItem;
 import javax.swing.JScrollPane;
 import javax.swing.SwingUtilities;
 
@@ -82,6 +85,7 @@ import jalview.analysis.Dna;
 import jalview.analysis.GeneticCodeI;
 import jalview.analysis.ParseProperties;
 import jalview.analysis.SequenceIdMatcher;
+import jalview.api.AlignCalcWorkerI;
 import jalview.api.AlignExportSettingsI;
 import jalview.api.AlignViewControllerGuiI;
 import jalview.api.AlignViewControllerI;
@@ -150,6 +154,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;
@@ -157,6 +162,7 @@ import jalview.util.Platform;
 import jalview.util.imagemaker.BitmapImageSizing;
 import jalview.viewmodel.AlignmentViewport;
 import jalview.viewmodel.ViewportRanges;
+import jalview.workers.SecondaryStructureConsensusThread;
 import jalview.ws.DBRefFetcher;
 import jalview.ws.DBRefFetcher.FetchFinishedListenerI;
 import jalview.ws.jws1.Discoverer;
@@ -5675,6 +5681,133 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     viewport.setShowUnconserved(showNonconservedMenuItem.getState());
     alignPanel.paintAlignment(false, false);
   }
+  
+  @Override
+  protected void updateShowSSRadioButtons(JMenu showSS, ButtonGroup ssButtonGroup){
+    
+    List<String> ssSources = new ArrayList<String>();
+    AlignmentAnnotation[] anns = alignPanel.getAlignment()
+            .getAlignmentAnnotation();
+    
+    ssSources = AlignmentUtils.extractSSSourceInAlignmentAnnotation(anns);
+    
+    // Get the currently selected radio button
+    String selectedButtonModelName = getSelectedRadioButtonDisplayString(ssButtonGroup);
+    boolean selectedButtonModel =  false;
+    
+    // Clear existing radio buttons
+    showSS.removeAll();
+    JRadioButtonMenuItem radioButtonAllSS = new JRadioButtonMenuItem("All");
+    radioButtonAllSS.addActionListener(new ActionListener() {
+      @Override
+      public void actionPerformed(ActionEvent e) {
+          showSS_actionPerformed("All");
+      }
+    });
+    ssButtonGroup.add(radioButtonAllSS);
+    showSS.add(radioButtonAllSS);
+    
+    JRadioButtonMenuItem radioButtonNoneSS = new JRadioButtonMenuItem("None");
+    radioButtonNoneSS.addActionListener(new ActionListener() {
+      @Override
+      public void actionPerformed(ActionEvent e) {
+          showSS_actionPerformed("None");
+      }
+    });
+    ssButtonGroup.add(radioButtonNoneSS);
+    showSS.add(radioButtonNoneSS);
+    showSS.addSeparator();
+
+       for(String ssSource : ssSources) {
+        
+        JRadioButtonMenuItem radioButton = new JRadioButtonMenuItem(ssSource);
+        radioButton.addActionListener(new ActionListener() {
+          @Override
+          public void actionPerformed(ActionEvent e) {
+              showSS_actionPerformed(ssSource);
+          }
+        });
+        ssButtonGroup.add(radioButton);
+        showSS.add(radioButton);
+        
+        // Check if this radio button's name matches the selected radio button's name
+        if (ssSource.equals(selectedButtonModelName)) {
+            radioButton.setSelected(true); // Select this radio button
+            selectedButtonModel = true;
+        }
+        
+      } 
+
+       if (selectedButtonModelName == "None") {
+         // If no radio button was previously selected, select "All"
+         ssButtonGroup.setSelected(radioButtonNoneSS.getModel(), true);
+         selectedButtonModel = true;
+       }
+       if (!selectedButtonModel) {
+           // If no radio button was previously selected, select "All"
+           ssButtonGroup.setSelected(radioButtonAllSS.getModel(), true);
+       }
+  }
+  
+  @Override
+  protected void showSS_actionPerformed(String ssSourceSelection){
+    
+
+    AlignmentAnnotation[] annotations = alignPanel.getAlignment()
+            .getAlignmentAnnotation();
+    
+    for (AlignmentAnnotation aa: annotations) {
+      
+      for (String label : Constants.SECONDARY_STRUCTURE_LABELS.keySet()) {
+        
+        if (label.equals(aa.label)) { 
+          
+          aa.visible = false;
+          
+          if(ssSourceSelection == "All") {
+            aa.visible = true;
+          }
+          
+          else {
+            String ssSource = AlignmentUtils.extractSSSourceFromAnnotationDescription(aa);
+            if(ssSource.equals(ssSourceSelection)) {
+              aa.visible = true;
+            }
+            
+            
+          }
+        }
+      }
+      
+    }
+    
+      
+      List<AlignCalcWorkerI> workers = viewport.getCalcManager()
+              .getRegisteredWorkersOfClass(SecondaryStructureConsensusThread.class);
+      if (!workers.isEmpty()) {
+          
+        viewport.getCalcManager().startWorker(workers.remove(0));
+
+      }
+      
+      PaintRefresher.Refresh(this, viewport.getSequenceSetId());
+      alignPanel.updateAnnotation();
+      alignPanel.paintAlignment(true, true);
+          
+    
+    
+  }
+  
+  protected String getSelectedRadioButtonDisplayString(ButtonGroup ssButtonGroup) {
+    Enumeration<AbstractButton> buttons = ssButtonGroup.getElements();
+    while (buttons.hasMoreElements()) {
+        AbstractButton button = buttons.nextElement();
+        if (button.isSelected()) {
+            return button.getText();
+        }
+    }
+    return null; // No radio button is selected
+}
 
   /*
    * (non-Javadoc)