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-2024 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://www.tudelft.nl/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      /** {@inheritDoc} */
76      @SuppressWarnings("unchecked")
77      @Override
78      public InputParameterFloatScalar<U, T> getParameter()
79      {
80          return (InputParameterFloatScalar<U, T>) super.getParameter();
81      }
82  
83      /**
84       * Return the numeric value of the field.
85       * @return the float value of the field in the gui.
86       * @throws InputParameterException on invalid input
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       * Return the float part of the entered value.
100      * @return float; the float part of the entered value
101      * @throws InputParameterException on invalid input
102      */
103     public float getFloatValue() throws InputParameterException
104     {
105         return getFloatValue(this.floatField.getText(), this.parameter.getShortName());
106     }
107 
108     /**
109      * Return the unit part of the entered value.
110      * @return U; the unit part of the entered value
111      * @throws InputParameterException on invalid input
112      */
113     public U getUnit() throws InputParameterException
114     {
115         return getParameter().getUnitParameter().getOptions().get(this.unitField.getSelectedItem().toString());
116     }
117 
118     /**
119      * Return the numeric value of the field.
120      * @param s String; the String to test
121      * @param shortName String; the name of the field to test
122      * @return the float value of the field in the gui.
123      * @throws InputParameterException on invalid input
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 }