From 80e629589462b2947641cf352a03d9643f1df906 Mon Sep 17 00:00:00 2001 From: "Peter A. Jonsson" Date: Sun, 28 Aug 2022 01:08:27 +0200 Subject: [PATCH] Cooja: default to a modern look and feel Add a command-line option to select look and feel, and set the default to FlatLaf. This allows us to remove the fallback code for setting look and feel, so the user either gets what they requested, or a failure. Setting dragMode leaves residue from the borders when moving windows on OS X, so use the default dragMode. --- build.gradle | 2 ++ java/org/contikios/cooja/Cooja.java | 5 ++-- java/org/contikios/cooja/GUI.java | 42 ++++++++++++----------------- java/org/contikios/cooja/Main.java | 9 ++++++- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/build.gradle b/build.gradle index 5d44b4afc..ccc9bd167 100644 --- a/build.gradle +++ b/build.gradle @@ -22,6 +22,8 @@ dependencies { implementation 'ch.qos.logback:logback-classic:1.4.11' // https://mvnrepository.com/artifact/com.github.cliftonlabs/json-simple implementation 'com.github.cliftonlabs:json-simple:4.0.1' + // https://mvnrepository.com/artifact/com.formdev/flatlaf + implementation 'com.formdev:flatlaf:3.2' // https://mvnrepository.com/artifact/de.sciss/syntaxpane implementation 'de.sciss:syntaxpane:1.3.0' // https://mvnrepository.com/artifact/info.picocli/picocli diff --git a/java/org/contikios/cooja/Cooja.java b/java/org/contikios/cooja/Cooja.java index dab56c9de..6e2c79b6e 100644 --- a/java/org/contikios/cooja/Cooja.java +++ b/java/org/contikios/cooja/Cooja.java @@ -161,7 +161,7 @@ public static Cooja makeCooja() throws ParseProjectsException { return new RunnableInEDT() { @Override public Cooja work() { - GUI.setLookAndFeel(); + GUI.setLookAndFeel(configuration.lookAndFeel); try { return new Cooja(); } catch (ParseProjectsException e) { @@ -1685,7 +1685,8 @@ public static void loadQuickHelp(final Object obj) { * When SimConfig contains an identical field, these values are the default * values when creating a new simulation in the File menu. */ - public record Config(LogbackColors logColors, boolean vis, String externalToolsConfig, + public record Config(LogbackColors logColors, boolean vis, GUI.LookAndFeel lookAndFeel, + String externalToolsConfig, String logDir, String contikiPath, String coojaPath, String javac) {} public record LogbackColors(String error, String warn, String info, String fallback) {} diff --git a/java/org/contikios/cooja/GUI.java b/java/org/contikios/cooja/GUI.java index b8ef56602..a2f7dba81 100644 --- a/java/org/contikios/cooja/GUI.java +++ b/java/org/contikios/cooja/GUI.java @@ -27,6 +27,7 @@ package org.contikios.cooja; +import com.formdev.flatlaf.FlatLightLaf; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; @@ -115,6 +116,9 @@ /** The graphical user interface for Cooja. */ public class GUI { + /** The look and feel of Cooja. */ + enum LookAndFeel { CrossPlatform, FlatLaf, Nimbus, System } + private static final Logger logger = LoggerFactory.getLogger(GUI.class); static final String WINDOW_TITLE = "Cooja: The Contiki Network Simulator"; @@ -168,7 +172,10 @@ public void endDraggingFrame(JComponent f) { updateDesktopSize(); } }); - myDesktopPane.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); + // Dragging windows on OS X leaves residue from the borders with FlatLaf, so avoid setting dragMode. + if (cooja.configuration.lookAndFeel() != LookAndFeel.FlatLaf) { + myDesktopPane.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); + } frame = new JFrame(WINDOW_TITLE); // Help panel. @@ -1789,34 +1796,19 @@ private static boolean warnMemory() { return n != JOptionPane.YES_OPTION; } - static void setLookAndFeel() { + static void setLookAndFeel(LookAndFeel lookAndFeel) { JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); ToolTipManager.sharedInstance().setDismissDelay(60000); - // Nimbus. try { - String osName = System.getProperty("os.name").toLowerCase(); - if (osName.startsWith("linux")) { - try { - for (var info : UIManager.getInstalledLookAndFeels()) { - if ("Nimbus".equals(info.getName())) { - UIManager.setLookAndFeel(info.getClassName()); - break; - } - } - } catch (UnsupportedLookAndFeelException e) { - UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); - } - } else { - UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); - } - } catch (Exception e) { - // System. - try { - UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); - } catch (Exception e2) { - throw new RuntimeException("Failed to set look and feel", e2); - } + switch (lookAndFeel) { + case CrossPlatform -> UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); + case FlatLaf -> UIManager.setLookAndFeel(new FlatLightLaf()); + case Nimbus -> UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); + case System -> UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } + } catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException | IllegalAccessException e) { + throw new RuntimeException("Failed to set look and feel", e); } } diff --git a/java/org/contikios/cooja/Main.java b/java/org/contikios/cooja/Main.java index c81def80b..68d7b19a8 100644 --- a/java/org/contikios/cooja/Main.java +++ b/java/org/contikios/cooja/Main.java @@ -59,6 +59,12 @@ "JVM: ${java.version} (${java.vendor} ${java.vm.name} ${java.vm.version})", "OS: ${os.name} ${os.version} ${os.arch}"}, sortOptions = false, sortSynopsis = false) class Main { + /** + * Option for specifying look and feel. + */ + @Option(names = "--look-and-feel", paramLabel = "LookAndFeel", description = "one of: ${COMPLETION-CANDIDATES}") + GUI.LookAndFeel lookAndFeel = GUI.LookAndFeel.FlatLaf; + /** * Option for specifying if a GUI should be used. */ @@ -154,6 +160,7 @@ class Main { public static void main(String[] args) { Main options = new Main(); CommandLine commandLine = new CommandLine(options); + commandLine.setCaseInsensitiveEnumValuesAllowed(true); try { commandLine.parseArgs(args); } catch (CommandLine.ParameterException e) { @@ -309,7 +316,7 @@ public static void main(String[] args) { // Use colors that are good on a dark background and readable on a white background. var colors = new LogbackColors(ANSIConstants.BOLD + "91", "96", ANSIConstants.GREEN_FG, ANSIConstants.DEFAULT_FG); - var cfg = new Config(colors, options.gui, options.externalUserConfig, + var cfg = new Config(colors, options.gui, options.lookAndFeel, options.externalUserConfig, options.logDir, options.contikiPath, options.coojaPath, options.javac); Cooja.go(cfg, simConfigs); } else { // Start MSPSim.