1 17 18 package org.objectweb.jac.aspects.gui.web; 19 20 import java.lang.IllegalAccessException ; 21 import org.apache.log4j.Logger; 22 import org.objectweb.jac.aspects.gui.FieldEditor; 23 import org.objectweb.jac.aspects.gui.FieldUpdate; 24 import org.objectweb.jac.aspects.gui.GuiAC; 25 import org.objectweb.jac.aspects.gui.Length; 26 import org.objectweb.jac.aspects.gui.Utils; 27 import org.objectweb.jac.core.Collaboration; 28 import org.objectweb.jac.core.rtti.ClassItem; 29 import org.objectweb.jac.core.rtti.FieldItem; 30 import org.objectweb.jac.core.rtti.MethodItem; 31 import org.objectweb.jac.aspects.gui.Unit; 32 33 36 public abstract class AbstractFieldEditor extends AbstractView 37 implements FieldEditor, FieldUpdate 38 { 39 static Logger logger = Logger.getLogger("gui.editor"); 40 41 protected Object substance; 44 protected FieldItem field; 45 protected ClassItem type; 46 protected MethodItem setter; 47 protected Object value; 48 Object oldValue; 49 protected boolean isEmbedded; 50 boolean isValueSet = false; 51 52 public AbstractFieldEditor(Object substance, FieldItem field) { 53 this.substance = substance; 54 setField(field); 55 width = new Length("10ex"); 56 } 57 58 public Object getValue() { 59 return value; 60 } 61 62 public void setValue(Object value) { 63 logger.debug(this+".setValue "+oldValue+"->"+value); 64 this.value = value; 65 if (!isValueSet) { 66 oldValue = value; 67 isValueSet = true; 68 } 69 } 70 71 public void setSubstance(Object substance) { 72 this.substance = substance; 73 } 74 75 public Object getSubstance() { 76 return substance; 77 } 78 79 public void setEditedType(ClassItem type) { 80 this.type = type; 81 } 82 83 public void setField(FieldItem field) { 84 Utils.unregisterField(substance,this.field,this); 85 this.field = field; 86 if (field!=null) 87 this.setter = field.getSetter(); 88 Utils.registerField(substance,field,this); 89 } 90 91 public FieldItem getField() { 92 return field; 93 } 94 95 public void setAutoUpdate(boolean autoUpdate) { 96 } 97 98 public void setEmbedded(boolean isEmbedded) { 99 this.isEmbedded = isEmbedded; 100 } 101 102 public void close(boolean validate) { 103 if (validate) { 104 Object paramValue = 105 WebDisplay.getRequest().getParameter(getLabel()); 106 logger.debug("reading value for "+getLabel()+": "+paramValue); 107 readValue(paramValue); 108 if (isEmbedded) 109 commit(); 110 } 111 Utils.unregisterField(substance,field,this); 112 context.removeEditor(this); 113 } 114 115 public void onSetFocus(Object param) { 116 } 117 118 public boolean readValue(Object parameter) { 119 if (disabled) 120 return false; 121 else 122 return doReadValue(parameter); 123 } 124 125 protected abstract boolean doReadValue(Object parameter); 126 127 130 public void commit() { 131 if (setter!=null && valueHasChanged()) { 132 logger.debug(this+": "+field.getName()+ 133 "'s value changed: "+getValue()); 134 try { 135 field.setThroughWriter(substance,getValue()); 136 } catch (IllegalAccessException e) { 137 logger.error("Failed to commit value "+getValue()+ 138 " for field "+substance+"."+field,e); 139 } 140 oldValue = this.value; 141 } 142 } 143 144 147 boolean valueHasChanged() { 148 boolean ret; 149 if( oldValue == null && value != null ) { 150 ret = true; 151 } else if( oldValue == null ) { 152 ret = false; 153 } else { 154 ret = ! oldValue.equals(value); 155 } 156 logger.debug("valueHasChanged("+field.getName()+") "+ 157 oldValue+" / "+value+" -> "+ret); 158 return ret; 159 } 160 161 public void fieldUpdated(Object substance, FieldItem field, 162 Object value, Object param) { 163 setValue(value); 164 } 165 166 boolean disabled = false; 168 public boolean isEnabled() { 169 return !disabled; 170 } 171 public void setEnabled(boolean enabled) { 172 logger.debug((enabled?"enable ":"disable ")+this); 173 this.disabled = !enabled; 174 } 175 176 protected String sizeSpec() { 177 String size = ""; 178 if (width!=null && width.unit==Unit.EX) 179 size += " size=\""+width+"\""; 180 size += " style=\"width:"+width+"\""; 181 return size; 182 } 183 } 184 | Popular Tags |