1 16 package org.apache.myfaces.custom.updateactionlistener; 17 18 import javax.faces.FacesException; 19 import javax.faces.component.StateHolder; 20 import javax.faces.component.UIComponentBase; 21 import javax.faces.component.ValueHolder; 22 import javax.faces.context.FacesContext; 23 import javax.faces.convert.Converter; 24 import javax.faces.el.ValueBinding; 25 import javax.faces.event.AbortProcessingException; 26 import javax.faces.event.ActionEvent; 27 import javax.faces.event.ActionListener; 28 29 46 public class UpdateActionListener 47 implements ActionListener, ValueHolder, StateHolder 48 { 49 51 private ValueBinding _propertyBinding; 52 private Object _value; 53 private ValueBinding _valueBinding; 54 private Converter _converter; 55 56 public void setPropertyBinding(ValueBinding propertyBinding) 57 { 58 _propertyBinding = propertyBinding; 59 } 60 61 public ValueBinding getPropertyBinding() 62 { 63 return _propertyBinding; 64 } 65 66 public void setValue(Object value) 67 { 68 _value = value; 69 } 70 71 public Object getValue() 72 { 73 if (_value != null) return _value; 74 ValueBinding vb = getValueBinding(); 75 if (vb != null) 76 { 77 FacesContext context = FacesContext.getCurrentInstance(); 78 return vb.getValue(context); 79 } 80 return null; 81 } 82 83 public Object getLocalValue() 84 { 85 return _value; 86 } 87 88 public ValueBinding getValueBinding() 89 { 90 return _valueBinding; 91 } 92 93 public void setValueBinding(ValueBinding valueBinding) 94 { 95 _valueBinding = valueBinding; 96 } 97 98 public Converter getConverter() 99 { 100 return _converter; 101 } 102 103 public void setConverter(Converter converter) 104 { 105 _converter = converter; 106 } 107 108 public void processAction(ActionEvent actionEvent) throws AbortProcessingException 109 { 110 FacesContext context = FacesContext.getCurrentInstance(); 111 ValueBinding updateBinding = getPropertyBinding(); 112 Object v = getValue(); 113 if (v != null && 114 v instanceof String ) 115 { 116 Class type = updateBinding.getType(context); 117 if (!type.equals(String .class) && ! type.equals(Object .class)) 118 { 119 Converter converter = getConverter(); 120 if (converter == null) 121 { 122 try 123 { 124 converter = context.getApplication().createConverter(type); 125 } 126 catch (Exception e) 127 { 128 throw new FacesException("No Converter registered with UpdateActionListener and no appropriate standard converter found. Needed to convert String to " + type.getName(), e); 129 } 130 } 131 v = converter.getAsObject(context, context.getViewRoot(), (String )v); 132 } 133 } 134 updateBinding.setValue(context, v); 135 } 136 137 138 139 141 public Object saveState(FacesContext context) 142 { 143 Object values[] = new Object [4]; 144 values[0] = UIComponentBase.saveAttachedState(context, _propertyBinding); 145 values[1] = _value; 146 values[2] = UIComponentBase.saveAttachedState(context, _valueBinding); 147 values[3] = UIComponentBase.saveAttachedState(context, _converter); 148 return ((Object ) (values)); 149 } 150 151 public void restoreState(FacesContext context, Object state) 152 { 153 Object values[] = (Object [])state; 154 _propertyBinding = (ValueBinding)UIComponentBase.restoreAttachedState(context, values[0]); 155 _value = values[1]; 156 _valueBinding = (ValueBinding)UIComponentBase.restoreAttachedState(context, values[2]);; 157 _converter = (Converter)UIComponentBase.restoreAttachedState(context, values[3]);; 158 } 159 160 public boolean isTransient() 161 { 162 return false; 163 } 164 165 public void setTransient(boolean newTransientValue) 166 { 167 if (newTransientValue == true) throw new IllegalArgumentException (); 168 } 169 170 } 171 | Popular Tags |