From 6b2314c302152a6a89a8371740a07a9b3ebbc4fa Mon Sep 17 00:00:00 2001 From: RememBerBer Date: Fri, 26 Nov 2021 13:52:20 +0800 Subject: [PATCH] Some code optimization --- .../moo/info/ui/form/PowerSourceForm.java | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/luoboduner/moo/info/ui/form/PowerSourceForm.java b/src/main/java/com/luoboduner/moo/info/ui/form/PowerSourceForm.java index d3c643e..a9564fc 100644 --- a/src/main/java/com/luoboduner/moo/info/ui/form/PowerSourceForm.java +++ b/src/main/java/com/luoboduner/moo/info/ui/form/PowerSourceForm.java @@ -105,24 +105,26 @@ public class PowerSourceForm { powerPanel.add(capacityLabel, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); JLabel temperatureLabel = new JLabel(); - temperatureLabel.setText(String.format("Temperature: %.1f°C", powerSource.getTemperature())); + temperatureLabel.setHorizontalAlignment(SwingConstants.RIGHT); + temperatureLabel.setHorizontalTextPosition(SwingConstants.RIGHT); + + StringBuilder powerTextBuilder = new StringBuilder(); + if (powerSource.isCharging()) { + temperatureLabel.setIcon(new FlatSVGIcon("icons/Charging.svg")); + powerTextBuilder.append("Charging"); + } else { + temperatureLabel.setIcon(new FlatSVGIcon("icons/indicator_light.svg")); + powerTextBuilder.append("Remaining Time: " + formatTimeRemaining(powerSource.getTimeRemainingEstimated())); + } + +// powerTextBuilder.append(String.format(" / Temperature: %.1f°C", powerSource.getTemperature())); + temperatureLabel.setText(powerTextBuilder.toString()); powerPanel.add(temperatureLabel, new GridConstraints(2, 3, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); final Spacer spacer2 = new Spacer(); powerPanel.add(spacer2, new GridConstraints(2, 1, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false)); JLabel label1 = new JLabel(); - label1.setHorizontalAlignment(SwingConstants.RIGHT); - label1.setHorizontalTextPosition(SwingConstants.RIGHT); - - if (powerSource.isCharging()) { - label1.setIcon(new FlatSVGIcon("icons/Charging.svg")); - label1.setText("Charging"); - } else { - label1.setIcon(new FlatSVGIcon("icons/indicator_light.svg")); - label1.setText("Remaining Time: " + formatTimeRemaining(powerSource.getTimeRemainingEstimated())); - } - powerPanel.add(label1, new GridConstraints(0, 3, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false)); final Spacer spacer3 = new Spacer(); powerPanel.add(spacer3, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false)); @@ -149,6 +151,7 @@ public class PowerSourceForm { powerInfoBuilder.append("
Power Usage Rate: ").append(powerSource.getPowerUsageRate()); powerInfoBuilder.append("
Voltage: ").append(powerSource.getVoltage()); powerInfoBuilder.append("
Amperage: ").append(powerSource.getAmperage()); + powerInfoBuilder.append("
Temperature: ").append(String.format("%.1f°C", powerSource.getTemperature())); powerInfoBuilder.append("
Power OnLine: ").append(powerSource.isPowerOnLine()); powerInfoBuilder.append("
Charging: ").append(powerSource.isCharging()); powerInfoBuilder.append("
Discharging: ").append(powerSource.isDischarging());