Skip to content

Commit

Permalink
Simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Dec 15, 2024
1 parent 8b0672e commit afa6255
Show file tree
Hide file tree
Showing 25 changed files with 144 additions and 162 deletions.
4 changes: 2 additions & 2 deletions src/main/java/mpo/dayon/assistant/control/ControlEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected void execute() {
});
}

private int getActingMouseButton(final int button) {
private static int getActingMouseButton(final int button) {
if (MouseEvent.BUTTON1 == button) {
return NetworkMouseControlMessage.BUTTON1;
}
Expand All @@ -91,7 +91,7 @@ protected void execute() {
/**
* Fix missing pair'd PRESSED event from RELEASED
*/
private final HashMap<Integer, Character> pressedKeys = new HashMap<>();
private final HashMap<Integer, Character> pressedKeys = new HashMap<>(4);

/**
* From AWT thread (!)
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/mpo/dayon/assistant/gui/Assistant.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ public class Assistant implements ClipboardOwner {

private final NetworkAssistantEngine networkEngine;

private final ClipboardDispatcher clipboardDispatcher;

private BitCounter receivedBitCounter;

private TileCounter receivedTileCounter;
Expand Down Expand Up @@ -145,7 +143,6 @@ public Assistant(String tokenServerUrl, String language) {
Log.warn("Could not set the [" + lnf + "] L&F!", ex);
}
initGui();
clipboardDispatcher = new ClipboardDispatcher();
}

private void initGui() {
Expand Down Expand Up @@ -306,7 +303,7 @@ public void actionPerformed(ActionEvent ev) {
}

private void sendLocalClipboard() {
clipboardDispatcher.sendClipboard(networkEngine, frame, this);
ClipboardDispatcher.sendClipboard(networkEngine, frame, this);
}

/**
Expand Down Expand Up @@ -411,7 +408,7 @@ private void updateCaptureConfiguration(CaptureEngineConfiguration newCaptureEng
}
}

private Gray8Bits toGrayLevel(int value) {
private static Gray8Bits toGrayLevel(int value) {
return Gray8Bits.values()[6 - value];
}

Expand Down Expand Up @@ -504,7 +501,7 @@ public void actionPerformed(ActionEvent ev) {
return configure;
}

private String validatePurgeValue(JTextField purgeSizeTf, int maxValue) {
private static String validatePurgeValue(JTextField purgeSizeTf, int maxValue) {
final String purge = purgeSizeTf.getText();
if (purge.isEmpty()) {
return translate("compression.cache.purge.msg1");
Expand Down Expand Up @@ -708,7 +705,7 @@ public boolean isUpnpEnabled() {
synchronized (upnpEnabledLOCK) {
while (upnpEnabled == null) {
try {
upnpEnabledLOCK.wait();
upnpEnabledLOCK.wait(5000);
} catch (InterruptedException e) {
Log.warn("Swallowed", e);
Thread.currentThread().interrupt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public boolean equals(Object o) {
return false;
}
final AssistantConfiguration that = (AssistantConfiguration) o;
return language.equals(that.getLanguage());
return language.equals(that.language);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/mpo/dayon/assistant/gui/AssistantFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ private JTabbedPane createTabbedPane() {
return tabbedPane;
}

private Component createTokenButton(Action tokenAction) {
private static Component createTokenButton(Action tokenAction) {
String token = (String) tokenAction.getValue("token");
JButton button = createButton(tokenAction);
if (token != null) {
Expand All @@ -290,7 +290,7 @@ private Component createTokenButton(Action tokenAction) {
return button;
}

private StatusBar createStatusBar(ArrayList<Counter<?>> counters) {
private static StatusBar createStatusBar(ArrayList<Counter<?>> counters) {
final StatusBar statusBar = new StatusBar();
final Component horizontalStrut = Box.createHorizontalStrut(10);
statusBar.add(horizontalStrut);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void cancel() {
fireOnDisconnecting();
}

public void manageRouterPorts(int oldPort, int newPort) {
public static void manageRouterPorts(int oldPort, int newPort) {
if (!UPnP.isUPnPAvailable()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public boolean equals(Object o) {

final NetworkAssistantEngineConfiguration that = (NetworkAssistantEngineConfiguration) o;

return port == that.getPort() && tokenServerUrl.equals(that.getTokenServerUrl());
return port == that.port && tokenServerUrl.equals(that.tokenServerUrl);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public interface NetworkUtilities {
static List<String> getInetAddresses() {
final List<String> addresses = new ArrayList<>();
final List<String> addresses = new ArrayList<>(8);

try {
InetAddress loopback = null;
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/mpo/dayon/assisted/gui/Assisted.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import mpo.dayon.common.gui.common.ImageUtilities;
import mpo.dayon.common.log.Log;
import mpo.dayon.common.network.ClipboardDispatcher;
import mpo.dayon.common.network.NetworkEngine;
import mpo.dayon.common.network.message.*;

import javax.swing.*;
Expand Down Expand Up @@ -55,8 +56,6 @@ public class Assisted implements Subscriber, ClipboardOwner {

private NetworkAssistedEngine networkEngine;

private final ClipboardDispatcher clipboardDispatcher;

private boolean coldStart = true;

private CaptureEngineConfiguration captureEngineConfiguration;
Expand Down Expand Up @@ -84,7 +83,6 @@ public Assisted(String tokenServerUrl) {
} catch (Exception ex) {
Log.warn(format("Could not set the L&F [%s]", lnf), ex);
}
clipboardDispatcher = new ClipboardDispatcher();
}

/**
Expand Down Expand Up @@ -171,14 +169,14 @@ private boolean requestConnectionSettings() {
return ok;
}

private String validateIpAddress(String ipAddress) {
private static String validateIpAddress(String ipAddress) {
if (ipAddress.isEmpty()) {
return translate("connection.settings.emptyIpAddress");
}
return isValidIpAddressOrHostName(ipAddress.trim()) ? null : translate("connection.settings.invalidIpAddress");
}

private String validatePortNumber(String portNumber) {
private static String validatePortNumber(String portNumber) {
if (portNumber.isEmpty()) {
return translate("connection.settings.emptyPortNumber");
}
Expand Down Expand Up @@ -316,7 +314,7 @@ private void stop(String serverName) {
frame.onDisconnecting();
}

private NetworkAssistedEngineConfiguration extractConfiguration(String connectionParams) {
private static NetworkAssistedEngineConfiguration extractConfiguration(String connectionParams) {
if (connectionParams != null) {
int portStart = connectionParams.lastIndexOf('*');
if (portStart > 0) {
Expand Down Expand Up @@ -395,13 +393,13 @@ private void onCompressorEngineConfigured(NetworkCompressorConfigurationMessage
*/
private void onClipboardRequested() {
Log.info("Clipboard transfer request received");
clipboardDispatcher.sendClipboard(networkEngine, frame, this);
ClipboardDispatcher.sendClipboard(networkEngine, frame, this);
}

private void onScreenshotRequested(){
Log.info("Screenshot request received");
try {
networkEngine.setClipboardContents(new Robot().createScreenCapture(ScreenUtilities.getSharedScreenSize()), this);
NetworkEngine.setClipboardContents(new Robot().createScreenCapture(ScreenUtilities.getSharedScreenSize()), this);
} catch (AWTException e) {
Log.error("Failed to capture screen", e);
}
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/mpo/dayon/assisted/utils/ScreenUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ private ScreenUtilities() {
}
}

public static synchronized void setShareAllScreens(boolean doShareAllScreens) {
shareAllScreens = doShareAllScreens;
sharedScreenSize = doShareAllScreens ? COMBINED_SCREEN_SIZE : DEFAULT_SIZE;
rgb = new int[sharedScreenSize.height * sharedScreenSize.width];
gray = new byte[rgb.length];
public static void setShareAllScreens(boolean doShareAllScreens) {
synchronized (ScreenUtilities.class) {
shareAllScreens = doShareAllScreens;
sharedScreenSize = doShareAllScreens ? COMBINED_SCREEN_SIZE : DEFAULT_SIZE;
rgb = new int[sharedScreenSize.height * sharedScreenSize.width];
gray = new byte[rgb.length];
}
}

public static Rectangle getSharedScreenSize() {
Expand Down
34 changes: 18 additions & 16 deletions src/main/java/mpo/dayon/common/babylon/Babylon.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,27 @@ public final class Babylon {
private Babylon() {
}

public static synchronized String translate(String tag, Object... arguments) {
final Locale locale = Locale.getDefault();
final ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE, locale);
String value;
try {
value = bundle.getString(tag);
if (value.trim().isEmpty()) {
public static String translate(String tag, Object... arguments) {
synchronized (Babylon.class) {
final Locale locale = Locale.getDefault();
final ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE, locale);
String value;
try {
value = bundle.getString(tag);
if (value.trim().isEmpty()) {
value = tag;
}
} catch (MissingResourceException ignored) {
value = tag;
}
} catch (MissingResourceException ignored) {
value = tag;
}
if (arguments != null && arguments.length > 0) {
value = formatValue(locale, value, tag, arguments);
}
if (value != null) {
return value.trim();
if (arguments != null && arguments.length > 0) {
value = formatValue(locale, value, tag, arguments);
}
if (value != null) {
return value.trim();
}
return null;
}
return null;
}

@java.lang.SuppressWarnings("squid:S4973")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public boolean equals(Object o) {

final CaptureEngineConfiguration that = (CaptureEngineConfiguration) o;

return captureTick == that.getCaptureTick() && captureQuantization == that.getCaptureQuantization() && captureColors == that.captureColors;
return captureTick == that.captureTick && captureQuantization == that.captureQuantization && captureColors == that.captureColors;
}

@Override
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/mpo/dayon/common/gui/common/BaseFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ private void updateInputLocale() {
}
}

protected JButton createButton(Action action) {
protected static JButton createButton(Action action) {
return createButton(action, true);
}

protected JButton createButton(Action action, boolean visible) {
protected static JButton createButton(Action action, boolean visible) {
final JButton button = new JButton();
addButtonProperties(action, button);
button.setVisible(visible);
Expand All @@ -161,7 +161,7 @@ protected JToggleButton createToggleButton(Action action) {
return createToggleButton(action, true);
}

protected JToggleButton createToggleButton(Action action, boolean visible) {
protected static JToggleButton createToggleButton(Action action, boolean visible) {
final JToggleButton button = new JToggleButton();
addButtonProperties(action, button);
button.setVisible(visible);
Expand All @@ -174,7 +174,7 @@ protected JToggleButton createToggleButton(Action action, boolean visible, boole
return button;
}

private void addButtonProperties(Action action, AbstractButton button) {
private static void addButtonProperties(Action action, AbstractButton button) {
button.setMargin(ZERO_INSETS);
button.setHideActionText(true);
button.setAction(action);
Expand Down Expand Up @@ -401,7 +401,7 @@ private JPanel createPanel(JTextField addressTextField, JTextField portNumberTex
return panel;
}

private String toUpperFirst(String text) {
private static String toUpperFirst(String text) {
return Pattern.compile("^.").matcher(text).replaceFirst(m -> m.group().toUpperCase());
}

Expand All @@ -428,7 +428,7 @@ private String validateInputFields(JTextField addressTextField, JTextField portN
return null;
}

private void updateAssistedNetworkConfiguration(JTextField addressTextField, JTextField portNumberTextField, JCheckBox autoConnectCheckBox, String newTokenServerUrl) {
private static void updateAssistedNetworkConfiguration(JTextField addressTextField, JTextField portNumberTextField, JCheckBox autoConnectCheckBox, String newTokenServerUrl) {
final NetworkAssistedEngineConfiguration newNetworkConfiguration = new NetworkAssistedEngineConfiguration(
addressTextField.getText().trim(), Integer.parseInt(portNumberTextField.getText()), autoConnectCheckBox.isSelected(), newTokenServerUrl);

Expand All @@ -437,35 +437,35 @@ private void updateAssistedNetworkConfiguration(JTextField addressTextField, JTe
}
}

private void updateAssistantNetworkConfiguration(JTextField portNumberTextField, String newTokenServerUrl, NetworkAssistantEngine networkEngine) {
private static void updateAssistantNetworkConfiguration(JTextField portNumberTextField, String newTokenServerUrl, NetworkAssistantEngine networkEngine) {
final NetworkAssistantEngineConfiguration newNetworkConfiguration = new NetworkAssistantEngineConfiguration(
Integer.parseInt(portNumberTextField.getText()), newTokenServerUrl);

NetworkAssistantEngineConfiguration networkConfiguration = new NetworkAssistantEngineConfiguration();
if (!newNetworkConfiguration.equals(networkConfiguration)) {
networkEngine.manageRouterPorts(networkConfiguration.getPort(), newNetworkConfiguration.getPort());
NetworkAssistantEngine.manageRouterPorts(networkConfiguration.getPort(), newNetworkConfiguration.getPort());
newNetworkConfiguration.persist();
networkEngine.reconfigure(newNetworkConfiguration);
}
}

private void updateSystemProperty(String newTokenServerUrl) {
private static void updateSystemProperty(String newTokenServerUrl) {
if (newTokenServerUrl.isEmpty()) {
System.clearProperty("dayon.custom.tokenServer");
return;
}
System.setProperty("dayon.custom.tokenServer", newTokenServerUrl);
}

private GridBagConstraints createGridBagConstraints(int gridy) {
private static GridBagConstraints createGridBagConstraints(int gridy) {
GridBagConstraints gc = new GridBagConstraints();
gc.fill = HORIZONTAL;
gc.gridx = 0;
gc.gridy = gridy;
return gc;
}

private boolean isActiveTokenServer(String tokenServer) {
private static boolean isActiveTokenServer(String tokenServer) {
CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(() -> {
try {
// HttpClient doesn't implement AutoCloseable nor close before Java 21!
Expand Down Expand Up @@ -575,7 +575,7 @@ protected static JLabel getFingerprints() {
return fingerprints;
}

protected void clearFingerprints() {
protected static void clearFingerprints() {
fingerprints.setText(null);
fingerprints.setIcon(null);
fingerprints.setCursor(null);
Expand Down
Loading

0 comments on commit afa6255

Please sign in to comment.