2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.api.AlignmentViewPanel;
24 import jalview.util.MessageManager;
25 import jalview.util.Platform;
27 import java.awt.Component;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.event.ItemEvent;
31 import java.awt.event.ItemListener;
32 import java.awt.event.MouseAdapter;
33 import java.awt.event.MouseEvent;
34 import java.util.List;
36 import javax.swing.JCheckBoxMenuItem;
37 import javax.swing.JMenu;
38 import javax.swing.JMenuItem;
39 import javax.swing.event.MenuEvent;
40 import javax.swing.event.MenuListener;
43 * this is an implementation of an abstract Jalview GUI class that provides a
44 * dialog/menu which allows the user to select/deselect specific views from a
45 * list of associated views.
47 * Includes patches related to JAL-641
52 public class ViewSelectionMenu extends JMenu
54 public interface ViewSetProvider
56 public AlignmentPanel[] getAllAlignmentPanels();
59 private ViewSetProvider _allviews;
61 private List<AlignmentViewPanel> _selectedviews;
63 private ItemListener _handler;
66 * create a new view selection menu. This menu has some standard entries
67 * (select all, invert selection), and a checkbox for every view. Mousing over
68 * a view entry will cause it to be raised/selected in the Desktop, allowing
69 * the user to easily identify which view is being referred to.
74 * all the views that might be selected
75 * @param selectedviews
76 * the list of selected views which will be updated when
77 * selection/deselections occur
79 * a handler called for each selection/deselection - use this to
80 * update any gui elements which need to reflect current
81 * selection/deselection state
83 public ViewSelectionMenu(String title, final ViewSetProvider allviews,
84 final List<AlignmentViewPanel> selectedviews,
85 final ItemListener handler)
88 this._allviews = allviews;
89 this._selectedviews = selectedviews;
90 this._handler = handler;
91 addMenuListener(new MenuListener()
95 public void menuSelected(MenuEvent e)
102 public void menuDeselected(MenuEvent e)
104 // TODO Auto-generated method stub
109 public void menuCanceled(MenuEvent e)
111 // TODO Auto-generated method stub
118 * view selection modifier flag - indicates if an action key is pressed when
119 * menu selection event occurred.
121 private boolean append = false;
124 * flag indicating if the itemStateChanged listener for view associated menu
125 * items is currently enabled
127 private boolean enabled = true;
129 private JMenuItem selectAll, invertSel;
131 private JCheckBoxMenuItem toggleview = null;
133 private void rebuild()
136 AlignmentPanel[] allviews = _allviews.getAllAlignmentPanels();
137 if (allviews == null)
142 if (allviews.length >= 2)
144 // ensure we update menu state to reflect external selection list state
145 append = append || _selectedviews.size() > 1;
146 toggleview = new JCheckBoxMenuItem(
147 MessageManager.getString("label.select_many_views"), append);
148 toggleview.setToolTipText(
149 MessageManager.getString("label.toggle_enabled_views"));
150 toggleview.addItemListener(new ItemListener()
154 public void itemStateChanged(ItemEvent arg0)
159 selectAll.setEnabled(append);
160 invertSel.setEnabled(append);
167 add(selectAll = new JMenuItem(
168 MessageManager.getString("label.select_all_views")));
169 selectAll.addActionListener(new ActionListener()
173 public void actionPerformed(ActionEvent e)
175 for (Component c : getMenuComponents())
179 if (c instanceof JCheckBoxMenuItem)
181 if (toggleview != c && !((JCheckBoxMenuItem) c).isSelected())
183 ((JCheckBoxMenuItem) c).doClick();
190 add(invertSel = new JMenuItem(
191 MessageManager.getString("label.invert_selection")));
192 invertSel.addActionListener(new ActionListener()
196 public void actionPerformed(ActionEvent e)
200 for (Component c : getMenuComponents())
202 if (toggleview != c && c instanceof JCheckBoxMenuItem)
204 ((JCheckBoxMenuItem) c).doClick();
210 invertSel.setEnabled(append);
211 selectAll.setEnabled(append);
213 for (final AlignmentPanel ap : allviews)
215 String nm = ((ap.getViewName() == null
216 || ap.getViewName().length() == 0) ? ""
217 : ap.getViewName() + " for ")
218 + ap.alignFrame.getTitle();
219 final JCheckBoxMenuItem checkBox = new JCheckBoxMenuItem(nm,
220 _selectedviews.contains(ap));
221 checkBox.addItemListener(new ItemListener()
224 public void itemStateChanged(ItemEvent e)
231 // toggle the inclusion state
232 if (_selectedviews.indexOf(ap) == -1)
234 _selectedviews.add(ap);
235 checkBox.setSelected(true);
239 _selectedviews.remove(ap);
240 checkBox.setSelected(false);
243 _handler.itemStateChanged(e);
247 // Deselect everything and select this item only
248 _selectedviews.clear();
249 _selectedviews.add(ap);
251 for (Component c : getMenuComponents())
253 if (c instanceof JCheckBoxMenuItem)
255 ((JCheckBoxMenuItem) c).setSelected(checkBox == c);
259 // only fire event if we weren't selected before
260 _handler.itemStateChanged(e);
265 final ViewSelectionMenu us = this;
266 checkBox.addMouseListener(new MouseAdapter()
269 public void mouseExited(MouseEvent e)
273 } catch (Exception ex)
279 public void mouseEntered(MouseEvent e)
283 ap.setAlignFrameView();
284 } catch (Exception ex)