Swing Data Binding. Karsten Lentzsch www.jgoodies.com



Similar documents
Swing Data Validation. Karsten Lentzsch

Karsten Lentzsch JGoodies SWING WITH STYLE


GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

Developing GUI Applications: Architectural Patterns Revisited

JiST Graphical User Interface Event Viewer. Mark Fong

Extending Desktop Applications to the Web

Mapping to the Windows Presentation Framework

Swing. A Quick Tutorial on Programming Swing Applications

Principles of Software Construction: Objects, Design and Concurrency. GUIs with Swing. toad Spring 2013

CS 335 Lecture 06 Java Programming GUI and Swing

How Scala Improved Our Java

@ - Internal # - Online TH PR OR TW TOTAL HOURS # @ 175

Having Fun with QNAP and your Home Automation

ADF Code Corner. 92. Caching ADF Web Service results for in-memory filtering. Abstract: twitter.com/adfcodecorner

Model-View-Controller (MVC) Design Pattern

<Insert Picture Here> Betting Big on JavaServer Faces: Components, Tools, and Tricks

... Introduction... 17

Summer Internship 2013

This course provides students with the knowledge and skills to develop ASP.NET MVC 4 web applications.

Oracle Application Development Framework Overview

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

Building and Using Web Services With JDeveloper 11g

The Oracle Fusion Development Platform

Artificial Intelligence. Class: 3 rd

Developing Rich Web Applications with Oracle ADF and Oracle WebCenter Portal

Core J2EE Patterns, Frameworks and Micro Architectures

Mobile App Design and Development

Assignment # 2: Design Patterns and GUIs

Java is commonly used for deploying applications across a network. Compiled Java code

Building an Agile PLM Web Application with JDeveloper and Agile 93 Web Services

MiniDraw Introducing a framework... and a few patterns

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

Design. in NetBeans 6.0

GUI and Web Programming

CrownPeak Java Web Hosting. Version 0.20

Software Development Kit

Microsoft Excel 2013: Macro to apply Custom Margins, Titles, Gridlines, Autofit Width & Add Macro to Quick Access Toolbar & How to Delete a Macro.

Dell KACE K1000 System Management Appliance Version 5.4. Service Desk Administrator Guide

GUI Event-Driven Programming

Programming with Java GUI components

What is ArcGIS Comprised Of?

Syllabus for CS 134 Java Programming

Application Note 8: TrendView Recorders DCOM Settings and Firewall Plus DCOM Settings for Trendview Historian Server

ADF Code Corner. 66. How-to color-highlight the bar in a graph that represents the current row in the collection. Abstract: twitter.

Stock Trader System. Architecture Description

Tutorial: Time Of Day Part 2 GUI Design in NetBeans

ORACLE ADF MOBILE DATA SHEET

Liferay Portal User Guide. Joseph Shum Alexander Chow

PTC Integrity Eclipse and IBM Rational Development Platform Guide

<Insert Picture Here> Oracle Application Express 4.0

Customer Bank Account Management System Technical Specification Document

Event processing in Java: what happens when you click?

SOA REFERENCE ARCHITECTURE: WEB TIER

<Insert Picture Here> Building a Complex Web Application Using ADF and Siebel

Nexawebホワイトペーパー. Developing with Nexaweb ~ Nexaweb to Improve Development Productivity and Maintainability

XPoints: Extension Interfaces for Multilayered Applications

GUI Components: Part 2

How to develop your own app

Vaadin Application Tutorial. Building a Simple Address Book

Oracle Forms Developer 10g: Build Internet Applications

DbSchema Tutorial with Introduction in SQL Databases

Note: With v3.2, the DocuSign Fetch application was renamed DocuSign Retrieve.

Tutorial: Building a Web Application with Struts

Compiere Technical Architecture Modern, configurable, extendible

September 18, Modular development in Magento 2. Igor Miniailo Magento

Introducing Apache Pivot. Greg Brown, Todd Volkert 6/10/2010

SharePoint Integration Framework Developers Cookbook

BHARATHIAR UNIVERSITY COIMBATORE SCHOOL OF DISTANCE EDUCATION

Gadget: A Tool for Extracting the Dynamic Structure of Java Programs

INFOPATH FORMS FOR OUTLOOK, SHAREPOINT, OR THE WEB

Implementação. Interfaces Pessoa Máquina 2010/ Salvador Abreu baseado em material Alan Dix. Thursday, June 2, 2011

Skills and Topics for TeenCoder: Java Programming

Data Warehouse Troubleshooting Tips

Module 11 Setting up Customization Environment

