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    
026    import javax.faces.component.UIComponent;
027    import javax.faces.context.FacesContext;
028    import javax.faces.el.ValueBinding;
029    import javax.faces.event.FacesEvent;
030    import javax.faces.event.PhaseId;
031    import java.io.IOException;
032    import java.util.Iterator;
033    
034    /*
035     * User: weber
036     * Date: Apr 4, 2005
037     * Time: 5:02:10 PM
038     */
039    public class UICommand extends javax.faces.component.UICommand {
040    
041      public static final String COMPONENT_TYPE = "org.apache.myfaces.tobago.Command";
042    
043      private Boolean defaultCommand;
044      private Boolean disabled;
045      private String[] renderedPartially;
046      private String target;
047      private Boolean transition;
048    
049      public boolean isDefaultCommand() {
050        if (defaultCommand != null) {
051          return defaultCommand;
052        }
053        ValueBinding vb = getValueBinding(ATTR_DEFAULT_COMMAND);
054        if (vb != null) {
055          return Boolean.TRUE.equals(vb.getValue(getFacesContext()));
056        } else {
057          return false;
058        }
059      }
060    
061      public void setDefaultCommand(boolean defaultCommand) {
062        this.defaultCommand = defaultCommand;
063      }
064    
065      public String[] getRenderedPartially() {
066        if (renderedPartially != null) {
067          return renderedPartially;
068        }
069        ValueBinding vb = getValueBinding(ATTR_RENDERED_PARTIALLY);
070        if (vb != null) {
071          return (String[]) vb.getValue(getFacesContext());
072        } else {
073          return new String[0];
074        }
075      }
076    
077      public void setRenderedPartially(String renderedPartially) {
078        if (renderedPartially != null) {
079          String[] components = renderedPartially.split(",");
080          setRenderedPartially(components);
081        }
082      }
083    
084      public void setRenderedPartially(String[] renderedPartially) {
085        this.renderedPartially = renderedPartially;
086      }
087    
088      public boolean isDisabled() {
089        if (disabled != null) {
090          return disabled;
091        }
092        ValueBinding vb = getValueBinding(ATTR_DISABLED);
093        if (vb != null) {
094          return Boolean.TRUE.equals(vb.getValue(getFacesContext()));
095        } else {
096          return false;
097        }
098      }
099    
100      public void setDisabled(boolean disabled) {
101        this.disabled = disabled;
102      }
103    
104      public boolean isTransition() {
105        if (transition != null) {
106          return transition;
107        }
108        ValueBinding vb = getValueBinding(ATTR_TRANSITION);
109        if (vb != null) {
110          return Boolean.TRUE.equals(vb.getValue(getFacesContext()));
111        } else {
112          return true;
113        }
114      }
115    
116      public void setTransition(boolean transition) {
117        this.transition = transition;
118      }
119    
120      public String getTarget() {
121        if (target != null) {
122          return target;
123        }
124        ValueBinding vb = getValueBinding(ATTR_TARGET);
125        if (vb != null) {
126          return (String) vb.getValue(getFacesContext());
127        } else {
128          return null;
129        }
130      }
131    
132      public void setTarget(String target) {
133        this.target = target;
134      }
135    
136    
137      public Object saveState(FacesContext context) {
138        Object[] saveState = new Object[6];
139        saveState[0] = super.saveState(context);
140        saveState[1] = defaultCommand;
141        saveState[2] = disabled;
142        saveState[3] = renderedPartially;
143        saveState[4] = target;
144        saveState[5] = transition;
145        return saveState;
146      }
147    
148      public void restoreState(FacesContext context, Object savedState) {
149        Object[] values = (Object[]) savedState;
150        super.restoreState(context, values[0]);
151        defaultCommand = (Boolean) values[1];
152        disabled = (Boolean) values[2];
153        renderedPartially = (String[]) values[3];
154        target = (String) values[4];
155        transition = (Boolean) values[5];
156      }
157    
158    
159      public void processDecodes(FacesContext context) {
160        if (context == null) {
161          throw new NullPointerException();
162        }
163    
164        // Skip processing if our rendered flag is false
165        if (!isRendered()) {
166          return;
167        }
168    
169        // Process this component itself
170        try {
171          decode(context);
172        } catch (RuntimeException e) {
173          context.renderResponse();
174          throw e;
175        }
176    
177        Iterator kids = getFacetsAndChildren();
178        while (kids.hasNext()) {
179          UIComponent kid = (UIComponent) kids.next();
180          kid.processDecodes(context);
181        }
182    
183      }
184    
185      public void encodeChildren(FacesContext facesContext) throws IOException {
186        if (isRendered()) {
187          UILayout.getLayout(this).encodeChildrenOfComponent(facesContext, this);
188        }
189      }
190    
191      public void queueEvent(FacesEvent facesEvent) {
192        // fix for TOBAGO-262
193        super.queueEvent(facesEvent);
194        if (this == facesEvent.getSource()) {
195          if (isImmediate()) {
196            facesEvent.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
197          } else {
198            facesEvent.setPhaseId(PhaseId.INVOKE_APPLICATION);
199          }
200        }
201      }
202    }