Search in sources :

Example 1 with SwingBindingAssembler

use of org.qi4j.lib.swing.binding.SwingBindingAssembler in project qi4j-sdk by Qi4j.

the class Main method main.

public static void main(String[] args) throws Exception {
    SingletonAssembler assembler = new SingletonAssembler() {

        public void assemble(ModuleAssembly module) throws AssemblyException {
            module.transients(BoundPersonComposite.class);
            module.transients(AddressTransient.class);
            module.values(CityValue.class, CountryValue.class);
            new SwingBindingAssembler().assemble(module);
        }
    };
    module = assembler.module();
    Address address1 = createAddress("Vista Damai", "Jalan Tun Razak");
    Address address2 = createAddress("Mutiara", "Jalan Ceylon");
    TransientBuilder<BoundPersonComposite> builder = module.newTransientBuilder(BoundPersonComposite.class);
    PersonComposite prototype = builder.prototype();
    prototype.address().set(address1);
    prototype.firstName().set("Niclas");
    final BoundPersonComposite p1 = builder.newInstance();
    prototype.address().set(address2);
    prototype.firstName().set("Edward");
    final BoundPersonComposite p2 = builder.newInstance();
    final StateModel<BoundPersonComposite> model = module.newObject(StateModel.class, BoundPersonComposite.class);
    Form form = new Form<BoundPersonComposite>(model);
    JFrame frame = new JFrame("testing");
    frame.add(form, BorderLayout.CENTER);
    JPanel checkBoxPanel = new JPanel();
    checkBoxPanel.setFocusable(true);
    checkBoxPanel.addFocusListener(new FocusListener() {

        public void focusGained(FocusEvent e) {
            System.out.println("Gained!");
        }

        public void focusLost(FocusEvent e) {
            System.out.println("LOst!");
        }
    });
    JCheckBox sw = new JCheckBox("Toggle");
    sw.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (((JCheckBox) e.getSource()).isSelected()) {
                model.use(p1);
                System.out.println(p1.firstName().get());
            } else {
                model.use(p2);
                System.out.println(p2.firstName().get());
            }
        }
    });
    checkBoxPanel.add(sw);
    frame.add(checkBoxPanel, BorderLayout.EAST);
    frame.pack();
    System.out.println(model);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
Also used : JPanel(javax.swing.JPanel) SwingBindingAssembler(org.qi4j.lib.swing.binding.SwingBindingAssembler) ActionEvent(java.awt.event.ActionEvent) FocusEvent(java.awt.event.FocusEvent) JCheckBox(javax.swing.JCheckBox) ModuleAssembly(org.qi4j.bootstrap.ModuleAssembly) ActionListener(java.awt.event.ActionListener) JFrame(javax.swing.JFrame) SingletonAssembler(org.qi4j.bootstrap.SingletonAssembler) FocusListener(java.awt.event.FocusListener)

Aggregations

ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 FocusEvent (java.awt.event.FocusEvent)1 FocusListener (java.awt.event.FocusListener)1 JCheckBox (javax.swing.JCheckBox)1 JFrame (javax.swing.JFrame)1 JPanel (javax.swing.JPanel)1 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)1 SingletonAssembler (org.qi4j.bootstrap.SingletonAssembler)1 SwingBindingAssembler (org.qi4j.lib.swing.binding.SwingBindingAssembler)1