Decomposition into Parts. Software Engineering, Lecture 4. Data and Function Cohesion. Allocation of Functions and Data. Component Interfaces

ADF Code Corner How-to pass values from a parent page to a popup dialog. Abstract: twitter.com/adfcodecorner

JavaFX Session Agenda

Quick Instructions Installing on a VPS (Virtual Private Server)

Operating System Structures

ADF. Joe Huang Joe Huang Senior Principal Product Manager, Mobile Development Platform, Oracle Application Development Tools

RESCO MOBILE CRM USER GUIDE. Access your CRM data on any mobile platform ipad, iphone, Android, Windows Phone or Win XP/Vista/7/8

Colligo Engage Windows App 7.0. Administrator s Guide

Deploying an ASP.NET Web Application to a Hosting Provider using Visual Studio

ORACLE BUSINESS INTELLIGENCE WORKSHOP

Working With Your FTP Site

INFOPATH FORMS FOR OUTLOOK, SHAREPOINT, OR THE WEB

DEVELOPMENT OF AN ANALYSIS AND REPORTING TOOL FOR ORACLE FORMS SOURCE CODES

ORACLE MOBILE APPLICATION FRAMEWORK DATA SHEET

Web Application Development for the SOA Age Thinking in XML

Mobile Solutions for Data Collection. Sarah Croft and Laura Pierik

Take full advantage of IBM s IDEs for end- to- end mobile development

Portal Factory CMIS Connector Module documentation

Dell KACE K1000 Management Appliance. Service Desk Administrator Guide. Release 5.3. Revision Date: May 13, 2011

ARCHITECTURAL DESIGN OF MODERN WEB APPLICATIONS

Transcription:

Swing Data Binding Karsten Lentzsch www.jgoodies.com

Presentation Goals Understand MVC and Swing models. Learn how to bind domain objects to Swing UI components.

Speaker Qualifications Karsten builds elegant Swing apps works with Objects since 1990 helps others with UI and architectures provides libraries that complement Swing provides examples for Swing architectures writes about Java desktop issues

Agenda Introduction MVC and Swing How to bind single values? How to bind lists A 3-tier Swing architecture How binding works in projects

Swing Building Blocks Application 1 Application 2 Applications Panels Utils Help Printing Helper Code/Libs Components, Application Management, etc. Application Frame Appearance Layout Binding Validation Basic Libraries JRE / Swing Foundation

Swing Building Blocks Application 1 Application 2 Applications Panels Utils Help Printing Helper Code/Libs Components, Application Management, etc. Application Frame Appearance Layout Binding Validation Basic Libraries JRE / Swing Foundation

Questions Where do I find MVC in Swing? How to structure a Swing application? What is part of the model? How do I choose models? How to build a view? What does a controller do? Do I need controllers?

I - Basics MVC and Swing

Before MVC Painting Code State Operations (Control Behavior) State How to live without MVC?

Before MVC: 2 Layers Painting Code State Operations Client State Server

Separate Domain from Views Domain logic contains no GUI code Presentation handles all UI issues Advantages: Each part is easier to understand Each part is easier to change

Domain and Presentation Painting Code State Modifications Presentation Layer State Domain Layer

Loose Coupling The domain shall not reference the GUI Presentation refers to domain and can modify it Advantages: Reduces complexity Allows to build multiple presentations of a single domain object

Loose Coupling Painting Code State Modifications reports changes refers to/modifies State

Separate View and Controller If you separate the painting code (View) from the state modifications (Controller), it's easier to: combine views and controllers reuse views and controllers

MVC refers to View Controller reports changes Model

MVC refers to View Controller reports changes Model

UI State vs. Data State We can categorize models into: domain related GUI related GUI state can make up its own layer.

MVC plus Model Layer View Controller Presentation Layer GUI Model (GUI) Model Layer Domain Model Domain Layer

Candidates for a Model Layer TreeModel: converts a domain object tree into a form useable for JTree Models that do not belong to the domain: GUI state, e. g. mouse pressed, mouse over Password in a login dialog Search values

Conbining MVC Triads A typical MVC UI combines MVC triads. Defines models as a graph of domain objects Composes larger views from small views Composes controllers from subcontrollers

MVC Triads with Model Layer V V C C Presentation Layer V C M M M Model Layer Domain Object DO DO Domain Layer

Factoring out the Look&Feel Swing can change its appearance and behavior or in other words: look and feel.

M-JComponent-VC JComponent View Controller Swing Model(s)

Example: JCheckBox JCheckBox Painting Event Handling MetalCheckBoxUI ToggleButtonModel

