Clover icon

Coverage Report

  1. Project Clover database Fri Dec 6 2024 13:47:14 GMT
  2. Package jalview.gui

File ViewSelectionMenu.java

 

Coverage histogram

../../img/srcFileCovDistChart1.png
56% of files have more coverage

Code metrics

22
68
11
2
293
205
27
0.4
6.18
5.5
2.45

Classes

Class Line # Actions
ViewSelectionMenu 52 68 27
0.059405945.9%
ViewSelectionMenu.ViewSetProvider 54 0 0
-1.0 -
 

Contributing tests

This file is covered by 180 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.gui;
22   
23    import jalview.api.AlignmentViewPanel;
24    import jalview.util.MessageManager;
25    import jalview.util.Platform;
26   
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;
35   
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;
41   
42    /**
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.
46    *
47    * Includes patches related to JAL-641
48    *
49    * @author JimP
50    *
51    */
 
52    public class ViewSelectionMenu extends JMenu
53    {
 
54    public interface ViewSetProvider
55    {
56    public AlignmentPanel[] getAllAlignmentPanels();
57    }
58   
59    private ViewSetProvider _allviews;
60   
61    private List<AlignmentViewPanel> _selectedviews;
62   
63    private ItemListener _handler;
64   
65    /**
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.
70    *
71    * @param title
72    * Name of menu
73    * @param allviews
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
78    * @param handler
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
82    */
 
83  542 toggle public ViewSelectionMenu(String title, final ViewSetProvider allviews,
84    final List<AlignmentViewPanel> selectedviews,
85    final ItemListener handler)
86    {
87  542 super(title);
88  542 this._allviews = allviews;
89  542 this._selectedviews = selectedviews;
90  542 this._handler = handler;
91  542 addMenuListener(new MenuListener()
92    {
93   
 
94  0 toggle @Override
95    public void menuSelected(MenuEvent e)
96    {
97  0 rebuild();
98   
99    }
100   
 
101  0 toggle @Override
102    public void menuDeselected(MenuEvent e)
103    {
104    // TODO Auto-generated method stub
105   
106    }
107   
 
108  0 toggle @Override
109    public void menuCanceled(MenuEvent e)
110    {
111    // TODO Auto-generated method stub
112   
113    }
114    });
115    }
116   
117    /**
118    * view selection modifier flag - indicates if an action key is pressed when
119    * menu selection event occurred.
120    */
121    private boolean append = false;
122   
123    /**
124    * flag indicating if the itemStateChanged listener for view associated menu
125    * items is currently enabled
126    */
127    private boolean enabled = true;
128   
129    private JMenuItem selectAll, invertSel;
130   
131    private JCheckBoxMenuItem toggleview = null;
132   
 
133  0 toggle private void rebuild()
134    {
135  0 removeAll();
136  0 AlignmentPanel[] allviews = _allviews.getAllAlignmentPanels();
137  0 if (allviews == null)
138    {
139  0 setVisible(false);
140  0 return;
141    }
142  0 if (allviews.length >= 2)
143    {
144    // ensure we update menu state to reflect external selection list state
145  0 append = append || _selectedviews.size() > 1;
146  0 toggleview = new JCheckBoxMenuItem(
147    MessageManager.getString("label.select_many_views"), append);
148  0 toggleview.setToolTipText(
149    MessageManager.getString("label.toggle_enabled_views"));
150  0 toggleview.addItemListener(new ItemListener()
151    {
152   
 
153  0 toggle @Override
154    public void itemStateChanged(ItemEvent arg0)
155    {
156  0 if (enabled)
157    {
158  0 append = !append;
159  0 selectAll.setEnabled(append);
160  0 invertSel.setEnabled(append);
161    }
162   
163    }
164   
165    });
166  0 add(toggleview);
167  0 add(selectAll = new JMenuItem(
168    MessageManager.getString("label.select_all_views")));
169  0 selectAll.addActionListener(new ActionListener()
170    {
171   
 
172  0 toggle @Override
173    public void actionPerformed(ActionEvent e)
174    {
175  0 for (Component c : getMenuComponents())
176    {
177  0 boolean t = append;
178  0 append = true;
179  0 if (c instanceof JCheckBoxMenuItem)
180    {
181  0 if (toggleview != c && !((JCheckBoxMenuItem) c).isSelected())
182    {
183  0 ((JCheckBoxMenuItem) c).doClick();
184    }
185    }
186  0 append = t;
187    }
188    }
189    });
190  0 add(invertSel = new JMenuItem(
191    MessageManager.getString("label.invert_selection")));
192  0 invertSel.addActionListener(new ActionListener()
193    {
194   
 
195  0 toggle @Override
196    public void actionPerformed(ActionEvent e)
197    {
198  0 boolean t = append;
199  0 append = true;
200  0 for (Component c : getMenuComponents())
201    {
202  0 if (toggleview != c && c instanceof JCheckBoxMenuItem)
203    {
204  0 ((JCheckBoxMenuItem) c).doClick();
205    }
206    }
207  0 append = t;
208    }
209    });
210  0 invertSel.setEnabled(append);
211  0 selectAll.setEnabled(append);
212    }
213  0 for (final AlignmentPanel ap : allviews)
214    {
215  0 String nm = ((ap.getViewName() == null
216    || ap.getViewName().length() == 0) ? ""
217    : ap.getViewName() + " for ")
218    + ap.alignFrame.getTitle();
219  0 final JCheckBoxMenuItem checkBox = new JCheckBoxMenuItem(nm,
220    _selectedviews.contains(ap));
221  0 checkBox.addItemListener(new ItemListener()
222    {
 
223  0 toggle @Override
224    public void itemStateChanged(ItemEvent e)
225    {
226  0 if (enabled)
227    {
228  0 if (append)
229    {
230  0 enabled = false;
231    // toggle the inclusion state
232  0 if (_selectedviews.indexOf(ap) == -1)
233    {
234  0 _selectedviews.add(ap);
235  0 checkBox.setSelected(true);
236    }
237    else
238    {
239  0 _selectedviews.remove(ap);
240  0 checkBox.setSelected(false);
241    }
242  0 enabled = true;
243  0 _handler.itemStateChanged(e);
244    }
245    else
246    {
247    // Deselect everything and select this item only
248  0 _selectedviews.clear();
249  0 _selectedviews.add(ap);
250  0 enabled = false;
251  0 for (Component c : getMenuComponents())
252    {
253  0 if (c instanceof JCheckBoxMenuItem)
254    {
255  0 ((JCheckBoxMenuItem) c).setSelected(checkBox == c);
256    }
257    }
258  0 enabled = true;
259    // only fire event if we weren't selected before
260  0 _handler.itemStateChanged(e);
261    }
262    }
263    }
264    });
265  0 final ViewSelectionMenu us = this;
266  0 checkBox.addMouseListener(new MouseAdapter()
267    {
 
268  0 toggle @Override
269    public void mouseExited(MouseEvent e)
270    {
271  0 try
272    {
273    } catch (Exception ex)
274    {
275    }
276    }
277   
 
278  0 toggle @Override
279    public void mouseEntered(MouseEvent e)
280    {
281  0 try
282    {
283  0 ap.setAlignFrameView();
284    } catch (Exception ex)
285    {
286    }
287    }
288    });
289  0 add(checkBox);
290    }
291    }
292   
293    }