Summary

SwingアプリケーションのLookAndFeelagentを使って外部から切り替えてデバッグします。

LookAndFeelDebugAgent.png

ソースコード

package swinghelper;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Optional;
import java.util.logging.Logger;
import javax.swing.*;
import com.formdev.flatlaf.*;

public class LookAndFeelDebugAgent {
  public static Component makeUI() {
    Box box = LookAndFeelUtils.createLookAndFeelBox();
    box.add(Box.createVerticalGlue());
    box.setBorder(BorderFactory.createEmptyBorder(5, 25, 5, 25));
    return box;
  }

  public static void premain(String args) {
    EventQueue.invokeLater(() -> {
      JFrame frame = new JFrame("LookAndFeelDebugAgent");
      frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(makeUI());
      frame.pack();
      frame.setVisible(true);
    });
  }
}

final class LookAndFeelUtils {
  private static String lookAndFeel = UIManager.getLookAndFeel().getClass().getName();

  private LookAndFeelUtils() {
    /* Singleton */
  }

  public static Box createLookAndFeelBox() {
    Box box = Box.createVerticalBox();
    ButtonGroup buttonGroup = new ButtonGroup();
    for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
      AbstractButton b = makeButton(info);
      initLookAndFeelAction(info, b);
      box.add(b);
      buttonGroup.add(b);
    }
    for (Class<?> clz : Arrays.asList(FlatLightLaf.class, FlatDarkLaf.class)) {
      String full = clz.getName();
      String name = full.substring(full.lastIndexOf('.') + 1);
      UIManager.LookAndFeelInfo info = new UIManager.LookAndFeelInfo(name, full);
      AbstractButton b = makeButton(info);
      initLookAndFeelAction(info, b);
      box.add(b);
      buttonGroup.add(b);
    }
    return box;
  }

  private static AbstractButton makeButton(UIManager.LookAndFeelInfo info) {
    boolean selected = info.getClassName().equals(lookAndFeel);
    return new JRadioButton(info.getName(), selected);
  }

  public static void initLookAndFeelAction(UIManager.LookAndFeelInfo info, AbstractButton b) {
    String cmd = info.getClassName();
    b.setText(info.getName());
    b.setActionCommand(cmd);
    b.setHideActionText(true);
    b.addActionListener(e -> setLookAndFeel(cmd));
  }

  private static void setLookAndFeel(String newLookAndFeel) {
    String oldLookAndFeel = lookAndFeel;
    if (!oldLookAndFeel.equals(newLookAndFeel)) {
      try {
        UIManager.setLookAndFeel(newLookAndFeel);
        lookAndFeel = newLookAndFeel;
      } catch (UnsupportedLookAndFeelException ignored) {
        Toolkit.getDefaultToolkit().beep();
      } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
        Logger.getGlobal().severe(ex::getMessage);
        return;
      }
      updateLookAndFeel();
    }
  }

  private static void updateLookAndFeel() {
    for (Window window : Window.getWindows()) {
      SwingUtilities.updateComponentTreeUI(window);
    }
  }
}

Explanation

  • アプリケーションと同時にLook And Feelを切り替えるための別パネルを起動して、対象アプリケーションのソースコードを変更することなくLookAndFeelの変更がテスト可能
  • このサンプルではデフォルト以外のLook And FeelとしてFlatLightLafFlatDarkLafを追加して以下のようにBoot-Class-Pathを指定してコンパイルしている
    <target name="package" depends="compile">
      <jar jarfile="${build.dir}/${name}-${version}.jar" basedir="${build.dest}">
        <manifest>
          <!--attribute name="Main-Class" value="${main.class}" /-->
          <!--attribute name="Agent-Class" value="${main.class}" /-->
          <attribute name="Premain-Class" value="${main.class}" />
          <attribute name="Class-Path" value="${runtime.classpath}" />
          <attribute name="Can-Retransform-Classes" value="true"/>
          <attribute name="Boot-Class-Path" value="flatlaf-3.5.jar" />
        </manifest>
      </jar>
    </target>
    
  • javaagentを使用するため、JDK 1.6以上で以下のように起動する
    java -javaagent:lnfagent1.0.1.jar -jar example.jar
    

Reference

Comment