1 package nl.tudelft.simulation.dsol.swing.gui.inputparameters;
2
3 import java.awt.GridLayout;
4
5 import javax.swing.JComboBox;
6 import javax.swing.JLabel;
7 import javax.swing.JPanel;
8 import javax.swing.JTextField;
9
10 import org.djunits.unit.Unit;
11 import org.djunits.value.vfloat.scalar.base.FloatScalar;
12
13 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterException;
14 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterFloatScalar;
15
16
17
18
19
20
21
22
23
24
25
26 public class InputFieldFloatScalar<U extends Unit<U>, T extends FloatScalar<U, T>> extends AbstractInputField
27 {
28
29 @SuppressWarnings("checkstyle:visibilitymodifier")
30 protected JTextField floatField;
31
32
33 @SuppressWarnings("checkstyle:visibilitymodifier")
34 protected JComboBox<String> unitField;
35
36
37
38
39
40
41 public InputFieldFloatScalar(final JPanel panel, final InputParameterFloatScalar<U, T> parameter)
42 {
43 super(parameter);
44 JLabel label = new JLabel(parameter.getShortName());
45 this.floatField = new JTextField(20);
46 this.floatField
47 .setText("" + parameter.getDefaultTypedValue().getInUnit(parameter.getDefaultTypedValue().getDisplayUnit()));
48 JLabel explanation = new JLabel(parameter.getDescription());
49
50 String[] selections = new String[parameter.getUnitParameter().getOptions().size()];
51 int defaultIndex = 0;
52 int i = 0;
53 for (String option : parameter.getUnitParameter().getOptions().keySet())
54 {
55 selections[i] = option.toString();
56 U value = parameter.getUnitParameter().getOptions().get(option);
57 if (value.equals(parameter.getUnitParameter().getDefaultValue()))
58 {
59 defaultIndex = i;
60 }
61 i++;
62 }
63 this.unitField = new JComboBox<>(selections);
64 this.unitField.setSelectedIndex(defaultIndex);
65
66 panel.add(label);
67 JPanel scalarPanel = new JPanel();
68 scalarPanel.setLayout(new GridLayout(1, 2, 5, 0));
69 scalarPanel.add(this.floatField);
70 scalarPanel.add(this.unitField);
71 panel.add(scalarPanel);
72 panel.add(explanation);
73 }
74
75
76 @SuppressWarnings("unchecked")
77 @Override
78 public InputParameterFloatScalar<U, T> getParameter()
79 {
80 return (InputParameterFloatScalar<U, T>) super.getParameter();
81 }
82
83
84
85
86
87
88 public T getFloatScalarValue() throws InputParameterException
89 {
90 float floatValue = getFloatValue(this.floatField.getText(), this.parameter.getShortName());
91 getParameter().getFloatParameter().setFloatValue(floatValue);
92 U unit = getParameter().getUnitParameter().getOptions().get(this.unitField.getSelectedItem().toString());
93 getParameter().getUnitParameter().setMapValue(unit);
94 getParameter().setCalculatedValue();
95 return getParameter().getCalculatedValue();
96 }
97
98
99
100
101
102
103 public float getFloatValue() throws InputParameterException
104 {
105 return getFloatValue(this.floatField.getText(), this.parameter.getShortName());
106 }
107
108
109
110
111
112
113 public U getUnit() throws InputParameterException
114 {
115 return getParameter().getUnitParameter().getOptions().get(this.unitField.getSelectedItem().toString());
116 }
117
118
119
120
121
122
123
124
125 public static float getFloatValue(final String s, final String shortName) throws InputParameterException
126 {
127 try
128 {
129 return Float.parseFloat(s);
130 }
131 catch (NumberFormatException exception)
132 {
133 throw new InputParameterException(
134 "Field " + shortName + " does not contain a valid float value -- value = '" + s + "'");
135 }
136 }
137 }