From daa309c27dc7009ddd1e8b1e924093907c5c3ec5 Mon Sep 17 00:00:00 2001 From: Sibo Van Gool Date: Thu, 27 Jan 2022 23:38:50 +0100 Subject: [PATCH] [fixes #825] Add preference choice to UpdatePlatform AssetHandler is also rewritten a bit to get this feature to work --- .../communication/AssetHandler.java | 59 ++++++++++++------- .../gui/dialogs/UpdateInfoDialog.java | 39 +++++++++--- .../openrocket/gui/util/SwingPreferences.java | 14 ++++- 3 files changed, 82 insertions(+), 30 deletions(-) diff --git a/swing/src/net/sf/openrocket/communication/AssetHandler.java b/swing/src/net/sf/openrocket/communication/AssetHandler.java index c61aec22f..0e00ab42d 100644 --- a/swing/src/net/sf/openrocket/communication/AssetHandler.java +++ b/swing/src/net/sf/openrocket/communication/AssetHandler.java @@ -1,7 +1,7 @@ package net.sf.openrocket.communication; -import net.sf.openrocket.arch.SystemInfo; -import net.sf.openrocket.arch.SystemInfo.Platform; +import net.sf.openrocket.gui.util.SwingPreferences; +import net.sf.openrocket.startup.Application; import java.util.HashMap; import java.util.List; @@ -14,18 +14,26 @@ import java.util.TreeMap; * @author Sibo Van Gool */ public class AssetHandler { - private static final Map mapExtensionToPlatform = new HashMap<>(); // Map file extensions to operating platform - private static final Map mapPlatformToName = new HashMap<>(); // Map operating platform to a name - static { - mapExtensionToPlatform.put(".dmg", Platform.MAC_OS); - mapExtensionToPlatform.put(".exe", Platform.WINDOWS); - mapExtensionToPlatform.put(".AppImage", Platform.UNIX); - mapExtensionToPlatform.put(".jar", null); + private static final Map mapExtensionToPlatform = new HashMap<>(); // Map file extensions to operating platform + private static final Map mapPlatformToName = new HashMap<>(); // Map operating platform to a name - mapPlatformToName.put(Platform.MAC_OS, "Mac OS"); - mapPlatformToName.put(Platform.WINDOWS, "Windows"); - mapPlatformToName.put(Platform.UNIX, "Linux"); - mapPlatformToName.put(null, "JAR"); + public enum UpdatePlatform { + WINDOWS, + MAC_OS, + LINUX, + JAR + } + + static { + mapExtensionToPlatform.put(".dmg", UpdatePlatform.MAC_OS); + mapExtensionToPlatform.put(".exe", UpdatePlatform.WINDOWS); + mapExtensionToPlatform.put(".AppImage", UpdatePlatform.LINUX); + mapExtensionToPlatform.put(".jar", UpdatePlatform.JAR); + + mapPlatformToName.put(UpdatePlatform.MAC_OS, "Mac OS"); + mapPlatformToName.put(UpdatePlatform.WINDOWS, "Windows"); + mapPlatformToName.put(UpdatePlatform.LINUX, "Linux"); + mapPlatformToName.put(UpdatePlatform.JAR, "JAR"); } /** @@ -35,14 +43,14 @@ public class AssetHandler { * @param urls list of asset URLs * @return map with as key the operating platform name and as value the corresponding asset URL */ - public static Map mapURLToPlatformName(List urls) { - Map output = new TreeMap<>(); + public static Map mapURLToPlatform(List urls) { + Map output = new TreeMap<>(); if (urls == null) return null; for (String url : urls) { for (String ext : mapExtensionToPlatform.keySet()) { if (url.endsWith(ext)) { - output.put(mapPlatformToName.get(mapExtensionToPlatform.get(ext)), url); + output.put(mapExtensionToPlatform.get(ext), url); } } } @@ -50,13 +58,20 @@ public class AssetHandler { } /** - * Returns the operating platform name based on the operating system that the user is running on, or the value + * Returns the operating platform based on the operating system that the user is running on, or the value * stored in preferences. - * @return operating platform name + * @return operating platform */ - public static String getPlatformName() { - Platform currentPlatform = SystemInfo.getPlatform(); - // TODO: select right option based on preference - return mapPlatformToName.get(currentPlatform); + public static UpdatePlatform getUpdatePlatform() { + return ((SwingPreferences) Application.getPreferences()).getUpdatePlatform(); + } + + /** + * Get the name of a platform (e.g. for Platform.MAC_OS, return "Mac OS") + * @param platform platform to get the name from + * @return name of the platform + */ + public static String getPlatformName(UpdatePlatform platform) { + return mapPlatformToName.get(platform); } } diff --git a/swing/src/net/sf/openrocket/gui/dialogs/UpdateInfoDialog.java b/swing/src/net/sf/openrocket/gui/dialogs/UpdateInfoDialog.java index feadafc8b..da22da924 100644 --- a/swing/src/net/sf/openrocket/gui/dialogs/UpdateInfoDialog.java +++ b/swing/src/net/sf/openrocket/gui/dialogs/UpdateInfoDialog.java @@ -1,5 +1,6 @@ package net.sf.openrocket.gui.dialogs; +import java.awt.Component; import java.awt.Desktop; import java.awt.Dimension; import java.awt.event.ActionEvent; @@ -13,14 +14,17 @@ import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JLabel; +import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextPane; import javax.swing.event.HyperlinkEvent; import javax.swing.event.HyperlinkListener; +import javax.swing.plaf.basic.BasicComboBoxRenderer; import net.miginfocom.swing.MigLayout; import net.sf.openrocket.communication.AssetHandler; +import net.sf.openrocket.communication.AssetHandler.UpdatePlatform; import net.sf.openrocket.communication.ReleaseInfo; import net.sf.openrocket.communication.UpdateInfo; import net.sf.openrocket.gui.util.GUIUtil; @@ -113,18 +117,26 @@ public class UpdateInfoDialog extends JDialog { // Install operating system combo box List assetURLs = release.getAssetURLs(); - Map mappedAssets = AssetHandler.mapURLToPlatformName(assetURLs); - JComboBox comboBox; + Map mappedAssets = AssetHandler.mapURLToPlatform(assetURLs); + JComboBox comboBox; if (mappedAssets == null || mappedAssets.size() == 0) { comboBox = new JComboBox<>(new String[]{ String.format("- %s -", trans.get("update.dlg.updateAvailable.combo.noDownloads"))}); } else { - comboBox = new JComboBox<>(mappedAssets.keySet().toArray(new String[0])); + comboBox = new JComboBox<>(mappedAssets.keySet().toArray(new UpdatePlatform[0])); + comboBox.setRenderer(new CustomComboBoxRenderer()); + UpdatePlatform platform = AssetHandler.getUpdatePlatform(); + // TODO: check select null? + comboBox.setSelectedItem(platform); + comboBox.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + ((SwingPreferences) Application.getPreferences()).setUpdatePlatform((UpdatePlatform) comboBox.getSelectedItem()); + } + }); } panel.add(comboBox, "pushx, right"); - String platformName = AssetHandler.getPlatformName(); - comboBox.setSelectedItem(platformName); // Install update button JButton btnInstall = new SelectColorButton(trans.get("update.dlg.updateAvailable.but.install")); @@ -132,7 +144,7 @@ public class UpdateInfoDialog extends JDialog { @Override public void actionPerformed(ActionEvent e) { if (mappedAssets == null) return; - String url = mappedAssets.get((String) comboBox.getSelectedItem()); + String url = mappedAssets.get((UpdatePlatform) comboBox.getSelectedItem()); Desktop desktop = Desktop.getDesktop(); try { desktop.browse(new URI(url)); @@ -164,5 +176,18 @@ public class UpdateInfoDialog extends JDialog { this.setLocationRelativeTo(null); GUIUtil.setDisposableDialogOptions(this, btnCancel); } - + + /** + * ComboBox renderer to display an UpdatePlatform by the platform name + */ + private static class CustomComboBoxRenderer extends BasicComboBoxRenderer { + @Override + public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { + super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); + if (value instanceof UpdatePlatform) { + setText(AssetHandler.getPlatformName((UpdatePlatform)value)); + } + return this; + } + } } diff --git a/swing/src/net/sf/openrocket/gui/util/SwingPreferences.java b/swing/src/net/sf/openrocket/gui/util/SwingPreferences.java index 3b79c012f..a43d5c8d2 100644 --- a/swing/src/net/sf/openrocket/gui/util/SwingPreferences.java +++ b/swing/src/net/sf/openrocket/gui/util/SwingPreferences.java @@ -15,6 +15,7 @@ import java.util.Set; import java.util.prefs.BackingStoreException; import java.util.prefs.Preferences; +import net.sf.openrocket.communication.AssetHandler.UpdatePlatform; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -247,7 +248,18 @@ public class SwingPreferences extends net.sf.openrocket.startup.Preferences { } return compdir; } - + + public void setUpdatePlatform(UpdatePlatform platform) { + if (platform == null) return; + putString("UpdatePlatform", platform.name()); + } + + public UpdatePlatform getUpdatePlatform() { + String p = getString("UpdatePlatform", SystemInfo.getPlatform().name()); + if (p == null) return null; + return UpdatePlatform.valueOf(p); + } + /** * Return a list of files/directories to be loaded as custom thrust curves. *