View Javadoc
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   * Swing InputField for Floats with a unit. <br>
18   * <br>
19   * Copyright (c) 2003-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
20   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
21   * source code and binary code of this software is proprietary information of Delft University of Technology.
22   * @author <a href="https://github.com/averbraeck" target="_blank">Alexander Verbraeck</a>
23   * @param <U> the unit type
24   * @param <T> the scalar type
25   */
26  public class InputFieldFloatScalar<U extends Unit<U>, T extends FloatScalar<U, T>> extends AbstractInputField
27  {
28      /** field for the float value. */
29      @SuppressWarnings("checkstyle:visibilitymodifier")
30      protected JTextField floatField;
31  
32      /** combo box for the unit. */
33      @SuppressWarnings("checkstyle:visibilitymodifier")
34      protected JComboBox<String> unitField;
35  
36      /**
37       * Create a float field with a unit on the screen.
38       * @param panel JPanel; panel to add the field to
39       * @param parameter InputParameterFloatScalar&lt;U,T&gt;; the parameter
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       * Return the numeric value of the field.
84       * @return the float value of the field in the gui.
85       * @throws InputParameterException on invalid input
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       * Return the float part of the entered value.
99       * @return float; the float part of the entered value
100      * @throws InputParameterException on invalid input
101      */
102     public float getFloatValue() throws InputParameterException
103     {
104         return getFloatValue(this.floatField.getText(), this.parameter.getShortName());
105     }
106 
107     /**
108      * Return the unit part of the entered value.
109      * @return U; the unit part of the entered value
110      * @throws InputParameterException on invalid input
111      */
112     public U getUnit() throws InputParameterException
113     {
114         return getParameter().getUnitParameter().getOptions().get(this.unitField.getSelectedItem().toString());
115     }
116 
117     /**
118      * Return the numeric value of the field.
119      * @param s String; the String to test
120      * @param shortName String; the name of the field to test
121      * @return the float value of the field in the gui.
122      * @throws InputParameterException on invalid input
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 }