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.KeyAdapter;
7 import java.awt.event.KeyEvent;
8
9 import javax.swing.JLabel;
10 import javax.swing.JTextField;
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 text based preferences
41 * @author Stephen Cowx
42 * @version
43 */
44 public class TextPreferenceEditorAndRenderer extends FormBasedPreferenceRendererAndEditor {
45
46 private JTextField textField = new JTextField();
47 private JLabel label = new JLabel();
48 private static Logger log = LogManager.getLogger(TextPreferenceEditorAndRenderer.class);
49
50 /***
51 * Constructs an instance of this class
52 * @param preference
53 */
54 public TextPreferenceEditorAndRenderer(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 textField.setText(getPreference().getValue().toString());
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(textField , 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 textField.addKeyListener(new KeyAdapter(){
77 public void keyReleased(KeyEvent e)
78 {
79 textUpdated();
80 }
81 });
82 }
83
84 /***
85 * @throws Exception
86 */
87 private void textUpdated() {
88 log.debug("Preference updated");
89 try {
90 getPreference().setValue(textField.getText());
91 } catch (Exception e) {
92 ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while updating preference.");
93 }
94 }
95
96 }