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.event;
021    
022    import org.apache.myfaces.tobago.TobagoConstants;
023    import org.apache.myfaces.tobago.component.UIPopup;
024    import org.apache.myfaces.tobago.component.ComponentUtil;
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.event.ActionListener;
030    import javax.faces.event.ActionEvent;
031    import javax.faces.event.AbortProcessingException;
032    import javax.faces.context.FacesContext;
033    import javax.faces.el.ValueBinding;
034    import javax.faces.webapp.UIComponentTag;
035    import java.io.Serializable;
036    
037    /*
038     * Date: Dec 23, 2006
039     * Time: 10:59:53 AM
040     */
041    // TODO implement StateHolder
042    public class PopupActionListener implements ActionListener, Serializable {
043    
044      private static final Log LOG = LogFactory.getLog(PopupActionListener.class);
045    
046      private String popupId;
047    
048      private ValueBinding popupIdBinding;
049    
050      private boolean facet;
051    
052    
053      public PopupActionListener() {
054      }
055    
056      public PopupActionListener(String popupId) {
057    
058        if (UIComponentTag.isValueReference(popupId)) {
059          popupIdBinding = ComponentUtil.createValueBinding(popupId);
060        } else {
061          this.popupId = popupId;
062        }
063        if (LOG.isDebugEnabled()) {
064          LOG.debug("Add ActionListener: " + popupId);
065        }
066      }
067    
068      public PopupActionListener(UIPopup popup) {
069        facet = true;
070      }
071    
072      public void processAction(ActionEvent actionEvent) throws AbortProcessingException {
073        FacesContext facesContext = FacesContext.getCurrentInstance();
074        UIPopup popup = null;
075        if (facet) {
076          UIComponent component = actionEvent.getComponent().getFacet(TobagoConstants.FACET_POPUP);
077          if (component instanceof UIPopup) {
078            popup = (UIPopup) component;
079          }
080          if (popup == null) {
081            LOG.error("Found no popup facet in component "
082                  + actionEvent.getComponent().getClientId(facesContext));
083          }
084        } else {
085          String id;
086          if (popupIdBinding != null) {
087            id = (String) popupIdBinding.getValue(facesContext);
088          } else {
089            id = popupId;
090          }
091          popup = (UIPopup) ComponentUtil.findComponent(actionEvent.getComponent(), id);
092          if (popup == null) {
093            LOG.error("Found no popup for \""
094              + (popupIdBinding != null ? popupIdBinding.getExpressionString() + "\" := \"" : "")
095              + id + "\"! Search base componentId : "
096                  + actionEvent.getComponent().getClientId(facesContext));
097          }
098        }
099        if (popup != null) {
100          if (LOG.isDebugEnabled()) {
101            LOG.debug("activated "
102                + actionEvent.getComponent().getClientId(facesContext));
103          }
104          popup.setActivated(true);
105        } 
106      }
107    
108    }