1 /*******************************************************************************
2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 *******************************************************************************/
20 import java.awt.Component;
21 import java.awt.event.ActionEvent;
22 import java.awt.event.ActionListener;
23 import java.awt.event.ItemEvent;
24 import java.awt.event.ItemListener;
25 import java.awt.event.MouseEvent;
26 import java.awt.event.MouseListener;
27 import java.util.List;
29 import javax.swing.JCheckBoxMenuItem;
30 import javax.swing.JMenu;
31 import javax.swing.JMenuItem;
32 import javax.swing.event.MenuEvent;
33 import javax.swing.event.MenuListener;
36 * this is an implementation of an abstract Jalview GUI class that provides a
37 * dialog/menu which allows the user to select/deselect specific views from a
38 * list of associated views.
40 * Includes patches related to JAL-641
45 public class ViewSelectionMenu extends JMenu
47 public interface ViewSetProvider
49 public AlignmentPanel[] getAllAlignmentPanels();
52 private ViewSetProvider _allviews;
54 private List<AlignmentPanel> _selectedviews;
56 private ItemListener _handler;
59 protected void finalize() throws Throwable
61 _selectedviews = null;
68 * create a new view selection menu. This menu has some standard entries
69 * (select all, invert selection), and a checkbox for every view. Mousing over
70 * a view entry will cause it to be raised/selected in the Desktop, allowing
71 * the user to easily identify which view is being referred to.
76 * all the views that might be selected
77 * @param selectedviews
78 * the list of selected views which will be updated when
79 * selection/deselections occur
81 * a handler called for each selection/deselection - use this to
82 * update any gui elements which need to reflect current
83 * selection/deselection state
85 public ViewSelectionMenu(String title, final ViewSetProvider allviews,
86 final List<AlignmentPanel> selectedviews,
87 final ItemListener handler)
90 this._allviews = allviews;
91 this._selectedviews = selectedviews;
92 this._handler = handler;
93 addMenuListener(new MenuListener()
97 public void menuSelected(MenuEvent e)
104 public void menuDeselected(MenuEvent e)
106 // TODO Auto-generated method stub
111 public void menuCanceled(MenuEvent e)
113 // TODO Auto-generated method stub
120 * view selection modifier flag - indicates if an action key is pressed when
121 * menu selection event occurred.
123 private boolean append = false;
126 * flag indicating if the itemStateChanged listener for view associated menu
127 * items is currently enabled
129 private boolean enabled = true;
131 private JMenuItem selectAll, invertSel;
133 private JCheckBoxMenuItem toggleview = null;
135 private void rebuild()
138 AlignmentPanel[] allviews = _allviews.getAllAlignmentPanels();
139 if (allviews == null)
144 if (allviews.length >= 2)
146 // ensure we update menu state to reflect external selection list state
147 append = append || _selectedviews.size() > 1;
148 toggleview = new JCheckBoxMenuItem("Select many views", append);
150 .setToolTipText("When enabled, allows many views to be selected.");
151 toggleview.addItemListener(new ItemListener()
155 public void itemStateChanged(ItemEvent arg0)
160 selectAll.setEnabled(append);
161 invertSel.setEnabled(append);
168 add(selectAll = new JMenuItem("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("Invert selection"));
191 invertSel.addActionListener(new ActionListener()
195 public void actionPerformed(ActionEvent e)
199 for (Component c : getMenuComponents())
201 if (toggleview != c && c instanceof JCheckBoxMenuItem)
203 ((JCheckBoxMenuItem) c).doClick();
209 invertSel.setEnabled(append);
210 selectAll.setEnabled(append);
212 for (AlignmentPanel ap : allviews)
214 String nm = ((ap.getViewName() == null || ap.getViewName().length() == 0) ? ""
215 : ap.getViewName() + " for ")
216 + ap.alignFrame.getTitle();
217 final JCheckBoxMenuItem a = new JCheckBoxMenuItem(nm,
218 _selectedviews.contains(ap));
219 final AlignmentPanel p = ap;
220 a.addItemListener(new ItemListener()
223 public void itemStateChanged(ItemEvent e)
230 // toggle the inclusion state
231 if (_selectedviews.indexOf(p) == -1)
233 _selectedviews.add(p);
238 _selectedviews.remove(p);
239 a.setSelected(false);
242 _handler.itemStateChanged(e);
246 // Deselect everything and select this item only
247 _selectedviews.clear();
248 _selectedviews.add(p);
250 for (Component c : getMenuComponents())
252 if (c instanceof JCheckBoxMenuItem)
254 ((JCheckBoxMenuItem) c).setSelected(a == c);
258 // only fire event if we weren't selected before
259 _handler.itemStateChanged(e);
264 a.addMouseListener(new MouseListener()
268 public void mouseReleased(MouseEvent e)
270 // TODO Auto-generated method stub
275 public void mousePressed(MouseEvent e)
277 // TODO Auto-generated method stub
282 public void mouseExited(MouseEvent e)
286 p.setSelected(false);
287 } catch (Exception ex)
294 public void mouseEntered(MouseEvent e)
300 } catch (Exception ex)
307 public void mouseClicked(MouseEvent e)