Clover icon

Coverage Report

  1. Project Clover database Mon Nov 11 2024 16:01:40 GMT
  2. Package jalview.gui

File ColourMenuHelper.java

 

Coverage histogram

../../img/srcFileCovDistChart6.png
36% of files have more coverage

Code metrics

34
75
9
2
321
205
32
0.43
8.33
4.5
3.56

Classes

Class Line # Actions
ColourMenuHelper 42 75 32
0.550847555.1%
ColourMenuHelper.ColourChangeListener 44 0 0
-1.0 -
 

Contributing tests

This file is covered by 185 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.bin.Cache;
24    import jalview.datamodel.AnnotatedCollectionI;
25    import jalview.schemes.ColourSchemeI;
26    import jalview.schemes.ColourSchemeLoader;
27    import jalview.schemes.ColourSchemes;
28    import jalview.schemes.ResidueColourScheme;
29    import jalview.schemes.UserColourScheme;
30    import jalview.util.MessageManager;
31   
32    import java.awt.Component;
33    import java.awt.event.ActionEvent;
34    import java.awt.event.ActionListener;
35    import java.awt.event.MouseAdapter;
36    import java.awt.event.MouseEvent;
37   
38    import javax.swing.ButtonGroup;
39    import javax.swing.JMenu;
40    import javax.swing.JRadioButtonMenuItem;
41   
 
