From af5555fa4f2a32c80981be667cab2f7eeb033f4e Mon Sep 17 00:00:00 2001 From: retgal Date: Thu, 15 Feb 2024 06:43:31 +0100 Subject: [PATCH] Add language selection in ui closes #79 --- .../mpo/dayon/assistant/gui/Assistant.java | 85 +++++++++++-------- .../dayon/assistant/gui/AssistantFrame.java | 19 +++-- src/main/java/mpo/dayon/common/Runner.java | 3 +- .../dayon/common/gui/common/BaseFrame.java | 4 +- .../common/gui/common/FrameConfiguration.java | 9 +- .../dayon/common/gui/statusbar/StatusBar.java | 2 +- .../java/mpo/dayon/common/utils/Language.java | 30 +++++++ .../dayon/common/utils/SystemUtilities.java | 5 -- 8 files changed, 102 insertions(+), 55 deletions(-) create mode 100644 src/main/java/mpo/dayon/common/utils/Language.java diff --git a/src/main/java/mpo/dayon/assistant/gui/Assistant.java b/src/main/java/mpo/dayon/assistant/gui/Assistant.java index 6b5a738d..9d3911f8 100644 --- a/src/main/java/mpo/dayon/assistant/gui/Assistant.java +++ b/src/main/java/mpo/dayon/assistant/gui/Assistant.java @@ -20,6 +20,7 @@ import mpo.dayon.common.network.message.NetworkMouseLocationMessageHandler; import mpo.dayon.common.squeeze.CompressionMethod; import mpo.dayon.common.network.FileUtilities; +import mpo.dayon.common.utils.Language; import javax.swing.*; import java.awt.*; @@ -58,19 +59,21 @@ public class Assistant implements ClipboardOwner { private final NetworkAssistantEngine network; - private final BitCounter receivedBitCounter; + private BitCounter receivedBitCounter; - private final TileCounter receivedTileCounter; + private TileCounter receivedTileCounter; - private final SkippedTileCounter skippedTileCounter; + private SkippedTileCounter skippedTileCounter; - private final MergedTileCounter mergedTileCounter; + private MergedTileCounter mergedTileCounter; - private final CaptureCompressionCounter captureCompressionCounter; + private CaptureCompressionCounter captureCompressionCounter; + + private Set> counters; private AssistantFrame frame; - private final AssistantActions actions; + private AssistantActions actions; private AssistantConfiguration configuration; @@ -103,6 +106,7 @@ public Assistant(String tokenServerUrl, String language) { } this.configuration = new AssistantConfiguration(); + // has not been overridden by command line if (language == null) { if (!Locale.getDefault().getLanguage().equals(configuration.getLanguage())) { Locale.setDefault(new Locale(configuration.getLanguage())); @@ -111,36 +115,11 @@ public Assistant(String tokenServerUrl, String language) { initUpnp(); - this.configuration = new AssistantConfiguration(); - if (language == null) { - if (!Locale.getDefault().getLanguage().equals(configuration.getLanguage())) { - Locale.setDefault(new Locale(configuration.getLanguage())); - } - } - - receivedBitCounter = new BitCounter("receivedBits", translate("networkBandwidth")); - receivedBitCounter.start(1000); - - receivedTileCounter = new TileCounter("receivedTiles", translate("receivedTileNumber")); - receivedTileCounter.start(1000); - - skippedTileCounter = new SkippedTileCounter("skippedTiles", translate("skippedCaptureNumber")); - skippedTileCounter.start(1000); - - mergedTileCounter = new MergedTileCounter("mergedTiles", translate("mergedCaptureNumber")); - mergedTileCounter.start(1000); - - captureCompressionCounter = new CaptureCompressionCounter("captureCompression", translate("captureCompression")); - captureCompressionCounter.start(1000); - - Set> counters = new HashSet<>(Arrays.asList(receivedBitCounter, receivedTileCounter, skippedTileCounter, mergedTileCounter, captureCompressionCounter)); - DeCompressorEngine decompressor = new DeCompressorEngine(new MyDeCompressorEngineListener()); decompressor.start(8); NetworkMouseLocationMessageHandler mouseHandler = mouse -> frame.onMouseLocationUpdated(mouse.getX(), mouse.getY()); network = new NetworkAssistantEngine(decompressor, mouseHandler, this); - networkConfiguration = new NetworkAssistantEngineConfiguration(); network.configure(networkConfiguration); network.addListener(new MyNetworkAssistantEngineListener()); @@ -154,14 +133,36 @@ public Assistant(String tokenServerUrl, String language) { } catch (Exception ex) { Log.warn("Could not set the [" + lnf + "] L&F!", ex); } + initGui(); + } + + private void initGui() { + createCounters(); actions = createAssistantActions(); + if (frame != null) { + frame.setVisible(false); + } frame = new AssistantFrame(actions, counters, createLanguageSelection()); FatalErrorHandler.attachFrame(frame); frame.addListener(new ControlEngine(network)); frame.setVisible(true); } + private void createCounters() { + receivedBitCounter = new BitCounter("receivedBits", translate("networkBandwidth")); + receivedBitCounter.start(1000); + receivedTileCounter = new TileCounter("receivedTiles", translate("receivedTileNumber")); + receivedTileCounter.start(1000); + skippedTileCounter = new SkippedTileCounter("skippedTiles", translate("skippedCaptureNumber")); + skippedTileCounter.start(1000); + mergedTileCounter = new MergedTileCounter("mergedTiles", translate("mergedCaptureNumber")); + mergedTileCounter.start(1000); + captureCompressionCounter = new CaptureCompressionCounter("captureCompression", translate("captureCompression")); + captureCompressionCounter.start(1000); + counters = new HashSet<>(Arrays.asList(receivedBitCounter, receivedTileCounter, skippedTileCounter, mergedTileCounter, captureCompressionCounter)); + } + private boolean isUpnpEnabled() { while (upnpEnabled == null) { try { @@ -660,22 +661,32 @@ public void actionPerformed(ActionEvent ev) { return compatibilityMode; } - public JComboBox createLanguageSelection() { - final JComboBox languageSelection = new JComboBox<>(getSupportedLanguages()); + private JComboBox createLanguageSelection() { + final JComboBox languageSelection = new JComboBox<>(Language.values()); + languageSelection.setMaximumRowCount(languageSelection.getItemCount()); languageSelection.setBorder(BorderFactory.createEmptyBorder(7, 3, 6, 2)); languageSelection.setFocusable(false); - languageSelection.setSelectedItem(Locale.getDefault().getLanguage()); - languageSelection.setToolTipText("TODO"); + languageSelection.setSelectedItem(Arrays.stream(Language.values()).filter(e -> e.getShortName().equals(Locale.getDefault().getLanguage())).findFirst().orElse(Language.en)); + languageSelection.setRenderer(new LanguageRenderer()); languageSelection.addActionListener(ev -> { Locale.setDefault(new Locale(languageSelection.getSelectedItem().toString())); - Log.info(format("New Locale %s", Locale.getDefault().getLanguage())); - configuration = new AssistantConfiguration(getDefaultLookAndFeel(), languageSelection.getSelectedItem().toString()); + Log.info(format("New language %s", Locale.getDefault().getLanguage())); + configuration = new AssistantConfiguration(getDefaultLookAndFeel(), Locale.getDefault().getLanguage()); configuration.persist(); + initGui(); } ); return languageSelection; } + private static class LanguageRenderer extends DefaultListCellRenderer { + @Override + public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { + super.getListCellRendererComponent(list, ((Language) value).getName(), index, isSelected, cellHasFocus); + return this; + } + } + private class NetWorker extends SwingWorker { @Override protected String doInBackground() { diff --git a/src/main/java/mpo/dayon/assistant/gui/AssistantFrame.java b/src/main/java/mpo/dayon/assistant/gui/AssistantFrame.java index 9276c60c..c9397df8 100644 --- a/src/main/java/mpo/dayon/assistant/gui/AssistantFrame.java +++ b/src/main/java/mpo/dayon/assistant/gui/AssistantFrame.java @@ -8,6 +8,7 @@ import mpo.dayon.common.gui.toolbar.ToolBar; import mpo.dayon.common.log.Log; import mpo.dayon.common.monitoring.counter.Counter; +import mpo.dayon.common.utils.Language; import javax.swing.*; import java.awt.*; @@ -16,7 +17,6 @@ import java.io.IOException; import java.net.Socket; import java.time.Instant; -import java.util.Locale; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; @@ -80,9 +80,9 @@ class AssistantFrame extends BaseFrame { private JTabbedPane tabbedPane; - private final JComboBox languageSelection; + private final JComboBox languageSelection; - AssistantFrame(AssistantActions actions, Set> counters, JComboBox languageSelection) { + AssistantFrame(AssistantActions actions, Set> counters, JComboBox languageSelection) { RepeatingReleasedEventsFixer.install(); super.setFrameType(FrameType.ASSISTANT); this.actions = actions; @@ -362,26 +362,33 @@ void onReady() { hideSpinner(); validate(); repaint(); + // connection actions.getStartAction().setEnabled(true); actions.getStopAction().setEnabled(false); startButton.setVisible(true); stopButton.setVisible(false); - actions.getNetworkConfigurationAction().setEnabled(true); actions.getToggleCompatibilityModeAction().setEnabled(true); actions.getIpAddressAction().setEnabled(true); - actions.getCaptureEngineConfigurationAction().setEnabled(true); + // session actions.getResetAction().setEnabled(false); + // settings + actions.getNetworkConfigurationAction().setEnabled(true); + actions.getCaptureEngineConfigurationAction().setEnabled(true); + languageSelection.setEnabled(true); disableControls(); getStatusBar().setMessage(translate("ready")); } void onHttpStarting(int port) { + // connection startButton.setVisible(false); actions.getStopAction().setEnabled(true); stopButton.setVisible(true); - actions.getNetworkConfigurationAction().setEnabled(false); actions.getToggleCompatibilityModeAction().setEnabled(false); actions.getIpAddressAction().setEnabled(false); + // settings + actions.getNetworkConfigurationAction().setEnabled(false); + languageSelection.setEnabled(false); toolbar.clearFingerprints(); getStatusBar().setMessage(translate("listening", port)); } diff --git a/src/main/java/mpo/dayon/common/Runner.java b/src/main/java/mpo/dayon/common/Runner.java index e8aed67b..d6a4c1db 100644 --- a/src/main/java/mpo/dayon/common/Runner.java +++ b/src/main/java/mpo/dayon/common/Runner.java @@ -4,6 +4,7 @@ import mpo.dayon.assisted.AssistedRunner; import mpo.dayon.common.error.FatalErrorHandler; import mpo.dayon.common.log.Log; +import mpo.dayon.common.utils.Language; import javax.swing.*; import java.io.File; @@ -61,7 +62,7 @@ static Map extractProgramArgs(String[] args) { } static String overrideLocale(String arg) { - if (arg != null && Arrays.stream(getSupportedLanguages()).anyMatch(e -> e.equalsIgnoreCase(arg))) { + if (arg != null && Arrays.stream(Language.values()).map(Language::getShortName).anyMatch(e -> e.equalsIgnoreCase(arg))) { Locale.setDefault(new Locale(arg)); return arg; } diff --git a/src/main/java/mpo/dayon/common/gui/common/BaseFrame.java b/src/main/java/mpo/dayon/common/gui/common/BaseFrame.java index 050d93b1..000a4c79 100644 --- a/src/main/java/mpo/dayon/common/gui/common/BaseFrame.java +++ b/src/main/java/mpo/dayon/common/gui/common/BaseFrame.java @@ -22,14 +22,14 @@ public abstract class BaseFrame extends JFrame { + protected final Object[] OK_CANCEL_OPTIONS = {translate("cancel"), translate("ok")}; + private static final String HTTP_HOME = "https://github.com/retgal/dayon"; private static final String HTTP_SUPPORT = "https://retgal.github.io/Dayon/" + translate("support.html"); private static final String HTTP_FEEDBACK = HTTP_HOME + "/issues"; - protected static final Object[] OK_CANCEL_OPTIONS = {translate("cancel"), translate("ok")}; - private transient FrameConfiguration configuration; private transient Position position; diff --git a/src/main/java/mpo/dayon/common/gui/common/FrameConfiguration.java b/src/main/java/mpo/dayon/common/gui/common/FrameConfiguration.java index 937580af..24adeb5a 100644 --- a/src/main/java/mpo/dayon/common/gui/common/FrameConfiguration.java +++ b/src/main/java/mpo/dayon/common/gui/common/FrameConfiguration.java @@ -4,6 +4,9 @@ import java.awt.*; +import static mpo.dayon.common.gui.common.FrameType.ASSISTANT; +import static mpo.dayon.common.gui.common.FrameType.ASSISTED; + class FrameConfiguration { private int version = 1; @@ -36,10 +39,10 @@ public int getHeight() { final Preferences prefs = Preferences.getPreferences(); version = prefs.getIntPreference(type.getPrefix() + PREF_VERSION_SUFFIX, 0); position = new Position(prefs.getIntPreference(type.getPrefix() + PREF_X_SUFFIX, 100), prefs.getIntPreference(type.getPrefix() + PREF_Y_SUFFIX, 100)); - if (type.equals(FrameType.ASSISTANT)) { - dimension = new Dimension(prefs.getIntPreference(type.getPrefix() + PREF_WIDTH_SUFFIX, 800), prefs.getIntPreference(type.getPrefix() + PREF_HEIGHT_SUFFIX, 600)); + if (type.equals(ASSISTANT)) { + dimension = new Dimension(prefs.getIntPreference(type.getPrefix() + PREF_WIDTH_SUFFIX, ASSISTANT.getMinWidth()), prefs.getIntPreference(type.getPrefix() + PREF_HEIGHT_SUFFIX, ASSISTANT.getMinHeight())); } else { - dimension = new Dimension(prefs.getIntPreference(type.getPrefix() + PREF_WIDTH_SUFFIX, 550), prefs.getIntPreference(type.getPrefix() + PREF_HEIGHT_SUFFIX, 200)); + dimension = new Dimension(prefs.getIntPreference(type.getPrefix() + PREF_WIDTH_SUFFIX, ASSISTED.getMinWidth()), prefs.getIntPreference(type.getPrefix() + PREF_HEIGHT_SUFFIX, ASSISTED.getMinHeight())); } } diff --git a/src/main/java/mpo/dayon/common/gui/statusbar/StatusBar.java b/src/main/java/mpo/dayon/common/gui/statusbar/StatusBar.java index a79b0851..736b5635 100644 --- a/src/main/java/mpo/dayon/common/gui/statusbar/StatusBar.java +++ b/src/main/java/mpo/dayon/common/gui/statusbar/StatusBar.java @@ -44,7 +44,7 @@ public void setSessionDuration(String sessionDuration) { public void setKeyboardLayout(String keyboardLayout) { this.keyboardLayout.setText(keyboardLayout); - this.keyboardLayout.setToolTipText(format("\u2328 %s", keyboardLayout)); + this.keyboardLayout.setToolTipText(format("⌨ %s", keyboardLayout)); } public String getKeyboardLayout() { diff --git a/src/main/java/mpo/dayon/common/utils/Language.java b/src/main/java/mpo/dayon/common/utils/Language.java new file mode 100644 index 00000000..61658e54 --- /dev/null +++ b/src/main/java/mpo/dayon/common/utils/Language.java @@ -0,0 +1,30 @@ +package mpo.dayon.common.utils; + +public enum Language { + de("de", "Deutsch"), + en("en", "English"), + es("es", "Español"), + fr("fr", "Français"), + it("it", "Italiano"), + ru("ru", "Русский"), + sv("sv", "Svenska"), + tr("tr", "Türkçe"), + zh("zh", "简体中文"); + + private final String shortName; + private final String name; + + Language(String shortName, String name) { + this.shortName = shortName; + this.name = name; + } + + public String getName() { + return name; + } + + public String getShortName() { + return shortName; + } + +} diff --git a/src/main/java/mpo/dayon/common/utils/SystemUtilities.java b/src/main/java/mpo/dayon/common/utils/SystemUtilities.java index ecb4d318..b254e4e8 100644 --- a/src/main/java/mpo/dayon/common/utils/SystemUtilities.java +++ b/src/main/java/mpo/dayon/common/utils/SystemUtilities.java @@ -26,15 +26,10 @@ public final class SystemUtilities { public static final String FLATPAK_BROWSER = "/app/bin/dayon.browser"; private static final String JAVA_VENDOR = "java.vendor"; public static final String DEFAULT_TOKEN_SERVER_URL = "https://fensterkitt.ch/dayon/"; - private static final String[] SUPPORTED_LANGUAGES = {"de", "en", "es", "fr", "it", "ru", "sv", "tr", "zh"}; private SystemUtilities() { } - public static String[] getSupportedLanguages() { - return SUPPORTED_LANGUAGES.clone(); - } - public static URI getQuickStartURI(String quickstartPage, String section) { return URI.create(format("http://retgal.github.io/Dayon/%s#%s-setup", quickstartPage, section)); }