JCheckBox: Some Details JCheckBox Change- Listener ToggleButtonModel Painting Event Handling MetalCheckBoxUI Basic- ButtonUI MouseListener MouseMotionListener FocusListener PropertyChangeListener ChangeListener

JCheckBox: Types of State JCheckBox enabled text,... GUI Properties ToggleButtonModel armed pressed,... selected GUI State Data State

JCheckBox: Binding Task JCheckBox GUI Component ToggleButtonModel selected Data Model analbum title= Preludes classical=true Domain object

JCheckBox: Binding Task ajcheckbox GUI Component atogglebuttonmodel selected=true analbum title= Preludes classical=true Data Model connect, synchronize Domain Object

Summary Swing doesn't use the original MVC Swing uses an extended form of MVC Swing shares the motivation behind MVC Swing adds features to the original MVC Therefore, we will search and compare binding solutions for Swing, not MVC.

II - Binding Values How to connect domain objects with UI components?

Binding Tasks Read and write domain object properties Get and set GUI model state Report and handle changes in the domain Buffer values delay until OK pressed Change management commit required? Indirection as in an Master-Detail view Convert types, e. g. Date to String

Copying Values to Views #setselected 2. Write ajcheckbox atogglebuttonmodel selected=false 1. Read #isclassical analbum title= Preludes classical=true

Copying Values to Views ajcheckbox atogglebuttonmodel 3. Value changed selected=true analbum title= Preludes classical=true

Copying Values to the Domain ajcheckbox #isselected 1. Read atogglebuttonmodel selected=true analbum title= Preludes classical=false 2. Write #setclassical

Copying Values to the Domain ajcheckbox atogglebuttonmodel selected=true analbum title= Preludes classical=true 3. Value changed

Code Example: Copy to View public void modeltoview() { Album analbum = geteditedalbum(); classicalbox.setselected( analbum.isclassical()); } titlefield.settext( analbum.gettitle());...

