1 17 18 package org.objectweb.jac.aspects.gui.swing; 19 20 import java.awt.event.FocusEvent ; 21 import java.awt.event.FocusListener ; 22 import java.lang.IllegalAccessException ; 23 import org.apache.log4j.Logger; 24 import org.objectweb.jac.aspects.gui.CommitException; 25 import org.objectweb.jac.aspects.gui.FieldEditor; 26 import org.objectweb.jac.aspects.gui.FieldUpdate; 27 import org.objectweb.jac.aspects.gui.GuiAC; 28 import org.objectweb.jac.aspects.gui.Utils; 29 import org.objectweb.jac.core.Collaboration; 30 import org.objectweb.jac.core.rtti.ClassItem; 31 import org.objectweb.jac.core.rtti.FieldItem; 32 import org.objectweb.jac.core.rtti.MethodItem; 33 34 public abstract class AbstractFieldEditor extends AbstractView 35 implements FocusListener , FieldEditor, FieldUpdate 36 { 37 static Logger logger = Logger.getLogger("gui.editor"); 38 static Logger loggerFocus = Logger.getLogger("gui.focus"); 39 40 Object substance; 43 FieldItem field; 44 MethodItem setter; 45 Object value; 46 boolean isEmbedded; 47 protected ClassItem type; 48 49 public AbstractFieldEditor(Object substance, FieldItem field) { 50 this.substance = substance; 51 setField(field); 52 addFocusListener(this); 53 } 54 55 public void fieldUpdated(Object object, FieldItem field, 56 Object value, Object param) { 57 setValue(value); 58 } 59 60 public abstract Object getValue(); 61 62 public void setValue(Object value) { 63 this.value = value; 64 } 65 66 public void setField(FieldItem field) { 67 Utils.unregisterField(substance,this.field,this); 68 this.field = field; 69 Utils.registerField(substance,this.field,this); 70 if (field!=null) 71 this.setter = field.getSetter(); 72 } 73 74 public FieldItem getField() { 75 return field; 76 } 77 78 public void setSubstance(Object substance) { 79 logger.debug("setSubstance("+substance+")"); 80 this.substance = substance; 81 } 82 83 86 public Object getSubstance() { 87 return substance; 88 } 89 90 public void setEditedType(ClassItem type) { 91 this.type = type; 92 } 93 94 public void setEmbedded(boolean isEmbedded) { 95 this.isEmbedded = isEmbedded; 96 if (isEmbedded) 97 Utils.registerField(substance,field,this); 98 else 99 Utils.unregisterField(substance,field,this); 100 } 101 102 public void setAutoUpdate(boolean autoUpdate) { 103 } 105 106 public void close(boolean validate) { 107 Utils.unregisterField(substance,field,this); 108 if (validate) 109 commit(); 110 substance = null; 111 super.close(validate); 112 } 113 114 public void onSetFocus(Object extraOption) {} 115 116 119 public void commit() { 120 if (setter!=null && valueHasChanged()) { 121 logger.debug("value changed for "+substance+"."+field.getName()); 122 try { 123 field.setThroughWriter(substance,getValue()); 124 } catch (IllegalAccessException e) { 125 logger.error("Failed to commit value "+getValue()+ 126 " for field "+substance+"."+field,e); 127 } catch (Exception e) { 128 throw new CommitException(e,substance,field); 129 } 130 value = getValue(); 131 } 132 } 133 134 137 boolean valueHasChanged() { 138 Object newValue = getValue(); 139 boolean ret; 140 if (value == null && newValue != null) { 141 ret = true; 142 } else if (value == null) { 143 ret = false; 144 } else { 145 ret = ! value.equals(newValue); 146 } 147 logger.debug("valueHasChanged("+field.getName()+") "+ 148 value+" / "+newValue+" -> "+ret); 149 return ret; 150 } 151 152 154 public void focusGained(FocusEvent e) { 155 156 loggerFocus.debug("focus gained on "+getClass().getName()); 157 } 158 159 public void focusLost(FocusEvent e) { 160 161 loggerFocus.debug("focus lost on "+getClass().getName()); 162 if (field!=null && isEmbedded && !closed) { 165 invokeInContext(this,"commit", new Object []{}); 166 } else { 167 loggerFocus.debug("ignoring focusLost event"); 168 } 169 } 170 171 } 172 | Popular Tags |