1 /*
2 Copyright (c) 2003, Laurent Caillette.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modifica-
6 tion, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10
11 2. Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14
15 3. The end-user documentation included with the redistribution, if any, must
16 include the following acknowledgment: "This product includes software
17 written by Laurent Caillette."
18 Alternately, this acknowledgment may appear in the software itself, if
19 and wherever such third-party acknowledgments normally appear.
20
21 4. The name "Laurent Caillette" must not be used to endorse or
22 promote products derived from this software without
23 prior written permission. For written permission, please contact
24 laurent.caillette@laposte.net
25
26 5. Products derived from this software may not be called
27 "Laurent Caillette", nor may "Laurent Caillette" appear
28 in their name, without prior written permission of the
29 author.
30
31 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
32 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
33 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
34 AUTHOR (LAURENT CAILLETTE) BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
36 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
37 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
42 package tweed.binding;
43 import java.awt.EventQueue;
44 import java.lang.reflect.InvocationTargetException;
45 import tweed.context.Context;
46
47
48 /***
49 * Base class for transferring data between model and view.
50 * Subclasses must declare a constructor taking both as input parameters.
51 * Model and view members must be declared final. Both should be declared
52 * with the strongest possible type.
53 * <p>
54 * This class defines templates methods, which are especially useful
55 * for managing threading issues and exceptions.
56 *
57 * @author Laurent Caillette
58 * @version $Id$
59 */
60 public abstract class Binding {
61
62 private final Context context ;
63
64 protected Binding( Context context ) {
65 if( context == null ) {
66 throw new IllegalArgumentException( "context == null" ) ;
67 }
68 this.context = context ;
69 }
70
71 protected final Context getContext() {
72 return context ;
73 }
74
75 public InputValidationHub.ValidationMessageBuilder createMessage(
76 String message
77 ) {
78 InputValidationHub.ValidationMessageBuilder messageBuilder =
79 getContext().getInputValidationHub().
80 createValidationMessage( message )
81 ;
82 return messageBuilder ;
83 }
84
85 private void update( final boolean model ) {
86 if( EventQueue.isDispatchThread() ) {
87 if( model ) {
88 doUpdateModel() ;
89 } else {
90 doUpdateView() ;
91 }
92 } else {
93 try {
94 EventQueue.invokeAndWait( new Runnable() {
95 public void run() {
96 if( model ) {
97 doUpdateModel() ;
98 } else {
99 doUpdateView() ;
100 }
101 }
102 } ) ;
103 } catch( InterruptedException ex1 ) {
104 throw new RuntimeException( "Should never happen" ) ;
105 } catch( InvocationTargetException ex2 ) {
106 Throwable cause = ex2.getCause() ;
107 if( cause instanceof RuntimeException ) {
108 throw ( ( RuntimeException ) cause ) ;
109 } else {
110 throw new RuntimeException( ex2.getCause() ) ;
111 }
112 }
113 }
114 }
115
116 public final void updateView() {
117 update( false ) ;
118 }
119
120 public final void updateModel() {
121 update( true ) ;
122 }
123
124 protected abstract void doUpdateView() ;
125
126 protected abstract void doUpdateModel() ;
127
128 /***
129 * Override this method to provide a "fail-fast" approach. When this
130 * method returns <code>true</code>, any failing binding will throw a
131 * {@link ValidationException} at the end of the execution of
132 * {@link #updateModel()} method.
133 * @return <code>false</code> by default.
134 */
135 // public boolean isFailFast() {
136 // return false ;
137 // }
138
139
140 }
This page was automatically generated by Maven