Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select language #94

Merged
merged 5 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/mpo/dayon/assistant/AssistantRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private static String[] appendAssistant(String[] args) {
return combined;
}

public static void launchAssistant() {
new Assistant(readPresetFile("assistant.yaml").get("tokenServerUrl"));
public static void launchAssistant(String language) {
new Assistant(readPresetFile("assistant.yaml").get("tokenServerUrl"), language);
}
}
93 changes: 64 additions & 29 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,19 @@ 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 AssistantFrame frame;
private Set<Counter<?>> counters;

private final AssistantActions actions;
private AssistantFrame frame;

private AssistantConfiguration configuration;

Expand All @@ -94,61 +95,69 @@ public class Assistant implements ClipboardOwner {

private final AtomicBoolean compatibilityModeActive = new AtomicBoolean(false);

public Assistant(String tokenServerUrl) {
public Assistant(String tokenServerUrl, String language) {
if (tokenServerUrl != null) {
this.tokenServerUrl = tokenServerUrl + PORT_PARAM;
System.setProperty("dayon.custom.tokenServer", tokenServerUrl);
} else {
this.tokenServerUrl = DEFAULT_TOKEN_SERVER_URL + PORT_PARAM;
}

initUpnp();

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);
this.configuration = new AssistantConfiguration();
// has not been overridden by command line
if (language == null && !Locale.getDefault().getLanguage().equals(configuration.getLanguage())) {
Locale.setDefault(new Locale(configuration.getLanguage()));
}

Set<Counter<?>> counters = new HashSet<>(Arrays.asList(receivedBitCounter, receivedTileCounter, skippedTileCounter, mergedTileCounter, captureCompressionCounter));
initUpnp();

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());

captureEngineConfiguration = new CaptureEngineConfiguration();
compressorEngineConfiguration = new CompressorEngineConfiguration();

this.configuration = new AssistantConfiguration();
final String lnf = configuration.getLookAndFeelClassName();
try {
UIManager.setLookAndFeel(lnf);
} catch (Exception ex) {
Log.warn("Could not set the [" + lnf + "] L&F!", ex);
}
initGui();

actions = createAssistantActions();
frame = new AssistantFrame(actions, counters);
}

