Serban Iordache
2013-12-13 19:00:38 UTC
Hi all,
I would like to contribute a pico container that populates itself by means of a configuration file in JSON format.
For a usage example, let's consider the following code fragment:
public class Banner extends JFrame { public Banner(JTextComponent textComp, String text, int size, boolean bold) { textComp.setText(text); textComp.setFont(new Font("Arial", (bold ? Font.BOLD : Font.PLAIN), size)); add(textComp); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); pack(); }
public static void main(String[] args) { MutablePicoContainer pico = new DefaultPicoContainer(); pico.addComponent("textComp", JTextArea.class, DefaultConstructorParameter.INSTANCE); pico.addComponent("banner", Banner.class, new ComponentParameter("textComp"), new ConstantParameter("Hello, world!"), new ComponentParameter("size"), new ComponentParameter("bold")); pico.addConfig("size", 36); pico.addConfig("bold", true);
Banner banner = (Banner)pico.getComponent("banner"); banner.setVisible(true); } }
Using the JsonPicoContainer, the main method above can be rewritten as:
public static void main(String[] args) { MutablePicoContainer pico = new JsonPicoContainer("banner.json");
Banner banner = (Banner)pico.getComponent("banner"); banner.setVisible(true); }
And here is the content of the file banner.json:
[ {key: textComp, impl: javax.swing.JTextArea, parameters: [{}]}, {key: banner, impl: com.picocontainer.gems.containers.Banner, parameters: [{key: textComp}, {value: "Hello, world"}, {key: size}, {key: bold}]}, {key: size, value: 36, type: int}, {key: bold, value: true, type: boolean}]
The JSON container has a dependency on google-gson, therefore it would belong to the gems. I've tested the container using the code from the picocontainer-git repository. (I assume that the development on Pico 2.x has been discontinued.)
Do you find such a JSON container useful? If yes, how can I contribute it?
Best regards,Serban
I would like to contribute a pico container that populates itself by means of a configuration file in JSON format.
For a usage example, let's consider the following code fragment:
public class Banner extends JFrame { public Banner(JTextComponent textComp, String text, int size, boolean bold) { textComp.setText(text); textComp.setFont(new Font("Arial", (bold ? Font.BOLD : Font.PLAIN), size)); add(textComp); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); pack(); }
public static void main(String[] args) { MutablePicoContainer pico = new DefaultPicoContainer(); pico.addComponent("textComp", JTextArea.class, DefaultConstructorParameter.INSTANCE); pico.addComponent("banner", Banner.class, new ComponentParameter("textComp"), new ConstantParameter("Hello, world!"), new ComponentParameter("size"), new ComponentParameter("bold")); pico.addConfig("size", 36); pico.addConfig("bold", true);
Banner banner = (Banner)pico.getComponent("banner"); banner.setVisible(true); } }
Using the JsonPicoContainer, the main method above can be rewritten as:
public static void main(String[] args) { MutablePicoContainer pico = new JsonPicoContainer("banner.json");
Banner banner = (Banner)pico.getComponent("banner"); banner.setVisible(true); }
And here is the content of the file banner.json:
[ {key: textComp, impl: javax.swing.JTextArea, parameters: [{}]}, {key: banner, impl: com.picocontainer.gems.containers.Banner, parameters: [{key: textComp}, {value: "Hello, world"}, {key: size}, {key: bold}]}, {key: size, value: 36, type: int}, {key: bold, value: true, type: boolean}]
The JSON container has a dependency on google-gson, therefore it would belong to the gems. I've tested the container using the code from the picocontainer-git repository. (I assume that the development on Pico 2.x has been discontinued.)
Do you find such a JSON container useful? If yes, how can I contribute it?
Best regards,Serban