Skip to content

Commit d3373ff

Browse files
authored
Merge pull request #65 from tferr/flatlaf
OptionsLookAndFeel: Support for FlatLaf
2 parents d8ce567 + b0bdd2c commit d3373ff

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
<!-- NB: Deploy releases to the SciJava Maven repository. -->
111111
<releaseProfiles>sign,deploy-to-scijava</releaseProfiles>
112112

113+
<flatlaf.version>2.4</flatlaf.version>
113114
<jdatepicker.version>1.3.2</jdatepicker.version>
114115
<object-inspector.version>0.1</object-inspector.version>
115116
</properties>
@@ -157,6 +158,11 @@
157158
<groupId>org.jfree</groupId>
158159
<artifactId>jfreesvg</artifactId>
159160
</dependency>
161+
<dependency>
162+
<groupId>com.formdev</groupId>
163+
<artifactId>flatlaf</artifactId>
164+
<version>${flatlaf.version}</version>
165+
</dependency>
160166
<dependency>
161167
<groupId>com.github.sbridges.object-inspector</groupId>
162168
<artifactId>object-inspector</artifactId>

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)