Using buffered adapting ValueModels created by a PresentationModel : Data Binding « Swing Components « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Swing Components » Data BindingScreenshots 
Using buffered adapting ValueModels created by a PresentationModel
Using buffered adapting ValueModels created by a PresentationModel


/*
 * Copyright (c) 2002-2005 JGoodies Karsten Lentzsch. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 
 *  o Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer. 
 *     
 *  o Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution. 
 *     
 *  o Neither the name of JGoodies Karsten Lentzsch nor the names of 
 *    its contributors may be used to endorse or promote products derived 
 *    from this software without specific prior written permission. 
 *     
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

package com.jgoodies.binding.tutorial.basics;

import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;
import javax.swing.text.JTextComponent;

import com.jgoodies.binding.PresentationModel;
import com.jgoodies.binding.adapter.BasicComponentFactory;
import com.jgoodies.binding.tutorial.Album;
import com.jgoodies.binding.tutorial.TutorialUtils;
import com.jgoodies.binding.value.AbstractValueModel;
import com.jgoodies.binding.value.ValueModel;
import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.factories.ButtonBarFactory;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

/**
 * Builds an editor with components bound to the domain object properties
 * using buffered adapting ValueModels created by a PresentationModel.
 * These buffers can be committed on 'Apply' and flushed on 'Reset'.
 * A second set of components displays the domain object properties.
 *
 @author Karsten Lentzsch
 @version $Revision: 1.5 $
 
 @see com.jgoodies.binding.PresentationModel
 @see com.jgoodies.binding.value.BufferedValueModel
 @see com.jgoodies.binding.value.Trigger
 */

public class EditorBufferedExample {
    
    /**
     * Holds the edited Album and vends ValueModels that adapt Album properties.
     * In this example we will request BufferedValueModels for the editor.
     */
    private final PresentationModel presentationModel;

    private JTextComponent titleField;
    private JTextComponent artistField;
    private JCheckBox      classicalBox;
    private JTextComponent composerField;
    
    private JTextComponent unbufferedTitleField;
    private JTextComponent unbufferedArtistField;
    private JCheckBox      unbufferedClassicalBox;
    private JTextComponent unbufferedComposerField;
    
