Skip to content

Commit

Permalink
Add language selection in ui closes #79
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Feb 15, 2024
1 parent 9d4cd3e commit af5555f
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 55 deletions.
85 changes: 48 additions & 37 deletions src/main/java/mpo/dayon/assistant/gui/Assistant.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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<Counter<?>> counters;

private AssistantFrame frame;

private final AssistantActions actions;
private AssistantActions actions;

private AssistantConfiguration configuration;

Expand Down Expand Up @@ -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()));
Expand All @@ -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<Counter<?>> 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());
Expand All @@ -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 {
Expand Down Expand Up @@ -660,22 +661,32 @@ public void actionPerformed(ActionEvent ev) {
return compatibilityMode;
}

public JComboBox<String> createLanguageSelection() {
final JComboBox<String> languageSelection = new JComboBox<>(getSupportedLanguages());
private JComboBox<Language> createLanguageSelection() {
final JComboBox<Language> 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<String, String> {
@Override
protected String doInBackground() {
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/mpo/dayon/assistant/gui/AssistantFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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;

Expand Down Expand Up @@ -80,9 +80,9 @@ class AssistantFrame extends BaseFrame {

private JTabbedPane tabbedPane;

private final JComboBox<String> languageSelection;
private final JComboBox<Language> languageSelection;

AssistantFrame(AssistantActions actions, Set<Counter<?>> counters, JComboBox<String> languageSelection) {
AssistantFrame(AssistantActions actions, Set<Counter<?>> counters, JComboBox<Language> languageSelection) {
RepeatingReleasedEventsFixer.install();
super.setFrameType(FrameType.ASSISTANT);
this.actions = actions;
Expand Down Expand Up @@ -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));
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/mpo/dayon/common/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,7 +62,7 @@ static Map<String, String> 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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mpo/dayon/common/gui/common/BaseFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/mpo/dayon/common/utils/Language.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
5 changes: 0 additions & 5 deletions src/main/java/mpo/dayon/common/utils/SystemUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit af5555f

Please sign in to comment.