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