Skip to content

Commit

Permalink
Clear token on config change
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Dec 26, 2024
1 parent e2a5093 commit bae30d6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src/main/java/mpo/dayon/assistant/gui/Assistant.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public Assistant(String tokenServerUrl, String language) {
private void initGui() {
createCounters();
if (frame != null) {
frame.setVisible(false);
frame.dispose();
}
frame = new AssistantFrame(createAssistantActions(), counters, createLanguageSelection(), compatibilityModeActive.get(), this);
FatalErrorHandler.attachFrame(frame);
Expand Down Expand Up @@ -191,6 +191,14 @@ private AssistantActions createAssistantActions() {
return assistantActions;
}

public void clearToken() {
token = null;
JButton button = (JButton) frame.getActions().getTokenAction().getValue("button");
if (button != null) {
button.setText("");
}
}

private void stopNetwork() {
frame.hideSpinner();
networkEngine.cancel();
Expand Down Expand Up @@ -549,6 +557,7 @@ private Action createTokenAction() {
@Override
public void actionPerformed(ActionEvent ev) {
final JButton button = (JButton) ev.getSource();
this.putValue("button", button);

if (token == null) {
CompletableFuture.supplyAsync(() -> {
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/mpo/dayon/assistant/gui/AssistantFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class AssistantFrame extends BaseFrame {

private final JButton screenshotButton;

private final JButton tokenButton;

private final AtomicBoolean controlActivated = new AtomicBoolean(false);

private final AtomicBoolean windowsKeyActivated = new AtomicBoolean(false);
Expand Down Expand Up @@ -97,6 +99,7 @@ class AssistantFrame extends BaseFrame {
this.actions.setNetworkConfigurationAction(createConnectionSettingsAction(assistant));
this.startButton = createButton(actions.getStartAction());
this.stopButton = createButton(actions.getStopAction(), false);
this.tokenButton = createTokenButton(actions.getTokenAction());
this.compatibilityToggleButton = createToggleButton(actions.getToggleCompatibilityModeAction(), true, compatibilityModeActive);
this.controlToggleButton = createToggleButton(createToggleControlMode());
this.fitToScreenToggleButton = createToggleButton(createToggleFixScreenAction());
Expand All @@ -118,6 +121,10 @@ class AssistantFrame extends BaseFrame {
onReady();
}

public AssistantActions getActions() {
return actions;
}

private void addListeners() {
addFocusListener();
addKeyListeners();
Expand Down Expand Up @@ -248,7 +255,7 @@ private JTabbedPane createTabbedPane() {
connectionPanel.setBorder(BorderFactory.createEmptyBorder(0, 8, 0, 0));
connectionPanel.add(startButton);
connectionPanel.add(stopButton);
connectionPanel.add(createTokenButton(actions.getTokenAction()));
connectionPanel.add(tokenButton);
connectionPanel.add(createButton(actions.getIpAddressAction()));
connectionPanel.add(compatibilityToggleButton);

Expand Down Expand Up @@ -280,7 +287,7 @@ private JTabbedPane createTabbedPane() {
return tabbedPane;
}

private static Component createTokenButton(Action tokenAction) {
private static JButton createTokenButton(Action tokenAction) {
String token = (String) tokenAction.getValue("token");
JButton button = createButton(tokenAction);
if (token != null) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/mpo/dayon/common/gui/common/BaseFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public void actionPerformed(ActionEvent ev) {
if (assistant == null) {
updateAssistedNetworkConfiguration(addressTextField, portNumberTextField, autoConnectCheckBox, newTokenServerUrl);
} else {
updateAssistantNetworkConfiguration(portNumberTextField, newTokenServerUrl, assistant.getNetworkEngine());
updateAssistantNetworkConfiguration(portNumberTextField, newTokenServerUrl, assistant);
}
}
}
Expand Down Expand Up @@ -438,15 +438,16 @@ private static void updateAssistedNetworkConfiguration(JTextField addressTextFie
}
}

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

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

Expand Down

0 comments on commit bae30d6

Please sign in to comment.