Example: tourism industry

Swing Data Binding - JDiskReport

Swing data BindingKarsten Lentzsch :: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignPresentation GoalsUnderstand MVC and Swing how to bind domain objects to Swing UI components.:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignSpeaker 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:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignAgenda introduction MVC and Swing How to bind single values? How to bind lists A 3-tier Swing architecture How Binding works in projects:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignSwing Building BlocksJRE / SwingPanelsFoundationBasic LibrariesApplication FrameHelper Code/LibsValidationApplicationsBindingLa youtApplication 1 AppearanceComponents, Application Management, etc.

:: JGOODIES :: Java User Interface Design Agenda Introduction MVC and Swing How to bind single values? How to bind lists A 3-tier Swing architecture How binding works in …

Tags:

  Introduction, Data, Binding, Swing, Swing data binding

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of Swing Data Binding - JDiskReport

1 Swing data BindingKarsten Lentzsch :: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignPresentation GoalsUnderstand MVC and Swing how to bind domain objects to Swing UI components.:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignSpeaker 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:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignAgenda introduction MVC and Swing How to bind single values? How to bind lists A 3-tier Swing architecture How Binding works in projects:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignSwing Building BlocksJRE / SwingPanelsFoundationBasic LibrariesApplication FrameHelper Code/LibsValidationApplicationsBindingLa youtApplication 1 AppearanceComponents, Application Management, etc.

2 UtilsHelpPrintingApplication 2:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignSwing Building BlocksJRE / SwingPanelsFoundationBasic LibrariesApplication FrameHelper Code/LibsValidationApplicationsBindingLa youtApplication 1 AppearanceComponents, Application Management, etc. UtilsHelpPrintingApplication 2:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignQuestions 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?:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignI - BasicsMVC and Swing :: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignBefore MVCS tateState Operations(Control Behavior)Painting CodeHow to live without MVC?:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignBefore MVC: 2 LayersStateState OperationsPainting CodeClientServer:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignSeparate 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:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignDomain and PresentationStateState ModificationsPainting CodePresentation LayerDomain Layer:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignLoose 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:: JGOODIES :: :: JGOODIES.

3 Java User Interface DesignJava User Interface DesignLoose CouplingStateState ModificationsPainting Codereports changesrefers to/modifies:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignSeparate View and ControllerIf you separate the painting code (View) from the state modifications (Controller),it's easier to: combine views and controllers reuse views and controllers:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignMVCM odelControllerViewchangesreportsrefers to:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignMVCM odelControllerViewchangesreportsrefers to:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignUI State vs. data StateWe can categorize models into: domain related GUI relatedGUI state can make up its own layer.:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignMVC plus Model LayerDomain ModelControllerViewPresentation LayerDomain LayerGUI Model(GUI) Model Layer:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignCandidates 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.

4 G. mouse pressed, mouse over Password in a login dialog Search values:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignConbining MVC TriadsA typical MVC UI combines MVC triads. Defines models as a graph of domain objects Composes larger views from small views Composes controllers from subcontrollers:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignMVC Triads with Model LayerDOCVP resentation LayerDomain LayerMModel LayerVVCCMMD omain ObjectDO:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignFactoring out the Look&FeelSwing can change its appearance and behavioror in other words: look and feel.:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignM-JComponent-VCControllerViewSwing Model(s)JComponent :: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignMetalCheckBoxUIExample: JCheckBoxEventHandlingPaintingToggleButt onModelJCheckBox:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignMetalCheckBoxUIJC heckBox: Some DetailsEventHandlingPaintingToggleButton ModelJCheckBoxBasic-ButtonUIMouseListene rMouseMotionListenerChangeListenerProper tyChangeListenerFocusListenerChange-List ener:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignJCheckBox: Types of StateToggleButtonModelJCheckBoxenabledte xt.

