1 package com.explosion.utilities.preferences.editandrender.table;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.awt.Component;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26
27 import javax.swing.DefaultCellEditor;
28 import javax.swing.JCheckBox;
29 import javax.swing.JTable;
30
31 import com.explosion.utilities.exception.ExceptionManagerFactory;
32 import com.explosion.utilities.preferences.Preference;
33
34 public class BooleanPreferenceEditor extends DefaultCellEditor
35 {
36 private JCheckBox checkBox = new JCheckBox();
37 private Object value = null;
38
39 public BooleanPreferenceEditor()
40 {
41 super(new JCheckBox());
42
43 ((JCheckBox) editorComponent).addActionListener(new CheckBoxActionListener((JCheckBox) editorComponent, this));
44
45 ((JCheckBox) editorComponent).addActionListener(new ActionListener()
46 {
47
48 public void actionPerformed(ActionEvent e)
49 {
50 fireEditingStopped();
51 }
52 });
53 }
54
55 protected void fireEditingStopped()
56 {
57 super.fireEditingStopped();
58 }
59
60 public Object getCellEditorValue()
61 {
62 return value;
63 }
64
65 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
66 {
67
68 this.value = value;
69
70 if (value instanceof Preference)
71 {
72 ((JCheckBox) editorComponent).setSelected( ((Boolean) ((Preference)value).getValue()).booleanValue());
73 }
74 else if (value instanceof Boolean)
75 {
76 ((JCheckBox) editorComponent).setSelected(((Boolean)value).booleanValue());
77 }
78 return editorComponent;
79 }
80
81 /***
82 * Returns the colour value for this editor
83 * @return
84 */
85 public Boolean getTruth()
86 {
87 if (value instanceof Preference)
88 {
89 return (Boolean) ((Preference)value).getValue();
90 }
91 else if (value instanceof Boolean)
92 {
93 return (Boolean) value;
94 }
95
96 return null;
97 }
98
99 /***
100 * Sets the color value for this editor
101 * @param colour
102 * @throws Exception
103 */
104 public void setTruth(boolean truth) throws Exception
105 {
106 if (value instanceof Preference)
107 {
108 ((Preference)value).setValue(new Boolean(truth));
109 }
110 else if (value instanceof Boolean)
111 {
112 value = new Boolean(truth);
113 }
114 }
115
116 }
117
118 class CheckBoxActionListener implements ActionListener
119 {
120
121 private BooleanPreferenceEditor editor = null;
122 private JCheckBox checkBox;
123
124 public CheckBoxActionListener(JCheckBox checkBox, BooleanPreferenceEditor colorEditor)
125 {
126 this.editor = colorEditor;
127 this.checkBox = checkBox;
128 }
129
130 public void actionPerformed(ActionEvent e)
131 {
132 try
133 {
134 editor.setTruth(checkBox.isSelected());
135 }
136 catch (Exception e1)
137 {
138 ExceptionManagerFactory.getExceptionManager().manageException(e1,"Exception caught while setting value of boolean property");
139 }
140 }
141 }