improve some checks when switching look and feel

This commit is contained in:
OMGeeky
2025-07-15 12:51:49 +02:00
parent e6d9d8fbda
commit bc3333bd0e
2 changed files with 199 additions and 191 deletions

View File

@@ -70,22 +70,8 @@ public static final String APP_VERSION = readVersionFromFile();
ConfigCache.init(); ConfigCache.init();
try {
String laf = ConfigCache.getFavoriteLaFClassName(); String laf = ConfigCache.getFavoriteLaFClassName();
if (laf == null) setLookAndFeel(laf);
laf = UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(laf);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
scaleUIFont();
// Need to keep a strong reference to it, to avoid garbage collection that'll // Need to keep a strong reference to it, to avoid garbage collection that'll
// reset this setting. // reset this setting.
@@ -120,7 +106,9 @@ public static final String APP_VERSION = readVersionFromFile();
frame = new StudioFrame(APP_NAME + " " + APP_VERSION); frame = new StudioFrame(APP_NAME + " " + APP_VERSION);
frame.setVisible(true); frame.setVisible(true);
frame.setDefaultCloseOperation(StudioFrame.DO_NOTHING_ON_CLOSE); frame.setDefaultCloseOperation(StudioFrame.DO_NOTHING_ON_CLOSE);
}; }
;
}); });
for (File f : ConfigCache.getKnownWorkspaces()) { for (File f : ConfigCache.getKnownWorkspaces()) {
if (workspaceRoot.equals(f)) { if (workspaceRoot.equals(f)) {
@@ -138,6 +126,34 @@ public static final String APP_VERSION = readVersionFromFile();
}); });
} }
public static void setLookAndFeel(String laf) {
if (laf == null)
{
System.out.println("No look and feel specified, using system default.");
laf = UIManager.getSystemLookAndFeelClassName();
}
System.out.println("Info: Setting look and feel to: " + laf);
try {
UIManager.setLookAndFeel(laf);
} catch (ClassNotFoundException e) {
System.err.println("Failed to load system look and feel. ");
System.err.println("Installed look and feel classes: ");
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
System.err.println(" " + info.getName() + " (" + info.getClassName() + ")");
}
System.err.println("Tried to load: " + laf + " but got this error:");
e.printStackTrace();
} catch (InstantiationException | UnsupportedLookAndFeelException | IllegalAccessException e) {
e.printStackTrace();
}
var newLaF = UIManager.getLookAndFeel();
System.out.println("Using look and feel: " + newLaF.getName() + " (" + newLaF.getClass().getName() + ")");
scaleUIFont();
}
private static void checkUpdate() { private static void checkUpdate() {
BufferedReader in = null; BufferedReader in = null;
try { try {
@@ -226,6 +242,7 @@ public static final String APP_VERSION = readVersionFromFile();
} }
} }
} }
private static String readVersionFromFile() { private static String readVersionFromFile() {
try (BufferedReader reader = new BufferedReader(new InputStreamReader( try (BufferedReader reader = new BufferedReader(new InputStreamReader(
Objects.requireNonNull(ATContentStudio.class.getResourceAsStream("/ATCS_latest"))))) { Objects.requireNonNull(ATContentStudio.class.getResourceAsStream("/ATCS_latest"))))) {

View File

@@ -162,21 +162,12 @@ public class StudioFrame extends JFrame {
changeLaF.add(lafItem); changeLaF.add(lafItem);
lafItem.addActionListener(new ActionListener() { lafItem.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e1) {
try { String lookAndFeel = i.getClassName();
UIManager.setLookAndFeel(i.getClassName()); ATContentStudio.setLookAndFeel(lookAndFeel);
ATContentStudio.scaleUIFont();
SwingUtilities.updateComponentTreeUI(ATContentStudio.frame); SwingUtilities.updateComponentTreeUI(ATContentStudio.frame);
ConfigCache.setFavoriteLaFClassName(i.getClassName()); ConfigCache.setFavoriteLaFClassName(lookAndFeel);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
}
} }
}); });
} }