Desktop Patterns and Data Binding Karsten Lentzsch Desktop Patterns - - PowerPoint PPT Presentation

desktop patterns and data binding
SMART_READER_LITE
LIVE PREVIEW

Desktop Patterns and Data Binding Karsten Lentzsch Desktop Patterns - - PowerPoint PPT Presentation

J-Fall 2006 Desktop Patterns and Data Binding Karsten Lentzsch Desktop Patterns and Data Binding JGoodies J-Fall 2006 Goal Learn how to organize presentation logic and how to bind domain data to views Desktop Patterns and Data Binding


slide-1
SLIDE 1

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Desktop Patterns and Data Binding

Karsten Lentzsch

slide-2
SLIDE 2

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Goal

Learn how to organize presentation logic and how to bind domain data to views

slide-3
SLIDE 3

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Agenda

Introduction Autonomous View Model View Presenter Presentation Model Data Binding

slide-4
SLIDE 4

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Agenda

Introduction Autonomous View Model View Presenter Presentation Model Data Binding

slide-5
SLIDE 5

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Legend

Domain Object Presentation Logic Presentation (View) Refers to Notifies

slide-6
SLIDE 6

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Legend

  • Domain/business logic
  • Examples:
  • Book
  • Person
  • Address
  • Invoice
  • More generally:
  • bject graph

Domain Object

slide-7
SLIDE 7

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Legend

  • Handlers for:
  • List selection changes
  • Check box selection
  • Drag drop end
  • UI models
  • ListModel
  • TableModel
  • TreeSelectionModel
  • Swing Actions

Presentation Logic

slide-8
SLIDE 8

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Event Handling vs. Presentation Logic

  • Toolkit handles fine-grained events:
  • Mouse entered, exited
  • Mouse pressed
  • Radio button pressed, armed, rollover
  • Application handles coarse-grained events:
  • Radio button selected
  • Action performed
  • List items added
  • Domain property changed
slide-9
SLIDE 9

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Legend

  • Container:
  • JPanel, JDialog, JFrame
  • Contains components:
  • JTextField, JList, JTable
  • Component initialization
  • Panel building code
  • GUI state:
  • Check box pressed
  • Mouse over

Presentation (View)

slide-10
SLIDE 10

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Legend

  • Role1 and Role2

“sit together” in a class

  • Can access each other
  • Separated layers

Role2 Role1

slide-11
SLIDE 11

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Legend

  • A refers to B
  • A holds a reference to B
  • B indirectly refers to A

A B Refers to Notifies

slide-12
SLIDE 12

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Domain Presentation Logic Presentation (View)

All Mixed Together

slide-13
SLIDE 13

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Domain Presentation Logic Presentation (View)

Pattern: Separated Presentation

slide-14
SLIDE 14

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Business Logic Presentation Logic Presentation (View)

Business Logic in the Presentation

Domain

slide-15
SLIDE 15

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Decouple Domain from Presentation

  • The domain shall not reference the presentation
  • Presentation refers to domain and modifies it
  • Advantages:
  • Reduces complexity
  • Multiple presentations
slide-16
SLIDE 16

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Domain Presentation Logic Presentation (View) Refers to Notifies

Separated Presentation with Observer

slide-17
SLIDE 17

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Agenda

Introduction Autonomous View Model View Presenter Presentation Model Data Binding

slide-18
SLIDE 18

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Presentation Logic Presentation (View)

Pattern: Autonomous View

slide-19
SLIDE 19

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Autonomous View

  • Often one class per window or screen
  • Often a subclass of JDialog, JFrame, JPanel
  • Contains:
  • Fields for UI components
  • Component initialization
  • Panel building/layout
  • Model initialization
  • Presentation logic: listeners, operations
slide-20
SLIDE 20

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Example GUI

Composer field is enabled, if classical is selected

slide-21
SLIDE 21

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Autonomous View Sample (1/2)

public class AlbumDialog extends JDialog { private final Album album; private JTextField artistField; ... public AlbumDialog(Album album) { ... } private void initComponents() { ... } private void initPresentationLogic() { ... } private JComponent buildContent() { ... }

slide-22
SLIDE 22

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Autonomous View Sample (2/2)

class ClassicalChangeHandler implements ChangeListener { public void stateChanged(ChangeEvent e) {

// Check the classical state.

boolean classical = classicalBox.isSelected(); // Update the composer field enablement. composerField.setEnabled(classical); } }

slide-23
SLIDE 23

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Autonomous View: Tips

  • Build dialogs, frames, panels
  • Extend JDialog, JFrame, JPanel if necessary.

Do you extend or use HashMap?

slide-24
SLIDE 24

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Autonomous View

