View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2006 Christian W. Hargraves (engrean@hotmail.com)
4   
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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,   //rows, cols
44                                          6, 6,   //initX, initY
45                                          6, 6);  //xPad, yPad
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 }