From a164bd92e7f2e6964a88edede6f1686119dd9c9f Mon Sep 17 00:00:00 2001 From: jherkel Date: Sun, 20 Mar 2016 23:31:26 +0100 Subject: [PATCH] added support for custom formatting of lcd value. When it is set it will be used instead of setLcdDecimals --- .../steelseries/gauges/AbstractLinear.java | 59 +++++++++++++----- .../steelseries/gauges/AbstractRadial.java | 59 +++++++++++++----- .../steelseries/gauges/DisplayMulti.java | 60 ++++++++++++++----- .../steelseries/gauges/DisplaySingle.java | 59 ++++++++++++++---- .../eu/hansolo/steelseries/gauges/Lcd.java | 4 ++ .../eu/hansolo/steelseries/tools/Model.java | 17 ++++++ 6 files changed, 203 insertions(+), 55 deletions(-) diff --git a/src/main/java/eu/hansolo/steelseries/gauges/AbstractLinear.java b/src/main/java/eu/hansolo/steelseries/gauges/AbstractLinear.java index e2c2156..49b959b 100644 --- a/src/main/java/eu/hansolo/steelseries/gauges/AbstractLinear.java +++ b/src/main/java/eu/hansolo/steelseries/gauges/AbstractLinear.java @@ -65,6 +65,8 @@ import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; +import java.text.DecimalFormat; +import java.text.NumberFormat; /** @@ -96,6 +98,7 @@ public abstract class AbstractLinear extends AbstractGauge implements Lcd, Actio private final TimelineEase STANDARD_EASING; private final TimelineEase RETURN_TO_ZERO_EASING; private TimelineCallback timelineCallback; + private NumberFormat nf = null; // // @@ -582,6 +585,30 @@ public void setLcdDecimals(final int DECIMALS) { getModel().setLcdDecimals(DECIMALS); repaint(getInnerBounds()); } + + @Override + public String getLcdFormat() { + return getModel().getLcdFormat(); + } + + @Override + public void setLcdFormat(String format) { + if (format == null) { + nf = null; + } else { + if (format.isEmpty() == true) { + nf = null; + } else { + if (getModel().getLcdFormat() != null && getModel().getLcdFormat().compareTo(format) == 0) { + return; + } else { + nf = new DecimalFormat(format,new java.text.DecimalFormatSymbols(java.util.Locale.US)); + getModel().setLcdFormat(format); + } + } + } + repaint(getInnerBounds()); + } @Override public LcdColor getLcdColor() { @@ -633,24 +660,28 @@ public void setCustomLcdForeground(final Color CUSTOM_LCD_FOREGROUND) { @Override public String formatLcdValue(final double VALUE) { - final StringBuilder DEC_BUFFER = new StringBuilder(16); - DEC_BUFFER.append("0"); + if (nf != null) { + return nf.format(VALUE); + } else { + final StringBuilder DEC_BUFFER = new StringBuilder(16); + DEC_BUFFER.append("0"); - if (getModel().getLcdDecimals() > 0) { - DEC_BUFFER.append("."); - } + if (getModel().getLcdDecimals() > 0) { + DEC_BUFFER.append("."); + } - for (int i = 0; i < getModel().getLcdDecimals(); i++) { - DEC_BUFFER.append("0"); - } + for (int i = 0; i < getModel().getLcdDecimals(); i++) { + DEC_BUFFER.append("0"); + } - if (getModel().isLcdScientificFormatEnabled()) { - DEC_BUFFER.append("E0"); - } - DEC_BUFFER.trimToSize(); - final java.text.DecimalFormat DEC_FORMAT = new java.text.DecimalFormat(DEC_BUFFER.toString(), new java.text.DecimalFormatSymbols(java.util.Locale.US)); + if (getModel().isLcdScientificFormatEnabled()) { + DEC_BUFFER.append("E0"); + } + DEC_BUFFER.trimToSize(); + final java.text.DecimalFormat DEC_FORMAT = new java.text.DecimalFormat(DEC_BUFFER.toString(), new java.text.DecimalFormatSymbols(java.util.Locale.US)); - return DEC_FORMAT.format(VALUE); + return DEC_FORMAT.format(VALUE); + } } @Override diff --git a/src/main/java/eu/hansolo/steelseries/gauges/AbstractRadial.java b/src/main/java/eu/hansolo/steelseries/gauges/AbstractRadial.java index b274dcf..915b1dd 100644 --- a/src/main/java/eu/hansolo/steelseries/gauges/AbstractRadial.java +++ b/src/main/java/eu/hansolo/steelseries/gauges/AbstractRadial.java @@ -70,6 +70,8 @@ import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; +import java.text.DecimalFormat; +import java.text.NumberFormat; import java.util.Arrays; import java.util.List; @@ -122,6 +124,7 @@ public abstract class AbstractRadial extends AbstractGauge implements Lcd { // Alignment related private int horizontalAlignment; private int verticalAlignment; + private NumberFormat nf = null; // @@ -829,6 +832,30 @@ public void setLcdDecimals(final int DECIMALS) { getModel().setLcdDecimals(DECIMALS); repaint(getInnerBounds()); } + + @Override + public String getLcdFormat() { + return getModel().getLcdFormat(); + } + + @Override + public void setLcdFormat(String format) { + if (format == null) { + nf = null; + } else { + if (format.isEmpty() == true) { + nf = null; + } else { + if (getModel().getLcdFormat() != null && getModel().getLcdFormat().compareTo(format) == 0) { + return; + } else { + nf = new DecimalFormat(format,new java.text.DecimalFormatSymbols(java.util.Locale.US)); + getModel().setLcdFormat(format); + } + } + } + repaint(getInnerBounds()); + } @Override public LcdColor getLcdColor() { @@ -885,25 +912,29 @@ public void setCustomLcdForeground(final Color CUSTOM_LCD_FOREGROUND) { @Override public String formatLcdValue(final double VALUE) { - final StringBuilder DEC_BUFFER = new StringBuilder(16); - DEC_BUFFER.append("0"); + if (nf != null) { + return nf.format(VALUE); + } else { + final StringBuilder DEC_BUFFER = new StringBuilder(16); + DEC_BUFFER.append("0"); - if (getModel().getLcdDecimals() > 0) { - DEC_BUFFER.append("."); - } + if (getModel().getLcdDecimals() > 0) { + DEC_BUFFER.append("."); + } - for (int i = 0; i < getModel().getLcdDecimals(); i++) { - DEC_BUFFER.append("0"); - } + for (int i = 0; i < getModel().getLcdDecimals(); i++) { + DEC_BUFFER.append("0"); + } - if (getModel().isLcdScientificFormatEnabled()) { - DEC_BUFFER.append("E0"); - } + if (getModel().isLcdScientificFormatEnabled()) { + DEC_BUFFER.append("E0"); + } - DEC_BUFFER.trimToSize(); - final java.text.DecimalFormat DEC_FORMAT = new java.text.DecimalFormat(DEC_BUFFER.toString(), new java.text.DecimalFormatSymbols(java.util.Locale.US)); + DEC_BUFFER.trimToSize(); + final java.text.DecimalFormat DEC_FORMAT = new java.text.DecimalFormat(DEC_BUFFER.toString(), new java.text.DecimalFormatSymbols(java.util.Locale.US)); - return DEC_FORMAT.format(VALUE); + return DEC_FORMAT.format(VALUE); + } } @Override diff --git a/src/main/java/eu/hansolo/steelseries/gauges/DisplayMulti.java b/src/main/java/eu/hansolo/steelseries/gauges/DisplayMulti.java index 15bfdb2..fbcca76 100644 --- a/src/main/java/eu/hansolo/steelseries/gauges/DisplayMulti.java +++ b/src/main/java/eu/hansolo/steelseries/gauges/DisplayMulti.java @@ -61,6 +61,8 @@ import java.awt.geom.Rectangle2D; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; +import java.text.DecimalFormat; +import java.text.NumberFormat; /** @@ -119,6 +121,8 @@ public final class DisplayMulti extends JComponent implements Lcd, ActionListene private Color glowColor; private boolean glowing; private BufferedImage glowImageOn; + private NumberFormat nf = null; + private String lcdFormat = null; private final transient ComponentListener COMPONENT_LISTENER = new ComponentAdapter() { @Override @@ -460,6 +464,30 @@ public void setLcdDecimals(final int DECIMALS) { repaint(getInnerBounds()); } + @Override + public String getLcdFormat() { + return lcdFormat; + } + + @Override + public void setLcdFormat(String format) { + if (format == null) { + nf = null; + } else { + if (format.isEmpty() == true) { + nf = null; + } else { + if (lcdFormat != null && lcdFormat.compareTo(format) == 0) { + return; + } else { + nf = new DecimalFormat(format,new java.text.DecimalFormatSymbols(java.util.Locale.US)); + this.lcdFormat = format; + } + } + } + repaint(getInnerBounds()); + } + @Override public String getLcdUnitString() { return this.lcdUnitString; @@ -604,25 +632,29 @@ public void setCustomLcdForeground(final Color CUSTOM_LCD_FOREGROUND) { @Override public String formatLcdValue(final double VALUE) { - final StringBuilder DEC_BUFFER = new StringBuilder(16); - DEC_BUFFER.append("0"); + if (nf != null) { + return nf.format(VALUE); + } else { + final StringBuilder DEC_BUFFER = new StringBuilder(16); + DEC_BUFFER.append("0"); - if (this.lcdDecimals > 0) { - DEC_BUFFER.append("."); - } + if (this.lcdDecimals > 0) { + DEC_BUFFER.append("."); + } - for (int i = 0; i < this.lcdDecimals; i++) { - DEC_BUFFER.append("0"); - } + for (int i = 0; i < this.lcdDecimals; i++) { + DEC_BUFFER.append("0"); + } - if (lcdScientificFormat) { - DEC_BUFFER.append("E0"); - } - DEC_BUFFER.trimToSize(); + if (lcdScientificFormat) { + DEC_BUFFER.append("E0"); + } + DEC_BUFFER.trimToSize(); - final java.text.DecimalFormat DEC_FORMAT = new java.text.DecimalFormat(DEC_BUFFER.toString(), new java.text.DecimalFormatSymbols(java.util.Locale.US)); + final java.text.DecimalFormat DEC_FORMAT = new java.text.DecimalFormat(DEC_BUFFER.toString(), new java.text.DecimalFormatSymbols(java.util.Locale.US)); - return DEC_FORMAT.format(VALUE); + return DEC_FORMAT.format(VALUE); + } } @Override diff --git a/src/main/java/eu/hansolo/steelseries/gauges/DisplaySingle.java b/src/main/java/eu/hansolo/steelseries/gauges/DisplaySingle.java index 0cb2ffe..93bf602 100644 --- a/src/main/java/eu/hansolo/steelseries/gauges/DisplaySingle.java +++ b/src/main/java/eu/hansolo/steelseries/gauges/DisplaySingle.java @@ -59,6 +59,8 @@ import java.awt.geom.Rectangle2D; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; +import java.text.DecimalFormat; +import java.text.NumberFormat; import java.util.ArrayList; import java.util.List; import javax.swing.JComponent; @@ -150,6 +152,8 @@ public final class DisplaySingle extends JComponent implements Lcd, ActionListen private float lcdTextX; private Timer TEXT_SCROLLER; private long animationDuration; + private NumberFormat nf = null; + private String lcdFormat = null; private final transient ComponentListener COMPONENT_LISTENER = new ComponentAdapter() { @Override @@ -770,6 +774,31 @@ public void setLcdDecimals(final int DECIMALS) { this.lcdDecimals = DECIMALS; repaint(getInnerBounds()); } + + @Override + public String getLcdFormat() { + return lcdFormat; + } + + @Override + public void setLcdFormat(String format) { + if (format == null) { + nf = null; + } else { + if (format.isEmpty() == true) { + nf = null; + } else { + if (lcdFormat != null && lcdFormat.compareTo(format) == 0) { + return; + } else { + nf = new DecimalFormat(format,new java.text.DecimalFormatSymbols(java.util.Locale.US)); + this.lcdFormat = format; + } + } + } + repaint(getInnerBounds()); + } + @Override public String getLcdUnitString() { @@ -915,24 +944,28 @@ public void setCustomLcdForeground(final Color CUSTOM_LCD_FOREGROUND) { @Override public String formatLcdValue(final double VALUE) { - final StringBuilder DEC_BUFFER = new StringBuilder(16); - DEC_BUFFER.append("0"); + if (nf != null) { + return nf.format(VALUE); + } else { + final StringBuilder DEC_BUFFER = new StringBuilder(16); + DEC_BUFFER.append("0"); - if (this.lcdDecimals > 0) { - DEC_BUFFER.append("."); - } + if (this.lcdDecimals > 0) { + DEC_BUFFER.append("."); + } - for (int i = 0; i < this.lcdDecimals; i++) { - DEC_BUFFER.append("0"); - } + for (int i = 0; i < this.lcdDecimals; i++) { + DEC_BUFFER.append("0"); + } - if (lcdScientificFormat) { - DEC_BUFFER.append("E0"); - } + if (lcdScientificFormat) { + DEC_BUFFER.append("E0"); + } - final java.text.DecimalFormat DEC_FORMAT = new java.text.DecimalFormat(DEC_BUFFER.toString(), new java.text.DecimalFormatSymbols(java.util.Locale.US)); + final java.text.DecimalFormat DEC_FORMAT = new java.text.DecimalFormat(DEC_BUFFER.toString(), new java.text.DecimalFormatSymbols(java.util.Locale.US)); - return DEC_FORMAT.format(VALUE); + return DEC_FORMAT.format(VALUE); + } } @Override diff --git a/src/main/java/eu/hansolo/steelseries/gauges/Lcd.java b/src/main/java/eu/hansolo/steelseries/gauges/Lcd.java index 281254c..e546ed8 100644 --- a/src/main/java/eu/hansolo/steelseries/gauges/Lcd.java +++ b/src/main/java/eu/hansolo/steelseries/gauges/Lcd.java @@ -70,6 +70,10 @@ public interface Lcd { public int getLcdDecimals(); public void setLcdDecimals(final int DECIMALS); + + public void setLcdFormat(final String format); + + public String getLcdFormat(); public String getLcdUnitString(); diff --git a/src/main/java/eu/hansolo/steelseries/tools/Model.java b/src/main/java/eu/hansolo/steelseries/tools/Model.java index 2ba6657..1c87b00 100644 --- a/src/main/java/eu/hansolo/steelseries/tools/Model.java +++ b/src/main/java/eu/hansolo/steelseries/tools/Model.java @@ -183,6 +183,7 @@ public class Model implements Cloneable { private Font lcdInfoFont; private int lcdDecimals; private Paint rangeOfMeasuredValuesPaint; + private String lcdFormat; // // @@ -2693,6 +2694,22 @@ public void setLcdDecimals(final int LCD_DECIMALS) { fireStateChanged(); } + /** + * Returns lcd format + * @return lcd format + */ + public String getLcdFormat() { + return lcdFormat; + } + + /** + * Sets the lcd format. When it is set it will be used instead of setLcdDecimals. See @link java.text.DecimalFormat. + */ + public void setLcdFormat(final String format) { + this.lcdFormat = format; + fireStateChanged(); + } + /** * Returns true if the calculation method will try to calculate * nice values for min and max values of the scale.