Source Code Cross Referenced for EditorBoundExample.java in  » Swing-Library » jgoodies-data-binding » com » jgoodies » binding » tutorial » basics » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
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 Source Code / Java Documentation » Swing Library » jgoodies data binding » com.jgoodies.binding.tutorial.basics 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003:         *
004:         * Redistribution and use in source and binary forms, with or without
005:         * modification, are permitted provided that the following conditions are met:
006:         *
007:         *  o Redistributions of source code must retain the above copyright notice,
008:         *    this list of conditions and the following disclaimer.
009:         *
010:         *  o Redistributions in binary form must reproduce the above copyright notice,
011:         *    this list of conditions and the following disclaimer in the documentation
012:         *    and/or other materials provided with the distribution.
013:         *
014:         *  o Neither the name of JGoodies Karsten Lentzsch nor the names of
015:         *    its contributors may be used to endorse or promote products derived
016:         *    from this software without specific prior written permission.
017:         *
018:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019:         * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020:         * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021:         * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022:         * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023:         * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024:         * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025:         * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026:         * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027:         * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028:         * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029:         */
030:
031:        package com.jgoodies.binding.tutorial.basics;
032:
033:        import java.awt.event.ActionEvent;
034:
035:        import javax.swing.*;
036:        import javax.swing.text.JTextComponent;
037:
038:        import com.jgoodies.binding.adapter.BasicComponentFactory;
039:        import com.jgoodies.binding.beans.PropertyConnector;
040:        import com.jgoodies.binding.tutorial.Album;
041:        import com.jgoodies.binding.tutorial.AlbumPresentationModel;
042:        import com.jgoodies.binding.tutorial.TutorialUtils;
043:        import com.jgoodies.forms.builder.PanelBuilder;
044:        import com.jgoodies.forms.factories.ButtonBarFactory;
045:        import com.jgoodies.forms.layout.CellConstraints;
046:        import com.jgoodies.forms.layout.FormLayout;
047:
048:        /**
049:         * Builds an editor with components bound to the domain object properties
050:         * using adapting ValueModels created by a PresentationModel.
051:         *
052:         * @author Karsten Lentzsch
053:         * @version $Revision: 1.13 $
054:         *
055:         * @see com.jgoodies.binding.PresentationModel
056:         */
057:
058:        public final class EditorBoundExample {
059:
060:            /**
061:             * Holds the edited Album and vends ValueModels that adapt Album properties.
062:             */
063:            private final AlbumPresentationModel presentationModel;
064:
065:            private JTextComponent titleField;
066:            private JTextComponent artistField;
067:            private JCheckBox classicalBox;
068:            private JTextComponent composerField;
069:            private JButton closeButton;
070:
071:            // Launching **************************************************************
072:
073:            public static void main(String[] args) {
074:                try {
075:                    UIManager
076:                            .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
077:                } catch (Exception e) {
078:                    // Likely PlasticXP is not in the class path; ignore.
079:                }
080:                JFrame frame = new JFrame();
081:                frame.setTitle("Binding Tutorial :: Editor (Bound)");
082:                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
083:                EditorBoundExample example = new EditorBoundExample();
084:                JComponent panel = example.buildPanel();
085:                frame.getContentPane().add(panel);
086:                frame.pack();
087:                TutorialUtils.locateOnOpticalScreenCenter(frame);
088:                frame.setVisible(true);
089:            }
090:
091:            // Instance Creation ******************************************************
092:
093:            /**
094:             * Constructs an editor on an Album example instance.
095:             */
096:            public EditorBoundExample() {
097:                this (Album.ALBUM1);
098:            }
099:
100:            /**
101:             * Constructs an editor for an Album to be edited.
102:             *
103:             * @param album    the Album to be edited
104:             */
105:            public EditorBoundExample(Album album) {
106:                this .presentationModel = new AlbumPresentationModel(album);
107:            }
108:
109:            // Initialization *********************************************************
110:
111:            /**
112:             * Creates, binds and configures the UI components.
113:             * Changes are committed to the value models on focus lost.<p>
114:             *
115:             * The coding style used here is based on standard Swing components.
116:             * Therefore we can create and bind the components in one step.
117:             * And that's the purpose of the BasicComponentFactory class.<p>
118:             *
119:             * If you need to bind custom components, for example MyTextField,
120:             * MyCheckBox, MyComboBox, you can use the more basic Bindings class.
121:             * The code would then read:<pre>
122:             * titleField = new MyTextField();
123:             * Bindings.bind(titleField,
124:             *               presentationModel.getModel(Album.PROPERTYNAME_TITLE));
125:             * </pre><p>
126:             *
127:             * I strongly recommend to use a custom ComponentFactory,
128:             * the BasicComponentFactory or the Bindings class. These classes
129:             * hide details of the binding.
130:             * So you better <em>not</em> write the following code:<pre>
131:             * titleField = new JTextField();
132:             * titleField.setDocument(new DocumentAdapter(
133:             *     presentationModel.getModel(Album.PROPERTYNAME_TITLE)));
134:             * </pre>
135:             */
136:            private void initComponents() {
137:                titleField = BasicComponentFactory
138:                        .createTextField(presentationModel
139:                                .getModel(Album.PROPERTYNAME_TITLE));
140:                artistField = BasicComponentFactory
141:                        .createTextField(presentationModel
142:                                .getModel(Album.PROPERTYNAME_ARTIST));
143:                classicalBox = BasicComponentFactory.createCheckBox(
144:                        presentationModel
145:                                .getModel(Album.PROPERTYNAME_CLASSICAL),
146:                        "Classical");
147:                composerField = BasicComponentFactory
148:                        .createTextField(presentationModel
149:                                .getModel(Album.PROPERTYNAME_COMPOSER));
150:                closeButton = new JButton(new CloseAction());
151:
152:                boolean composerEnabled = presentationModel.isComposerEnabled();
153:                composerField.setEnabled(composerEnabled);
154:            }
155:
156:            /**
157:             * Registers a listener with the presentation model's "composerEnabled"
158:             * property to switch the composer field's enablement. For demonstration
159:             * purposes a listener is registered that writes changes to the console.
160:             */
161:            private void initEventHandling() {
162:                // Synchronize the composer field enablement with 'composerEnabled'.
163:                PropertyConnector.connect(composerField, "enabled",
164:                        presentationModel,
165:                        AlbumPresentationModel.PROPERTYNAME_COMPOSER_ENABLED);
166:
167:                // Report changes in all bound Album properties.
168:                presentationModel.addBeanPropertyChangeListener(TutorialUtils
169:                        .createDebugPropertyChangeListener());
170:            }
171:
172:            // Building ***************************************************************
173:
174:            /**
175:             * Builds and returns the panel.
176:             *
177:             * @return the built panel
178:             */
179:            public JComponent buildPanel() {
180:                initComponents();
181:                initEventHandling();
182:
183:                FormLayout layout = new FormLayout(
184:                        "right:pref, 3dlu, 150dlu:grow",
185:                        "p, 3dlu, p, 3dlu, p, 3dlu, p, 9dlu, p");
186:
187:                PanelBuilder builder = new PanelBuilder(layout);
188:                builder.setDefaultDialogBorder();
189:                CellConstraints cc = new CellConstraints();
190:
191:                builder.addLabel("Artist", cc.xy(1, 1));
192:                builder.add(artistField, cc.xy(3, 1));
193:                builder.addLabel("Title", cc.xy(1, 3));
194:                builder.add(titleField, cc.xy(3, 3));
195:                builder.add(classicalBox, cc.xy(3, 5));
196:                builder.addLabel("Composer", cc.xy(1, 7));
197:                builder.add(composerField, cc.xy(3, 7));
198:                builder.add(buildButtonBar(), cc.xyw(1, 9, 3));
199:
200:                return builder.getPanel();
201:            }
202:
203:            private JComponent buildButtonBar() {
204:                return ButtonBarFactory.buildRightAlignedBar(closeButton);
205:            }
206:
207:            // Presentation Model *****************************************************
208:
209:            /**
210:             * An Action that prints the current bean to the System console
211:             * before it exists the System.
212:             *
213:             * Actions belong to the presentation model. However, to keep
214:             * this tutorial small I've chosen to reuse a single presentation model
215:             * for all album examples and so, couldn't put in different close actions.
216:             */
217:            private final class CloseAction extends AbstractAction {
218:
219:                private CloseAction() {
220:                    super ("Close");
221:                }
222:
223:                public void actionPerformed(ActionEvent e) {
224:                    System.out.println(presentationModel.getBean());
225:                    System.exit(0);
226:                }
227:            }
228:
229:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.