KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > swing > BindingBase


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.swing;
57
58 import ognl.Ognl;
59 import ognl.OgnlException;
60
61 import org.objectstyle.cayenne.CayenneRuntimeException;
62 import org.objectstyle.cayenne.util.Util;
63 import org.objectstyle.cayenne.validation.ValidationException;
64
65 /**
66  * @author Andrei Adamchik
67  */

68 // TODO: extend BindingExpression, unless we decide to make it a composition...
69
public abstract class BindingBase implements ObjectBinding {
70
71     private Object JavaDoc compiled;
72
73     protected Object JavaDoc context;
74     protected String JavaDoc expression;
75
76     protected BindingDelegate delegate;
77     protected boolean modelUpdateDisabled;
78
79     protected boolean usingNullForEmptyStrings;
80     protected boolean checkingForValueChange;
81
82     static Throwable JavaDoc unwind(Throwable JavaDoc th) {
83         if (th instanceof OgnlException) {
84             Throwable JavaDoc reason = ((OgnlException) th).getReason();
85             return (reason != null) ? unwind(reason) : th;
86         }
87         else {
88             return Util.unwindException(th);
89         }
90     }
91
92     public BindingBase(String JavaDoc propertyExpression) {
93
94         try {
95             this.compiled = Ognl.parseExpression(propertyExpression);
96         }
97         catch (OgnlException ex) {
98             throw new CayenneRuntimeException("Invalid expression - "
99                     + propertyExpression, BindingBase.unwind(ex));
100         }
101
102         this.expression = propertyExpression;
103     }
104
105     public Object JavaDoc getContext() {
106         return context;
107     }
108
109     public void setContext(Object JavaDoc object) {
110         this.context = object;
111     }
112
113     public boolean isCheckingForValueChange() {
114         return checkingForValueChange;
115     }
116
117     public void setCheckingForValueChange(boolean checkingForValueChange) {
118         this.checkingForValueChange = checkingForValueChange;
119     }
120
121     public boolean isUsingNullForEmptyStrings() {
122         return usingNullForEmptyStrings;
123     }
124
125     public void setUsingNullForEmptyStrings(boolean b) {
126         this.usingNullForEmptyStrings = b;
127     }
128
129     public String JavaDoc getExpression() {
130         return expression;
131     }
132
133     public BindingDelegate getDelegate() {
134         return delegate;
135     }
136
137     public void setDelegate(BindingDelegate delegate) {
138         this.delegate = delegate;
139     }
140
141     /**
142      * Pushes a new value to the bound context. If binding delegate is set, notifies it
143      * delegate about the update.
144      */

145     public void setValue(Object JavaDoc value) {
146         if (context == null) {
147             throw new BindingException("No context");
148         }
149
150         try {
151             // prevent the same thread from calling setValue recursively - something that
152
// may happen due to event loops.
153

154             if (modelUpdateDisabled) {
155                 return;
156             }
157
158             Object JavaDoc oldValue = null;
159
160             modelUpdateDisabled = true;
161             try {
162
163                 if (delegate != null) {
164                     // find old value
165
oldValue = getValue();
166                 }
167
168                 if (isUsingNullForEmptyStrings() && "".equals(value)) {
169                     value = null;
170                 }
171
172                 if (isCheckingForValueChange()) {
173                     // avoid calling getValue() twice...
174

175                     Object JavaDoc existingValue = (delegate != null) ? oldValue : getValue();
176                     if (Util.nullSafeEquals(value, existingValue)) {
177                         return;
178                     }
179                 }
180
181                 Ognl.setValue(compiled, context, value);
182             }
183             finally {
184                 modelUpdateDisabled = false;
185             }
186
187             if (delegate != null) {
188                 delegate.modelUpdated(this, oldValue, value);
189             }
190         }
191         catch (OgnlException ex) {
192             processException(ex);
193         }
194     }
195
196     /**
197      * Pulls bound value from the context.
198      */

199     public Object JavaDoc getValue() {
200         if (context == null) {
201             throw new BindingException("No context");
202         }
203
204         try {
205             return Ognl.getValue(compiled, context);
206         }
207         catch (OgnlException ex) {
208             processException(ex);
209             return null;
210         }
211     }
212
213     protected void processException(Throwable JavaDoc th) throws ValidationException,
214             BindingException {
215         Throwable JavaDoc root = BindingBase.unwind(th);
216         if (root instanceof ValidationException) {
217             throw (ValidationException) root;
218         }
219         else if (root instanceof NumberFormatException JavaDoc) {
220             throw new ValidationException("Invalid numeric string");
221         }
222
223         throw new BindingException("Evaluation failed in context: " + context, root);
224     }
225
226 }
Popular Tags