private void initGui() {
createCounters();
if (frame != null) {
frame.setVisible(false);
}
frame = new AssistantFrame(createAssistantActions(), 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 @@ -647,6 +656,32 @@ public void actionPerformed(ActionEvent ev) {
return compatibilityMode;
}

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(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 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,44 @@
import mpo.dayon.common.configuration.Configuration;
import mpo.dayon.common.utils.SystemUtilities;

import java.util.Locale;

import static mpo.dayon.common.preference.Preferences.*;

public class AssistantConfiguration extends Configuration {
private static final String PREF_VERSION = "assistant.version";

private static final String PREF_LOOK_AND_FEEL = "assistant.lookAndFeel";

private static final String PREF_LANGUAGE = "assistant.language";

private final String lookAndFeelClassName;

private final String language;

/**
* Default : takes its values from the current preferences.
*
* @see mpo.dayon.common.preference.Preferences
*/
public AssistantConfiguration() {
lookAndFeelClassName = getPreferences().getStringPreference(PREF_LOOK_AND_FEEL, SystemUtilities.getDefaultLookAndFeel());
language = getPreferences().getStringPreference(PREF_LANGUAGE, Locale.getDefault().getLanguage());
}

AssistantConfiguration(String lookAndFeelClassName) {
AssistantConfiguration(String lookAndFeelClassName, String language) {
this.lookAndFeelClassName = lookAndFeelClassName;
this.language = language;
}

String getLookAndFeelClassName() {
return lookAndFeelClassName;
}

String getLanguage() {
return language;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -55,6 +67,7 @@ protected void persist(boolean clear) {
final Props props = new Props();
props.set(PREF_VERSION, String.valueOf(1));
props.set(PREF_LOOK_AND_FEEL, lookAndFeelClassName);
props.set(PREF_LANGUAGE, language);
getPreferences().update(props); // atomic (!)
}

Expand Down
22 changes: 17 additions & 5 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 Down Expand Up @@ -79,7 +80,9 @@ class AssistantFrame extends BaseFrame {

private JTabbedPane tabbedPane;

AssistantFrame(AssistantActions actions, Set<Counter<?>> counters) {
private final JComboBox<Language> languageSelection;

AssistantFrame(AssistantActions actions, Set<Counter<?>> counters, JComboBox<Language> languageSelection) {
RepeatingReleasedEventsFixer.install();
super.setFrameType(FrameType.ASSISTANT);
this.actions = actions;
Expand All @@ -90,6 +93,7 @@ class AssistantFrame extends BaseFrame {
this.keepAspectRatioToggleButton = createToggleButton(createToggleKeepAspectRatioAction(), false);
this.windowsKeyToggleButton = createToggleButton(createSendWindowsKeyAction());
this.ctrlKeyToggleButton = createToggleButton(createSendCtrlKeyAction());
this.languageSelection = languageSelection;
setupToolBar(createToolBar());
setupStatusBar(createStatusBar(counters));
assistantPanel = new AssistantPanel();
Expand Down Expand Up @@ -243,6 +247,7 @@ private JTabbedPane createTabbedPane() {
settingsPanel.add(createButton(actions.getCaptureEngineConfigurationAction()));
settingsPanel.add(createButton(actions.getCompressionEngineConfigurationAction()));
settingsPanel.add(createButton(actions.getNetworkConfigurationAction()));
settingsPanel.add(languageSelection);

tabbedPane = new JTabbedPane();
tabbedPane.addTab(translate("connection"), connectionPanel);
Expand Down Expand Up @@ -357,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 All @@ -394,7 +406,7 @@ private void showSpinner() {
boolean onAccepted(Socket connection) {
if (JOptionPane.showOptionDialog(this, translate("connection.incoming.msg1"),
translate("connection.incoming", connection.getInetAddress().getHostAddress()), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
getOrCreateIcon(ImageNames.USERS), OK_CANCEL_OPTIONS, OK_CANCEL_OPTIONS[1]) == 0) {
getOrCreateIcon(ImageNames.USERS), okCancelOptions, okCancelOptions[1]) == 0) {
return false;
}
hideSpinner();
Expand Down
12 changes: 7 additions & 5 deletions 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 All @@ -24,11 +25,11 @@ static void main(String[] args) {
Runner.setDebug(args);
final File appHomeDir = Runner.getOrCreateAppHomeDir();
Map<String, String> programArgs = Runner.extractProgramArgs(args);
Runner.overrideLocale(programArgs.get("lang"));
String language = Runner.overrideLocale(programArgs.get("lang"));
if (hasAssistant(args)) {
Runner.logAppInfo("dayon_assistant");
try {
SwingUtilities.invokeLater(AssistantRunner::launchAssistant);
SwingUtilities.invokeLater(() -> AssistantRunner.launchAssistant(language));
} catch (Exception ex) {
FatalErrorHandler.bye("The assistant is dead!", ex);
}
Expand Down Expand Up @@ -60,11 +61,12 @@ static Map<String, String> extractProgramArgs(String[] args) {
.collect(Collectors.toMap(pair -> pair[0], pair -> pair[1], (a, b) -> b));
}

static void overrideLocale(String arg) {
final String[] supported = {"de", "en", "es", "fr", "it", "ru", "sv", "tr", "zh"};
if (arg != null && Arrays.stream(supported).anyMatch(e -> e.equalsIgnoreCase(arg))) {
static String overrideLocale(String arg) {
if (arg != null && Arrays.stream(Language.values()).map(Language::getShortName).anyMatch(e -> e.equalsIgnoreCase(arg))) {
Locale.setDefault(new Locale(arg));
return arg;
}
return null;
}

static void setDebug(String[] args) {
Expand Down
8 changes: 4 additions & 4 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 transient Object[] okCancelOptions = {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 All @@ -56,8 +56,8 @@ public void windowClosing(WindowEvent ev) {

private void doExit() {
if (JOptionPane.showOptionDialog(this, translate("exit.confirm"), translate("exit"),
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, OK_CANCEL_OPTIONS,
OK_CANCEL_OPTIONS[1]) == 1) {
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, okCancelOptions,
okCancelOptions[1]) == 1) {
Log.info("Bye!");
System.exit(0);
}
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
Loading