001    package org.apache.myfaces.tobago.util;
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.Facets;
021    import org.apache.myfaces.tobago.internal.component.AbstractUIMenu;
022    import org.apache.myfaces.tobago.internal.util.Deprecation;
023    
024    import javax.faces.component.UIComponent;
025    
026    /**
027     * Utility class to provide a type save way to get a specific facet from a components.
028     */
029    public class FacetUtils {
030    
031      private FacetUtils() {
032      }
033    
034      /**
035       * A type save utility to get the facet <code>contextMenu</code> from a component.
036       */
037      public static AbstractUIMenu getContextMenu(UIComponent component) {
038        return (AbstractUIMenu) component.getFacet(Facets.CONTEXT_MENU);
039      }
040    
041      public static void setContextMenu(UIComponent component, AbstractUIMenu menu) {
042        component.getFacets().put(Facets.CONTEXT_MENU, menu);
043      }
044    
045      /**
046       * A type save utility to get the facet <code>dropDownMenu</code> from a component.
047       * It also returns the deprecated facet <code>menupopup</code>
048       */
049      public static AbstractUIMenu getDropDownMenu(UIComponent component) {
050        UIComponent result = component.getFacet(Facets.DROP_DOWN_MENU);
051        if (result == null) {
052          result = component.getFacet(Facets.MENUPOPUP);
053          if (result != null) {
054            if (Deprecation.LOG.isWarnEnabled()) {
055              Deprecation.LOG.warn("Facet 'menupopup' was deprecated, please rename it to 'dropDownMenu'");
056            }
057          }
058        }
059        return (AbstractUIMenu) result;
060      }
061    
062      public static void setDropDownMenu(UIComponent component, AbstractUIMenu menu) {
063        component.getFacets().put(Facets.DROP_DOWN_MENU, menu);
064      }
065    }