1 46 package groovy.model; 47 48 import groovy.lang.Closure; 49 50 58 public class ClosureModel implements ValueModel, NestedValueModel { 59 60 private ValueModel sourceModel; 61 private Closure readClosure; 62 private Closure writeClosure; 63 private Class type; 64 65 public ClosureModel(ValueModel sourceModel, Closure readClosure) { 66 this(sourceModel, readClosure, null); 67 } 68 69 public ClosureModel(ValueModel sourceModel, Closure readClosure, Closure writeClosure) { 70 this(sourceModel, readClosure, writeClosure, Object .class); 71 } 72 73 public ClosureModel(ValueModel sourceModel, Closure readClosure, Closure writeClosure, Class type) { 74 this.sourceModel = sourceModel; 75 this.readClosure = readClosure; 76 this.writeClosure = writeClosure; 77 this.type = type; 78 } 79 80 public ValueModel getSourceModel() { 81 return sourceModel; 82 } 83 84 public Object getValue() { 85 Object source = sourceModel.getValue(); 86 if (source != null) { 87 return readClosure.call(source); 88 } 89 return null; 90 } 91 92 public void setValue(Object value) { 93 if (writeClosure != null) { 94 Object source = sourceModel.getValue(); 95 if (source != null) { 96 writeClosure.call(new Object [] { source, value }); 97 } 98 } 99 } 100 101 public Class getType() { 102 return type; 103 } 104 105 public boolean isEditable() { 106 return writeClosure != null; 107 } 108 } 109 | Popular Tags |