| /** |
| * Copyright (c) 2011, 2014 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Florian Pirchner - Initial implementation |
| */ |
| package org.eclipse.osbp.vaaclipse.addons.ecview; |
| |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Locale; |
| import java.util.Map; |
| |
| import org.eclipse.e4.core.contexts.IEclipseContext; |
| import org.eclipse.osbp.ecview.xtext.builder.participant.IECViewAddonsMetadataService; |
| import org.eclipse.osbp.runtime.common.i18n.II18nService; |
| import org.eclipse.osbp.vaaclipse.addons.common.api.explorer.IExplorerCategory; |
| import org.eclipse.osbp.vaaclipse.addons.common.api.explorer.IExplorerInfo; |
| import org.eclipse.osbp.vaaclipse.addons.common.api.explorer.IExplorerInfoManager; |
| import org.osgi.service.component.annotations.Component; |
| import org.osgi.service.component.annotations.Reference; |
| import org.osgi.service.component.annotations.ReferenceCardinality; |
| import org.osgi.service.component.annotations.ReferencePolicy; |
| |
| /** |
| * The Class ExplorerInfoManager. |
| */ |
| @Component(property = { "service.ranking=100" }, immediate = true) |
| public class ExplorerInfoManager implements IExplorerInfoManager { |
| |
| /** The Constant I18NKEY_CATEGORY_DESCRIPTION. */ |
| private static final String I18NKEY_CATEGORY_DESCRIPTION = "desc.%s"; |
| |
| /** The Constant I18NKEY_CATEGORY. */ |
| private static final String I18NKEY_CATEGORY = "category.%s"; |
| |
| /** The ecview service. */ |
| private IECViewAddonsMetadataService ecviewService; |
| |
| /** The i18n service. */ |
| private II18nService i18nService; |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.vaaclipse.addons.common.api.explorer.IExplorerInfoManager#getExplorerInfo(org.eclipse.osbp.vaaclipse.addons.common.api.explorer.IExplorerCategory, org.eclipse.e4.core.contexts.IEclipseContext) |
| */ |
| @Override |
| public Iterable<IExplorerInfo> getExplorerInfo(IExplorerCategory parent, |
| IEclipseContext context) { |
| if (parent == null) { |
| return getRootInfos(context); |
| } else { |
| return getChildren(context, parent); |
| } |
| } |
| |
| /** |
| * Bind ec view metadata service. |
| * |
| * @param service |
| * the service |
| */ |
| @Reference(name = "ecviewMetadataService", unbind = "unbindECViewMetadataService", cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.DYNAMIC) |
| protected void bindECViewMetadataService( |
| IECViewAddonsMetadataService service) { |
| // handle mandatory dynamic |
| if (service != this.ecviewService) { |
| this.ecviewService = service; |
| } |
| } |
| |
| /** |
| * Unbind ec view metadata service. |
| * |
| * @param service |
| * the service |
| */ |
| protected void unbindECViewMetadataService( |
| IECViewAddonsMetadataService service) { |
| // handle mandatory dynamic |
| if (service == this.ecviewService) { |
| this.ecviewService = null; |
| } |
| } |
| |
| /** |
| * Bind i18n service. |
| * |
| * @param service |
| * the service |
| */ |
| @Reference(name = "i18nService", unbind = "unbindI18nService", cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.DYNAMIC) |
| protected void bindI18nService(II18nService service) { |
| // handle mandatory dynamic |
| if (service != this.i18nService) { |
| this.i18nService = service; |
| } |
| } |
| |
| /** |
| * Unbind i18n service. |
| * |
| * @param service |
| * the service |
| */ |
| protected void unbindI18nService(II18nService service) { |
| // handle mandatory dynamic |
| if (service == this.i18nService) { |
| this.i18nService = null; |
| } |
| } |
| |
| /** |
| * Creates a category for each package name of found views. |
| * |
| * @param context |
| * the context |
| * @return the root infos |
| */ |
| protected Iterable<IExplorerInfo> getRootInfos(IEclipseContext context) { |
| Map<String, IExplorerInfo> infos = new HashMap<String, IExplorerInfo>(); |
| |
| List<String> viewNames = getSortedRootViewNames(null, false); |
| // access all views that are contained in root packages |
| for (String viewFQN : viewNames) { |
| String pkg = viewFQN.substring(0, viewFQN.lastIndexOf(".")); |
| String categoryId = toCategoryI18nKey(pkg); |
| ExplorerCategory category = null; |
| if (!infos.containsKey(categoryId)) { |
| category = new ExplorerCategory(context); |
| category.setId(categoryId); |
| category.setTarget(pkg); |
| category.setI18nLabelKey(categoryId); |
| category.setIconI18nKey(toImageI18nKey(categoryId)); |
| category.setI18nDescriptionKey(toDescriptionI18nKey(categoryId)); |
| category.setLabel(translate(category.getI18nLabelKey())); |
| category.setDescription(translate(category |
| .getI18nDescriptionKey())); |
| category.setIconURI(translate(category.getIconI18nKey())); |
| infos.put(categoryId, category); |
| } |
| } |
| |
| return infos.values(); |
| } |
| |
| /** |
| * Fetches all children for the given category. |
| * |
| * @param context |
| * the context |
| * @param parent |
| * the parent |
| * @return the children |
| */ |
| protected Iterable<IExplorerInfo> getChildren(IEclipseContext context, |
| IExplorerCategory parent) { |
| Map<String, IExplorerInfo> infos = new HashMap<String, IExplorerInfo>(); |
| |
| List<String> viewNames = getSortedRootViewNames(parent.getTarget(), |
| false); |
| // access all views that are contained in the parent category |
| for (String viewFQN : viewNames) { |
| String pkg = viewFQN.substring(0, viewFQN.lastIndexOf(".")); |
| String categoryId = toCategoryI18nKey(pkg); |
| |
| if (pkg.equals(parent.getTarget())) { |
| // the package is the same as the parent#target. So lets add an |
| // application leaf |
| ExplorerApplicationLeaf leaf = new ExplorerApplicationLeaf(); |
| leaf.setId(viewFQN); |
| leaf.setTarget(viewFQN); |
| leaf.setI18nLabelKey(viewFQN); |
| leaf.setIconI18nKey(toImageI18nKey(viewFQN)); |
| leaf.setI18nDescriptionKey(toDescriptionI18nKey(viewFQN)); |
| leaf.setLabel(translate(leaf.getI18nLabelKey())); |
| leaf.setDescription(translate(leaf.getI18nDescriptionKey())); |
| leaf.setIconURI(translate(leaf.getIconI18nKey())); |
| |
| infos.put(viewFQN, leaf); |
| } else { |
| // must be a new category |
| ExplorerCategory category = null; |
| |
| // if not already created, then do so |
| if (!infos.containsKey(categoryId)) { |
| category = new ExplorerCategory(context); |
| category.setId(categoryId); |
| category.setTarget(pkg); |
| category.setI18nLabelKey(categoryId); |
| category.setIconI18nKey(toImageI18nKey(categoryId)); |
| category.setI18nDescriptionKey(toDescriptionI18nKey(categoryId)); |
| category.setLabel(translate(category.getI18nLabelKey())); |
| category.setDescription(translate(category |
| .getI18nDescriptionKey())); |
| category.setIconURI(translate(category.getIconI18nKey())); |
| |
| infos.put(categoryId, category); |
| } |
| } |
| } |
| |
| List<IExplorerInfo> result = new ArrayList<IExplorerInfo>( |
| infos.values()); |
| Collections.sort(result); |
| |
| return result; |
| } |
| |
| /** |
| * Gets the sorted root view names. |
| * |
| * @param pkgName |
| * the pkg name |
| * @param includeChildren |
| * the include children |
| * @return the sorted root view names |
| */ |
| private List<String> getSortedRootViewNames(String pkgName, |
| boolean includeChildren) { |
| List<String> viewNames = ecviewService.getIDEViewNames(pkgName, |
| includeChildren); |
| // Collections.sort(viewNames, new Comparator<String>() { |
| // @Override |
| // public int compare(String o0, String o1) { |
| // String pkg0 = o0.substring(0, o0.lastIndexOf(".")); |
| // String pkg1 = o1.substring(0, o1.lastIndexOf(".")); |
| // |
| // return pkg0.length() - pkg1.length(); |
| // } |
| // }); |
| return viewNames; |
| } |
| |
| /** |
| * To category i18n key. |
| * |
| * @param pkg |
| * the pkg |
| * @return the string |
| */ |
| private String toCategoryI18nKey(String pkg) { |
| return String.format(I18NKEY_CATEGORY, pkg); |
| } |
| |
| /** |
| * To image i18n key. |
| * |
| * @param category |
| * the category |
| * @return the string |
| */ |
| private String toImageI18nKey(String category) { |
| return category + ".image"; |
| } |
| |
| /** |
| * To description i18n key. |
| * |
| * @param categoryId |
| * the category id |
| * @return the string |
| */ |
| private String toDescriptionI18nKey(String categoryId) { |
| return String.format(I18NKEY_CATEGORY_DESCRIPTION, categoryId); |
| } |
| |
| /** |
| * Translate. |
| * |
| * @param key |
| * the key |
| * @return the string |
| */ |
| private String translate(String key) { |
| return i18nService.getValue(key, Locale.getDefault()); |
| } |
| } |