Skip to content

Commit

Permalink
Fix hint system
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Nov 25, 2023
1 parent 2f163f7 commit 05247ab
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
*/
package net.pistonmaster.serverwrecker.gui;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
Expand All @@ -29,6 +33,7 @@
import java.util.Properties;

public class GUIClientProps {
private static final Logger LOGGER = LoggerFactory.getLogger(GUIClientProps.class);
private static final Path SETTINGS_PATH = GUIManager.DATA_FOLDER.resolve("gui-data.properties");
private static final Properties SETTINGS = new Properties();

Expand All @@ -42,26 +47,25 @@ public static void loadSettings() {

try (var is = Files.newInputStream(SETTINGS_PATH)) {
SETTINGS.load(new InputStreamReader(is, StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
} catch (IOException e) {
LOGGER.error("Failed to load settings!", e);
}
}

public static void saveSettings() {
try (var os = Files.newOutputStream(SETTINGS_PATH, StandardOpenOption.CREATE)) {
SETTINGS.store(new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8)), "ServerWrecker GUI Settings");
} catch (Exception e) {
e.printStackTrace();
} catch (IOException e) {
LOGGER.error("Failed to save settings!", e);
}
}

public static boolean getBoolean(String key, boolean def) {
return Boolean.parseBoolean(SETTINGS.getProperty(key, String.valueOf(def)));
return Boolean.parseBoolean(getString(key, String.valueOf(def)));
}

public static void setBoolean(String key, boolean value) {
SETTINGS.setProperty(key, String.valueOf(value));
saveSettings();
setString(key, String.valueOf(value));
}

public static String getString(String key, String def) {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/net/pistonmaster/serverwrecker/gui/GUIFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import javax.swing.*;
import java.awt.*;
import java.util.Objects;

public class GUIFrame extends JFrame {
public static final String MAIN_MENU = "MainMenu";
Expand All @@ -41,7 +42,7 @@ public static void showHints(Injector injector) {

var commandsHint = new HintManager.Hint(
"Use \"help\" to get a list of all commands.",
(Component) logPanel.getClientProperty("log-panel-command-input"),
(Component)Objects.requireNonNull( logPanel.getClientProperty("log-panel-command-input")),
SwingConstants.TOP, "hint.commandInput", null);

var controlsHint = new HintManager.Hint(
Expand All @@ -51,22 +52,22 @@ public static void showHints(Injector injector) {

var pluginsHint = new HintManager.Hint(
"Click to configure plugins to make the attack more effective.",
(Component) cardContainer.getClientProperty("plugin-menu-button"),
(Component)Objects.requireNonNull( cardContainer.getClientProperty("plugin-menu-button")),
SwingConstants.BOTTOM, "hint.pluginsButton", controlsHint);

var proxyHint = new HintManager.Hint(
"Click to import HTTP, SOCKS4 and SOCKS5 proxies",
(Component) cardContainer.getClientProperty("proxy-menu-button"),
(Component)Objects.requireNonNull( cardContainer.getClientProperty("proxy-menu-button")),
SwingConstants.LEFT, "hint.proxyButton", pluginsHint);

var accountsHint = new HintManager.Hint(
"Click to configure the bot offline-mode key format or bring your own accounts.",
(Component) cardContainer.getClientProperty("account-menu-button"),
(Component) Objects.requireNonNull(cardContainer.getClientProperty("account-menu-button")),
SwingConstants.RIGHT, "hint.accountsButton", proxyHint);

var settingsHint = new HintManager.Hint(
"Click to configure host, port, version and more.",
(Component) cardContainer.getClientProperty("settings-menu-button"),
(Component) Objects.requireNonNull(cardContainer.getClientProperty("bot-button")),
SwingConstants.BOTTOM, "hint.settingsButton", accountsHint);

var logsHint = new HintManager.Hint(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.nio.file.Path;

public class ThemeUtil {
public static final Path THEME_PATH = ServerWreckerBootstrap.DATA_FOLDER.resolve("theme.json");
private static final Logger LOGGER = LoggerFactory.getLogger(ThemeUtil.class);

private ThemeUtil() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ private HintManager() {
public static void showHint(Hint hint) {
// check whether user already closed the hint
if (GUIClientProps.getBoolean(hint.prefsKey, false)) {
if (hint.nextHint != null)
System.out.println("HintManager: hint '" + hint.prefsKey + "' was already closed." + hint.nextHint);
if (hint.nextHint != null) {
showHint(hint.nextHint);
}

return;
}

Expand All @@ -69,14 +72,13 @@ public static void hideAllHints() {
}
}

//---- class HintPanel ----------------------------------------------------
@RequiredArgsConstructor
public static class Hint {
private final String message;
private final Component owner;
private final int position;
private final String prefsKey;
private final Hint nextHint;
public record Hint(
String message,
Component owner,
int position,
String prefsKey,
Hint nextHint
) {
}

//---- class HintPanel ----------------------------------------------------
Expand All @@ -99,7 +101,7 @@ private HintPanel(Hint hint) {
setOpaque(false);
updateBalloonBorder();

hintLabel.setText("<html>" + hint.message + "</html>");
hintLabel.setText(SwingTextUtils.htmlText(hint.message));

// grab all mouse events to avoid that components overlapped
// by the hint panel receive them
Expand Down Expand Up @@ -135,6 +137,10 @@ private void updateBalloonBorder() {
}

void showHint() {
if (hint.owner == null) {
throw new IllegalStateException("hint owner is null");
}

var rootPane = SwingUtilities.getRootPane(hint.owner);
if (rootPane == null) {
return;
Expand Down Expand Up @@ -224,8 +230,7 @@ private void initComponents() {
// columns
"[::200,fill]",
// rows
"[]para" +
"[]"));
"[]para[]"));

//---- hintLabel ----
hintLabel.setText("hint");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package net.pistonmaster.serverwrecker.gui.libs;

import org.intellij.lang.annotations.Language;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
Expand All @@ -28,6 +30,8 @@
import java.net.URISyntaxException;

public class SwingTextUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(SwingTextUtils.class);

private SwingTextUtils() {
}

Expand All @@ -38,14 +42,13 @@ public static JTextPane createPane(@Language("html") String text) {
pane.setEditable(false);
pane.setBackground(null);
pane.setBorder(null);
pane.addHyperlinkListener(e -> {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
if (Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().browse(e.getURL().toURI());
} catch (IOException | URISyntaxException e1) {
e1.printStackTrace();
}
pane.addHyperlinkListener(event -> {
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED
&& Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().browse(event.getURL().toURI());
} catch (IOException | URISyntaxException e) {
LOGGER.error("Failed to open link!", e);
}
}
});
Expand All @@ -54,6 +57,10 @@ public static JTextPane createPane(@Language("html") String text) {
}

public static String htmlCenterText(String text) {
return "<html><center>" + text + "</center></html>";
return htmlText("<center>" + text + "</center>");
}

public static String htmlText(String text) {
return "<html>" + text + "</html>";
}
}

0 comments on commit 05247ab

Please sign in to comment.