JAL-1264 tidy up use of MouseListener for showHideAnnotations
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 25 Sep 2014 12:48:40 +0000 (13:48 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Thu, 25 Sep 2014 12:48:40 +0000 (13:48 +0100)
src/jalview/gui/AlignFrame.java
src/jalview/jbgui/GAlignFrame.java

index 544a971..4168516 100644 (file)
@@ -334,6 +334,8 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
     setMenusFromViewport(viewport);
     buildSortByAnnotationScoresMenu();
     buildTreeMenu();
+    buildShowHideAnnotationMenus();
+    
     if (viewport.wrapAlignment)
     {
       wrapMenuItem_actionPerformed(null);
@@ -5715,6 +5717,85 @@ public class AlignFrame extends GAlignFrame implements DropTargetListener,
   {
     new AnnotationChooser(this.alignPanel);
   }
+
+  /**
+   * Get a list of unique annotation types for the alignment, optionally
+   * restricted to sequence-specific annotations.
+   */
+  protected List<String> getAnnotationTypes(boolean sequenceSpecificOnly)
+  {
+    // TODO move this to an AlignmentUtils class or similar
+    List<String> types = new ArrayList<String>();
+    for (AlignmentAnnotation aa : alignPanel.getAlignment()
+            .getAlignmentAnnotation())
+    {
+      if (sequenceSpecificOnly && aa.sequenceRef == null)
+      {
+        continue;
+      }
+      String type = aa.label;
+      if (!types.contains(type))
+      {
+        types.add(type);
+      }
+    }
+    return types;
+  }
+
+  /**
+   * Action on selection of an annotation type to Show or Hide.
+   * 
+   * @param type
+   * @param doShow
+   */
+  @Override
+  protected void showHideAnnotation_actionPerformed(String type, boolean doShow)
+  {
+    for (AlignmentAnnotation aa : alignPanel.getAlignment()
+            .getAlignmentAnnotation())
+    {
+      if (type.equals(aa.label))
+      {
+        aa.visible = doShow;
+      }
+    }
+    this.alignPanel.paintAlignment(true);
+  }
+
+  /**
+   * Dynamically build the list of annotation types to show or hide.
+   */
+  @Override
+  protected void buildShowHideAnnotationMenus()
+  {
+    showAnnotations.removeAll();
+    hideAnnotations.removeAll();
+
+    List<String> types = getAnnotationTypes(false);
+    for (final String type : types)
+    {
+      final JMenuItem showitem = new JMenuItem(type);
+      showitem.addActionListener(new java.awt.event.ActionListener()
+      {
+        @Override
+        public void actionPerformed(ActionEvent e)
+        {
+          showHideAnnotation_actionPerformed(type, true);
+        }
+      });
+      showAnnotations.add(showitem);
+      final JMenuItem hideitem = new JMenuItem(type);
+      hideitem.addActionListener(new java.awt.event.ActionListener()
+      {
+        @Override
+        public void actionPerformed(ActionEvent e)
+        {
+          showHideAnnotation_actionPerformed(type, false);
+        }
+      });
+      hideAnnotations.add(hideitem);
+    }
+  }
 }
 
 class PrintThread extends Thread
index 3b8568c..0fad6cf 100755 (executable)
@@ -304,6 +304,10 @@ public class GAlignFrame extends JInternalFrame
 
   JMenuItem showAllhidden = new JMenuItem();
 
+  protected JMenu showAnnotations = new JMenu();
+
+  protected JMenu hideAnnotations = new JMenu();
+
   protected JCheckBoxMenuItem hiddenMarkers = new JCheckBoxMenuItem();
 
   JMenuItem invertColSel = new JMenuItem();
@@ -384,6 +388,7 @@ public class GAlignFrame extends JInternalFrame
       }
     } catch (Exception e)
     {
+      System.err.println(e.toString());
     }
 
     if (!new jalview.util.Platform().isAMac())
@@ -1921,6 +1926,24 @@ public class GAlignFrame extends JInternalFrame
         showAllhidden_actionPerformed(e);
       }
     });
+    showAnnotations.setText(MessageManager.getString("label.annotations"));
+    showAnnotations.addMouseListener(new MouseAdapter()
+    {
+      @Override
+      public void mouseEntered(MouseEvent e)
+      {
+        buildShowHideAnnotationMenus();
+      }
+    });
+    hideAnnotations.setText(MessageManager.getString("label.annotations"));
+    hideAnnotations.addMouseListener(new MouseAdapter()
+    {
+      @Override
+      public void mouseEntered(MouseEvent e)
+      {
+        buildShowHideAnnotationMenus();
+      }
+    });
 
     hiddenMarkers.setText(MessageManager
             .getString("action.show_hidden_markers"));
@@ -2215,10 +2238,12 @@ public class GAlignFrame extends JInternalFrame
     jMenu3.add(showAllColumns);
     jMenu3.add(showAllSeqs);
     jMenu3.add(showAllhidden);
+    jMenu3.add(showAnnotations);
     hideMenu.add(hideSelColumns);
     hideMenu.add(hideSelSequences);
     hideMenu.add(hideAllSelection);
     hideMenu.add(hideAllButSelection);
+    hideMenu.add(hideAnnotations);
 
     formatMenu.add(font);
     formatMenu.addSeparator();
@@ -2253,6 +2278,17 @@ public class GAlignFrame extends JInternalFrame
   }
 
   /**
+   * Dynamically build list of annotation types to show or hide.
+   */
+  protected void buildShowHideAnnotationMenus()
+  {
+  }
+
+  protected void showHideAnnotation_actionPerformed(String type, boolean b)
+  {
+  }
+
+  /**
    * Action on menu item "Show/hide sequence annotations..."
    */
   protected void chooseAnnotations_actionPerformed()