Code Example: Copy to Domain public void viewtomodel() { Album analbum = geteditedalbum(); analbum.setclassical( classicalbox.isselected(); } analbum.settitle( titlefield.gettext();...

Copying: Pros and Cons Easy to understand, easy to explain Works in almost all situations Easy to debug explicit data operations Blows up the view code It's difficult to synchronize views Handles domain changes poorly

Alternative ajcheckbox atogglebuttonmodel selected=false analbum title= Preludes classical=true Note: you can't share the model

Direct Adapter: TableModel ajtable myalbumtableadapter implements TableModel Converts Album properties to TableModel abook abook analbum title= Swing title= Swing title= Preludes available=true available=true classical=true

Direct Adapter: JCheckBox ajcheckbox analbumclassical- ToggleButtonAdapter implements ToggleButtonModel Converts Album#classical to ToggleButtonModel analbum title= Preludes classical=true

Problem with Direct Adapters Requires an individual adapter for each domain object property. Similar to incompatible electric connectors. Code is all the same except for the methods that read and write domain properties.

Concept Use a universal model (ValueModel) Convert domain properties to ValueModel Build converters from ValueModel to Swing models: ToggleButtonModel, etc. We end up with about 15 classes.

ValueModel and Adapter ajcheckbox atogglebuttonadapter avaluemodel analbum title= Preludes classical=true

ValueModel: Requirements We want to get its value We want to set its value We want to observe changes

The ValueModel Interface public interface ValueModel { Object getvalue(); void setvalue(object newvalue); void addchangelistener(changelistener l); } void removechangelistener(changelistener l);

Which Event Type? ChangeEvent reports no new value; must be read from the model if necessary PropertyChangeEvent provides the old and new value; both can be null

ValueModel & PropertyAdapter ajcheckbox atogglebuttonadapter implements ToggleButtonModel apropertyadapter propertyname= classical analbum title= Preludes classical=true implements ValueModel

Domain Object Requirements We want to get and set values We want to do so in a uniform way Changes shall be observable That's what Java Beans provide.

(Bound) Bean Properties Java Beans have properties, that we can get and set in a uniform way. Bean properties are bound, if we can observe property changes by means of PropertyChangeListeners.

PropertyAdapter BeanAdapter and PropertyAdapter convert Bean properties to ValueModel Observe bound properties Use Bean Introspection that in turn uses Reflection to get and set bean properties

ValueModel & PropertyAdapter ajcheckbox atogglebuttonadapter apropertyadapter propertyname= classical implements ValueModel analbum (Bean) title= Preludes classical=true get/set

Build a Chain of Adapters private void initcomponents() { Album album = geteditedalbum(); ValueModel avaluemodel = new PropertyAdapter(album, classical ); } JCheckBox classicalbox = new JCheckBox(); classicalbox.setmodel( new ToggleButtonAdapter(aValueModel));

ComponentFactory private void initcomponents() { Album album = geteditedalbum(); } JCheckBox classicalbox = ComponentFactory.createCheckBox( album, Album.PROPERTYNAME_CLASSICAL);

Buffering: Delaying Commits Selecting the JCheckBox changes the bound domain property immediately. Often we want to delay value commits until the user presses OK or Accept. We can buffer in the adapter chain or in the domain layer.

BufferedValueModel ajcheckbox atogglebuttonadapter Indicates Commit or Flush atriggerchannel abufferedvaluemodel implements ValueModel apropertyadapter implements analbum classical=true

Sharing a Buffer Trigger ajcheckbox ajtextfield atogglebuttonadapter abufferedvaluemodel atrigger- Channel adocumentadapter abufferedvaluemodel apropertyadapter apropertyadapter analbum title= Preludes classical=true

Adapter vs. Connector ajtextfield ajformattedtextfield adocumentadapter apropertyconnector apropertyadapter for Album#title apropertyadapter for Album#releaseDate analbum title= Preludes releasedate=dec-5-1967

Converter vs. Formatter ajtextfield adocumentadapter DateToStringConverter uses Formatter ajformattedtextfield apropertyconnector apropertyadapter for Album#releaseDate apropertyadapter for Album#releaseDate analbum releasedate=dec-5-1967

Indirection ajcheckbox ajtextfield atogglebuttonadapter adocumentadapter apropertyadapter propertyname= classical Holds the edited album abeanchannel apropertyadapter propertyname= title analbum title= Preludes classical=true analbum title= Etudes classical=true

Indirection ajcheckbox ajtextfield atogglebuttonadapter adocumentadapter apropertyadapter propertyname= classical Holds the edited album abeanchannel apropertyadapter propertyname= title analbum title= Preludes classical=true analbum title= Etudes classical=true

III - Binding Lists How to connect Lists of domain values with Swing components?

List Binding Problems List views require fine grained change events. We want to observe list content changes. Otherwise list views poorly handle the selection and scroll state.

Requirements for a List Model Get list elements Provide the list size Report changes: if elements change if elements have been added if elements have been removed The Swing class ListModel provides this.

ListModel Implementations ArrayListModel extends ArrayList, implements ListModel LinkedListModel extends LinkedList, implements ListModel We can operate on List and can observe ListModel changes.

Binding Lists to JList ajlist anarraylistmodel abook abook analbum implements ListModel List

Binding Lists to JTable ajtable analbumtableadapter implements TableModel anarraylistmodel abook abook analbum implements ListModel List

Often: List with Single Selection We build a compound model that holds the ListModel and a selection in the list. This model reports changes of: the selection the selection index the list contents the list

SelectionInList ajlist SelectionAdapter ListModel aselectioninlist SelectionHolder abook abook analbum

Overview / Detail ajlist ajtextfield SelectionAdapter aselectioninlist ListModel SelectionHolder adocumentadapter apropertyadapter propertyname= title beanchannel abook abook analbum

IV - Architecture A 3-tier Swing client architecture

Design Goals Works with standard Swing components Works with custom Swing components Requires no special components Requires no special panels Integrates well with validation Works with different validation styles

3-Tier Client Architecture JList JButton JTextField Presentation ListModel Action ValueModel Model Layer Domain Object Domain Layer DO DO

Benefit of 3 Layers Views are easy to build Views are decoupled Domain layer is separated Developers know where to put what code Synchronization is easy Decreased complexity Model operations located in a single layer Poor code limited to the model layer

Multiple Views View 1 View 2 JTextField JButton JMenuItem JLabel Presentation JList ListModel Action ValueModel Model Layer

Setting UI Properties: Actions JButton #enabled ButtonActionPropertyChangeListener Updates button property if the Action has changed ListModel Action #enabled ValueModel

Setting UI Properties JTextField JButton JList View ModelChangeHandler Updates textfield property if the model has changed GUI Model ValueModel Can provide bound bean properties

Adapting Multiple Properties JTextField JTextField JCheckBox Presentation BeanAdapter Vends PropertyAdapters Album #title #artist #classical Domain Layer

Example View Source Code 1) Variables for UI components 2) Constructors 3) Create, bind, configure UI components 4) Register GUI state handlers with the model 5) Build and return panel 6) Handlers that update GUI state