  • Common and workable
  • Has disadvantages:
  • Difficult to test logically
  • Difficult to overview, manage, maintain, and debug,

if the view or logic is complex

  • Consider to separate the logic from the view
slide-25
SLIDE 25

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Domain Presentation Logic Presentation (View)

Presentation Logic Separated

slide-26
SLIDE 26

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Separated Logic: Advantages I

  • Allows to test the presentation logic logically
  • Simplifies team synchronization
  • Each part is smaller and easier to overview
  • Allows to build “forbidden zones”
  • For team members
  • Before you ship a new release
  • Layout changes allowed
  • Design is done, but bug fixes in the logic are still allowed
slide-27
SLIDE 27

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Separated Logic: Advantages II

  • Thin GUI:
  • Easier to build, understand, maintain
  • Can follow syntactical patterns
  • More team members can work with it
  • Logic can ignore presentation details,

e.g. component types (JTable vs. JList)

  • Logic can be reused for different views
slide-28
SLIDE 28

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Separated Logic: Disadvantages

  • Extra machinery to support the separation
  • Extra effort to read and manage multiple sources
slide-29
SLIDE 29

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Separating Logic from the View

  • Can simplify or add complexity
  • Separation costs vary with the pattern used
  • Opinion: typically you benefit from the separation

My personal guideline for team projects:

  • Use Autonomous View for message dialogs
  • Otherwise separate the logic from the view
slide-30
SLIDE 30

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Agenda

Introduction Autonomous View Model View Presenter Presentation Model Data Binding

slide-31
SLIDE 31

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Model Presenter View

Pattern: Model View Presenter (MVP)

slide-32
SLIDE 32

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Model

  • Holds domain data
  • Provides business logic

Presenter

  • Reads domain data
  • Sets GUI state
  • Presentation Logic
  • Changes the domain

View

  • Holds UI components
  • Holds GUI state
  • Inits components
  • Builds panel

Model View Presenter

slide-33
SLIDE 33

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Album AlbumDialog

Album Example: Autonomous View

JTextField JTextField JCheckBox JTextField ChangeHandler

slide-34
SLIDE 34

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Album AlbumPresenter AlbumView

Album Example: Model View Presenter

JTextField JTextField JCheckBox JTextField ChangeHandler

slide-35
SLIDE 35

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

From Autonomous View ...

public class AlbumDialog extends JDialog { private JTextField artistField; public AlbumDialog(Album album) { ... } private void initComponents() { ... } private JComponent buildContent() { ... } private final Album album; private void initPresentationLogic() { ... } private void readGUIStateFromDomain() { ... } private void writeGUIStateToDomain() { ... } class ClassicalChangeHandler implements ... class OKActionHandler implements ... }

slide-36
SLIDE 36

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

... to Model View Presenter

class AlbumView extends JDialog { JTextField artistField; public AlbumView() { ... } private void initComponents() { ... } private JComponent buildContent() { ... } } public class AlbumPresenter { private final AlbumView view; private Album album; private void initPresentationLogic() { ... } private void readGUIStateFromDomain() { ... } private void writeGUIStateToDomain() { ... } class ClassicalChangeHandler implements ... class OKActionHandler implements ... }

slide-37
SLIDE 37

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

... to Model View Presenter

class AlbumView extends JDialog { JTextField artistField; public AlbumView() { ... } private void initComponents() { ... } private JComponent buildContent() { ... } } public class AlbumPresenter { private final AlbumView view; private Album album; private void initPresentationLogic() { ... } private void readGUIStateFromDomain() { ... } private void writeGUIStateToDomain() { ... } class ClassicalChangeHandler implements ... class OKActionHandler implements ... }

slide-38
SLIDE 38

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Presenter: Example Logic

class ClassicalChangeHandler implements ChangeListener { public void stateChanged(ChangeEvent e) { // Check the view's classical state. boolean classical = view.classicalBox.isSelected(); // Update the composer field enablement. view.composerField.setEnabled(classical); } }

slide-39
SLIDE 39

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Agenda

Introduction Autonomous View Model View Presenter Presentation Model Data Binding

slide-40
SLIDE 40

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Domain Presentation Model View

Pattern: Presentation Model

View View

slide-41
SLIDE 41

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Domain Presentation Model

  • Reads domain data
  • Holds relevant state
  • Presentation Logic
  • Fires state changes
  • Changes the domain

View

  • Holds UI components
  • Holds all GUI state
  • Inits components
  • Builds panel
  • Listens to PM changes

Presentation Model

slide-42
SLIDE 42

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Action

