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