Example View 1/7 public final class AlbumView { // Refers to the model provider private AlbumPresentationModel model; // UI components private JTextField titlefield; private JCheckBox classicalbox; private JButton buynowbutton; private JList referenceslist;...

Example View 2/7 public AlbumView(AlbumPresentationModel m) { // Store a ref to the presentation model this.model = m; } // Do some custom setup....

Example View 3/7 private void initcomponents() { titlefield = ComponentFactory.createField( model.gettitlemodel()); titlefield.seteditable(false); buynowbutton = new JButton( model.getbuynowaction()); referencelist = new JList( model.getreferencelistmodel()); referencelist.setselectionmodel( model.getreferenceselectionmodel());...

Example View 4/7 private initeventhandling(){ // Observe the model to update GUI state model.addpropertychangelistener( composerenabled, new ComposerEnablementHandler()); }

Example View 5/7 public JPanel buildpanel() { // Create, bind and configure components initcomponents(); // Register handlers that change UI state initeventhandling(); FormLayout layout = new FormLayout( right:pref, 3dlu, pref, // 3 columns p, 3dlu, p ); // 3 rows...

Example View 6/7 PanelBuilder builder = new PanelBuilder(layout); CellConstraints cc = new CellConstraints(); builder.addlabel( Title, cc.xy(1, 1)); builder.add(titlefield, cc.xy(3, 1)); builder.add(availablebox, cc.xy(3, 3)); builder.add(buynowbutton, cc.xy(3, 5)); builder.add(referencelist, cc.xy(3, 7)); } return builder.getpanel();

Example View 7/7 /* Listens to #composerenabled, changes #enabled of the composerfield. */ private class ComposerEnablementHandler implements PropertyChangeListener { public void propertychange( PropertyChangeEvent evt) { } } composerfield.setenabled( model.iscomposerenabled());

Simpler Event Handling private initeventhandling(){ // Synchronize model with GUI state PropertyConnector.connect( model, composerenabled, composerfield, enabled ); }

V - Field Report How does Adapter Binding work?

Costs Adapter Binding: increases learning costs decreases production costs a little can significantly reduce change costs

Use a ComponentFactory! Encapsulate the creation of adapters from ValueModel to Swing components. Some components have no appropriate model, e. g. JFormattedTextField Vends components for ValueModels

Buffering Use BufferedValueModel judicously prevents validation on domain models makes it harder to use domain logic The client domain layer can buffer if: domain objects are copies domain objects temporarily accept invalid data

Performance Adapter chains fire many change events That seems to be no performance problem ListModel can improve the performance compared to copying list contents

Debugging Copying approach is easy to debug; you can see when where what happens. Adapter chains move values implicitly; it's harder to understand updates. Reflection and Introspection hide who reads and writes values. Favor named over anonymous listeners.

Renaming Methods Reflection and Introspection make it more difficult to rename bean properties and their getter and setters. Use constants for bean property names! Obfuscators fail to detect the call graph.

When is Binding Useful? I guess that adapter binding can be applied to about 80% of all Swing projects. However, you need at least one expert who masters the binding classes.

Benefits of Adapter Binding Adapter binding can save a lot of code. Code is easier to read. Helps you separate code into layers. Can significantly reduce the complexity.

Where does Binding stand? Approach is 10 years old and stable. Architecture of the Java port is stable. Tests cover 90% of the classes. Little documentation. Tutorial is quite small.

End Summary and References

Summary We've learned about MVC and Swing We've identified Binding tasks We've motivated the ValueModel interface We've learned how to bind single values We've learned how to bind lists We've seen a 3-tier architecture

JGoodies Swing Suite Example 1 Example 2 Example n Applications Convenience Sources Helper Code User Interface Framework (UIF) Framework Looks Forms Binding Validation Basic Libraries JRE / Swing Foundation

References I Fowler's Enterprise Patterns martinfowler.com/eaadev/ JGoodies Binding binding.dev.java.net JGoodies Articles www.jgoodies.com/articles/ JGoodies Demos www.jgoodies.com/freeware/

References II Sun's JDNC jdnc.dev.java.net Understanding and Using ValueModels c2.com/ppr/vmodels.html Oracle's JClient and ADF otn.oracle.com/, search for 'JClient' Spring Rich Client Project www.springframework.org/spring-rcp.html

Demo/Tutorial: JGoodies Binding Tutorial Binding Problems and Solutions (in progress) Ships with the JGoodies Binding library.

Questions and Answers

End Good Luck! Karsten Lentzsch