001 package org.apache.myfaces.tobago.component; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_DEFAULT_COMMAND; 021 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_DISABLED; 022 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_RENDERED_PARTIALLY; 023 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_TARGET; 024 import static org.apache.myfaces.tobago.TobagoConstants.ATTR_TRANSITION; 025 import org.apache.commons.logging.Log; 026 import org.apache.commons.logging.LogFactory; 027 028 import javax.faces.component.UIComponent; 029 import javax.faces.context.FacesContext; 030 import javax.faces.el.ValueBinding; 031 import javax.faces.event.FacesEvent; 032 import javax.faces.event.PhaseId; 033 import java.io.IOException; 034 import java.util.Iterator; 035 036 /* 037 * Date: Apr 4, 2005 038 * Time: 5:02:10 PM 039 * $Id: UICommand.java 601107 2007-12-04 22:12:14Z bommel $ 040 */ 041 public class UICommand extends javax.faces.component.UICommand { 042 043 public static final String COMPONENT_TYPE = "org.apache.myfaces.tobago.Command"; 044 045 private static final Log LOG = LogFactory.getLog(UICommand.class); 046 private static final String[] RENDERED_PARTIALLY_DEFAULT = {}; 047 048 private Boolean defaultCommand; 049 private Boolean disabled; 050 private String[] renderedPartially; 051 private String target; 052 private Boolean transition; 053 054 public boolean isDefaultCommand() { 055 if (defaultCommand != null) { 056 return defaultCommand; 057 } 058 ValueBinding vb = getValueBinding(ATTR_DEFAULT_COMMAND); 059 if (vb != null) { 060 return Boolean.TRUE.equals(vb.getValue(getFacesContext())); 061 } else { 062 return false; 063 } 064 } 065 066 public void setDefaultCommand(boolean defaultCommand) { 067 this.defaultCommand = defaultCommand; 068 } 069 070 public String[] getRenderedPartially() { 071 if (renderedPartially != null) { 072 return renderedPartially; 073 } 074 ValueBinding vb = getValueBinding(ATTR_RENDERED_PARTIALLY); 075 if (vb != null) { 076 Object value = vb.getValue(getFacesContext()); 077 if (value != null) { 078 if (value instanceof String[]) { 079 return (String[]) value; 080 } else if (value instanceof String) { 081 return ((String) value).split(","); 082 } else { 083 LOG.error("Ignoring RenderedPartially value binding. Unknown instance " + value.getClass().getName()); 084 } 085 } 086 } 087 return RENDERED_PARTIALLY_DEFAULT; 088 } 089 090 public void setRenderedPartially(String renderedPartially) { 091 if (renderedPartially != null) { 092 String[] components = renderedPartially.split(","); 093 setRenderedPartially(components); 094 } 095 } 096 097 public void setRenderedPartially(String[] renderedPartially) { 098 this.renderedPartially = renderedPartially; 099 } 100 101 public boolean isDisabled() { 102 if (disabled != null) { 103 return disabled; 104 } 105 ValueBinding vb = getValueBinding(ATTR_DISABLED); 106 if (vb != null) { 107 return Boolean.TRUE.equals(vb.getValue(getFacesContext())); 108 } else { 109 return false; 110 } 111 } 112 113 public void setDisabled(boolean disabled) { 114 this.disabled = disabled; 115 } 116 117 public boolean isTransition() { 118 if (transition != null) { 119 return transition; 120 } 121 ValueBinding vb = getValueBinding(ATTR_TRANSITION); 122 if (vb != null) { 123 return Boolean.TRUE.equals(vb.getValue(getFacesContext())); 124 } else { 125 return true; 126 } 127 } 128 129 public void setTransition(boolean transition) { 130 this.transition = transition; 131 } 132 133 public String getTarget() { 134 if (target != null) { 135 return target; 136 } 137 ValueBinding vb = getValueBinding(ATTR_TARGET); 138 if (vb != null) { 139 return (String) vb.getValue(getFacesContext()); 140 } else { 141 return null; 142 } 143 } 144 145 public void setTarget(String target) { 146 this.target = target; 147 } 148 149 150 public Object saveState(FacesContext context) { 151 Object[] saveState = new Object[6]; 152 saveState[0] = super.saveState(context); 153 saveState[1] = defaultCommand; 154 saveState[2] = disabled; 155 saveState[3] = renderedPartially; 156 saveState[4] = target; 157 saveState[5] = transition; 158 return saveState; 159 } 160 161 public void restoreState(FacesContext context, Object savedState) { 162 Object[] values = (Object[]) savedState; 163 super.restoreState(context, values[0]); 164 defaultCommand = (Boolean) values[1]; 165 disabled = (Boolean) values[2]; 166 renderedPartially = (String[]) values[3]; 167 target = (String) values[4]; 168 transition = (Boolean) values[5]; 169 } 170 171 172 public void processDecodes(FacesContext context) { 173 if (context == null) { 174 throw new NullPointerException(); 175 } 176 177 // Skip processing if our rendered flag is false 178 if (!isRendered()) { 179 return; 180 } 181 182 // Process this component itself 183 try { 184 decode(context); 185 } catch (RuntimeException e) { 186 context.renderResponse(); 187 throw e; 188 } 189 190 Iterator kids = getFacetsAndChildren(); 191 while (kids.hasNext()) { 192 UIComponent kid = (UIComponent) kids.next(); 193 kid.processDecodes(context); 194 } 195 196 } 197 198 public void encodeChildren(FacesContext facesContext) throws IOException { 199 if (isRendered()) { 200 UILayout.getLayout(this).encodeChildrenOfComponent(facesContext, this); 201 } 202 } 203 204 public void queueEvent(FacesEvent facesEvent) { 205 // fix for TOBAGO-262 206 super.queueEvent(facesEvent); 207 if (this == facesEvent.getSource()) { 208 if (isImmediate()) { 209 facesEvent.setPhaseId(PhaseId.APPLY_REQUEST_VALUES); 210 } else { 211 facesEvent.setPhaseId(PhaseId.INVOKE_APPLICATION); 212 } 213 } 214 } 215 }