| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package javax.faces.component; |
| 17 | |
|
| 18 | |
import javax.faces.context.FacesContext; |
| 19 | |
import javax.faces.el.MethodBinding; |
| 20 | |
import javax.faces.el.ValueBinding; |
| 21 | |
import javax.faces.event.AbortProcessingException; |
| 22 | |
import javax.faces.event.ActionEvent; |
| 23 | |
import javax.faces.event.ActionListener; |
| 24 | |
import javax.faces.event.FacesEvent; |
| 25 | |
import javax.faces.event.PhaseId; |
| 26 | |
import javax.faces.internal.UICommandUtil; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
public class UICommand extends UIComponentBase implements ActionSource { |
| 32 | |
|
| 33 | |
public static final String COMPONENT_FAMILY = "javax.faces.Command"; |
| 34 | |
|
| 35 | |
public static final String COMPONENT_TYPE = "javax.faces.Command"; |
| 36 | |
|
| 37 | 468 | private MethodBinding action = null; |
| 38 | |
|
| 39 | 468 | private MethodBinding actionListener = null; |
| 40 | |
|
| 41 | 468 | private boolean immediate = false; |
| 42 | |
|
| 43 | 468 | private boolean immediateSet = false; |
| 44 | |
|
| 45 | 468 | private Object value = null; |
| 46 | |
|
| 47 | |
private static final String IMMEDIATE_BINDING_NAME = "immediate"; |
| 48 | |
|
| 49 | |
private static final String VALUE_BINDING_NAME = "value"; |
| 50 | |
|
| 51 | |
private static final String DEFAULT_RENDER_TYPE = "javax.faces.Button"; |
| 52 | |
|
| 53 | |
public UICommand() { |
| 54 | 468 | super(); |
| 55 | 468 | setRendererType(DEFAULT_RENDER_TYPE); |
| 56 | 468 | } |
| 57 | |
|
| 58 | |
public String getFamily() { |
| 59 | 36 | return COMPONENT_FAMILY; |
| 60 | |
} |
| 61 | |
|
| 62 | |
public MethodBinding getAction() { |
| 63 | 16 | return action; |
| 64 | |
} |
| 65 | |
|
| 66 | |
public void setAction(MethodBinding action) { |
| 67 | 12 | this.action = action; |
| 68 | 12 | } |
| 69 | |
|
| 70 | |
public MethodBinding getActionListener() { |
| 71 | 45 | return actionListener; |
| 72 | |
} |
| 73 | |
|
| 74 | |
public void setActionListener(MethodBinding actionListener) { |
| 75 | 14 | this.actionListener = actionListener; |
| 76 | 14 | } |
| 77 | |
|
| 78 | |
public boolean isImmediate() { |
| 79 | 23 | if (immediateSet) { |
| 80 | 17 | return immediate; |
| 81 | |
} |
| 82 | 6 | ValueBinding vb = getValueBinding(IMMEDIATE_BINDING_NAME); |
| 83 | 6 | return (vb != null) ? isBindingValueTrue(vb) : immediate; |
| 84 | |
} |
| 85 | |
|
| 86 | |
public void setImmediate(boolean immediate) { |
| 87 | 19 | this.immediate = immediate; |
| 88 | 19 | immediateSet = true; |
| 89 | 19 | } |
| 90 | |
|
| 91 | |
public Object getValue() { |
| 92 | 29 | if (value != null) { |
| 93 | 13 | return value; |
| 94 | |
} |
| 95 | 16 | ValueBinding vb = getValueBinding(VALUE_BINDING_NAME); |
| 96 | 16 | return (vb != null) ? getValueFromBinding(vb) : null; |
| 97 | |
} |
| 98 | |
|
| 99 | |
public void setValue(Object value) { |
| 100 | 16 | this.value = value; |
| 101 | 16 | } |
| 102 | |
|
| 103 | |
public void addActionListener(ActionListener listener) { |
| 104 | 12 | addFacesListener(listener); |
| 105 | 12 | } |
| 106 | |
|
| 107 | |
public ActionListener[] getActionListeners() { |
| 108 | 31 | ActionListener[] listeners = (ActionListener[]) getFacesListeners(ActionListener.class); |
| 109 | 30 | return listeners; |
| 110 | |
} |
| 111 | |
|
| 112 | |
public void removeActionListener(ActionListener listener) { |
| 113 | 6 | removeFacesListener(listener); |
| 114 | 6 | } |
| 115 | |
|
| 116 | |
public void restoreState(FacesContext context, Object state) { |
| 117 | 9 | Object[] values = (Object[]) state; |
| 118 | 9 | super.restoreState(context, values[0]); |
| 119 | 9 | action = (MethodBinding) restoreAttachedState(context, values[1]); |
| 120 | 9 | actionListener = (MethodBinding) restoreAttachedState(context, |
| 121 | |
values[2]); |
| 122 | 9 | immediate = ((Boolean) values[3]).booleanValue(); |
| 123 | 9 | immediateSet = ((Boolean) values[4]).booleanValue(); |
| 124 | 9 | value = values[5]; |
| 125 | 9 | } |
| 126 | |
|
| 127 | |
public Object saveState(FacesContext context) { |
| 128 | 12 | Object[] values = new Object[6]; |
| 129 | 12 | values[0] = super.saveState(context); |
| 130 | 12 | values[1] = saveAttachedState(context, action); |
| 131 | 12 | values[2] = saveAttachedState(context, actionListener); |
| 132 | 12 | values[3] = immediate ? Boolean.TRUE : Boolean.FALSE; |
| 133 | 12 | values[4] = immediateSet ? Boolean.TRUE : Boolean.FALSE; |
| 134 | 12 | values[5] = value; |
| 135 | 12 | return values; |
| 136 | |
} |
| 137 | |
|
| 138 | |
public void broadcast(FacesEvent event) throws AbortProcessingException { |
| 139 | 15 | super.broadcast(event); |
| 140 | 12 | if (event instanceof ActionEvent) { |
| 141 | 6 | FacesContext context = getFacesContext(); |
| 142 | 6 | MethodBinding mb = getActionListener(); |
| 143 | 6 | if (mb != null) { |
| 144 | 3 | mb.invoke(context, new Object[] { event }); |
| 145 | |
} |
| 146 | 6 | ActionListener listener = context.getApplication() |
| 147 | |
.getActionListener(); |
| 148 | 6 | if (listener != null) { |
| 149 | 6 | listener.processAction((ActionEvent) event); |
| 150 | |
} |
| 151 | |
} |
| 152 | 12 | } |
| 153 | |
|
| 154 | |
public void queueEvent(FacesEvent event) { |
| 155 | 21 | if (event instanceof ActionEvent) { |
| 156 | 9 | if (isImmediate()) { |
| 157 | 3 | event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES); |
| 158 | |
} else { |
| 159 | 6 | event.setPhaseId(PhaseId.INVOKE_APPLICATION); |
| 160 | |
} |
| 161 | 9 | final FacesContext facesContext = getFacesContext(); |
| 162 | 9 | UICommandUtil.setSubmittedCommand(facesContext, this); |
| 163 | |
} |
| 164 | 21 | super.queueEvent(event); |
| 165 | 15 | } |
| 166 | |
|
| 167 | |
private boolean isBindingValueTrue(ValueBinding vb) { |
| 168 | 3 | Object value = getValueFromBinding(vb); |
| 169 | 3 | return Boolean.TRUE.equals(value); |
| 170 | |
} |
| 171 | |
|
| 172 | |
private Object getValueFromBinding(ValueBinding vb) { |
| 173 | 3 | return vb.getValue(getFacesContext()); |
| 174 | |
} |
| 175 | |
} |