*/
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
* 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
{
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()
@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()