3 import jalview.bin.Cache;
4 import jalview.datamodel.AnnotatedCollectionI;
5 import jalview.schemes.ColourSchemeI;
6 import jalview.schemes.ColourSchemeLoader;
7 import jalview.schemes.ColourSchemes;
8 import jalview.schemes.ResidueColourScheme;
9 import jalview.schemes.UserColourScheme;
10 import jalview.util.MessageManager;
12 import java.awt.Component;
13 import java.awt.event.ActionEvent;
14 import java.awt.event.ActionListener;
15 import java.awt.event.MouseAdapter;
16 import java.awt.event.MouseEvent;
18 import javax.swing.ButtonGroup;
19 import javax.swing.JMenu;
20 import javax.swing.JRadioButtonMenuItem;
22 public class ColourMenuHelper
24 public interface ColourChangeListener
26 void changeColour_actionPerformed(String name);
30 * Adds items to the colour menu, as mutually exclusive members of a button
31 * group. The callback handler is responsible for the action on selecting any
32 * of these options. The callback method receives the name of the selected
33 * colour, or "None" or "User Defined". This method returns the ButtonGroup to
34 * which items were added.
38 * <li>...other 'built-in' colours</li>
39 * <li>...any user-defined colours</li>
40 * <li>User Defined..</li>
44 * the menu to attach items to
46 * a callback to handle menu selection
48 * the data the menu is being built for
50 * if true, only simple per-residue colour schemes are included
52 public static ButtonGroup addMenuItems(final JMenu colourMenu,
53 final ColourChangeListener client, AnnotatedCollectionI coll,
57 * ButtonGroup groups those items whose
58 * selection is mutually exclusive
60 ButtonGroup colours = new ButtonGroup();
64 JRadioButtonMenuItem noColourmenuItem = new JRadioButtonMenuItem(
65 MessageManager.getString("label.none"));
66 noColourmenuItem.setName(ResidueColourScheme.NONE);
67 noColourmenuItem.addActionListener(new ActionListener()
70 public void actionPerformed(ActionEvent e)
72 client.changeColour_actionPerformed(ResidueColourScheme.NONE);
75 colourMenu.add(noColourmenuItem);
76 colours.add(noColourmenuItem);
80 * scan registered colour schemes (built-in or user-defined
81 * and add them to the menu (in the order they were registered)
83 Iterable<ColourSchemeI> colourSchemes = ColourSchemes.getInstance()
85 for (ColourSchemeI scheme : colourSchemes)
87 if (simpleOnly && !scheme.isSimple())
93 * button text is i18n'd but the name is the canonical name of
94 * the colour scheme (inspected in setColourSelected())
96 final String name = scheme.getSchemeName();
97 String label = MessageManager.getStringOrReturn("label.colourScheme_"
98 + name.toLowerCase().replace(" ", "_"), name);
99 final JRadioButtonMenuItem radioItem = new JRadioButtonMenuItem(label);
100 radioItem.setName(name);
101 radioItem.setEnabled(scheme.isApplicableTo(coll));
102 if (scheme instanceof UserColourScheme)
105 * user-defined colour scheme loaded on startup or during the
106 * Jalview session; right-click on this offers the option to
107 * remove it as a colour choice
109 radioItem.addMouseListener(new MouseAdapter()
112 public void mousePressed(MouseEvent evt)
114 if (evt.isPopupTrigger()) // Mac
121 public void mouseReleased(MouseEvent evt)
123 if (evt.isPopupTrigger()) // Windows
131 ActionListener al = radioItem.getActionListeners()[0];
132 radioItem.removeActionListener(al);
133 int option = JvOptionPane.showInternalConfirmDialog(
134 Desktop.desktop, MessageManager
135 .getString("label.remove_from_default_list"),
137 .getString("label.remove_user_defined_colour"),
138 JvOptionPane.YES_NO_OPTION);
139 if (option == JvOptionPane.YES_OPTION)
141 ColourSchemes.getInstance().removeColourScheme(
142 radioItem.getName());
143 colourMenu.remove(radioItem);
148 radioItem.addActionListener(al);
153 radioItem.addActionListener(new ActionListener()
156 public void actionPerformed(ActionEvent evt)
158 client.changeColour_actionPerformed(name);
161 colourMenu.add(radioItem);
162 colours.add(radioItem);
166 * only add the option to load/configure a user-defined colour
167 * to the AlignFrame colour menu
169 if (client instanceof AlignFrame)
171 final String label = MessageManager.getString("action.user_defined");
172 JRadioButtonMenuItem userDefinedColour = new JRadioButtonMenuItem(
174 userDefinedColour.addActionListener(new ActionListener()
177 public void actionPerformed(ActionEvent e)
179 client.changeColour_actionPerformed(ResidueColourScheme.USER_DEFINED);
182 colourMenu.add(userDefinedColour);
183 colours.add(userDefinedColour);
190 * Marks as selected the colour menu item matching the given name, or the
191 * first item ('None') if no match is found
196 public static void setColourSelected(JMenu colourMenu, String colourName)
198 if (colourName == null)
203 JRadioButtonMenuItem none = null;
206 * select the radio button whose name matches the colour name
207 * (not the button text, as it may be internationalised)
209 for (Component menuItem : colourMenu.getMenuComponents())
211 if (menuItem instanceof JRadioButtonMenuItem)
213 String buttonName = ((JRadioButtonMenuItem) menuItem).getName();
214 if (colourName.equals(buttonName))
216 ((JRadioButtonMenuItem) menuItem).setSelected(true);
219 if (ResidueColourScheme.NONE.equals(buttonName))
221 none = (JRadioButtonMenuItem) menuItem;
227 none.setSelected(true);
232 * Marks as selected the colour menu item matching the given colour scheme, or
233 * the first item ('None') if no match is found
238 public static void setColourSelected(JMenu colourMenu, ColourSchemeI cs)
240 setColourSelected(colourMenu, cs == null ? ResidueColourScheme.NONE
241 : cs.getSchemeName());
245 * Updates the USER_DEFINE_COLOURS preference to remove any de-registered
248 static void updatePreferences()
250 StringBuilder coloursFound = new StringBuilder();
251 String[] files = Cache.getProperty("USER_DEFINED_COLOURS").split("\\|");
254 * the property does not include the scheme name, it is in the file;
255 * so just load the colour schemes and discard any whose name is not
258 for (String file : files)
262 UserColourScheme ucs = ColourSchemeLoader.loadColourScheme(file);
264 && ColourSchemes.getInstance().nameExists(ucs.getName()))
266 if (coloursFound.length() > 0)
268 coloursFound.append("|");
270 coloursFound.append(file);
272 } catch (Exception ex)
274 System.out.println("Error loading User ColourFile\n" + ex);
278 if (coloursFound.toString().length() > 1)
280 Cache.setProperty("USER_DEFINED_COLOURS", coloursFound.toString());
284 Cache.applicationProperties.remove("USER_DEFINED_COLOURS");