| /******************************************************************************* |
| * Copyright (c) 2010 Zoltan Ujhelyi and Istvan Rath and Daniel Varro. |
| * 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: |
| * Istvan Rath - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.viatra2.visualisation.common.extensions.internal; |
| |
| import java.io.Serializable; |
| import java.util.Collection; |
| import java.util.Comparator; |
| import java.util.HashMap; |
| import java.util.Map; |
| import java.util.SortedSet; |
| import java.util.TreeSet; |
| |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IConfigurationElement; |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.core.runtime.Platform; |
| import org.eclipse.core.runtime.Status; |
| import org.eclipse.jface.viewers.ViewerFilter; |
| import org.eclipse.viatra2.visualisation.ViatraColoredLabelProvider; |
| import org.eclipse.viatra2.visualisation.VisualisationPlugin; |
| import org.eclipse.viatra2.visualisation.common.extensions.Category; |
| import org.eclipse.viatra2.visualisation.common.extensions.FilterConfiguration; |
| import org.eclipse.viatra2.visualisation.common.extensions.NamedElement; |
| import org.eclipse.viatra2.visualisation.common.extensions.VisualisationPreset; |
| import org.eclipse.viatra2.visualisation.layouts.IViatraLayoutAlgorithm; |
| |
| /** |
| * Accessor class to extensions to the visualizer. |
| * @author Istvan Rath |
| * |
| */ |
| public class ExtensionAccess { |
| |
| private static final String ExtensionID = "org.eclipse.viatra2.visualisation.customization"; |
| |
| private static class CategoryComparator implements Comparator<Category>, Serializable { |
| |
| private static final long serialVersionUID = 6726325608227073546L; |
| |
| public int compare(Category o1, Category o2) { |
| String order1 = o1.orderID; |
| String order2 = o2.orderID; |
| int order = order1.compareTo(order2); |
| if (order == 0) { |
| order = o1.name.compareTo(o2.name); |
| } |
| if (order == 0) { |
| order = o1.id.compareTo(o2.id); |
| } |
| return order; |
| } |
| |
| } |
| |
| /** |
| * This is a singleton class for acessing the visualisation extensions |
| */ |
| public final static ExtensionAccess instance = new ExtensionAccess(); |
| |
| private ExtensionAccess() {} |
| |
| /** |
| * Reinitializes the access to the extensions by reading all extensions again. |
| */ |
| public static void reInit() { |
| instance.readExtensionPoint(); |
| } |
| |
| /** |
| * Returns all visualisation categories defined in extensions |
| * @return the collection of created categories |
| */ |
| public static SortedSet<Category> getAllCategories() { |
| SortedSet<Category> sortedCategores=new TreeSet<Category>(new CategoryComparator()); |
| sortedCategores.addAll(instance.categories.values()); |
| return sortedCategores; |
| } |
| |
| /** |
| * Returns all layout algorithms defined in extensions |
| * @return the collection of defined layout algorithms |
| */ |
| public static Collection<NamedElement<IViatraLayoutAlgorithm>> getAllLayoutAlgorithms() { |
| |
| return instance.defaultCategory.layouts.values(); |
| } |
| |
| /** |
| * Returns all defined label providers |
| * @return the collection of all defined label providers |
| */ |
| public static Collection<NamedElement<ViatraColoredLabelProvider>> getAllLabelProviders() { |
| return instance.defaultCategory.labelproviders.values(); |
| } |
| |
| /** |
| * Returns all defined filters |
| * @return the collection of defined filters |
| */ |
| public static Collection<NamedElement<ViewerFilter>> getAllFilters() { |
| return instance.defaultCategory.filters.values(); |
| } |
| |
| /** |
| * Returns all defined visualisation presets |
| * @return the collection of defined presets |
| */ |
| public static Collection<NamedElement<VisualisationPreset>> getAllPresets() { |
| return instance.defaultCategory.presets.values(); |
| } |
| |
| /** |
| * Calculates the category object for a selected category ID |
| * @param id |
| * @return the corresponding category object for a string ID |
| */ |
| public static Category getCategoryById(String id) { |
| return instance.categories.get(id); |
| } |
| |
| /** |
| * Returns all filter configurations in extensions |
| * @return the collection of all defined filter configurations |
| */ |
| public static Collection<NamedElement<FilterConfiguration>> getAllFilterConfigurations() { |
| return instance.defaultCategory.filterConfigurations.values(); |
| } |
| |
| // etc. a lot of other lookup functions can be added if necessary |
| |
| |
| /** |
| * The "default" category, to which all extensions belong. |
| */ |
| private Category defaultCategory; |
| |
| /** |
| * A map of categories defined using our extension point. |
| * key: category ID |
| */ |
| Map<String, Category> categories; |
| |
| |
| private void _readCategories() { |
| categories = new HashMap<String, Category>(); |
| defaultCategory = new Category("default","default","default"); |
| IConfigurationElement[] config = |
| Platform.getExtensionRegistry().getConfigurationElementsFor(ExtensionID); |
| for (IConfigurationElement element : config) { |
| try { |
| if ("category".equals(element.getName())) { |
| String name = element.getAttribute("name"); |
| String id = element.getAttribute("id"); |
| String orderid = element.getAttribute("orderID"); |
| Category c = new Category(id,name,orderid); |
| categories.put(id, c); |
| } |
| } catch (Exception e) { |
| Platform.getLog(VisualisationPlugin.getDefault().getBundle()).log(new Status(IStatus.ERROR, |
| VisualisationPlugin.PLUGIN_ID, e.getMessage())); |
| } |
| } |
| |
| } |
| |
| private<T> void registerElement( |
| IConfigurationElement element, |
| String attrName, |
| Map<String,NamedElement<T>> genericTargetMap, |
| Map<String,NamedElement<T>> specificTargetMap) throws CoreException { |
| String name = element.getAttribute("name"); |
| String id = element.getAttribute("id"); |
| @SuppressWarnings("unchecked") |
| T obj = (T) element.createExecutableExtension(attrName); |
| NamedElement<T> ne = new NamedElement<T>(name, obj); |
| genericTargetMap.put(id, ne); |
| if (specificTargetMap!=null) { |
| specificTargetMap.put(id, ne); |
| } |
| } |
| |
| private void _readExecutables() { |
| IConfigurationElement[] config = |
| Platform.getExtensionRegistry().getConfigurationElementsFor(ExtensionID); |
| for (IConfigurationElement element : config) { |
| try { |
| if ("algorithm".equals(element.getName())) { |
| Map<String,NamedElement<IViatraLayoutAlgorithm>> map = null; |
| if (element.getAttribute("category")!=null) { |
| map = categories.get(element.getAttribute("category")).layouts; |
| } |
| registerElement(element,"layoutalgorithm",defaultCategory.layouts,map); |
| } |
| else if ("filter".equals(element.getName())) { |
| Map<String,NamedElement<ViewerFilter>> map = null; |
| if (element.getAttribute("category")!=null) { |
| map = categories.get(element.getAttribute("category")).filters; |
| } |
| registerElement(element,"filter",defaultCategory.filters,map); |
| } |
| else if ("labelprovider".equals(element.getName())) { |
| Map<String,NamedElement<ViatraColoredLabelProvider>> map = null; |
| if (element.getAttribute("category")!=null) { |
| map = categories.get(element.getAttribute("category")).labelproviders; |
| } |
| registerElement(element,"labelprovider",defaultCategory.labelproviders,map); |
| } |
| } catch (Exception e) { |
| Platform.getLog(VisualisationPlugin.getDefault().getBundle()).log(new Status(IStatus.ERROR, |
| VisualisationPlugin.PLUGIN_ID, e.getMessage())); |
| } |
| } |
| } |
| |
| private void _readFilterConfigurations() { |
| IConfigurationElement[] config = |
| Platform.getExtensionRegistry().getConfigurationElementsFor(ExtensionID); |
| for (IConfigurationElement element : config) { |
| try { |
| if ("filterconfiguration".equals(element.getName())) { |
| String id = element.getAttribute("id"); |
| String name = element.getAttribute("name"); |
| String description = element.getAttribute("description"); |
| FilterConfiguration fc = new FilterConfiguration(id, name, description); |
| for (IConfigurationElement cd : element.getChildren("filterreference")) { |
| String filterID = cd.getAttribute("filterID"); |
| if (filterID!=null) { |
| fc.filters.add(defaultCategory.filters.get(filterID)); |
| } |
| } |
| NamedElement<FilterConfiguration> _fc = new NamedElement<FilterConfiguration>(name, fc); |
| defaultCategory.filterConfigurations.put(id, _fc); |
| if (element.getAttribute("category")!=null) { |
| categories.get(element.getAttribute("category")).filterConfigurations.put(id, _fc); |
| } |
| } |
| } |
| catch (Exception e) { |
| Platform.getLog(VisualisationPlugin.getDefault().getBundle()).log(new Status(IStatus.ERROR, |
| VisualisationPlugin.PLUGIN_ID, e.getMessage())); |
| } |
| } |
| } |
| |
| private void _readPresets() { |
| IConfigurationElement[] config = |
| Platform.getExtensionRegistry().getConfigurationElementsFor(ExtensionID); |
| for (IConfigurationElement element : config) { |
| try { |
| if ("preset".equals(element.getName())) { |
| String id = element.getAttribute("id"); |
| String name = element.getAttribute("name"); |
| // process layout and labelprovider |
| NamedElement<IViatraLayoutAlgorithm> layout = defaultCategory.layouts.get(element.getAttribute("layout")); |
| NamedElement<ViatraColoredLabelProvider> labelprovider = defaultCategory.labelproviders.get(element.getAttribute("labelprovider")); |
| // process filterconfiguration |
| FilterConfiguration filterconfig = defaultCategory.filterConfigurations.get(element.getAttribute("filters")).element; |
| if (layout!=null && labelprovider!=null && filterconfig!=null) { |
| VisualisationPreset vp = new VisualisationPreset(id,name,labelprovider,layout,filterconfig); |
| NamedElement<VisualisationPreset> vp_named = new NamedElement<VisualisationPreset>(name, vp); |
| defaultCategory.presets.put(id,vp_named); |
| Map<String,NamedElement<VisualisationPreset>> map = null; |
| if (element.getAttribute("category")!=null) { |
| map = categories.get(element.getAttribute("category")).presets; |
| map.put(name,vp_named); |
| } |
| } |
| } |
| } |
| catch (Exception e) { |
| Platform.getLog(VisualisationPlugin.getDefault().getBundle()).log(new Status(IStatus.ERROR, |
| VisualisationPlugin.PLUGIN_ID, e.getMessage())); |
| } |
| } |
| } |
| |
| private void readExtensionPoint() { |
| // the read order is very important here! |
| _readCategories(); |
| _readExecutables(); |
| _readFilterConfigurations(); |
| _readPresets(); |
| } |
| } |