Mittwoch, 9. Januar 2013

The J.O.R.I.S Model View Presenter


The J.O.R.I.S Project does not only provide means to integrate R into Java using the Springframework.
Since you need means to create and test drive (see last post) your User Interface the Model View Presenter (see at Martin Fowlers page) was chosen. Model View Presenter separates View from Model, and since both can be represented as an interface the application can be tested easier and more effective than many other classic MVC approaches.
The Model and View are not directly coupled but are glued together in the Presenter implementation. This allows to test the application behavior as a whole, even when the UI Application grows.
Since I gave a small practical example last time how to test drive an application my next idea is to show how to combine some rich client frameworks with the pattern above.
You may check MainFrame.java from the subversion repository repository to get started.
The application services are maintained by a Spring context. 

context = new ClassPathXmlApplicationContext(new String[] { 
    "classpath*:spring/hibernate-service-impl.spring.xml",
    "classpath:/spring/mmi-jrConnectionContext.spring.xml", 
    "classpath*:spring/r-service.spring.xml",
    "classpath:/spring/mmi-databaseContext.spring.xml", 
    "classpath:/spring/mmi-applicationContext.spring.xml" });
rConnection = context.getBean("managedConnection", IRConnection.class);

The application frame is defined (and thus the layout separated) in XML, leveraging the swixml Framework.

<?xml version="1.0" encoding="UTF-8"?>
<frame size="800,600" defaultCloseOperation="JFrame.EXIT_ON_CLOSE">
 <menubar>
  <menu text="R">
   <menuitem text="Scan Workspace" accelerator="W" id="scanWorkspace"/>
   <menuitem text="Load Data Set" accelerator="L" id="loadDataSet"/>
   <separator />
   <menuitem text="Quit" id="quit" accelerator="shift meta Q"/>
  </menu>
  <menu text="Database">
   <menuitem text="Search" accelerator="meta F" id="searchData" />
   <menuitem text="Configure" accelerator="meta C" />
  </menu>
 </menubar>

 <panel layout="BorderLayout">
  <splitpane orientation="JSplitPane.VERTICAL_SPLIT"
   dividerSize="5">
   <scrollpane height="500" PreferredSize="800,500"
    HorizontalScrollBarPolicy="JScrollPane.HORIZONTAL_SCROLLBAR_NEVER"
    VerticalScrollBarPolicy="JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED">
    <editorpane id="protocol" Editable="false" MinimumSize="320,200" PreferredSize="800,500" ContentType="text/html">
    </editorpane>
   </scrollpane>
   <textarea id="input">
   </textarea>
  </splitpane>
 </panel>
</frame>

A custom UIProcessor binds the standard swing events easily to the MessageBus. This binding is acchieved 
by a naming convention for Buttons and MenuItems.

For the menuItem Quit in the above example see the QuitEventHandler.java.QuitEventHandler handling 
org.rosuda.ui.event.QuitEvents. 

These utilities leverage small and readable classes for easy and fast test driven UI design.

Keine Kommentare:

Kommentar veröffentlichen