3 import jalview.bin.Cache;
4 import jalview.datamodel.AnnotatedCollectionI;
5 import jalview.schemes.ColourSchemeI;
6 import jalview.schemes.ColourSchemes;
7 import jalview.schemes.ResidueColourScheme;
8 import jalview.schemes.UserColourScheme;
9 import jalview.util.MessageManager;
11 import java.awt.Component;
12 import java.awt.event.ActionEvent;
13 import java.awt.event.ActionListener;
14 import java.awt.event.MouseAdapter;
15 import java.awt.event.MouseEvent;
17 import javax.swing.ButtonGroup;
18 import javax.swing.JMenu;
19 import javax.swing.JRadioButtonMenuItem;
21 public class ColourMenuHelper
23 public interface ColourChangeListener
25 void changeColour_actionPerformed(String name);
29 * Adds items to the colour menu, as mutually exclusive members of a button
30 * group. The callback handler is responsible for the action on selecting any
31 * of these options. The callback method receives the name of the selected
32 * colour, or "None" or "User Defined". This method returns the ButtonGroup to
33 * which items were added.
37 * <li>...other 'built-in' colours</li>
38 * <li>...any user-defined colours</li>
39 * <li>User Defined..</li>
43 * the menu to attach items to
45 * a callback to handle menu selection
47 * the data the menu is being built for
49 * if true, only simple per-residue colour schemes are included
51 public static ButtonGroup addMenuItems(final JMenu colourMenu,
52 final ColourChangeListener client, AnnotatedCollectionI coll,
56 * ButtonGroup groups those items whose
57 * selection is mutually exclusive
59 ButtonGroup colours = new ButtonGroup();
63 JRadioButtonMenuItem noColourmenuItem = new JRadioButtonMenuItem(
64 MessageManager.getString("label.none"));
65 noColourmenuItem.setName(ResidueColourScheme.NONE);
66 noColourmenuItem.addActionListener(new ActionListener()
69 public void actionPerformed(ActionEvent e)
71 client.changeColour_actionPerformed(ResidueColourScheme.NONE);
74 colourMenu.add(noColourmenuItem);
75 colours.add(noColourmenuItem);
79 * scan registered colour schemes (built-in or user-defined
80 * and add them to the menu (in the order they were registered)
82 Iterable<ColourSchemeI> colourSchemes = ColourSchemes.getInstance()
84 for (ColourSchemeI scheme : colourSchemes)
86 if (simpleOnly && !scheme.isSimple())
92 * button text is i18n'd but the name is the canonical name of
93 * the colour scheme (inspected in setColourSelected())
95 final String name = scheme.getSchemeName();
96 String label = MessageManager.getStringOrReturn("label.colourScheme_"
97 + name.toLowerCase().replace(" ", "_"), name);
98 final JRadioButtonMenuItem radioItem = new JRadioButtonMenuItem(label);
99 radioItem.setName(name);
100 radioItem.setEnabled(scheme.isApplicableTo(coll));
101 if (scheme instanceof UserColourScheme)
104 * user-defined colour scheme loaded on startup or during the
105 * Jalview session; right-click on this offers the option to
106 * remove it as a colour choice
108 radioItem.addMouseListener(new MouseAdapter()
111 public void mousePressed(MouseEvent evt)
113 if (evt.isPopupTrigger()) // Mac
120 public void mouseReleased(MouseEvent evt)
122 if (evt.isPopupTrigger()) // Windows
130 ActionListener al = radioItem.getActionListeners()[0];
131 radioItem.removeActionListener(al);
132 int option = JvOptionPane.showInternalConfirmDialog(
133 Desktop.desktop, MessageManager
134 .getString("label.remove_from_default_list"),
136 .getString("label.remove_user_defined_colour"),
137 JvOptionPane.YES_NO_OPTION);
138 if (option == JvOptionPane.YES_OPTION)
140 ColourSchemes.getInstance().removeColourScheme(
141 radioItem.getName());
142 colourMenu.remove(radioItem);
147 radioItem.addActionListener(al);
152 radioItem.addActionListener(new ActionListener()
155 public void actionPerformed(ActionEvent evt)
157 client.changeColour_actionPerformed(name);
160 colourMenu.add(radioItem);
161 colours.add(radioItem);
165 * only add the option to load/configure a user-defined colour
166 * to the AlignFrame colour menu
168 if (client instanceof AlignFrame)
170 final String label = MessageManager.getString("action.user_defined");
171 JRadioButtonMenuItem userDefinedColour = new JRadioButtonMenuItem(
173 userDefinedColour.addActionListener(new ActionListener()
176 public void actionPerformed(ActionEvent e)
178 client.changeColour_actionPerformed(ResidueColourScheme.USER_DEFINED);
181 colourMenu.add(userDefinedColour);
182 colours.add(userDefinedColour);
189 * Marks as selected the colour menu item matching the given name, or the
190 * first item ('None') if no match is found
195 public static void setColourSelected(JMenu colourMenu, String colourName)
197 if (colourName == null)
202 JRadioButtonMenuItem none = null;
205 * select the radio button whose name matches the colour name
206 * (not the button text, as it may be internationalised)
208 for (Component menuItem : colourMenu.getMenuComponents())
210 if (menuItem instanceof JRadioButtonMenuItem)
212 String buttonName = ((JRadioButtonMenuItem) menuItem).getName();
213 if (colourName.equals(buttonName))
215 ((JRadioButtonMenuItem) menuItem).setSelected(true);
218 if (ResidueColourScheme.NONE.equals(buttonName))
220 none = (JRadioButtonMenuItem) menuItem;
226 none.setSelected(true);
231 * Marks as selected the colour menu item matching the given colour scheme, or
232 * the first item ('None') if no match is found
237 public static void setColourSelected(JMenu colourMenu, ColourSchemeI cs)
239 setColourSelected(colourMenu, cs == null ? ResidueColourScheme.NONE
240 : cs.getSchemeName());
244 * Updates the USER_DEFINE_COLOURS preference to remove any de-registered
247 static void updatePreferences()
249 StringBuilder coloursFound = new StringBuilder();
250 String[] files = Cache.getProperty("USER_DEFINED_COLOURS").split("\\|");
253 * the property does not include the scheme name, it is in the file;
254 * so just load the colour schemes and discard any whose name is not
257 for (String file : files)
261 UserColourScheme ucs = ColourSchemes.loadColourScheme(file);
263 && ColourSchemes.getInstance().nameExists(ucs.getName()))
265 if (coloursFound.length() > 0)
267 coloursFound.append("|");
269 coloursFound.append(file);
271 } catch (Exception ex)
273 System.out.println("Error loading User ColourFile\n" + ex);
277 if (coloursFound.toString().length() > 1)
279 Cache.setProperty("USER_DEFINED_COLOURS", coloursFound.toString());
283 Cache.applicationProperties.remove("USER_DEFINED_COLOURS");