http://www.developer.com/

Back to article

An XML based Framework for Developing Swing Applications


May 13, 2004

Introduction

In today's world time to market is shrinking. Frameworks help in saving time during the design and development phase. IDEs help in easier development of the user interface. Standard GUI applications show an underlying pattern where the GUI flow can be defined by the flow of various panels. Based on this pattern, a framework is thought of for increasing the speed of implementation, achieving higher degree of parallelism and facilitating loosely-coupled development. Such a framework is described here by taking the applications that are based on Java swing.

Sample Application - Library Management System

Let's discuss a simple library management system to understand the xml based GUI framework.

The above flowchart describes how the library's user interface is designed. Home, Member Login, User Registration and Search book are the panels that exist in this application. From the flowchart, the set of possible paths that describe the flow of panels in this application are

Home ->User Registration -> Search book
Home -> Member Login -> Search book

Principle

The panel in display at any time depends on the outputs from the previous panels. Hence, a generic controller that forms the basis of the framework is designed such that it supplies the necessary inputs to the panels and processes the expected outputs from the panels. These panels are expected to implement the XSFPanel interface that has an execute() method which takes inputs in the form of a HashMap and return the outputs as another HashMap. The controller of the framework processes the outputs that are returned using the HashMap and determines which panel should be on display at any time.

public interface XSFPanel extends JPanel
{
   public HashMap execute(HashMap inputs);
   . . .
}

Controller and XML Flow Definition

The controller of the flow works on a simple XML that defines the flow of the user interface. This XML has to be defined based on the inputs that each panel takes and the output expected from each panel. The sample application that we have discussed, works based on the following XML.

<XFSFramework>

  <XFSPanels>

    <XFSPanel name="Home" class="library.Home">
      <inputs/>
      <outputs>
        <output variable="userOption"/>
      </outputs>
    </XFSPanel>

    <XFSPanel name="MemberLogin" class="library.MemberLogin">
      <inputs/>
      <outputs>
        <output variable="userName"/>
        <output variable="password"/>
      </outputs>
    </XFSPanel>

    <XFSPanel name="Registration" class="library.UserRegistration">
      <inputs/>
      <outputs>
        <output variable="userName"/>
        <output variable="password"/>
        <output variable="emailId"/>
        <output variable="address"/>
      </outputs>
    </XFSPanel>

    <XFSPanel name="RegistrationPreview" class="library.UserPreview">
      <inputs>
        <input variable="userName"/>
        <input variable="emailId"/>
        <input variable="address"/>
      </inputs>
      <outputs/>
    </XFSPanel>

    <XFSPanel name="searchBook" class="library.searchBook">
      <inputs/>
      <outputs>
        <output variable="criterion"/>
        <output variable="searchValue"/>
      </outputs>
    </XFSPanel>

  </XFSPanels>

  <XFSPanelFlow>
    <step id="s1" from="Home" to="MemberLogin">
      <conditions>
        <condition variable="userOption" value="login"/>
      </conditions>
    </step>
    <step id="s2" from="Home" to="Registration">
      <conditions>
        <condition variable="userOption" value="newRegistration"/>
      </conditions>
    </step>
    <step id="s3" from="Registration" to="RegistrationPreview">
      <conditions/>
    </step>
    <step id="s4" from="MemberLogin" to="searchBook">
      <conditions>
        <condition variable="controller_loginResult" value="success"/>
      </conditions>
    </step>
    <step id="s5" from="RegistrationPreview" to="MemberLogin">
      <conditions>
        <condition variable="controller_accountCreationResult" value="success"/>
      </conditions>
    </step>    

  </XFSPanelFlow>

</XFSFramework>
Note: This xml is used just to describe the framework and may not completely describe the sample application discussed in the example.

In the above XML, all the panels that implement the XFSPanel interface are mentioned. The flow from one panel to another is also defined. The RegistrationPreview panel has been defined in the XML just for clarifying that a panel may take inputs from the controller.

From the above XML it can be identified that based on the userOption output from the Home panel, the following panel is displayed and the flow continues based on the xml flow definition.

How Parallelism is achieved

In the applications that are developed using this framework, the developers of the panels may focus on how to present the information and need not worry about the flow in the user interface. Once the flow xml is defined, many developers can work in parallel on various panels of the application.

How Loose-coupling is achieved

One important feature of this framework is that new panels can be added at will. The inputs to the existing panels also can be changed by only changing the XML.

In our application, if the user logged in is an administrator, we need to provide another user option to visit admin tasks panel from home panel. This can be done by the following additions.

The following should be added to XFSPanels:

    <XFSPanel name="AdminTasks" class="library.AdminTasks">
      <inputs/>
      <outputs>
        <output variable="adminTaskId"/>
      </outputs>
    </XFSPanel>

The following should be added to XFSPanelFlow

    <step id="s6" from="MemberLogin" to="AdminTasks">
      <conditions>
        <condition variable="controller_isAdmin" value="true"/>
      </conditions>
    </step>

This framework works based on the flow xml and when it is modified, the flow automatically gets modified. It can be noticed that, once the flow xml is changed, the developer may focus on developing the admin tasks panel and modifying the home panel but need not worry about the flow of the panels since this is taken care by the controller of the framework.

Conclusion

Requirements of UI applications that often change fit into this framework and this framework helps in getting those changes done easily. Though this is a java based framework, the underlying pattern can be applied to develop a framework under any language/environment. This framework for swing applications is used to explain that pattern behind GUI applications.

About the Authors

R Venkatavaradan has been working as a Technical Consultant in Hewlett Packard. He has a Masters Degree from School of Automation, Indian Institute of Science, Bangalore. He has around 13 years of industry experience. The field of work ranges from signal processing to web services, J2EE, Enterprise Application Integration etc. He has extensively worked on web service technologies like WSDL, SOAP and UDDI and provided technical consultancy to various projects in the field of mobile, telecom and EAI, which have been architected based on web service concepts. He can be reached at varadan@india.hp.com.

Suresh P R has been working as a Senior Software Engineer in Hewlett Packard. He has a Bachelors Degree from PSG College of Technology, India. He has about 3 years of industry experience in J2EE and web service related technologies such as WSDL, SOAP, UDDI. He can be reached at sureshpr@india.hp.com.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date