    private JButton applyButton;
    private JButton resetButton;

 
    // Launching **************************************************************
    
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
        catch (Exception e) {
            // Likely PlasticXP is not in the class path; ignore.
        }
        JFrame frame = new JFrame();
        frame.setTitle("Binding Tutorial :: Editor (Bound, Buffered)");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        EditorBufferedExample example = new EditorBufferedExample();
        JComponent panel = example.build();
        frame.getContentPane().add(panel);
        frame.pack();
        TutorialUtils.locateOnScreenCenter(frame);
        frame.setVisible(true);
    }
    
    
    // Instance Creation ******************************************************
    
    /**
     * Constructs a buffered editor on an example Album.
     */
    public EditorBufferedExample() {
        this(Album.ALBUM1);
    }
    

    /**
     * Constructs a buffered editor for an Album to be edited.
     
     @param album    the Album to be edited
     */
    public EditorBufferedExample(Album album) {
        this.presentationModel = new PresentationModel(album);
    }
    

    // Initialization *********************************************************

    /**
     * Creates, binds and configures the UI components. 
     * Changes are committed to the value models on apply and are
     * flushed on reset. 
     */
    private void initComponents() {
        titleField = BasicComponentFactory.createTextField(
                presentationModel.getBufferedModel(Album.PROPERTYNAME_TITLE));
        artistField = BasicComponentFactory.createTextField(
                presentationModel.getBufferedModel(Album.PROPERTYNAME_ARTIST));
        classicalBox = BasicComponentFactory.createCheckBox (
                presentationModel.getBufferedModel(Album.PROPERTYNAME_CLASSICAL)
                "Classical");
        composerField = BasicComponentFactory.createTextField(
                presentationModel.getBufferedModel(Album.PROPERTYNAME_COMPOSER));

        unbufferedTitleField = BasicComponentFactory.createTextField(
                presentationModel.getModel(Album.PROPERTYNAME_TITLE));
        unbufferedTitleField.setEditable(false);
        unbufferedArtistField = BasicComponentFactory.createTextField(
                presentationModel.getModel(Album.PROPERTYNAME_ARTIST));
        unbufferedArtistField.setEditable(false);
        unbufferedClassicalBox = BasicComponentFactory.createCheckBox (
                presentationModel.getModel(Album.PROPERTYNAME_CLASSICAL)
                "Classical");
        unbufferedClassicalBox.setEnabled(false);
        unbufferedComposerField = BasicComponentFactory.createTextField(
                presentationModel.getModel(Album.PROPERTYNAME_COMPOSER));
        unbufferedComposerField.setEditable(false);

        applyButton = new JButton(new ApplyAction());
        resetButton = new JButton(new ResetAction());
        
        updateComposerField();
    }
    
    
    /**
     * Observes the buffered <em>classical</em> property 
     * to update the composer field's enablement and contents.<p>
     *  
     * If the AlbumPresentationModel would provide a property
     * <em>bufferedComposerEnabled</em> we could use that instead.
     * I've not added the latter property to the AlbumPresentationModel
     * so it remains close to the example used in Martin Fowler's 
     * description of the 
     * <a href="http://martinfowler.com/eaaDev/PresentationModel.html">Presentation
     * Model</a> pattern. 
     */
    private void initEventHandling() {
        ValueModel bufferedClassicalModel = 
            presentationModel.getBufferedModel(Album.PROPERTYNAME_CLASSICAL);
        bufferedClassicalModel.addValueChangeListener(
                new BufferedClassicalChangeHandler());
    }
    
    
    // Building ***************************************************************

    /**
     * Builds and returns a panel that consists of the editor and display.
     
     @return the built panel
     */
    public JComponent build() {
        initComponents();
        initEventHandling();

        FormLayout layout = new FormLayout(
                "right:pref, 3dlu, 150dlu:grow",
                "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 17dlu, " +
                "p, 3dlu, p, 3dlu, p, 3dlu, p, 3dlu, p, 17dlu, p");
                
        PanelBuilder builder = new PanelBuilder(layout);
        builder.setDefaultDialogBorder();
        CellConstraints cc = new CellConstraints();

        builder.addSeparator("Buffered",        cc.xyw(1,  13));
        builder.addLabel("Title",               cc.xy (1,  3));
        builder.add(titleField,                 cc.xy (3,  3));
        builder.addLabel("Artist",              cc.xy (1,  5));
        builder.add(artistField,                cc.xy (3,  5));
        builder.add(classicalBox,               cc.xy (3,  7));
        builder.addLabel("Composer",            cc.xy (1,  9));
        builder.add(composerField,              cc.xy (3,  9));

        builder.addSeparator("Unbuffered",      cc.xyw(1113));
        builder.addLabel("Title",               cc.xy (113));
        builder.add(unbufferedTitleField,       cc.xy (313));
        builder.addLabel("Artist",              cc.xy (115));
        builder.add(unbufferedArtistField,      cc.xy (315));
        builder.add(unbufferedClassicalBox,     cc.xy (317));
        builder.addLabel("Composer",            cc.xy (119));
        builder.add(unbufferedComposerField,    cc.xy (319));
        
        builder.add(buildButtonBar(),           cc.xyw(1213));
        
        return builder.getPanel();
    }
    
    
    private JComponent buildButtonBar() {
        return ButtonBarFactory.buildRightAlignedBar(
                applyButton,
                resetButton);
    }
    
    
    // Event Handling *********************************************************
    
    /**
     * Updates the composer field's enablement and contents.
     * Sets the enablement according to the boolean state of 
     * the bufferd classical property. If the composer is not enabled, 
     * we copy the domain logic and set the composer to <code>null</code>.
     */
    private void updateComposerField() {
        AbstractValueModel bufferedClassicalModel = 
            presentationModel.getBufferedModel(Album.PROPERTYNAME_CLASSICAL);        
        boolean composerEnabled = bufferedClassicalModel.booleanValue();
        composerField.setEnabled(composerEnabled);
        if (!composerEnabled) {
            presentationModel.getBufferedModel(Album.PROPERTYNAME_COMPOSER).
                setValue(null);
        }
    }
    
    
    private class BufferedClassicalChangeHandler implements PropertyChangeListener {
        
        /**
         * The buffered (Boolean) classical property has changed.
         * Updates the enablement and contents of the composer field.
         */
        public void propertyChange(PropertyChangeEvent evt) {
            updateComposerField();
        }
    }
    
    
    // Actions ****************************************************************
    
    /** 
     * Commits the Trigger used to buffer the editor contents.
     */
    private final class ApplyAction extends AbstractAction {
        
        private ApplyAction() {
            super("Apply");
        }
        
        public void actionPerformed(ActionEvent e) {
            presentationModel.triggerCommit();
        }
    }

    
    /** 
     * Flushed the Trigger used to buffer the editor contents.
     */
    private final class ResetAction extends AbstractAction {
        
        private ResetAction() {
            super("Reset");
        }
        
        public void actionPerformed(ActionEvent e) {
            presentationModel.triggerFlush();
        }
    }

    
}

           
       
