1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jameleon.ui;
20
21 import net.sf.jameleon.util.Configurator;
22
23 import javax.swing.*;
24 import java.awt.*;
25 import java.util.Iterator;
26 import java.util.LinkedList;
27 import java.util.List;
28
29
30 public abstract class AbstractConfigPanel extends JPanel{
31
32 protected List fieldProperties;
33
34 public AbstractConfigPanel(){
35 super(new SpringLayout());
36 fieldProperties = new LinkedList();
37 init();
38 }
39
40 private void init(){
41 registerFields();
42 SpringUtilities.makeCompactGrid(this,
43 fieldProperties.size(), 2,
44 6, 6,
45 6, 6);
46 }
47
48 public abstract void registerFields();
49
50 protected void addFieldProperty(boolean isTextField, String label, String configName, String defaultValue, String toolTip){
51 fieldProperties.add(new FieldProperty(isTextField, label, configName, toolTip, defaultValue, this));
52 }
53
54 protected void updateProperties(){
55 Iterator it = fieldProperties.iterator();
56 FieldProperty fp;
57 while (it.hasNext()) {
58 fp = (FieldProperty)it.next();
59 fp.updateProperty();
60 }
61 }
62
63 private class FieldProperty{
64 private JCheckBox checkbox;
65 private JTextField textField;
66 private String configName;
67 private String defaultValue;
68
69 protected FieldProperty(boolean isTextField, String labelName, String configName, String toolTip, String defaultValue, JPanel panel){
70 this.configName = configName;
71 this.defaultValue = defaultValue;
72 if (isTextField) {
73 textField = (JTextField)createNewField(isTextField, labelName, toolTip, panel);
74 setTextFieldValue(textField, configName);
75 }else{
76 checkbox = (JCheckBox)createNewField(isTextField, labelName, toolTip, panel);
77 setCheckBoxValue(checkbox, configName);
78 }
79 }
80
81 protected void updateProperty(){
82 if (textField != null && textField.getText() != null &&
83 textField.getText().length() > 0 && !defaultValue.equalsIgnoreCase(textField.getText())) {
84 Configurator.getInstance().setValue(configName, textField.getText());
85 }else if (checkbox != null && checkbox.isSelected() != Boolean.valueOf(defaultValue).booleanValue()) {
86 String value = Boolean.toString(checkbox.isSelected());
87 Configurator.getInstance().setValue(configName, value);
88 }else{
89 Configurator.getInstance().setValue(configName, null);
90 }
91 }
92
93 private JComponent createNewField(boolean isTextField, String label, String toolTip, JPanel panel){
94 JLabel jl = new JLabel(label);
95 panel.add(jl);
96 JComponent field;
97 if (isTextField){
98 JTextField textField = new JTextField();
99 textField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 5));
100 field = textField;
101 }else{
102 field = new JCheckBox();
103 }
104 jl.setLabelFor(field);
105 field.setToolTipText("<html>"+toolTip+"</html>");
106 panel.add(field);
107 return field;
108 }
109
110 private void setTextFieldValue(JTextField field, String key){
111 field.setText(Configurator.getInstance().getValue(key, defaultValue));
112 }
113
114 private void setCheckBoxValue(JCheckBox field, String key){
115 String value = Configurator.getInstance().getValue(key, defaultValue);
116 field.setSelected(Boolean.valueOf(value).booleanValue());
117 }
118
119 }
120
121 }