View Javadoc
1   /*
2    * Copyright (C) 2003-2010 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.cs.common.ext;
18  
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  import org.exoplatform.container.PortalContainer;
23  import org.exoplatform.services.log.ExoLogger;
24  import org.exoplatform.services.log.Log;
25  import org.exoplatform.webui.core.UIComponent;
26  import org.exoplatform.webui.core.UIContainer;
27  import org.exoplatform.webui.ext.UIExtension;
28  import org.exoplatform.webui.ext.UIExtensionManager;
29  
30  /**
31   * Created by The eXo Platform SAS
32   * Author : eXoPlatform
33   *          exo@exoplatform.com
34   * Dec 16, 2010  
35   */
36  public final class UIExtensionUtils {
37    private static final Log logger = ExoLogger.getLogger(UIExtensionUtils.class);
38    
39    /**
40     * get list of ui components by extenstion type and parent component.
41     * @param extensionType - type of ui extenstion
42     * @param parent - parent component
43     * @param context
44     * @return map of extension name and ui component.
45     */
46    public static Map<String, UIComponent> getComponents(String extensionType, UIContainer parent, Map<String, Object> context) {
47      Map<String, UIComponent> components = new HashMap<String, UIComponent>();
48      UIExtensionManager manager = (UIExtensionManager) PortalContainer.getInstance().getComponentInstance(UIExtensionManager.class);
49      if (manager != null) {
50        List<UIExtension> extensions = manager.getUIExtensions(extensionType);
51        if (extensions != null) {
52          for (UIExtension extension : extensions) {
53            try {
54              UIComponent component = manager.addUIExtension(extension, context, parent);
55              if (component != null) components.put(extension.getName(), component);
56            } catch (Exception e) {
57              if (logger.isDebugEnabled()) logger.debug("failed to get ui component of uiextension: type=" + extensionType, e);
58            }
59          }
60        }
61      }
62      return components;
63    }
64  }