001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.myfaces.tobago.renderkit;
021    
022    import org.apache.myfaces.tobago.event.PageActionEvent;
023    import org.apache.myfaces.tobago.event.PageAction;
024    import org.apache.myfaces.tobago.event.PageActionUtil;
025    import org.apache.myfaces.tobago.component.UIData;
026    import org.apache.myfaces.tobago.component.ComponentUtil;
027    import static org.apache.myfaces.tobago.TobagoConstants.SUBCOMPONENT_SEP;
028    import org.apache.commons.logging.Log;
029    import org.apache.commons.logging.LogFactory;
030    
031    import javax.faces.context.FacesContext;
032    import javax.faces.component.UIComponent;
033    import java.util.Map;
034    
035    /*
036     * Date: 24.04.2006
037     * Time: 22:02:49
038     */
039    
040    //TODO move this back in the decode or SheetCommandRenderer
041    public class SheetUtils {
042      private static final Log LOG = LogFactory.getLog(SheetUtils.class);
043    
044      public static void decode(FacesContext facesContext, UIComponent component) {
045        String actionId = ComponentUtil.findPage(facesContext, component).getActionId();
046        String clientId = component.getClientId(facesContext);
047        if (LOG.isDebugEnabled()) {
048          LOG.debug("actionId = '" + actionId + "'");
049          LOG.debug("clientId = '" + clientId + "'");
050        }
051        if (actionId != null && actionId.equals(clientId)) {
052    
053          PageAction action;
054          try {
055            action = PageActionUtil.parse(component.getId());
056            if (action == null) {
057              LOG.error("Illegal value for PageAction :" + component.getId());
058              return;
059            }
060          } catch (Exception e) {
061            LOG.error("Illegal value for PageAction :" + component.getId());
062            return;
063          }
064          PageActionEvent event = new PageActionEvent((UIData) component.getParent(), action);
065    
066          switch (action) {
067            case TO_PAGE:
068            case TO_ROW:
069              Map map = facesContext.getExternalContext().getRequestParameterMap();
070              Object value = map.get(clientId + SUBCOMPONENT_SEP + "value");
071              try {
072                event.setValue(Integer.parseInt((String) value));
073              } catch (Exception e) {
074                LOG.error("Can't parse value for action " + action.name() + ": " + value);
075              }
076              break;
077            default:
078              // nothing more to do
079          }
080          component.queueEvent(event);
081        }
082      }
083    }