From e0edfabf5128547419e2fd192e98c74fd0f81faa Mon Sep 17 00:00:00 2001 From: jprocter Date: Thu, 21 Apr 2011 16:54:09 +0000 Subject: [PATCH] patch for JAL-733 --- src/jalview/gui/ViewSelectionMenu.java | 124 ++++++++++++++++++++++++-------- 1 file changed, 95 insertions(+), 29 deletions(-) diff --git a/src/jalview/gui/ViewSelectionMenu.java b/src/jalview/gui/ViewSelectionMenu.java index 5cad55d..b3ac405 100644 --- a/src/jalview/gui/ViewSelectionMenu.java +++ b/src/jalview/gui/ViewSelectionMenu.java @@ -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 _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 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() -- 1.7.10.2