1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
package tim.prune.function.estimate;
import java.awt.Component;
import java.awt.GridLayout;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import tim.prune.I18nManager;
import tim.prune.config.Config;
import tim.prune.data.Unit;
import tim.prune.gui.DisplayUtils;
/**
* Display panel for showing estimation parameters
* in a standard grid form
*/
public class ParametersPanel extends JPanel
{
private final Config _config;
/** Flag for whether average error should be shown */
private final boolean _showAverageError;
/** Labels for calculated parameters */
private JLabel _fsUnitsLabel = null, _flatSpeedLabel = null;
private JLabel _climbUnitsLabel = null;
private JLabel _gentleClimbLabel = null, _steepClimbLabel = null;
private JLabel _descentUnitsLabel = null;
private JLabel _gentleDescentLabel = null, _steepDescentLabel = null;
private JLabel _averageErrorLabel = null;
private final NumberFormat _numberFormatter;
/**
* Constructor
* @param inTitleKey key to use for title of panel
* @param inConfig config object
*/
public ParametersPanel(String inTitleKey, Config inConfig) {
this(inTitleKey, false, inConfig);
}
/**
* Constructor
* @param inTitleKey key to use for title of panel
* @param inShowAvgError true to show average error line
* @param inConfig config object
*/
public ParametersPanel(String inTitleKey, boolean inShowAvgError, Config inConfig)
{
super();
_config = inConfig;
_numberFormatter = NumberFormat.getNumberInstance();
if (_numberFormatter instanceof DecimalFormat) {
((DecimalFormat) _numberFormatter).applyPattern("0.00");
}
_showAverageError = inShowAvgError;
if (inTitleKey != null) {
setBorder(BorderFactory.createTitledBorder(I18nManager.getText(inTitleKey)));
}
setLayout(new GridLayout(0, 3, 3, 3));
addLabels();
}
private void addLabels()
{
// flat speed
_fsUnitsLabel = new JLabel(I18nManager.getText("dialog.estimatetime.parameters.timefor") + " 5km : ");
_fsUnitsLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
add(_fsUnitsLabel);
_flatSpeedLabel = new JLabel("60 minutes"); // (filled in later)
add(_flatSpeedLabel);
add(new JLabel(""));
// Headers for gentle and steep
add(new JLabel(""));
JLabel gentleLabel = new JLabel(I18nManager.getText("dialog.estimatetime.gentle"));
add(gentleLabel);
JLabel steepLabel = new JLabel(I18nManager.getText("dialog.estimatetime.steep"));
add(steepLabel);
// Climb
_climbUnitsLabel = new JLabel("Climb 100m: ");
_climbUnitsLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
add(_climbUnitsLabel);
_gentleClimbLabel = new JLabel("22 minutes"); // (filled in later)
add(_gentleClimbLabel);
_steepClimbLabel = new JLabel("22 minutes"); // (filled in later)
add(_steepClimbLabel);
// Descent
_descentUnitsLabel = new JLabel(I18nManager.getText("dialog.estimatetime.parameters.timefor") + ": ");
_descentUnitsLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
add(_descentUnitsLabel);
_gentleDescentLabel = new JLabel("22 minutes"); // (filled in later)
add(_gentleDescentLabel);
_steepDescentLabel = new JLabel("22 minutes"); // (filled in later)
add(_steepDescentLabel);
// Average error
if (_showAverageError)
{
JLabel errorLabel = new JLabel(I18nManager.getText("dialog.learnestimationparams.averageerror") + ": ");
errorLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
add(errorLabel);
_averageErrorLabel = new JLabel("22 minutes"); // (filled in later)
add(_averageErrorLabel);
}
}
/**
* Update the labels using the given parameters
* @param inParams the parameters used or calculated
* @param inAverageError average error as percentage
* @param inShowError true to show this error value, false otherwise
*/
private void updateParameters(EstimationParameters inParams, double inAverageError, boolean inShowError)
{
if (inParams == null)
{
_flatSpeedLabel.setText("");
_gentleClimbLabel.setText(""); _steepClimbLabel.setText("");
_gentleDescentLabel.setText(""); _steepDescentLabel.setText("");
}
else
{
final Unit distUnit = _config.getUnitSet().getDistanceUnit();
final String minsText = " " + I18nManager.getText("units.minutes");
_fsUnitsLabel.setText(I18nManager.getText("dialog.estimatetime.parameters.timefor") +
" " + EstimationParameters.getStandardDistance(distUnit) + ": ");
_flatSpeedLabel.setText(formatMinutes(inParams.getFlatMinutesLocal(distUnit)) + minsText);
final Unit altUnit = _config.getUnitSet().getAltitudeUnit();
final String heightString = " " + EstimationParameters.getStandardClimb(altUnit) + ": ";
_climbUnitsLabel.setText(I18nManager.getText("dialog.estimatetime.climb") + heightString);
_gentleClimbLabel.setText(formatMinutes(inParams.getGentleClimbMinutesLocal(altUnit)) + minsText);
_steepClimbLabel.setText(formatMinutes(inParams.getSteepClimbMinutesLocal(altUnit)) + minsText);
_descentUnitsLabel.setText(I18nManager.getText("dialog.estimatetime.descent") + heightString);
_gentleDescentLabel.setText(formatMinutes(inParams.getGentleDescentMinutesLocal(altUnit)) + minsText);
_steepDescentLabel.setText(formatMinutes(inParams.getSteepDescentMinutesLocal(altUnit)) + minsText);
}
// Average error
if (_averageErrorLabel != null)
{
if (inParams == null || !inShowError) {
_averageErrorLabel.setText("");
}
else {
_averageErrorLabel.setText(DisplayUtils.formatOneDp(inAverageError) + " %");
}
}
}
/** @return the number formatted using the current locale (and 2 decimal places) */
private String formatMinutes(double inMinutes) {
return _numberFormatter.format(inMinutes);
}
/**
* Just show the parameters, with no average error
* @param inParams parameters to show
*/
public void updateParameters(EstimationParameters inParams) {
updateParameters(inParams, 0.0, false);
}
/**
* Show the parameters and the average error
* @param inParams parameters to show
* @param inAverageError average error as percentage
*/
public void updateParameters(EstimationParameters inParams, double inAverageError) {
updateParameters(inParams, inAverageError, true);
}
}
|