Merge branch 'documentation/JAL-3407_2.11.1_release' into releases/Release_2_11_1_Branch
[jalview.git] / src / jalview / gui / JalviewBooleanRadioButtons.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.Font;
24 import java.awt.event.ActionListener;
25
26 import javax.swing.AbstractButton;
27 import javax.swing.ButtonGroup;
28 import javax.swing.JRadioButton;
29
30 public class JalviewBooleanRadioButtons extends AbstractButton
31 {
32   private static final Font LABEL_FONT = JvSwingUtils.getLabelFont();
33
34   private ButtonGroup buttonGroup = new ButtonGroup();
35
36   private JRadioButton buttonTrue = new JRadioButton();
37
38   private JRadioButton buttonFalse = new JRadioButton();
39
40   public JalviewBooleanRadioButtons(boolean value, String trueLabel,
41           String falseLabel)
42   {
43     init();
44     this.setLabels(trueLabel, falseLabel);
45   }
46
47   public JalviewBooleanRadioButtons(boolean value)
48   {
49     init();
50     setSelected(value);
51   }
52
53   public JalviewBooleanRadioButtons()
54   {
55     init();
56   }
57
58   protected void init()
59   {
60     buttonTrue.setFont(LABEL_FONT);
61     buttonFalse.setFont(LABEL_FONT);
62     buttonGroup.add(buttonTrue);
63     buttonGroup.add(buttonFalse);
64   }
65
66   public void setLabels(String trueLabel, String falseLabel)
67   {
68     buttonTrue.setText(trueLabel);
69     buttonFalse.setText(falseLabel);
70   }
71
72   @Override
73   public void setSelected(boolean b)
74   {
75     buttonFalse.setSelected(!b);
76     // this should probably happen automatically, no harm in forcing the issue!
77     // setting them this way round so the last setSelected is on buttonTrue
78     buttonTrue.setSelected(b);
79   }
80
81   @Override
82   public boolean isSelected()
83   {
84     // unambiguous selection
85     return buttonTrue.isSelected() && !buttonFalse.isSelected();
86   }
87
88   @Override
89   public void setEnabled(boolean b)
90   {
91     buttonTrue.setEnabled(b);
92     buttonFalse.setEnabled(b);
93   }
94
95   @Override
96   public boolean isEnabled()
97   {
98     return buttonTrue.isEnabled() && buttonFalse.isEnabled();
99   }
100
101   public JRadioButton getTrueButton()
102   {
103     return buttonTrue;
104   }
105
106   public JRadioButton getFalseButton()
107   {
108     return buttonFalse;
109   }
110   
111   @Override
112   public void addActionListener(ActionListener l)
113   {
114     buttonTrue.addActionListener(l);
115     buttonFalse.addActionListener(l);
116   }
117
118   public void addTrueActionListener(ActionListener l)
119   {
120     buttonTrue.addActionListener(l);
121   }
122
123   public void addFalseActionListener(ActionListener l)
124   {
125     buttonFalse.addActionListener(l);
126   }
127 }