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 java.awt.Component;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26 import java.awt.event.ItemEvent;
27 import java.awt.event.ItemListener;
28 import java.awt.event.MouseAdapter;
29 import java.awt.event.MouseEvent;
30 import java.util.List;
32 import javax.swing.JCheckBoxMenuItem;
33 import javax.swing.JMenu;
34 import javax.swing.JMenuItem;
35 import javax.swing.event.MenuEvent;
36 import javax.swing.event.MenuListener;
38 import jalview.util.MessageManager;
41 * this is an implementation of an abstract Jalview GUI class that provides a
42 * dialog/menu which allows the user to select/deselect specific views from a
43 * list of associated views.
45 * Includes patches related to JAL-641
50 public class ViewSelectionMenu extends JMenu
52 public interface ViewSetProvider
54 public AlignmentPanel[] getAllAlignmentPanels();
57 private ViewSetProvider _allviews;
59 private List<AlignmentPanel> _selectedviews;
61 private ItemListener _handler;
64 protected void finalize() throws Throwable
66 _selectedviews = null;
73 * create a new view selection menu. This menu has some standard entries
74 * (select all, invert selection), and a checkbox for every view. Mousing over
75 * a view entry will cause it to be raised/selected in the Desktop, allowing
76 * the user to easily identify which view is being referred to.
81 * all the views that might be selected
82 * @param selectedviews
83 * the list of selected views which will be updated when
84 * selection/deselections occur
86 * a handler called for each selection/deselection - use this to
87 * update any gui elements which need to reflect current
88 * selection/deselection state
90 public ViewSelectionMenu(String title, final ViewSetProvider allviews,
91 final List<AlignmentPanel> selectedviews,
92 final ItemListener handler)
95 this._allviews = allviews;
96 this._selectedviews = selectedviews;
97 this._handler = handler;
98 addMenuListener(new MenuListener()
102 public void menuSelected(MenuEvent e)
109 public void menuDeselected(MenuEvent e)
111 // TODO Auto-generated method stub
116 public void menuCanceled(MenuEvent e)
118 // TODO Auto-generated method stub
125 * view selection modifier flag - indicates if an action key is pressed when
126 * menu selection event occurred.
128 private boolean append = false;
131 * flag indicating if the itemStateChanged listener for view associated menu
132 * items is currently enabled
134 private boolean enabled = true;
136 private JMenuItem selectAll, invertSel;
138 private JCheckBoxMenuItem toggleview = null;
140 private void rebuild()
143 AlignmentPanel[] allviews = _allviews.getAllAlignmentPanels();
144 if (allviews == null)
149 if (allviews.length >= 2)
151 // ensure we update menu state to reflect external selection list state
152 append = append || _selectedviews.size() > 1;
153 toggleview = new JCheckBoxMenuItem(
154 MessageManager.getString("label.select_many_views"), append);
155 toggleview.setToolTipText(MessageManager
156 .getString("label.toggle_enabled_views"));
157 toggleview.addItemListener(new ItemListener()
161 public void itemStateChanged(ItemEvent arg0)
166 selectAll.setEnabled(append);
167 invertSel.setEnabled(append);
174 add(selectAll = new JMenuItem(
175 MessageManager.getString("label.select_all_views")));
176 selectAll.addActionListener(new ActionListener()
180 public void actionPerformed(ActionEvent e)
182 for (Component c : getMenuComponents())
186 if (c instanceof JCheckBoxMenuItem)
188 if (toggleview != c && !((JCheckBoxMenuItem) c).isSelected())
190 ((JCheckBoxMenuItem) c).doClick();
197 add(invertSel = new JMenuItem(
198 MessageManager.getString("label.invert_selection")));
199 invertSel.addActionListener(new ActionListener()
203 public void actionPerformed(ActionEvent e)
207 for (Component c : getMenuComponents())
209 if (toggleview != c && c instanceof JCheckBoxMenuItem)
211 ((JCheckBoxMenuItem) c).doClick();
217 invertSel.setEnabled(append);
218 selectAll.setEnabled(append);
220 for (final AlignmentPanel ap : allviews)
222 String nm = ((ap.getViewName() == null || ap.getViewName().length() == 0) ? ""
223 : ap.getViewName() + " for ")
224 + ap.alignFrame.getTitle();
225 final JCheckBoxMenuItem checkBox = new JCheckBoxMenuItem(nm,
226 _selectedviews.contains(ap));
227 checkBox.addItemListener(new ItemListener()
230 public void itemStateChanged(ItemEvent e)
237 // toggle the inclusion state
238 if (_selectedviews.indexOf(ap) == -1)
240 _selectedviews.add(ap);
241 checkBox.setSelected(true);
245 _selectedviews.remove(ap);
246 checkBox.setSelected(false);
249 _handler.itemStateChanged(e);
253 // Deselect everything and select this item only
254 _selectedviews.clear();
255 _selectedviews.add(ap);
257 for (Component c : getMenuComponents())
259 if (c instanceof JCheckBoxMenuItem)
261 ((JCheckBoxMenuItem) c).setSelected(checkBox == c);
265 // only fire event if we weren't selected before
266 _handler.itemStateChanged(e);
271 checkBox.addMouseListener(new MouseAdapter()
274 public void mouseExited(MouseEvent e)
278 ap.setSelected(false);
279 } catch (Exception ex)
285 public void mouseEntered(MouseEvent e)
289 ap.setSelected(true);
290 } catch (Exception ex)