JAL-3746 apply copyright to source
[jalview.git] / src / jalview / gui / JalviewColourChooser.java
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 java.awt.Color;
24 import java.awt.Component;
25 import java.awt.event.ActionListener;
26
27 import javax.swing.JColorChooser;
28 import javax.swing.JComponent;
29 import javax.swing.JDialog;
30
31 /**
32  * A helper class that shows a JColorChooser and passes the selected colour back
33  * to a listener
34  */
35 public class JalviewColourChooser
36 {
37   public interface ColourChooserListener
38   {
39     void colourSelected(Color c);
40
41     default void cancel()
42     {
43     };
44   }
45
46   /**
47    * Shows a colour chooser dialog with the given parent component, title, and
48    * (optionally) initially selected colour. The chosen colour is passed back to
49    * the listener. There is no action if the dialog is cancelled.
50    * 
51    * @param parent
52    * @param title
53    * @param initialColour
54    * @param listener
55    */
56   public static void showColourChooser(Component parent, String title,
57           Color initialColour, ColourChooserListener listener)
58   {
59     JColorChooser colorChooser = new JColorChooser();
60     if (initialColour != null)
61     {
62       colorChooser.setColor(initialColour);
63     }
64     ActionListener onChoose = evt -> listener
65             .colourSelected(colorChooser.getColor());
66     ActionListener onCancel = evt -> listener.cancel();
67     JDialog dialog = JColorChooser.createDialog(parent, title, true,
68             colorChooser, onChoose, onCancel);
69     dialog.setVisible(true);
70   }
71
72   /**
73    * A convenience method that shows a colour chooser, with initial colour the
74    * background of the given 'paintable', and updates its background colour and
75    * repaints it after a colour selection is made
76    * 
77    * @param parent
78    * @param title
79    * @param paintable
80    */
81   public static void showColourChooser(Component parent, String title,
82           JComponent paintable)
83   {
84     ColourChooserListener listener = new ColourChooserListener()
85     {
86       @Override
87       public void colourSelected(Color c)
88       {
89         paintable.setBackground(c);
90         paintable.repaint();
91       }
92     };
93     JalviewColourChooser.showColourChooser(parent, title,
94             paintable.getBackground(), listener);
95   }
96 }