From 24f64bdbc24848b66d96e9a30f72ee92e8633a3d Mon Sep 17 00:00:00 2001 From: jprocter Date: Fri, 18 Feb 2011 17:16:00 +0000 Subject: [PATCH] dynamic alignmentPanel menu class --- src/jalview/gui/ViewSelectionMenu.java | 217 ++++++++++++++++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 src/jalview/gui/ViewSelectionMenu.java diff --git a/src/jalview/gui/ViewSelectionMenu.java b/src/jalview/gui/ViewSelectionMenu.java new file mode 100644 index 0000000..5cad55d --- /dev/null +++ b/src/jalview/gui/ViewSelectionMenu.java @@ -0,0 +1,217 @@ +package jalview.gui; + +import java.awt.Component; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.util.List; + +import javax.swing.JCheckBoxMenuItem; +import javax.swing.JMenu; +import javax.swing.JMenuItem; +import javax.swing.event.MenuEvent; +import javax.swing.event.MenuListener; + +/** + * this is an implementation of an abstract Jalview GUI class that provides a + * dialog/menu which allows the user to select/deselect specific views from a + * list of associated views. + * + * @author JimP + * + */ +public class ViewSelectionMenu extends JMenu +{ + 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; + 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 + * a view entry will cause it to be raised/selected in the Desktop, allowing + * the user to easily identify which view is being referred to. + * + * @param title + * Name of menu + * @param allviews + * all the views that might be selected + * @param selectedviews + * the list of selected views which will be updated when + * selection/deselections occur + * @param handler + * a handler called for each selection/deselection - use this to + * update any gui elements which need to reflect current + * selection/deselection state + */ + public ViewSelectionMenu(String title, + final ViewSetProvider allviews, + final List selectedviews, + final ItemListener handler) + { + super(title); + this._allviews = allviews; + this._selectedviews = selectedviews; + 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() { + removeAll(); + 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() + { + + @Override + public void actionPerformed(ActionEvent e) + { + for (Component c : getMenuComponents()) + { + if (c instanceof JCheckBoxMenuItem) + { + if (!((JCheckBoxMenuItem) c).isSelected()) + { + ((JCheckBoxMenuItem) c).doClick(); + } + } + } + } + }); + add(item = new JMenuItem("Invert selection")); + item.addActionListener(new ActionListener() + { + + @Override + public void actionPerformed(ActionEvent e) + { + for (Component c : getMenuComponents()) + { + if (c instanceof JCheckBoxMenuItem) + { + ((JCheckBoxMenuItem) c).doClick(); + } + } + } + }); + + } + for (AlignmentPanel ap : allviews) + { + String nm = ((ap.getViewName() == null || ap.getViewName().length() == 0) ? "" + : ap.getViewName() + " for ") + + ap.alignFrame.getTitle(); + 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) + { + _selectedviews.add(p); + } + else + { + _selectedviews.remove(p); + } + _handler.itemStateChanged(e); + } + }); + a.addMouseListener(new MouseListener() + { + + @Override + public void mouseReleased(MouseEvent e) + { + // TODO Auto-generated method stub + + } + + @Override + public void mousePressed(MouseEvent e) + { + // TODO Auto-generated method stub + + } + + @Override + public void mouseExited(MouseEvent e) + { + try + { + p.setSelected(false); + } catch (Exception ex) + { + } + ; + } + + @Override + public void mouseEntered(MouseEvent e) + { + + try + { + p.setSelected(true); + } catch (Exception ex) + { + } + ; + } + + @Override + public void mouseClicked(MouseEvent e) + { + } + }); + add(a); + } + } + +} -- 1.7.10.2