Skip to content

Commit

Permalink
Fix theme settings storage
Browse files Browse the repository at this point in the history
SettingsManager is no longer suitable for this, and thus GUIClientProps will be the replacement for the foreseeable future.
  • Loading branch information
AlexProgrammerDE committed Nov 23, 2023
1 parent e9266d5 commit b13a259
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import net.pistonmaster.serverwrecker.grpc.RPCClient;
import net.pistonmaster.serverwrecker.gui.GUIClientProps;
import net.pistonmaster.serverwrecker.gui.GUIManager;
import net.pistonmaster.serverwrecker.gui.theme.ThemeUtil;
import net.pistonmaster.serverwrecker.gui.ThemeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,13 @@ public static void setBoolean(String key, boolean value) {
SETTINGS.setProperty(key, String.valueOf(value));
saveSettings();
}

public static String getString(String key, String def) {
return SETTINGS.getProperty(key, def);
}

public static void setString(String key, String value) {
SETTINGS.setProperty(key, value);
saveSettings();
}
}
14 changes: 8 additions & 6 deletions src/main/java/net/pistonmaster/serverwrecker/gui/SWMenuBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
import net.pistonmaster.serverwrecker.api.event.gui.WindowCloseEvent;
import net.pistonmaster.serverwrecker.gui.libs.JFXFileHelper;
import net.pistonmaster.serverwrecker.gui.popups.AboutPopup;
import net.pistonmaster.serverwrecker.gui.theme.ThemeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.swing.*;
Expand All @@ -45,6 +46,7 @@
import java.util.List;

public class SWMenuBar extends JMenuBar {
private static final Logger LOGGER = LoggerFactory.getLogger(SWMenuBar.class);
private static final List<Class<? extends BasicLookAndFeel>> THEMES;

static {
Expand Down Expand Up @@ -83,9 +85,9 @@ public SWMenuBar(GUIManager guiManager) {

try {
guiManager.getSettingsManager().loadProfile(file);
SettingsManager.LOGGER.info("Loaded profile!");
LOGGER.info("Loaded profile!");
} catch (IOException ex) {
SettingsManager.LOGGER.warn("Failed to load profile!", ex);
LOGGER.warn("Failed to load profile!", ex);
}
}, guiManager.getThreadPool());
});
Expand All @@ -110,9 +112,9 @@ public SWMenuBar(GUIManager guiManager) {

try {
guiManager.getSettingsManager().saveProfile(Path.of(path));
SettingsManager.LOGGER.info("Saved profile!");
LOGGER.info("Saved profile!");
} catch (IOException ex) {
SettingsManager.LOGGER.warn("Failed to save profile!", ex);
LOGGER.warn("Failed to save profile!", ex);
}
}, guiManager.getThreadPool());
});
Expand All @@ -131,7 +133,7 @@ public SWMenuBar(GUIManager guiManager) {
for (var theme : THEMES) {
var themeItem = new JMenuItem(theme.getSimpleName());
themeItem.addActionListener(e -> {
ThemeUtil.THEME_PROVIDER.setThemeClass(theme);
GUIClientProps.setString("theme", theme.getName());
SwingUtilities.invokeLater(ThemeUtil::setLookAndFeel);
});
themeSelector.add(themeItem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
*/
package net.pistonmaster.serverwrecker.gui.theme;
package net.pistonmaster.serverwrecker.gui;

import com.formdev.flatlaf.FlatDarculaLaf;
import com.formdev.flatlaf.FlatLaf;
Expand All @@ -29,29 +29,12 @@

import javax.swing.*;
import javax.swing.plaf.basic.BasicLookAndFeel;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;

public class ThemeUtil {
public static final Path THEME_PATH = ServerWreckerBootstrap.DATA_FOLDER.resolve("theme.json");
public static final SettingsManager THEME_MANAGER = new SettingsManager(Map.of("theme", ThemeSettings.class));
public static final ThemeProvider THEME_PROVIDER = new ThemeProvider(FlatDarculaLaf.class);
private static final Logger LOGGER = LoggerFactory.getLogger(ThemeUtil.class);

static {
THEME_MANAGER.registerDuplex(ThemeSettings.class, THEME_PROVIDER);

if (Files.exists(THEME_PATH)) {
try {
THEME_MANAGER.loadProfile(THEME_PATH);
} catch (IOException e) {
LOGGER.error("Failed to load theme settings!", e);
}
}
}

private ThemeUtil() {
}

Expand All @@ -61,13 +44,13 @@ private ThemeUtil() {
* You need to invoke SwingUtilities.updateComponentTreeUI(frame); after this method.
*/
public static void setLookAndFeel() {
var themeSettings = THEME_MANAGER.collectSettings().get(ThemeSettings.class);
if (themeSettings.themeClass().equals(UIManager.getLookAndFeel().getClass().getName())) {
var themeSettings = GUIClientProps.getString("theme", FlatDarculaLaf.class.getName());
if (themeSettings.equals(UIManager.getLookAndFeel().getClass().getName())) {
return;
}

try {
var theme = Class.forName(themeSettings.themeClass())
var theme = Class.forName(themeSettings)
.asSubclass(BasicLookAndFeel.class).getDeclaredConstructor().newInstance();

FlatAnimatedLafChange.showSnapshot();
Expand All @@ -80,12 +63,6 @@ public static void setLookAndFeel() {
} catch (UnsupportedLookAndFeelException | ReflectiveOperationException e) {
LOGGER.error("Failed to set theme!", e);
}

try {
THEME_MANAGER.saveProfile(THEME_PATH);
} catch (IOException e) {
LOGGER.error("Failed to save theme settings!", e);
}
}

public static void initFlatLaf() {
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit b13a259

Please sign in to comment.