[fixes #825] Add preference choice to UpdatePlatform

AssetHandler is also rewritten a bit to get this feature to work
This commit is contained in:
Sibo Van Gool 2022-01-27 23:38:50 +01:00
parent 5d5b0a13ce
commit daa309c27d
3 changed files with 82 additions and 30 deletions

View File

@ -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 <sibo.vangool@hotmail.com>
*/
public class AssetHandler {
private static final Map<String, Platform> mapExtensionToPlatform = new HashMap<>(); // Map file extensions to operating platform
private static final Map<Platform, String> 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<String, UpdatePlatform> mapExtensionToPlatform = new HashMap<>(); // Map file extensions to operating platform
private static final Map<UpdatePlatform, String> 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<String, String> mapURLToPlatformName(List<String> urls) {
Map<String, String> output = new TreeMap<>();
public static Map<UpdatePlatform, String> mapURLToPlatform(List<String> urls) {
Map<UpdatePlatform, String> 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);
}
}

View File

@ -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<String> assetURLs = release.getAssetURLs();
Map<String, String> mappedAssets = AssetHandler.mapURLToPlatformName(assetURLs);
JComboBox<String> comboBox;
Map<UpdatePlatform, String> mappedAssets = AssetHandler.mapURLToPlatform(assetURLs);
JComboBox<Object> 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;
}
}
}

View File

@ -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.
* <p>