5 Selected armed pressed, ..GUI StateData StateGUI Properties:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignJCheckBox: Binding TaskToggleButtonModelJCheckBoxselectedDa ta ModelGUI ComponentanAlbumtitle= Preludes classical=trueDomain object:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignJCheckBox: Binding TaskaToggleButtonModelaJCheckBoxselected =trueData ModelGUI ComponentanAlbumtitle= Preludes classical=trueDomain Objectconnect, synchronize:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignSummary 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 MVCT herefore, we will search and comparebinding solutions for Swing , not MVC.:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignII - Binding ValuesHow to connectdomain objects with UI components?:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignBinding 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?

6 Indirection as in an Master-Detail view Convert types, e. g. Date to String:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignCopying Values to ViewsaToggleButtonModelaJCheckBoxselecte d=falseanAlbumtitle= Preludes classical=true1. Read#isClassical#setSelected2. Write:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignCopying Values to ViewsaToggleButtonModelaJCheckBoxselecte d=trueanAlbumtitle= Preludes classical=true3. Value changed:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignCopying Values to the DomainaToggleButtonModelaJCheckBoxselect ed=trueanAlbumtitle= Preludes classical=false2. Write#setClassical#isSelected1. Read:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignCopying Values to the DomainaToggleButtonModelaJCheckBoxselect ed=trueanAlbumtitle= Preludes classical=true3. Value changed:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignCode Example: Copy to Viewpublic void modelToView() { Album anAlbum = getEditedAlbum(); ( ()); ( ()).}

7 }:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignCode Example: Copy to Domainpublic void viewToModel() { Album anAlbum = getEditedAlbum(); ( (); ( (); ..}:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignCopying: 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 :: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignAlternativeaToggleButtonModelaJChe ckBoxselected=falseanAlbumtitle= Preludes classical=trueNote: you can't share the model:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignaBooktitle= Swing available=trueDirect Adapter: TableModelmyAlbumTableAdapteraJTableaBoo ktitle= Swing available=trueanAlbumtitle= Preludes classical=trueTableModelimplementsConver ts Album propertiesto TableModel:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignDirect Adapter.))

8 JCheckBoxaJCheckBoxanAlbumtitle= Preludes classical=trueanAlbumClassical-ToggleBut tonAdapterToggleButtonModelimplementsCon verts Album#classicalto ToggleButtonModel:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignProblem with Direct AdaptersRequires an individual adapter for eachdomain object to incompatible electric is all the same except for the methods that read and write domain properties.:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignConcept 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.:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignValueModel and AdapteraToggleButtonAdapteraJCheckBoxanA lbumtitle= Preludes classical=trueaValueModel:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignValueModel: Requirements We want to get its value We want to set its value We want to observe changes:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignThe ValueModel Interfacepublic interface ValueModel { Object getValue(); void setValue(Object newValue); void addChangeListener(ChangeListener l); void removeChangeListener(ChangeListener l);}:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignWhich Event Type?

9 ChangeEvent reports no new value; must be read from the model if necessary PropertyChangeEvent provides the old and new value; both can be null:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignValueModel & PropertyAdapteraToggleButtonAdapteraJChe ckBoxanAlbumtitle= Preludes classical=trueToggleButtonModelaProperty AdapterpropertyName= classical ValueModelimplementsimplements:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignDomain 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.:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface Design(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.

10 :: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignPropertyAdapter 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:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignValueModel & PropertyAdapteraToggleButtonAdapteraJChe ckBoxanAlbum (Bean)title= Preludes classical=trueaPropertyAdapterpropertyNa me= classical ValueModelimplementsget/set:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignBuild a Chain of Adaptersprivate void initComponents() { Album album = getEditedAlbum(); ValueModel aValueModel = new PropertyAdapter(album, classical ); JCheckBox classicalBox = new JCheckBox(); ( new ToggleButtonAdapter(aValueModel));}:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignComponentFactoryprivate void initComponents() { Album album = getEditedAlbum(); JCheckBox classicalBox = ( album, );}:: JGOODIES :: :: JGOODIES :: Java User Interface DesignJava User Interface DesignBuffering: Delaying Commits Selecting the JCheckBox changes the bound domain property immediately.


Related search queries