Monday Apr 21, 2008

Converting Spring to Lookup

In the screenshot below, the content of the "Spring Window" is injected via a Spring configuration file:

The highlighted classes above are the Swing components and behavior that Spring injects into the TopComponent (except for 'SpringAction', which opens the window). In the editor area above, the following constructor is shown:

private SpringTopComponent() {

    initComponents();

    setName(NbBundle.getMessage(SpringTopComponent.class, "CTL_SpringTopComponent"));
    setToolTipText(NbBundle.getMessage(SpringTopComponent.class, "HINT_SpringTopComponent"));

    String[] contextPaths = new String[]{"org/netbeans/nbspringdemo/app-context.xml"};
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(contextPaths);

    Lookup lookup = NbSpring.create(ctx);
    Item item = lookup.lookupItem(new Template(JPanel.class, null, null));
    JPanel foo = (JPanel) ctx.getBean(item.getId()); 

    add(foo, java.awt.BorderLayout.CENTER);

}

The line in bold above is possible as a result of the new Spring/NetBeans API that is in 'contrib', as highlighted in my blog yesterday. That line is the thing that makes this possible at all. Without it, i.e., without being able to convert my Spring configuration file to Lookup, none of this would be possible. And what does my Spring configuration file look like? Exactly this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
           
    <bean id="mainPanel" class="org.netbeans.nbspringdemo.MyJPanel" init-method="init">
        <property name="axis">
            <value>1</value>
        </property>
        <property name="panelComponents">
            <list>
                <ref bean="textField1"/>
                <ref bean="textField2"/>
                <ref bean="textField3"/>
                <ref bean="buttonPanel"/>
            </list>
        </property>
    </bean>

    <bean id="buttonPanel" class="org.netbeans.nbspringdemo.MyJPanel" init-method="init">
        <property name="axis">
            <value>0</value>
        </property>
        <property name="panelComponents">
            <list>
                <ref bean="button1"/>
            </list>
        </property>
    </bean>

    <bean id="textField1" class="org.netbeans.nbspringdemo.MyJTextField" init-method="init">
        <property name="text">
            <value>hello 1</value>
        </property>
        <property name="rColor">
            <value>255</value>
        </property>
        <property name="gColor">
            <value>51</value>
        </property>
        <property name="bColor">
            <value>102</value>
        </property>
    </bean>

    <bean id="textField2" class="org.netbeans.nbspringdemo.MyJTextField" init-method="init">
        <property name="text">
            <value>hello 2</value>
        </property>
        <property name="rColor">
            <value>0</value>
        </property>
        <property name="gColor">
            <value>100</value>
        </property>
        <property name="bColor">
            <value>0</value>
        </property>
    </bean>

    <bean id="textField3" class="javax.swing.JTextField">
        <property name="text">
            <value>goodbye world</value>
        </property>
    </bean>

    <bean id="button1" class="org.netbeans.nbspringdemo.MyJButton" init-method="init">
        <property name="actionListener">
            <ref bean="myButtonActionListener"/>
        </property>
        <property name="text">
            <value>Click me!</value>
        </property>
    </bean>

    <bean id="myButtonActionListener" class="org.netbeans.nbspringdemo.MyActionListener"/>

</beans>

That's the Spring configuration file that I discussed in Spring: How to Create Decoupled Swing Components on JavaLobby. The definition of the classes, i.e., the JPanel, JButton, and JTextField, are also described in that article. Read the comments to that article to see some use cases where you might want to assemble your user interface via decoupled Swing components and Spring.

Pretty cool that this is now also possible on the NetBeans Platform. I am not advocating this approach, I am merely pointing out that this is possible.

In other news. Read this blog entry about NetBeans Day Fortaleza!

A Picture That Speaks 1000 Words

About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2008 »
SunMonTueWedThuFriSat
  
2
4
5
13
18
19
23
30
   
       
Today