binding.zip( 506 k)
Related examples in the same category
1. Demonstrates three different styles when to commit changesDemonstrates three different styles when to commit changes
2. Builds an editor with components bound to the domain object propertiesBuilds an editor with components bound to the domain object properties
3. Builds an editor that copies data from the domain back and forthBuilds an editor that copies data from the domain back and forth
4. JGoodies Binding: Selection In List Bean Channel ExampleJGoodies Binding: Selection In List Bean Channel Example
5. JGoodies Binding: Selection In List ExampleJGoodies Binding: Selection In List Example
6. JGoodies Binding: Selection In List Model ExampleJGoodies Binding: Selection In List Model Example
7. JGoodies Binding: Toggle Button Adapter ExampleJGoodies Binding: Toggle Button Adapter Example
8. JGoodies Binding: Value Holder ExampleJGoodies Binding: Value Holder Example
9. JGoodies Binding: Abstract Table Model ExampleJGoodies Binding: Abstract Table Model Example
10. JGoodies Binding: Basic Component Factory ExampleJGoodies Binding: Basic Component Factory Example
11. JGoodies Binding: Bean Adapter ExampleJGoodies Binding: Bean Adapter Example
12. JGoodies Binding: Bounded Range Adapter ExampleJGoodies Binding: Bounded Range Adapter Example
13. JGoodies Binding: Buffering Presentation Model ExampleJGoodies Binding: Buffering Presentation Model Example
14. JGoodies Binding: ComboBox Adapter ExampleJGoodies Binding: ComboBox Adapter Example
15. JGoodies Binding: Converter Factory ExampleJGoodies Binding: Converter Factory Example
16. JGoodies Binding: Feed Bean Example 1JGoodies Binding: Feed Bean Example 1
17. JGoodies Binding: Feed Bean Example 2JGoodies Binding: Feed Bean Example 2
18. JGoodies Binding: Feed Bean Example 3JGoodies Binding: Feed Bean Example 3
19. JGoodies Binding: Presentation Bean Channel ExampleJGoodies Binding: Presentation Bean Channel Example
20. JGoodies Binding: Presentation Bean Property Change Listener ExampleJGoodies Binding: Presentation Bean Property Change Listener Example
21. JGoodies Binding: Presentation Model Property Change ExampleJGoodies Binding: Presentation Model Property Change Example
22. JGoodies Binding: Property Adapter Example 1JGoodies Binding: Property Adapter Example 1
23. JGoodies Binding: Property Adapter Example 2JGoodies Binding: Property Adapter Example 2
24. JGoodies Binding: Property Adapter Example 3JGoodies Binding: Property Adapter Example 3
25. JGoodies Binding: Property Connector ExampleJGoodies Binding: Property Connector Example
26. JGoodies Binding: Radio Button Adapter ExampleJGoodies Binding: Radio Button Adapter Example
27. Swingx: Swing Data BindingSwingx: Swing Data Binding
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.