| /******************************************************************************* |
| * Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency |
| * 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: |
| * Pierre Allard, |
| * Regent L'Archeveque, |
| * Olivier L. Larouche - initial API and implementation |
| * |
| * SPDX-License-Identifier: EPL-1.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.apogy.common.emf.ui; |
| |
| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.List; |
| import java.util.Map; |
| |
| import javax.measure.unit.Unit; |
| |
| import org.eclipse.apogy.common.emf.ApogyCommonEMFFacade; |
| import org.eclipse.apogy.common.emf.Ranges; |
| import org.eclipse.apogy.common.emf.TimeSource; |
| import org.eclipse.apogy.common.emf.ui.preferences.PreferencesConstants; |
| import org.eclipse.apogy.common.emf.ui.utils.TimeSourceCompositeProviderFactory; |
| import org.eclipse.core.runtime.CoreException; |
| import org.eclipse.core.runtime.IConfigurationElement; |
| import org.eclipse.core.runtime.IExtensionPoint; |
| import org.eclipse.core.runtime.Platform; |
| import org.eclipse.emf.ecore.ETypedElement; |
| import org.eclipse.jface.preference.IPreferenceStore; |
| import org.eclipse.jface.resource.StringConverter; |
| import org.eclipse.jface.util.IPropertyChangeListener; |
| import org.eclipse.jface.util.PropertyChangeEvent; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.graphics.Color; |
| import org.eclipse.swt.graphics.Device; |
| import org.eclipse.swt.graphics.RGB; |
| import org.eclipse.swt.widgets.Display; |
| import org.eclipse.ui.plugin.AbstractUIPlugin; |
| import org.osgi.framework.BundleContext; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class Activator extends AbstractUIPlugin { |
| private static final Logger Logger = LoggerFactory.getLogger(Activator.class); |
| |
| private Map<Ranges, RGB> rangesToRGBColorMap; |
| |
| private Map<String, String> nativeToDisplayUnitsMap; |
| |
| private IPropertyChangeListener preferencesListener = null; |
| |
| // The plug-in ID |
| public static final String ID = "org.eclipse.apogy.common.emf.ui"; //$NON-NLS-1$ |
| |
| private static final String TIME_SOURCE_COMPOSITE_PROVIDER_POINT_ID = "org.eclipse.apogy.common.emf.ui.timeSourceCompositeProvider"; |
| private static final String TIME_SOURCE_COMPOSITE_PROVIDER_POINT_ID_CLASS = "Class"; |
| |
| // The Bundle Context |
| private static BundleContext bundleContext; |
| |
| // The shared instance |
| private static Activator instance; |
| |
| private TimeSourceCompositeProviderFactory timeSourceCompositeProviderFactory = null; |
| |
| /** |
| * The constructor |
| */ |
| public Activator() { |
| } |
| |
| /** |
| * Returns the shared instance |
| * |
| * @return the shared instance |
| */ |
| public static Activator getDefault() { |
| return instance; |
| } |
| |
| public static BundleContext getBundleContext() { |
| return bundleContext; |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework. |
| * BundleContext) |
| */ |
| @Override |
| public void start(BundleContext context) throws Exception { |
| super.start(context); |
| bundleContext = context; |
| instance = this; |
| |
| // Registers a listener to the properties used to keep up to date the Ranges |
| // color Map. |
| getPreferenceStore().addPropertyChangeListener(getPreferencesListener()); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see |
| * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) |
| */ |
| @Override |
| public void stop(BundleContext context) throws Exception { |
| // Unregisters a listener to the properties used to keep up to date the Ranges |
| // color Map. |
| getPreferenceStore().removePropertyChangeListener(getPreferencesListener()); |
| |
| instance = null; |
| bundleContext = null; |
| super.stop(context); |
| } |
| |
| /** |
| * Return the color associated with the given Range |
| * |
| * @param range The Range. |
| * @return The color associated to the range as defined in the Preferences, can |
| * be null. |
| */ |
| public Color getRangeColor(final Ranges range, final Device device) { |
| RGB rgb = getRangeToRBGMap().get(range); |
| |
| if (rgb != null) { |
| return new Color(device, rgb.red, rgb.green, rgb.blue); |
| } else { |
| Display display = Display.getDefault(); |
| return display.getSystemColor(SWT.COLOR_TRANSPARENT); |
| } |
| } |
| |
| /** |
| * Return the color associated with the given Range |
| * |
| * @param range The Range. |
| * @return The color associated to the range as defined in the Preferences, can |
| * be null. |
| */ |
| public RGB getRangeColor(final Ranges range) { |
| return getRangeToRBGMap().get(range); |
| } |
| |
| public Unit<?> getDisplayUnit(ETypedElement eTypedElement) { |
| Unit<?> displayUnits = getETypedElementSpecificUnit(eTypedElement); |
| |
| // If no ETypedElement specific unit have been define, use the defaults. |
| if (displayUnits == null) { |
| Unit<?> nativeUnits = ApogyCommonEMFFacade.INSTANCE.getEngineeringUnits(eTypedElement); |
| if (nativeUnits != null) { |
| displayUnits = getDefaultDisplayUnits(nativeUnits); |
| } else { |
| return null; |
| } |
| } |
| |
| return displayUnits; |
| } |
| |
| public Unit<?> getDefaultDisplayUnits(Unit<?> nativeUnit) { |
| Unit<?> displayUnits = null; |
| |
| String displayUnitAsString = getNativeToDisplayUnitsMap().get(nativeUnit.toString()); |
| |
| try { |
| displayUnits = Unit.valueOf(displayUnitAsString); |
| } catch (Throwable t) { |
| } |
| |
| return displayUnits; |
| } |
| |
| public Unit<?> getETypedElementSpecificUnit(ETypedElement eTypedElement) { |
| return null; |
| } |
| |
| private Map<Ranges, RGB> getRangeToRBGMap() { |
| if (this.rangesToRGBColorMap == null) { |
| this.rangesToRGBColorMap = new HashMap<Ranges, RGB>(); |
| initializeRangeToRBGMap(this.rangesToRGBColorMap); |
| } |
| |
| return this.rangesToRGBColorMap; |
| } |
| |
| private void initializeRangeToRBGMap(Map<Ranges, RGB> map) { |
| map.clear(); |
| |
| // Fills the table. |
| for (Ranges range : Ranges.VALUES) { |
| RGB rgb = getRGBFromPreference(range); |
| if (rgb != null) |
| map.put(range, rgb); |
| } |
| } |
| |
| /** |
| * Return the color associated with the Ranges. This implementation support null |
| * colors. |
| * |
| * @param range The range. |
| * @return The RGB color associated with the range, can be null. |
| */ |
| private RGB getRGBFromPreference(Ranges range) { |
| String value = getPreferenceStore().getString(range.getName()); |
| |
| if (IPreferenceStore.STRING_DEFAULT_DEFAULT.equals(value)) { |
| return null; |
| } else { |
| return StringConverter.asRGB(value, null); |
| } |
| } |
| |
| public Map<String, String> getNativeToDisplayUnitsMap() { |
| if (this.nativeToDisplayUnitsMap == null) { |
| this.nativeToDisplayUnitsMap = new HashMap<String, String>(); |
| initializeNativeToDisplayUnitsMap(this.nativeToDisplayUnitsMap); |
| } |
| return this.nativeToDisplayUnitsMap; |
| } |
| |
| /** |
| * Returns the list of TimeSourceCompositeProvider that have registered to the |
| * extension point. |
| * |
| * @return |
| */ |
| @SuppressWarnings("rawtypes") |
| public List<TimeSourceCompositeProvider> getRegisteredTimeSourceCompositeProvider() { |
| List<TimeSourceCompositeProvider> adapters = new ArrayList<TimeSourceCompositeProvider>(); |
| |
| IExtensionPoint extensionPoint = Platform.getExtensionRegistry() |
| .getExtensionPoint(TIME_SOURCE_COMPOSITE_PROVIDER_POINT_ID); |
| |
| IConfigurationElement[] contributors = extensionPoint.getConfigurationElements(); |
| |
| for (int i = 0; i < contributors.length; i++) { |
| IConfigurationElement contributor = contributors[i]; |
| |
| try { |
| TimeSourceCompositeProvider adapter = (TimeSourceCompositeProvider) contributor |
| .createExecutableExtension(TIME_SOURCE_COMPOSITE_PROVIDER_POINT_ID_CLASS); |
| adapters.add(adapter); |
| } catch (CoreException e) { |
| Logger.error(e.getMessage(), e); |
| } |
| } |
| |
| Logger.info("Found <" + adapters.size() + "> registered TimeSourceCompositeProvider."); |
| |
| return adapters; |
| } |
| |
| @SuppressWarnings("rawtypes") |
| public TimeSourceCompositeProvider getTimeSourceCompositeProvider(TimeSource timeSource) { |
| return getTimeSourceCompositeProviderFactory().getAdapterFor(timeSource); |
| } |
| |
| private TimeSourceCompositeProviderFactory getTimeSourceCompositeProviderFactory() { |
| if (this.timeSourceCompositeProviderFactory == null) { |
| this.timeSourceCompositeProviderFactory = new TimeSourceCompositeProviderFactory( |
| getRegisteredTimeSourceCompositeProvider()); |
| } |
| return this.timeSourceCompositeProviderFactory; |
| } |
| |
| private void initializeNativeToDisplayUnitsMap(Map<String, String> map) { |
| this.nativeToDisplayUnitsMap.clear(); |
| |
| String mapAsString = getPreferenceStore().getString(PreferencesConstants.NATIVE_TO_DISPLAY_UNITS_ID); |
| mapAsString = mapAsString.replace("{", ""); |
| mapAsString = mapAsString.replace("}", ""); |
| |
| String[] entries = mapAsString.split(","); |
| for (int i = 0; i < entries.length; i++) { |
| try { |
| String entry = entries[i]; |
| entry.trim(); |
| |
| String[] keyValue = entry.split("="); |
| String key = keyValue[0].trim(); |
| String value = keyValue[1].trim(); |
| map.put(key, value); |
| } catch (Exception e) { |
| Logger.warn("Failed to parse string <" + entries[i] + "> as a Native To Display Units entry!"); |
| } |
| } |
| } |
| |
| private IPropertyChangeListener getPreferencesListener() { |
| if (this.preferencesListener == null) { |
| this.preferencesListener = new IPropertyChangeListener() { |
| @Override |
| public void propertyChange(PropertyChangeEvent event) { |
| // Update the ColorMap |
| getRangeToRBGMap().clear(); |
| |
| // Fills the table. |
| initializeRangeToRBGMap(getRangeToRBGMap()); |
| |
| // Initialize the Unit map. |
| getNativeToDisplayUnitsMap().clear(); |
| initializeNativeToDisplayUnitsMap(getNativeToDisplayUnitsMap()); |
| } |
| }; |
| } |
| |
| return this.preferencesListener; |
| } |
| |
| } |