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.DefaultComboBoxModel;
10 import javax.swing.JComboBox;
11 import javax.swing.JLabel;
12
13 import org.apache.log4j.LogManager;
14 import org.apache.log4j.Logger;
15
16 import com.explosion.utilities.exception.ExceptionManagerFactory;
17 import com.explosion.utilities.preferences.Preference;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 /***
41 * This class provides a way to edit and rebder choice based preferences
42 * @author Stephen Cowx
43 * @version
44 */
45 public class ChoicePreferenceEditorAndRenderer extends FormBasedPreferenceRendererAndEditor {
46
47 private JComboBox comboBox = new JComboBox();
48 private JLabel label = new JLabel();
49 private static Logger log = LogManager.getLogger(ChoicePreferenceEditorAndRenderer.class);
50
51 /***
52 * Constructs an instance of this class
53 * @param preference
54 */
55 public ChoicePreferenceEditorAndRenderer(Preference preference)
56 {
57 super(preference);
58 init();
59 }
60
61 /***
62 * This method initialises this renderer editor combination
63 */
64 public void init()
65 {
66 this.setLayout(new GridBagLayout());
67
68
69 label.setText(getPreference().getLongName());
70 if (getPreference().getChoiceValues() != null)
71 {
72 comboBox.setModel(new DefaultComboBoxModel(getPreference().getChoiceValues().toArray()));
73 }
74 else
75 {
76 comboBox.setModel(new DefaultComboBoxModel());
77 }
78
79 comboBox.setSelectedItem(getPreference().getValue());
80
81 comboBox.addActionListener(new ActionListener(){
82 public void actionPerformed(ActionEvent e) {
83 choiceUpdated();
84 }
85 });
86
87
88
89 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));
90 this.add(comboBox , new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 2), 0, 0));
91 }
92
93 /***
94 * @throws Exception
95 */
96 private void choiceUpdated() {
97 log.debug("choiceUpdated");
98 try {
99 if (getPreference().getType() == Preference.PROPERTY_TYPE_STRING_CHOICE)
100 {
101 getPreference().setValue(comboBox.getSelectedItem().toString());
102 }
103 else
104 {
105 Object selectedItem = new Integer((String)comboBox.getSelectedItem());
106 }
107 } catch (Exception e) {
108 ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while updating preference.");
109 }
110 }
111
112 public void choicesUpdated()
113 {
114 comboBox.setModel(new DefaultComboBoxModel(getPreference().getChoiceValues().toArray()));
115 }
116
117 }