patch for JAL-733
authorjprocter <Jim Procter>
Thu, 21 Apr 2011 16:54:09 +0000 (16:54 +0000)
committerjprocter <Jim Procter>
Thu, 21 Apr 2011 16:54:09 +0000 (16:54 +0000)
src/jalview/gui/ViewSelectionMenu.java

index 5cad55d..b3ac405 100644 (file)
@@ -25,20 +25,26 @@ import javax.swing.event.MenuListener;
  */
 public class ViewSelectionMenu extends JMenu
 {
-  public interface ViewSetProvider {
+  public interface ViewSetProvider
+  {
     public AlignmentPanel[] getAllAlignmentPanels();
   }
+
   private ViewSetProvider _allviews;
+
   private List<AlignmentPanel> _selectedviews;
+
   private ItemListener _handler;
+
   @Override
   protected void finalize() throws Throwable
   {
-    _selectedviews=null;
-    _handler=null;
-    _allviews=null;
+    _selectedviews = null;
+    _handler = null;
+    _allviews = null;
     super.finalize();
   }
+
   /**
    * create a new view selection menu. This menu has some standard entries
    * (select all, invert selection), and a checkbox for every view. Mousing over
@@ -57,53 +63,83 @@ public class ViewSelectionMenu extends JMenu
    *          update any gui elements which need to reflect current
    *          selection/deselection state
    */
-  public ViewSelectionMenu(String title,
-          final ViewSetProvider allviews,
+  public ViewSelectionMenu(String title, final ViewSetProvider allviews,
           final List<AlignmentPanel> selectedviews,
           final ItemListener handler)
   {
     super(title);
     this._allviews = allviews;
     this._selectedviews = selectedviews;
-    this._handler=handler;
+    this._handler = handler;
     addMenuListener(new MenuListener()
     {
-      
+
       @Override
       public void menuSelected(MenuEvent e)
       {
         rebuild();
-        
+
       }
-      
+
       @Override
       public void menuDeselected(MenuEvent e)
       {
         // TODO Auto-generated method stub
-        
+
       }
-      
+
       @Override
       public void menuCanceled(MenuEvent e)
       {
         // TODO Auto-generated method stub
-        
+
       }
     });
   }
-  private void rebuild() {
+
+  /**
+   * view selection modifier flag - indicates if an action key is pressed when
+   * menu selection event occurred.
+   */
+  private boolean append = false;
+  /**
+   * flag indicating if the itemStateChanged listener for view associated menu items is currently enabled 
+   */
+  private boolean enabled = true;
+  private JMenuItem selectAll, invertSel;
+  private JCheckBoxMenuItem toggleview=null;
+  private void rebuild()
+  {
     removeAll();
-    AlignmentPanel[] allviews=_allviews.getAllAlignmentPanels();
-    if (allviews==null)
+    AlignmentPanel[] allviews = _allviews.getAllAlignmentPanels();
+    if (allviews == null)
     {
       setVisible(false);
       return;
     }
     if (allviews.length >= 2)
     {
-      JMenuItem item;
-      add(item = new JMenuItem("Select all views"));
-      item.addActionListener(new ActionListener()
+      
+      toggleview = new JCheckBoxMenuItem("Select many views",append);
+      toggleview.setToolTipText("When selected, toggles whether a particular view is used to colour the alignment.");
+      toggleview.addItemListener(new ItemListener() {
+
+        @Override
+        public void itemStateChanged(ItemEvent arg0)
+        {
+          if (enabled)
+          {
+            append = !append;
+            selectAll.setEnabled(append);
+            invertSel.setEnabled(append);
+          }
+          
+        }
+        
+      });
+      add(toggleview);
+      add(selectAll= new JMenuItem("Select all views"));
+      selectAll.addActionListener(new ActionListener()
       {
 
         @Override
@@ -111,40 +147,47 @@ public class ViewSelectionMenu extends JMenu
         {
           for (Component c : getMenuComponents())
           {
+            boolean t=append;
+            append=true;
             if (c instanceof JCheckBoxMenuItem)
             {
-              if (!((JCheckBoxMenuItem) c).isSelected())
+              if (toggleview!=c && !((JCheckBoxMenuItem) c).isSelected())
               {
                 ((JCheckBoxMenuItem) c).doClick();
               }
             }
+            append=t;
           }
         }
       });
-      add(item = new JMenuItem("Invert selection"));
-      item.addActionListener(new ActionListener()
+      add(invertSel = new JMenuItem("Invert selection"));
+      invertSel.addActionListener(new ActionListener()
       {
 
         @Override
         public void actionPerformed(ActionEvent e)
         {
+          boolean t=append;
+          append=true;
           for (Component c : getMenuComponents())
           {
-            if (c instanceof JCheckBoxMenuItem)
+            if (toggleview!=c && c instanceof JCheckBoxMenuItem)
             {
               ((JCheckBoxMenuItem) c).doClick();
             }
           }
+          append=t;
         }
       });
-
+      invertSel.setEnabled(append);
+      selectAll.setEnabled(append);
     }
     for (AlignmentPanel ap : allviews)
     {
       String nm = ((ap.getViewName() == null || ap.getViewName().length() == 0) ? ""
               : ap.getViewName() + " for ")
               + ap.alignFrame.getTitle();
-      JCheckBoxMenuItem a = new JCheckBoxMenuItem(nm,
+      final JCheckBoxMenuItem a = new JCheckBoxMenuItem(nm,
               _selectedviews.contains(ap));
       final AlignmentPanel p = ap;
       a.addItemListener(new ItemListener()
@@ -152,15 +195,38 @@ public class ViewSelectionMenu extends JMenu
         @Override
         public void itemStateChanged(ItemEvent e)
         {
-          if (e.getStateChange() == e.SELECTED)
+          if (enabled)
           {
-            _selectedviews.add(p);
+          if (append)
+          {
+            if (e.getStateChange() == e.SELECTED)
+            {
+              _selectedviews.add(p);
+            }
+            else
+            {
+              _selectedviews.remove(p);
+            }
+            _handler.itemStateChanged(e);
           }
           else
           {
-            _selectedviews.remove(p);
+            // Deselect everything and select this item only
+            _selectedviews.clear();
+            _selectedviews.add(p);
+            enabled=false;
+            for (Component c : getMenuComponents())
+            {
+              if (c instanceof JCheckBoxMenuItem)
+              {
+                ((JCheckBoxMenuItem) c).setSelected(a == c);
+              }
+            }
+            enabled=true;
+            // only fire event if we weren't selected before
+            _handler.itemStateChanged(e);
+          }
           }
-          _handler.itemStateChanged(e);
         }
       });
       a.addMouseListener(new MouseListener()