| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
package org.melati.poem; |
| 47 | |
|
| 48 | |
import org.melati.poem.generated.SettingBase; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
public class Setting extends SettingBase { |
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | 36 | public Setting() { } |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | 23 | private FieldAttributes<?> valueAttributes = null; |
| 98 | 23 | private Object raw = null; |
| 99 | 23 | private Object cooked = null; |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
public Setting(Integer typefactory, String name, String value, |
| 111 | 5 | String displayname, String description) { |
| 112 | 5 | setTypefactory_unsafe(typefactory); |
| 113 | 5 | setName_unsafe(name); |
| 114 | 5 | setValue_unsafe(value); |
| 115 | 5 | setDisplayname_unsafe(displayname); |
| 116 | 5 | setDescription_unsafe(description); |
| 117 | 5 | setUsereditable_unsafe(Boolean.TRUE); |
| 118 | 5 | setNullable_unsafe(Boolean.TRUE); |
| 119 | 5 | setSize_unsafe(new Integer(-1)); |
| 120 | 5 | setWidth_unsafe(new Integer(20)); |
| 121 | 5 | setHeight_unsafe(new Integer(1)); |
| 122 | 5 | setPrecision_unsafe(new Integer(22)); |
| 123 | 5 | setScale_unsafe(new Integer(2)); |
| 124 | 5 | } |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
public static class SettingValidationException extends PoemException { |
| 130 | |
private static final long serialVersionUID = 1L; |
| 131 | |
|
| 132 | |
|
| 133 | |
public String name; |
| 134 | |
|
| 135 | |
|
| 136 | |
public SettingValidationException(String name, Exception problem) { |
| 137 | |
super(problem); |
| 138 | |
this.name = name; |
| 139 | |
} |
| 140 | |
|
| 141 | |
|
| 142 | |
public String getMessage() { |
| 143 | |
return "A problem arose updating the value of the `" + name + |
| 144 | |
"' setting:\n" + subException.getMessage(); |
| 145 | |
} |
| 146 | |
} |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
public void setValue(String value) { |
| 154 | |
Object rawLocal; |
| 155 | |
try { |
| 156 | 4 | rawLocal = getType().rawOfString(value); |
| 157 | 1 | } catch (Exception e) { |
| 158 | 1 | throw new SettingValidationException(getName_unsafe(), e); |
| 159 | 3 | } |
| 160 | |
|
| 161 | 3 | super.setValue(value); |
| 162 | 3 | this.raw = rawLocal; |
| 163 | 3 | cooked = null; |
| 164 | 3 | } |
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
public void setRaw(Object raw) { |
| 171 | |
String string; |
| 172 | |
Object newRaw; |
| 173 | |
try { |
| 174 | 31 | string = getType().stringOfRaw(raw); |
| 175 | 1 | } catch (Exception e) { |
| 176 | 1 | throw new SettingValidationException(getName_unsafe(), e); |
| 177 | 30 | } |
| 178 | |
try { |
| 179 | 30 | newRaw = getType().rawOfString(string); |
| 180 | 2 | } catch (Exception e) { |
| 181 | 2 | throw new SettingValidationException(getName_unsafe(), e); |
| 182 | 28 | } |
| 183 | 28 | super.setValue(newRaw == null ? null : string); |
| 184 | 28 | this.raw = newRaw; |
| 185 | 28 | cooked = null; |
| 186 | 28 | } |
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
public Object getRaw() { |
| 192 | 24 | if (raw == null) |
| 193 | |
try { |
| 194 | 9 | raw = getType().rawOfString(getValue()); |
| 195 | 0 | } catch (Exception e) { |
| 196 | 0 | throw new SettingValidationException(getName_unsafe(), e); |
| 197 | 9 | } |
| 198 | |
|
| 199 | 24 | return raw; |
| 200 | |
} |
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
public Object getCooked() { |
| 206 | 23 | if (cooked == null) |
| 207 | 19 | cooked = getType().cookedOfRaw(getRaw()); |
| 208 | 23 | return cooked; |
| 209 | |
} |
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
public static class |
| 215 | |
SettingTypeMismatchException extends AppBugPoemException { |
| 216 | |
private static final long serialVersionUID = 1L; |
| 217 | |
|
| 218 | |
|
| 219 | |
public String name; |
| 220 | |
|
| 221 | |
public PoemTypeFactory type; |
| 222 | |
|
| 223 | |
public String reqType; |
| 224 | |
|
| 225 | |
|
| 226 | |
public SettingTypeMismatchException(String name, PoemTypeFactory type, |
| 227 | |
String reqType) { |
| 228 | |
this.name = name; |
| 229 | |
this.type = type; |
| 230 | |
this.reqType = reqType; |
| 231 | |
} |
| 232 | |
|
| 233 | |
|
| 234 | |
public String getMessage() { |
| 235 | |
return "The setting `" + name + "' has type `" + type + "' but " + |
| 236 | |
"the application asked for a value of type " + reqType; |
| 237 | |
} |
| 238 | |
} |
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
public Integer getIntegerCooked() { |
| 244 | 3 | Object cookedLocal = getCooked(); |
| 245 | 3 | if (cookedLocal == null && getNullable().booleanValue()) |
| 246 | 1 | return null; |
| 247 | 2 | else if (cookedLocal instanceof Integer) |
| 248 | 1 | return (Integer)cookedLocal; |
| 249 | |
else |
| 250 | 1 | throw new SettingTypeMismatchException(getName_unsafe(), |
| 251 | 1 | getTypefactory(), "Integer"); |
| 252 | |
} |
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
public String getStringCooked() { |
| 258 | 3 | Object cookedLocal = getCooked(); |
| 259 | 3 | if (cookedLocal == null && getNullable().booleanValue()) |
| 260 | 1 | return null; |
| 261 | 2 | else if (cookedLocal instanceof String) |
| 262 | 1 | return (String)cookedLocal; |
| 263 | |
else |
| 264 | 1 | throw new SettingTypeMismatchException(getName_unsafe(), |
| 265 | 1 | getTypefactory(), "String"); |
| 266 | |
} |
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
public Boolean getBooleanCooked() { |
| 272 | 3 | Object cookedLocal = getCooked(); |
| 273 | 3 | if (cookedLocal == null && getNullable().booleanValue()) |
| 274 | 1 | return null; |
| 275 | 2 | else if (cookedLocal instanceof Boolean) |
| 276 | 1 | return (Boolean)cookedLocal; |
| 277 | |
else |
| 278 | 1 | throw new SettingTypeMismatchException(getName_unsafe(), |
| 279 | 1 | getTypefactory(), "Boolean"); |
| 280 | |
} |
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
@SuppressWarnings("rawtypes") |
| 287 | |
private FieldAttributes valueFieldAttributes() { |
| 288 | 4 | if (valueAttributes == null) |
| 289 | 4 | valueAttributes = |
| 290 | 4 | fieldAttributesRenamedAs(getSettingTable().getValueColumn()); |
| 291 | |
|
| 292 | 4 | return valueAttributes; |
| 293 | |
} |
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
| 301 | |
public Field getValueField() { |
| 302 | |
try { |
| 303 | 4 | return new Field<String>(getRaw(), valueFieldAttributes()); |
| 304 | 0 | } catch (AccessPoemException accessException) { |
| 305 | 0 | return new Field<Exception>(accessException, valueFieldAttributes()); |
| 306 | |
} |
| 307 | |
} |
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
public void postEdit(boolean creating) { |
| 317 | 0 | super.postEdit(creating); |
| 318 | 0 | valueAttributes = null; |
| 319 | 0 | } |
| 320 | |
|
| 321 | |
} |