From 5e27fefc4c1dee11fed258483c213ac3e4b6be90 Mon Sep 17 00:00:00 2001 From: NicolaiO Date: Tue, 21 Nov 2023 19:45:59 +0000 Subject: [PATCH] Minor code cleanup See merge request main/Sumatra!1782 sumatra-commit: 85a86ce684747b15b3802ba38a9557d8b40241bd --- .../edu/tigers/sumatra/AMainPresenter.java | 9 +- .../tigers/sumatra/IMainFrameObserver.java | 44 +++--- .../sumatra/components/BetterScrollPane.java | 32 +---- .../sumatra/components/EnumCheckBoxPanel.java | 20 +-- .../sumatra/components/JImagePanel.java | 128 ------------------ .../sumatra/views/ASumatraPresenter.java | 28 ---- .../presenter/replay/ReplayPresenter.java | 2 +- 7 files changed, 36 insertions(+), 227 deletions(-) delete mode 100644 modules/common-gui/src/main/java/edu/tigers/sumatra/components/JImagePanel.java delete mode 100644 modules/common-gui/src/main/java/edu/tigers/sumatra/views/ASumatraPresenter.java diff --git a/modules/common-gui/src/main/java/edu/tigers/sumatra/AMainPresenter.java b/modules/common-gui/src/main/java/edu/tigers/sumatra/AMainPresenter.java index 76fb80da..71a65790 100644 --- a/modules/common-gui/src/main/java/edu/tigers/sumatra/AMainPresenter.java +++ b/modules/common-gui/src/main/java/edu/tigers/sumatra/AMainPresenter.java @@ -114,13 +114,6 @@ public void onDeleteLayout() } - @Override - public void onRefreshLayoutItems() - { - refreshLayoutItems(); - } - - @Override public void onLoadLayout(final String filename) { @@ -144,7 +137,7 @@ public void onLoadLayout(final String filename) public void onExit() { GlobalShortcuts.removeAllForFrame(getMainFrame()); - + // ### Persist user settings final Properties appProps = SumatraModel.getInstance().getUserSettings(); diff --git a/modules/common-gui/src/main/java/edu/tigers/sumatra/IMainFrameObserver.java b/modules/common-gui/src/main/java/edu/tigers/sumatra/IMainFrameObserver.java index 25a6c1af..364e356c 100644 --- a/modules/common-gui/src/main/java/edu/tigers/sumatra/IMainFrameObserver.java +++ b/modules/common-gui/src/main/java/edu/tigers/sumatra/IMainFrameObserver.java @@ -13,50 +13,44 @@ /** * MainFrame observer - * + * * @author AndreR */ public interface IMainFrameObserver { - + /** - * - */ + * + */ void onSaveLayout(); - - + + /** - * - */ + * + */ void onDeleteLayout(); - - + + /** - * - */ + * + */ void onExit(); - - + + /** - * + * */ default void onAbout() { } - - + + /** * @param filename */ void onLoadLayout(String filename); - - - /** - * - */ - void onRefreshLayoutItems(); - - + + /** * @param info */ diff --git a/modules/common-gui/src/main/java/edu/tigers/sumatra/components/BetterScrollPane.java b/modules/common-gui/src/main/java/edu/tigers/sumatra/components/BetterScrollPane.java index 761c62ad..6d5eb54d 100644 --- a/modules/common-gui/src/main/java/edu/tigers/sumatra/components/BetterScrollPane.java +++ b/modules/common-gui/src/main/java/edu/tigers/sumatra/components/BetterScrollPane.java @@ -4,9 +4,8 @@ package edu.tigers.sumatra.components; -import java.awt.Component; - import javax.swing.JScrollPane; +import java.awt.Component; /** @@ -18,36 +17,15 @@ public class BetterScrollPane extends JScrollPane { private static final int SCROLL_INCREMENT = 16; - - - public BetterScrollPane() - { - super(); - changeDefaults(); - } - - + + public BetterScrollPane(Component component) { super(component); changeDefaults(); } - - - public BetterScrollPane(int var1, int var2) - { - super(var1, var2); - changeDefaults(); - } - - - public BetterScrollPane(Component component, int var2, int var3) - { - super(component, var2, var3); - changeDefaults(); - } - - + + private void changeDefaults() { this.getVerticalScrollBar().setUnitIncrement(SCROLL_INCREMENT); // change vertical scroll speed diff --git a/modules/common-gui/src/main/java/edu/tigers/sumatra/components/EnumCheckBoxPanel.java b/modules/common-gui/src/main/java/edu/tigers/sumatra/components/EnumCheckBoxPanel.java index 02f9880b..75fb8520 100644 --- a/modules/common-gui/src/main/java/edu/tigers/sumatra/components/EnumCheckBoxPanel.java +++ b/modules/common-gui/src/main/java/edu/tigers/sumatra/components/EnumCheckBoxPanel.java @@ -3,8 +3,16 @@ */ package edu.tigers.sumatra.components; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import javax.swing.BorderFactory; +import javax.swing.BoxLayout; +import javax.swing.JButton; +import javax.swing.JCheckBox; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; +import java.io.Serial; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; @@ -14,15 +22,6 @@ import java.util.Map; import java.util.Set; import java.util.function.Function; -import java.util.stream.Collectors; - -import javax.swing.BorderFactory; -import javax.swing.BoxLayout; -import javax.swing.JButton; -import javax.swing.JCheckBox; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; /** @@ -34,6 +33,7 @@ */ public class EnumCheckBoxPanel> extends BasePanel> { + @Serial private static final long serialVersionUID = 5263861341015714105L; private static final Logger log = LogManager.getLogger(EnumCheckBoxPanel.class); @@ -99,7 +99,7 @@ private void createBoxes(final int orientation) setLayout(new BoxLayout(this, orientation)); List sortedEntries = Arrays.stream(enumClass.getEnumConstants()) - .sorted(Comparator.comparing(Enum::toString)).collect(Collectors.toList()); + .sorted(Comparator.comparing(Enum::toString)).toList(); for (T type : sortedEntries) { JCheckBox checkBox = new JCheckBox(getBoxLabel(type)); diff --git a/modules/common-gui/src/main/java/edu/tigers/sumatra/components/JImagePanel.java b/modules/common-gui/src/main/java/edu/tigers/sumatra/components/JImagePanel.java deleted file mode 100644 index 0da184fe..00000000 --- a/modules/common-gui/src/main/java/edu/tigers/sumatra/components/JImagePanel.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * ********************************************************* - * Copyright (c) 2009 - 2016, DHBW Mannheim - Tigers Mannheim - * Project: TIGERS - Sumatra - * Date: Jun 11, 2016 - * Author(s): "Lukas Magel" - * ********************************************************* - */ -package edu.tigers.sumatra.components; - -import java.awt.Dimension; -import java.awt.Graphics; - -import javax.swing.ImageIcon; -import javax.swing.JPanel; - -import edu.tigers.sumatra.util.ImageScaler; - - -/** - * Panel which displays an image. If the image does not fit the available space it is scaled appropriately while - * maintaining the original aspect ratio. - * - * @author "Lukas Magel" - */ -public class JImagePanel extends JPanel -{ - - /** */ - private static final long serialVersionUID = 5698886438859212214L; - - private ImageIcon originalImage; - - private ImageIcon scaledImage; - /** The size of the component that the scaledImage instance was scaled for */ - private Dimension scaleDimension = new Dimension(); - - - /** - * - */ - public JImagePanel() - { - - } - - - /** - * @param image - */ - public JImagePanel(final ImageIcon image) - { - originalImage = image; - scaledImage = image; - } - - - @Override - protected void paintComponent(final Graphics g) - { - super.paintComponent(g); - - if (originalImage == null) - { - return; - } - - Dimension availDim = getSize(); - int availWidth = availDim.width; - int availHeight = availDim.height; - - if ((availWidth == 0) || (availHeight == 0)) - { - return; - } - - if (!scaleDimension.equals(availDim)) - { - scale(availWidth, availHeight); - scaleDimension = availDim; - } - - int imgWidth = scaledImage.getIconWidth(); - int imgHeight = scaledImage.getIconHeight(); - - // Center the image if it does not perfectly fit the available space - int xOffset = Math.max((availWidth - imgWidth) / 2, 0); - int yOffset = Math.max((availHeight - imgHeight) / 2, 0); - - g.drawImage(scaledImage.getImage(), xOffset, yOffset, null); - } - - - private void scale(final int width, final int height) - { - int imgWidth = originalImage.getIconWidth(); - int imgHeight = originalImage.getIconHeight(); - - double widthRatio = (double) width / imgWidth; - double heightRatio = (double) height / imgHeight; - double ratio = Math.min(widthRatio, heightRatio); - - int finalHeight = (int) (imgHeight * ratio); - int finalWidth = (int) (imgWidth * ratio); - scaledImage = ImageScaler.scaleImageIconSmooth(originalImage, finalWidth, finalHeight); - } - - - /** - * @param image - */ - public void setImage(final ImageIcon image) - { - originalImage = image; - scaledImage = image; - scaleDimension = new Dimension(); - repaint(); - } - - - /** - * @return - */ - public ImageIcon getImage() - { - return originalImage; - } -} diff --git a/modules/common-gui/src/main/java/edu/tigers/sumatra/views/ASumatraPresenter.java b/modules/common-gui/src/main/java/edu/tigers/sumatra/views/ASumatraPresenter.java deleted file mode 100644 index 66c9b8ce..00000000 --- a/modules/common-gui/src/main/java/edu/tigers/sumatra/views/ASumatraPresenter.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2009 - 2022, DHBW Mannheim - TIGERs Mannheim - */ - -package edu.tigers.sumatra.views; - -public abstract class ASumatraPresenter implements ISumatraPresenter -{ - private boolean initialized; - - - @Override - public void onStart() - { - if (!initialized) - { - initialized = true; - onInit(); - } - ISumatraPresenter.super.onStart(); - } - - - protected void onInit() - { - // can be overwritten - } -} diff --git a/modules/sumatra-gui-replay/src/main/java/edu/tigers/sumatra/presenter/replay/ReplayPresenter.java b/modules/sumatra-gui-replay/src/main/java/edu/tigers/sumatra/presenter/replay/ReplayPresenter.java index 337614b7..9368480f 100644 --- a/modules/sumatra-gui-replay/src/main/java/edu/tigers/sumatra/presenter/replay/ReplayPresenter.java +++ b/modules/sumatra-gui-replay/src/main/java/edu/tigers/sumatra/presenter/replay/ReplayPresenter.java @@ -330,7 +330,7 @@ private void updateReplayTime() updateEndTime(); if (playing) { - replayCurTime += ((System.nanoTime() - replayLastTime) * speed); + replayCurTime += Math.round((System.nanoTime() - replayLastTime) * speed); if (replayCurTime < 0) { replayCurTime = 0;