| BooleanField.java |
1 /**
2 * Distribution License:
3 * JSword is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU Lesser General Public License, version 2.1 as published by
5 * the Free Software Foundation. This program is distributed in the hope
6 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 * See the GNU Lesser General Public License for more details.
9 *
10 * The License is available on the internet at:
11 * http://www.gnu.org/copyleft/lgpl.html
12 * or by writing to:
13 * Free Software Foundation, Inc.
14 * 59 Temple Place - Suite 330
15 * Boston, MA 02111-1307, USA
16 *
17 * Copyright: 2005
18 * The copyright to this program is held by it's authors.
19 *
20 * ID: $Id: BooleanField.java 2104 2011-03-07 18:54:34Z dmsmith $
21 */
22 package org.crosswire.common.config.swing;
23
24 import java.awt.FlowLayout;
25
26 import javax.swing.ButtonGroup;
27 import javax.swing.JComponent;
28 import javax.swing.JPanel;
29 import javax.swing.JRadioButton;
30
31 import org.crosswire.common.config.Choice;
32 import org.crosswire.common.swing.ActionFactory;
33 import org.crosswire.common.swing.CWMsg;
34 import org.crosswire.common.util.Convert;
35
36 /**
37 * Allow the user to choose from True/False.
38 *
39 * @see gnu.lgpl.License for license details.<br>
40 * The copyright to this program is held by it's authors.
41 * @author Joe Walker [joe at eireneh dot com]
42 */
43 public class BooleanField extends JPanel implements Field {
44 /**
45 * Give the values list (true/false) to the ComboBox
46 */
47 public BooleanField() {
48 ActionFactory actions = new ActionFactory(this);
49
50 // TRANSLATOR: This is the text on a "Yes" button
51 on = new JRadioButton(actions.addAction("Yes", CWMsg.gettext("Yes")));
52 // TRANSLATOR: This is the text on a "No" button
53 off = new JRadioButton(actions.addAction("No", CWMsg.gettext("No")));
54
55 ButtonGroup group = new ButtonGroup();
56 group.add(on);
57 group.add(off);
58
59 setLayout(new FlowLayout(FlowLayout.LEADING, 0, 0));
60 add(on);
61 add(off);
62 }
63
64 /**
65 * Some fields will need some extra info to display properly like the
66 * options in an options field. FieldMap calls this method with options
67 * provided by the choice.
68 *
69 * @param param
70 * The options provided by the Choice
71 */
72 public void setChoice(Choice param) {
73 }
74
75 /**
76 * Return a string for use in the properties file
77 *
78 * @return The current value
79 */
80 public String getValue() {
81 return Convert.boolean2String(on.isSelected());
82 }
83
84 /**
85 * Set the current value
86 *
87 * @param value
88 * The new text
89 */
90 public void setValue(String value) {
91 on.setSelected(Convert.string2Boolean(value));
92 off.setSelected(!Convert.string2Boolean(value));
93 }
94
95 public void doYes() {
96 on.setSelected(true);
97 }
98
99 public void doNo() {
100 off.setSelected(true);
101 }
102
103 /**
104 * Get the actual component that we can add to a Panel. (This can well be
105 * this in an implementation).
106 */
107 public JComponent getComponent() {
108 return this;
109 }
110
111 /**
112 * The 'on' button
113 */
114 private JRadioButton on;
115
116 /**
117 * The 'off' button
118 */
119 private JRadioButton off;
120
121 /**
122 * Serialization ID
123 */
124 private static final long serialVersionUID = 3617291237934053686L;
125 }
126