42    public class ColourMenuHelper
43    {
 
44    public interface ColourChangeListener
45    {
46    /**
47    * Change colour scheme to the selected scheme
48    *
49    * @param name
50    * the registered (unique) name of a colour scheme
51    */
52    void changeColour_actionPerformed(String name);
53    }
54   
55    /**
56    * Adds items to the colour menu, as mutually exclusive members of a button
57    * group. The callback handler is responsible for the action on selecting any
58    * of these options. The callback method receives the name of the selected
59    * colour, or "None" or "User Defined". This method returns the ButtonGroup to
60    * which items were added.
61    * <ul>
62    * <li>None</li>
63    * <li>Clustal</li>
64    * <li>...other 'built-in' colours</li>
65    * <li>...any user-defined colours</li>
66    * <li>User Defined..(only for AlignFrame menu)</li>
67    * </ul>
68    *
69    * @param colourMenu
70    * the menu to attach items to
71    * @param client
72    * a callback to handle menu selection
73    * @param coll
74    * the data the menu is being built for
75    * @param simpleOnly
76    * if true, only simple per-residue colour schemes are included
77    */
 
78  506 toggle public static ButtonGroup addMenuItems(final JMenu colourMenu,
79    final ColourChangeListener client, AnnotatedCollectionI coll,
80    boolean simpleOnly)
81    {
82    /*
83    * ButtonGroup groups those items whose
84    * selection is mutually exclusive
85    */
86  506 ButtonGroup colours = new ButtonGroup();
87   
88  506 if (!simpleOnly)
89    {
90  460 JRadioButtonMenuItem noColourmenuItem = new JRadioButtonMenuItem(
91    MessageManager.getString("label.none"));
92  460 noColourmenuItem.setName(ResidueColourScheme.NONE);
93  460 noColourmenuItem.addActionListener(new ActionListener()
94    {
 
95  0 toggle @Override
96    public void actionPerformed(ActionEvent e)
97    {
98  0 client.changeColour_actionPerformed(ResidueColourScheme.NONE);
99    }
100    });
101  460 colourMenu.add(noColourmenuItem);
102  460 colours.add(noColourmenuItem);
103    }
104   
105    /*
106    * scan registered colour schemes (built-in or user-defined)
107    * and add them to the menu (in the order they were registered)
108    */
109  506 Iterable<ColourSchemeI> colourSchemes = ColourSchemes.getInstance()
110    .getColourSchemes();
111  506 for (ColourSchemeI scheme : colourSchemes)
112    {
113  10442 if (simpleOnly && !scheme.isSimple())
114    {
115  276 continue;
116    }
117   
118    /*
119    * button text is i18n'd but the name is the canonical name of
120    * the colour scheme (inspected in setColourSelected())
121    */
122  10166 final String name = scheme.getSchemeName();
123  10166 String label = MessageManager.getStringOrReturn("label.colourScheme_",
124    name);
125  10166 final JRadioButtonMenuItem radioItem = new JRadioButtonMenuItem(
126    label);
127  10166 radioItem.setName(name);
128  10166 radioItem.setEnabled(scheme.isApplicableTo(coll));
129  10166 if (scheme instanceof UserColourScheme)
130    {
131    /*
132    * user-defined colour scheme loaded on startup or during the
133    * Jalview session; right-click on this offers the option to
134    * remove it as a colour choice (unless currently selected)
135    */
136  0 radioItem.addMouseListener(new MouseAdapter()
137    {
 
138  0 toggle @Override
139    public void mousePressed(MouseEvent evt)
140    {
141  0 if (evt.isPopupTrigger() && !radioItem.isSelected()) // Mac
142    {
143  0 offerRemoval();
144    }
145    }
146   
 
147  0 toggle @Override
148    public void mouseReleased(MouseEvent evt)
149    {
150  0 if (evt.isPopupTrigger() && !radioItem.isSelected()) // Windows
151    {
152  0 offerRemoval();
153    }
154    }
155   
 
156  0 toggle void offerRemoval()
157    {
158  0 ActionListener al = radioItem.getActionListeners()[0];
159  0 radioItem.removeActionListener(al);
160  0 int option = JvOptionPane.showInternalConfirmDialog(
161    Desktop.desktop,
162    MessageManager
163    .getString("label.remove_from_default_list"),
164    MessageManager
165    .getString("label.remove_user_defined_colour"),
166    JvOptionPane.YES_NO_OPTION);
167  0 if (option == JvOptionPane.YES_OPTION)
168    {
169  0 ColourSchemes.getInstance()
170    .removeColourScheme(radioItem.getName());
171  0 colourMenu.remove(radioItem);
172  0 updatePreferences();
173    }
174    else
175    {
176  0 radioItem.addActionListener(al);
177    }
178    }
179    });
180    }
181  10166 radioItem.addActionListener(new ActionListener()
182    {
 
183  0 toggle @Override
184    public void actionPerformed(ActionEvent evt)
185    {
186  0 client.changeColour_actionPerformed(name);
187    }
188    });
189  10166 colourMenu.add(radioItem);
190  10166 colours.add(radioItem);
191    }
192   
193    /*
194    * only add the option to load/configure a user-defined colour
195    * to the AlignFrame colour menu
196    */
197  506 if (client instanceof AlignFrame)
198    {
199  454 final String label = MessageManager.getString("action.user_defined");
200  454 JRadioButtonMenuItem userDefinedColour = new JRadioButtonMenuItem(
201    label);
202  454 userDefinedColour.setName(ResidueColourScheme.USER_DEFINED_MENU);
203  454 userDefinedColour.addActionListener(new ActionListener()
204    {
 
205  0 toggle @Override
206    public void actionPerformed(ActionEvent e)
207    {
208  0 client.changeColour_actionPerformed(
209    ResidueColourScheme.USER_DEFINED_MENU);
210    }
211    });
212  454 colourMenu.add(userDefinedColour);
213  454 colours.add(userDefinedColour);
214    }
215   
216  506 return colours;
217    }
218   
219    /**
220    * Marks as selected the colour menu item matching the given colour scheme, or
221    * the first item ('None') if no match is found. If the colour scheme is a
222    * user defined scheme, but not in the menu (this arises if a new scheme is
223    * defined and applied but not saved to file), then menu option "User
224    * Defined.." is selected.
225    *
226    * @param colourMenu
227    * @param cs
228    */
 
229  1307 toggle public static void setColourSelected(JMenu colourMenu, ColourSchemeI cs)
230    {
231  1307 String colourName = cs == null ? ResidueColourScheme.NONE
232    : cs.getSchemeName();
233   
234  1307 JRadioButtonMenuItem none = null;
235  1307 JRadioButtonMenuItem userDefined = null;
236   
237    /*
238    * select the radio button whose name matches the colour name
239    * (not the button text, as it may be internationalised)
240    */
241  1307 for (Component menuItem : colourMenu.getMenuComponents())
242    {
243  4305 if (menuItem instanceof JRadioButtonMenuItem)
244    {
245  1759 JRadioButtonMenuItem radioButton = (JRadioButtonMenuItem) menuItem;
246  1759 String buttonName = radioButton.getName();
247  1759 if (buttonName.equals(colourName))
248    {
249  850 radioButton.setSelected(true);
250  850 return;
251    }
252  909 if (ResidueColourScheme.NONE.equals(buttonName))
253    {
254  82 none = radioButton;
255    }
256  909 if (ResidueColourScheme.USER_DEFINED_MENU.equals(buttonName))
257    {
258  8 userDefined = radioButton;
259    }
260    }
261    }
262   
263    /*
264    * no match by name; select User Defined.. if current scheme is a
265    * user defined one, else select None
266    */
267  457 if (cs instanceof UserColourScheme && userDefined != null)
268    {
269  0 userDefined.setSelected(true);
270    }
271  457 else if (none != null)
272    {
273  0 none.setSelected(true);
274    }
275    }
276   
277    /**
278    * Updates the USER_DEFINE_COLOURS preference to remove any de-registered
279    * colour scheme
280    */
 
281  0 toggle static void updatePreferences()
282    {
283  0 StringBuilder coloursFound = new StringBuilder();
284  0 String[] files = Cache.getProperty("USER_DEFINED_COLOURS").split("\\|");
285   
286    /*
287    * the property does not include the scheme name, it is in the file;
288    * so just load the colour schemes and discard any whose name is not
289    * registered
290    */
291  0 for (String file : files)
292    {
293  0 try
294    {
295  0 UserColourScheme ucs = ColourSchemeLoader.loadColourScheme(file);
296  0 if (ucs != null
297    && ColourSchemes.getInstance().nameExists(ucs.getName()))
298    {
299  0 if (coloursFound.length() > 0)
300    {
301  0 coloursFound.append("|");
302    }
303  0 coloursFound.append(file);
304    }
305    } catch (Exception ex)
306    {
307  0 jalview.bin.Console
308    .outPrintln("Error loading User ColourFile\n" + ex);
309    }
310    }
311   
312  0 if (coloursFound.toString().length() > 1)
313    {
314  0 Cache.setProperty("USER_DEFINED_COLOURS", coloursFound.toString());
315    }
316    else
317    {
318  0 Cache.applicationProperties.remove("USER_DEFINED_COLOURS");
319    }
320    }
321    }