>
> For example we have 28, hopefully the same, wait.gif in our current
> codebase (also there is 4 incarnation of a wait.png)
>
> So I think the need of some kind of API is there.
>

You know, I think there is one already:
https://docs.oracle.com/javase/9/docs/api/javax/swing/UIManager.html#getIcon-java.lang.Object-

The objection I forsee is "But that would mean aggressively loading stuff
into UIManager from every module".  So here's how you solve that (the
delegating look and feel stuff would live in core.swing.plaf and just wrap
the LookAndFeel that's currently set on startup;  it could cache loaded
values into the LAF's defaults table or whatever seems like a good idea):

    static final class LAF extends LookAndFeel {

        private final LookAndFeel delegate;
        private final UID defaults;

        public LAF(LookAndFeel delegate) {
            this.delegate = delegate;
            defaults = new UID(delegate.getDefaults());
        }

        @Override
        public UIDefaults getDefaults() {
            return defaults;
        }
        // implement getName(), etc. to delegate here...
    }

    static class UID extends UIDefaults {
        private final UIDefaults delegate;

        public UID(UIDefaults delegate) {
            this.delegate = delegate;
        }

        @Override
        public Object get(Object key) {
            Object result = delegate.get(key);
            if (result == null) {
                result = lazyLoad(key);
            }
            return result;
        }

        private Object lazyLoad(Object key) {
            if ("hello".equals(key)) {
                return "world";
            }
            // lazy load from modules however you want here;
            // could be as simple as
Lookup.getDefault().lookupAll(UIDefaults.class)
            // and generate and register that class at build time for all
.svg files,
            // or whatever you want
            return null;
        }
    }

    public static void main(String[] args) throws
UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(new LAF(new MetalLookAndFeel()));
        EventQueue.invokeLater(() -> {
            JFrame jf = new JFrame();
            String txt = UIManager.getString("hello");
            jf.setContentPane(new JLabel("text is: '" + txt + "'"));
            jf.pack();
            jf.setVisible(true);
        });
    }

-Tim

Reply via email to