Appearance Sample Code

This sample program illustrates how information can be obtained from the object that is installed in the Aqua.appearance client property of every Swing component managed by VAqua. This program is designed to avoid relying on a shared class loader or even the presence of VAqua.


  public static void addListener(@NotNull JComponent c,
                                 @NotNull ChangeListener listener) {
    c.addPropertyChangeListener("Aqua.appearance", e->processChange(e, listener));
  }

  private static void processChange(@NotNull PropertyChangeEvent e,
                                    @NotNull ChangeListener listener) {
    ChangeEvent event = new ChangeEvent(e.getSource());
    listener.stateChanged(event);
  }

  public static boolean isDark(@NotNull JComponent c) {
    Object o = c.getClientProperty("Aqua.appearance");
    if (o != null) {
      try {
        Method m = o.getClass().getMethod("isDark");
        Object result = m.invoke(o);
        return Boolean.TRUE.equals(result);
      } catch (Exception ex) {
      }
    }
    return false;
  }

  public static boolean isHighContrast(@NotNull JComponent c) {
    Object o = c.getClientProperty("Aqua.appearance");
    if (o != null) {
      try {
        Method m = o.getClass().getMethod("isHighContrast");
        Object result = m.invoke(o);
        return Boolean.TRUE.equals(result);
      } catch (Exception ex) {
      }
    }
    return false;
  }

  public static @Nullable Map<String,Color> getColors(@NotNull JComponent c) {
    Object o = c.getClientProperty("Aqua.appearance");
    if (o != null) {
      try {
        Method m = o.getClass().getMethod("getColors");
        Object result = m.invoke(o);
        if (result instanceof Map) {
          return (Map) result;
        }
      } catch (Exception ex) {
      }
    }
    return null;
  }