2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.util.MessageManager;
25 import java.awt.Component;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.awt.event.ItemEvent;
29 import java.awt.event.ItemListener;
30 import java.awt.event.MouseEvent;
31 import java.awt.event.MouseListener;
32 import java.util.List;
34 import javax.swing.JCheckBoxMenuItem;
35 import javax.swing.JMenu;
36 import javax.swing.JMenuItem;
37 import javax.swing.event.MenuEvent;
38 import javax.swing.event.MenuListener;
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("Select many views", append);
155 .setToolTipText(MessageManager.getString("label.toggle_enabled_views"));
156 toggleview.addItemListener(new ItemListener()
160 public void itemStateChanged(ItemEvent arg0)
165 selectAll.setEnabled(append);
166 invertSel.setEnabled(append);
173 add(selectAll = new JMenuItem(MessageManager.getString("label.select_all_views")));
174 selectAll.addActionListener(new ActionListener()
178 public void actionPerformed(ActionEvent e)
180 for (Component c : getMenuComponents())
184 if (c instanceof JCheckBoxMenuItem)
186 if (toggleview != c && !((JCheckBoxMenuItem) c).isSelected())
188 ((JCheckBoxMenuItem) c).doClick();
195 add(invertSel = new JMenuItem(MessageManager.getString("label.invert_selection")));
196 invertSel.addActionListener(new ActionListener()
200 public void actionPerformed(ActionEvent e)
204 for (Component c : getMenuComponents())
206 if (toggleview != c && c instanceof JCheckBoxMenuItem)
208 ((JCheckBoxMenuItem) c).doClick();
214 invertSel.setEnabled(append);
215 selectAll.setEnabled(append);
217 for (AlignmentPanel ap : allviews)
219 String nm = ((ap.getViewName() == null || ap.getViewName().length() == 0) ? ""
220 : ap.getViewName() + " for ")
221 + ap.alignFrame.getTitle();
222 final JCheckBoxMenuItem a = new JCheckBoxMenuItem(nm,
223 _selectedviews.contains(ap));
224 final AlignmentPanel p = ap;
225 a.addItemListener(new ItemListener()
228 public void itemStateChanged(ItemEvent e)
235 // toggle the inclusion state
236 if (_selectedviews.indexOf(p) == -1)
238 _selectedviews.add(p);
243 _selectedviews.remove(p);
244 a.setSelected(false);
247 _handler.itemStateChanged(e);
251 // Deselect everything and select this item only
252 _selectedviews.clear();
253 _selectedviews.add(p);
255 for (Component c : getMenuComponents())
257 if (c instanceof JCheckBoxMenuItem)
259 ((JCheckBoxMenuItem) c).setSelected(a == c);
263 // only fire event if we weren't selected before
264 _handler.itemStateChanged(e);
269 a.addMouseListener(new MouseListener()
273 public void mouseReleased(MouseEvent e)
275 // TODO Auto-generated method stub
280 public void mousePressed(MouseEvent e)
282 // TODO Auto-generated method stub
287 public void mouseExited(MouseEvent e)
291 p.setSelected(false);
292 } catch (Exception ex)
299 public void mouseEntered(MouseEvent e)
305 } catch (Exception ex)
312 public void mouseClicked(MouseEvent e)