Skip to content

Commit 6eaa696

Browse files
committed
OptionsLookAndFeel: Support for FlatLaf
This implements support for FlatLaf Look and Feels. A static method is also included so that Look and Feel can be set at startup. E.g., for Fjii the following call in the StartupMacros could be used to set the Look and Feel programmatically at startup: eval("js","importClass(Packages.org.scijava.ui.swing.options.OptionsLookAndFeel);OptionsLookAndFeel.setupLookAndFeel('FlatLaf Dark');") NB: - Flatlaf depencency should be probably declared in the parent pom - There are currently several hardwired foreground/background colors in several classes (e.g., ConsolePanel) that will interfere with dark themes
1 parent 8414686 commit 6eaa696

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@
164164
<classifier>tests</classifier>
165165
<scope>test</scope>
166166
</dependency>
167-
167+
<dependency>
168+
<groupId>com.formdev</groupId>
169+
<artifactId>flatlaf</artifactId>
170+
<version>2.1</version>
171+
</dependency>
168172
</dependencies>
169173
</project>

src/main/java/org/scijava/ui/swing/options/OptionsLookAndFeel.java

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
import javax.swing.UIManager.LookAndFeelInfo;
4242
import javax.swing.UnsupportedLookAndFeelException;
4343

44+
import com.formdev.flatlaf.FlatLightLaf;
45+
import com.formdev.flatlaf.FlatDarkLaf;
46+
import com.formdev.flatlaf.FlatIntelliJLaf;
47+
import com.formdev.flatlaf.FlatDarculaLaf;
48+
4449
import org.scijava.display.Display;
4550
import org.scijava.display.DisplayService;
4651
import org.scijava.log.LogService;
@@ -131,8 +136,15 @@ public void run() {
131136

132137
protected void initLookAndFeel() {
133138
final String lafClass = UIManager.getLookAndFeel().getClass().getName();
139+
LookAndFeelInfo[] lookAndFeels = UIManager.getInstalledLookAndFeels();
140+
141+
// Make UIManager aware of FlatLaf look and feels, as needed
142+
if (!isRegistered(lookAndFeels, FlatLightLaf.NAME)) FlatLightLaf.installLafInfo();
143+
if (!isRegistered(lookAndFeels, FlatDarkLaf.NAME)) FlatDarkLaf.installLafInfo();
144+
if (!isRegistered(lookAndFeels, FlatDarculaLaf.NAME)) FlatDarculaLaf.installLafInfo();
145+
if (!isRegistered(lookAndFeels, FlatIntelliJLaf.NAME)) FlatIntelliJLaf.installLafInfo();
146+
lookAndFeels = UIManager.getInstalledLookAndFeels(); // retrieve updated list
134147

135-
final LookAndFeelInfo[] lookAndFeels = UIManager.getInstalledLookAndFeels();
136148
final ArrayList<String> lookAndFeelChoices = new ArrayList<>();
137149
for (final LookAndFeelInfo lafInfo : lookAndFeels) {
138150
final String lafName = lafInfo.getName();
@@ -149,6 +161,15 @@ protected void initLookAndFeel() {
149161

150162
// -- Helper methods --
151163

164+
/** Assesses whether lookAndFeels contains the laf associated with lafName*/
165+
private boolean isRegistered(final LookAndFeelInfo[] lookAndFeels, final String lafName) {
166+
for (final LookAndFeelInfo lafInfo : lookAndFeels) {
167+
if (lafInfo.getName().equals(lafName))
168+
return true;
169+
}
170+
return false;
171+
}
172+
152173
/** Tells all known Swing components to change to the new Look &amp; Feel. */
153174
private void refreshSwingComponents() {
154175
// TODO: Change this hacky logic to call a clean UIService API
@@ -202,6 +223,41 @@ private DisplayService displayService() {
202223
return getContext().service(DisplayService.class);
203224
}
204225

226+
/**
227+
* Sets the application look and feel.
228+
* <p>
229+
* Useful for setting up the look and feel early on in application startup
230+
* routine (e.g., through a macro or script)
231+
* </p>
232+
*
233+
* @param lookAndFeel the look and feel. Supported values include "FlatLaf
234+
* Light", "FlatLaf Dark", "FlatLaf Darcula", "FlatLaf
235+
* IntelliJ", and JVM defaults
236+
* ("javax.swing.plaf.metal.MetalLookAndFeel", etc.)
237+
*/
238+
public static void setupLookAndFeel(final String lookAndFeel) {
239+
switch (lookAndFeel) {
240+
case FlatLightLaf.NAME:
241+
FlatLightLaf.setup();
242+
return;
243+
case FlatDarkLaf.NAME:
244+
FlatDarkLaf.setup();
245+
return;
246+
case FlatDarculaLaf.NAME:
247+
FlatDarculaLaf.setup();
248+
return;
249+
case FlatIntelliJLaf.NAME:
250+
FlatIntelliJLaf.setup();
251+
return;
252+
default:
253+
try {
254+
UIManager.setLookAndFeel(lookAndFeel);
255+
} catch (final Exception ex) {
256+
ex.printStackTrace();
257+
}
258+
}
259+
}
260+
205261
// -- Deprecated methods --
206262

207263
@Deprecated

0 commit comments

Comments
 (0)