  • Holds relevant state
  • Fires state changes

JButton

  • Holds all GUI state
  • Listens to Action changes

Reminder: Swing Actions

slide-43
SLIDE 43

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

From Autonomous View ...

public class AlbumDialog extends JDialog { private JTextField artistField; public AlbumDialog(Album album) { ... } private void initComponents() { ... } private JComponent buildContent() { ... } private final Album album; private void initPresentationLogic() { ... } private void readGUIStateFromDomain() { ... } private void writeGUIStateToDomain() { ... } class ClassicalChangeHandler implements ... class OKActionHandler implements ... }

slide-44
SLIDE 44

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

... to Presentation Model

class AlbumView extends JDialog { private final AlbumPresentationModel model; private JTextField artistField; public AlbumView(AlbumPM model) { ... } private void initComponents() { ... } private JComponent buildContent() { ... } } public class AlbumPresentationModel { private Album album; private void initPresentationLogic() { ... } private void readPMStateFromDomain() { ... } private void writePMStateToDomain() { ... } class ClassicalChangeHandler implements ... class OKActionHandler implements ... }

slide-45
SLIDE 45

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

AlbumPM AlbumView

AlbumPresentationModel

JTextField JTextField JCheckBox JTextField Text Model Text Model Selection Model Text Model Album

slide-46
SLIDE 46

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Album AlbumPM AlbumView

AlbumPresentationModel: Logic

JTextField JTextField JCheckBox JTextField ChangeHandler Text Model Text Model Selection Model Text Model Enablement Model ChangeHandler

slide-47
SLIDE 47

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Album AlbumPM AlbumView

AlbumPresentationModel: Logic

JTextField JTextField JCheckBox JTextField ChangeHandler Text Model Text Model Selection Model Text Model Enablement Model ChangeHandler

slide-48
SLIDE 48

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Album AlbumPM AlbumView

AlbumPresentationModel: Logic

JTextField JTextField JCheckBox JTextField ChangeHandler Text Model Text Model Selection Model Text Model Enablement Model ChangeHandler

Updates

slide-49
SLIDE 49

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Album AlbumPM AlbumView

AlbumPresentationModel: Logic

JTextField JTextField JCheckBox JTextField ChangeHandler Text Model Text Model Selection Model Text Model Enablement Model ChangeHandler Notifies

slide-50
SLIDE 50

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Album AlbumPM AlbumView

AlbumPresentationModel: Logic

JTextField JTextField JCheckBox JTextField ChangeHandler Text Model Text Model Selection Model Text Model Enablement Model ChangeHandler

Updates enablement

slide-51
SLIDE 51

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

No Worries: Actions Again

  • Swing uses a similar machinery for Actions
  • Actions fire PropertyChangeEvents
  • JButton listens to the Action and updates its state
  • Swing synchronizes Action state and GUI state
  • All you need to write is:

new JButton(anAction)

slide-52
SLIDE 52

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Action

  • Text
  • Icon
  • Enablement
  • Mnemonic

JButton

Action with Multiple Views

JButton JMenuItem

slide-53
SLIDE 53

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Presentation Model: Multiple Views I

Presentation Model Panel with List and Button JList ListModel JButton Action Domain PopupMenu JMenuItem

slide-54
SLIDE 54

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Presentation Model: Multiple Views II

Presentation Model Display List JList ListModel Action Domain Table with Button JTable JButton TableModelAdapter

slide-55
SLIDE 55

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

MVP vs. Presentation Model: GUI State

  • MVP
  • View holds the GUI state
  • Presenter holds no state
  • Avoids having to synchronize copied GUI state
  • Presentation Model
  • View holds all GUI state
  • PM holds the relevant GUI state
  • Must synchronize PM state and View state
slide-56
SLIDE 56

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

MVP vs. Presentation Model: Testing

  • MVP
  • Allows to test the Presenter with a View stub
  • Allows to preview the View without the Presenter
  • Presentation Model
  • Allows to test the Presentation Model without the View
  • Allows to preview the View with a PM stub
slide-57
SLIDE 57

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

MVP vs. Presentation Model: Transformation Differences

  • Some Autonomous Views use low-level GUI state
  • Presenter can keep “dirty” low-level ops
  • Split to MVP is easier to do
  • Split to MVP may costs less
  • Split to PM may require extra work
  • Find and add GUI state abstractions
  • Add handlers to the view
  • You may benefit from the extra cleaning
slide-58
SLIDE 58

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

MVP vs. Presentation Model: General

