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 @SuppressWarnings("unchecked")
76 @Override
77 public InputParameterFloatScalar<U, T> getParameter()
78 {
79 return (InputParameterFloatScalar<U, T>) super.getParameter();
80 }
81
82
83
84
85
86
87 public T getFloatScalarValue() throws InputParameterException
88 {
89 float floatValue = getFloatValue(this.floatField.getText(), this.parameter.getShortName());
90 getParameter().getFloatParameter().setFloatValue(floatValue);
91 U unit = getParameter().getUnitParameter().getOptions().get(this.unitField.getSelectedItem().toString());
92 getParameter().getUnitParameter().setMapValue(unit);
93 getParameter().setCalculatedValue();
94 return getParameter().getCalculatedValue();
95 }
96
97
98
99
100
101
102 public float getFloatValue() throws InputParameterException
103 {
104 return getFloatValue(this.floatField.getText(), this.parameter.getShortName());
105 }
106
107
108
109
110
111
112 public U getUnit() throws InputParameterException
113 {
114 return getParameter().getUnitParameter().getOptions().get(this.unitField.getSelectedItem().toString());
115 }
116
117
118
119
120
121
122
123
124 public static float getFloatValue(final String s, final String shortName) throws InputParameterException
125 {
126 try
127 {
128 return Float.parseFloat(s);
129 }
130 catch (NumberFormatException exception)
131 {
132 throw new InputParameterException(
133 "Field " + shortName + " does not contain a valid float value -- value = '" + s + "'");
134 }
135 }
136 }