1 package com.explosion.utilities.preferences.editandrender.form;
2
3 import java.awt.GridBagConstraints;
4 import java.awt.GridBagLayout;
5 import java.awt.Insets;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8
9 import javax.swing.JCheckBox;
10 import javax.swing.JLabel;
11
12 import org.apache.log4j.LogManager;
13 import org.apache.log4j.Logger;
14
15 import com.explosion.utilities.exception.ExceptionManagerFactory;
16 import com.explosion.utilities.preferences.Preference;
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 /***
40 * This class provides a way to edit and rebder boolean Preferences
41 * @author Stephen Cowx
42 * @version
43 */
44 public class BooleanPreferenceEditorAndRenderer extends FormBasedPreferenceRendererAndEditor {
45
46 private JCheckBox checkBox = new JCheckBox();
47 private JLabel label = new JLabel();
48 private static Logger log = LogManager.getLogger(BooleanPreferenceEditorAndRenderer.class);
49
50 /***
51 * Constructs an instance of this class
52 * @param preference
53 */
54 public BooleanPreferenceEditorAndRenderer(Preference preference)
55 {
56 super(preference);
57 init();
58 }
59
60 /***
61 * This method initialises this renderer editor combination
62 */
63 public void init()
64 {
65 this.setLayout(new GridBagLayout());
66
67
68 label.setText(getPreference().getLongName());
69 checkBox.setSelected(((Boolean)getPreference().getValue()).booleanValue());
70
71
72 this.add(label , new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0));
73 this.add(checkBox , new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
74
75
76 checkBox.addActionListener(new ActionListener(){
77
78 public void actionPerformed(ActionEvent e)
79 {
80 checkboxUpdated();
81 }
82 });
83 }
84
85 /***
86 * @throws Exception
87 */
88 private void checkboxUpdated() {
89 log.debug("Preference updated");
90 try {
91 if (checkBox.isSelected())
92 {
93 getPreference().setValue(new Boolean(true));
94 }
95 else
96 {
97 getPreference().setValue(new Boolean(false));
98 }
99 } catch (Exception e) {
100 ExceptionManagerFactory.getExceptionManager().manageException(e, "Excaption caught while updating preference.");
101 }
102 }
103
104 }