1 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 68 public abstract class BindingBase implements ObjectBinding { 70 71 private Object compiled; 72 73 protected Object context; 74 protected String expression; 75 76 protected BindingDelegate delegate; 77 protected boolean modelUpdateDisabled; 78 79 protected boolean usingNullForEmptyStrings; 80 protected boolean checkingForValueChange; 81 82 static Throwable unwind(Throwable th) { 83 if (th instanceof OgnlException) { 84 Throwable 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 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 getContext() { 106 return context; 107 } 108 109 public void setContext(Object 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 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 145 public void setValue(Object value) { 146 if (context == null) { 147 throw new BindingException("No context"); 148 } 149 150 try { 151 154 if (modelUpdateDisabled) { 155 return; 156 } 157 158 Object oldValue = null; 159 160 modelUpdateDisabled = true; 161 try { 162 163 if (delegate != null) { 164 oldValue = getValue(); 166 } 167 168 if (isUsingNullForEmptyStrings() && "".equals(value)) { 169 value = null; 170 } 171 172 if (isCheckingForValueChange()) { 173 175 Object 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 199 public Object 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 th) throws ValidationException, 214 BindingException { 215 Throwable root = BindingBase.unwind(th); 216 if (root instanceof ValidationException) { 217 throw (ValidationException) root; 218 } 219 else if (root instanceof NumberFormatException ) { 220 throw new ValidationException("Invalid numeric string"); 221 } 222 223 throw new BindingException("Evaluation failed in context: " + context, root); 224 } 225 226 } | Popular Tags |