  • Developers are used to operate on view state
  • Presenter depends on GUI component types
  • MVP addresses problems many faced with PM
slide-59
SLIDE 59

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Agenda

Introduction Autonomous View Model View Presenter Presentation Model Data Binding

slide-60
SLIDE 60

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Data Binding

  • Synchronizes two data sources
  • One-way or two-way
  • Typically supports type conversion
  • May provide a validation
slide-61
SLIDE 61

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Binding Examples

Presentation Model View JTable JButton TableModel Action

slide-62
SLIDE 62

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Binding Examples

View JTextField JCheckBox enabled=true selected=true Album classical=true

slide-63
SLIDE 63

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Binding Examples

GUI Form JTextField JCheckBox JFormattedTextField Database

slide-64
SLIDE 64

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Useful Swing Bindings

Presentation Model View JList ListModel JTable JButton TableModel Action JTree TreeModel

slide-65
SLIDE 65

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Swing Binding to Low-Level Models

Presentation Model View JTextField Document JCheckBox JFormattedTextField ToggleButtonModel Document

slide-66
SLIDE 66

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Wanted: Higher-Level Binding

Presentation Model View JTextField Text Model JCheckBox JFormattedTextField Boolean Model Date Model

slide-67
SLIDE 67

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Wanted: Full Binding Path

Presentation Model View JTextField Text Model JCheckBox JFormattedTextField Boolean Model Date Model Album classical=true artist=”John” released=05/16/06

slide-68
SLIDE 68

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

JGoodies Binding

  • Uses Swing bindings:
  • JList, JTable, JComboBox, JTree, JButton
  • Fills the gap where Swing uses low-level models:
  • JTextField, JCheckBox, ...
  • Converts Bean properties to a uniform model

(ValueModel)

  • Makes the hard stuff possible
  • Makes simple things a bit easier
slide-69
SLIDE 69

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

AlbumView: Init & Bind Components

private void initComponents() { artistField = Factory.createTextField( presentationModel.getModel(“artist”)); classicalBox = Factory.createCheckBox( presentationModel.getModel(“classical”)); songList = Factory.createList( presentationModel.getSongsAndSelection());

  • kButton = new JButton(

presentationModel.getOKAction()); }

slide-70
SLIDE 70

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

AlbumView: EnablementHandler

private void initPresentationLogic() { // Synchronize field enablement // with the PresentationModel state. PropertyConnector.connect( presentationModel, “ composerEnabled”, composerField, “ enabled”); }

slide-71
SLIDE 71

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

JSR 295: Beans Binding

  • Synchronizes a data source with a target

(often two bound bean properties)

  • Shall support type conversion and validation
  • Has a BindingContext as a container

for multiple bindings

slide-72
SLIDE 72

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Copying ...

  • Easy to understand
  • Works in almost all situations
  • Easy to debug; all data operations are explicit
  • Difficult to synchronize views
  • Needs discipline in a team
  • Coarse-grained updates
  • Leads to a lot of boilerplate code
slide-73
SLIDE 73

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

... vs. Automatic Binding

  • Fine-grained updates
  • Simplifies synchronization
  • Harder to understand and debug
  • Extra work for method renaming and obfuscators
slide-74
SLIDE 74

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Costs for Automatic Binding

  • Increases learning costs
  • Decreases production costs a little
  • Can significantly reduce the change costs
slide-75
SLIDE 75

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Summary

  • Starting point: Separated Presentation
  • Common and workable: Autonomous View
  • MVP works with view GUI state
  • PM copies state and requires synchronization
  • Swing has some Presentation Model support
slide-76
SLIDE 76

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

Advice

  • Use Separated Presentation whenever possible
  • Split up Autonomous Views if appropriate
  • Read Fowler's “Organizing Presentation Logic”
  • Use an automatic binding only if
  • it's reliable and flexible
  • at least one expert in the team masters it
slide-77
SLIDE 77

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

For More Information

Web Resources

  • Fowler's Further P of EAA – martinfowler.com/eaaDev
  • SwingLabs data binding – databinding.dev.java.net
  • Eclipse 3.2 data binding – www.eclipse.org
  • Oracle ADF – otn.oracle.com, search 'JClient'
  • JGoodies Binding – binding.dev.java.net

Binding tutorial contains Presentation Model examples

  • JSR 295 Beans Binding – jcp.org/en/jsr/detail?id=295
slide-78
SLIDE 78

Desktop Patterns and Data Binding J-Fall 2006 JGoodies

For More Information

Book

  • Scott Delap: Desktop Java Live

Presentations - www.JGoodies.com/articles

  • Desktop Patterns & Data Binding
  • Swing Data Binding