| /******************************************************************************* |
| * 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.impl; |
| |
| import java.io.StringReader; |
| import java.io.StringWriter; |
| import java.net.MalformedURLException; |
| import java.net.URL; |
| import java.text.DecimalFormat; |
| import java.util.ArrayList; |
| import java.util.Collections; |
| import java.util.Comparator; |
| import java.util.List; |
| import java.util.TreeMap; |
| |
| import javax.measure.converter.MultiplyConverter; |
| import javax.measure.converter.UnitConverter; |
| import javax.measure.unit.Unit; |
| |
| import org.eclipse.apogy.common.emf.ApogyCommonEMFFacade; |
| import org.eclipse.apogy.common.emf.Named; |
| import org.eclipse.apogy.common.emf.Ranges; |
| import org.eclipse.apogy.common.emf.transaction.ApogyCommonTransactionFacade; |
| import org.eclipse.apogy.common.emf.ui.Activator; |
| import org.eclipse.apogy.common.emf.ui.DecimalFormatRegistry; |
| import org.eclipse.apogy.common.emf.ui.DisplayUnitsRegistry; |
| import org.eclipse.apogy.common.emf.ui.EOperationEParametersFormatProviderParameters; |
| import org.eclipse.apogy.common.emf.ui.EOperationEParametersUnitsProviderParameters; |
| import org.eclipse.apogy.common.emf.ui.FormatProvider; |
| import org.eclipse.apogy.common.emf.ui.UnitsProvider; |
| import org.eclipse.apogy.common.emf.ui.preferences.PreferencesConstants; |
| import org.eclipse.e4.ui.model.application.ui.basic.MPart; |
| import org.eclipse.emf.ecore.EClass; |
| import org.eclipse.emf.ecore.EClassifier; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.EOperation; |
| import org.eclipse.emf.ecore.ETypedElement; |
| import org.eclipse.emf.ecore.EcorePackage; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.emf.ecore.resource.ResourceSet; |
| import org.eclipse.emf.ecore.resource.URIConverter; |
| import org.eclipse.emf.ecore.resource.URIConverter.ReadableInputStream; |
| import org.eclipse.emf.ecore.resource.URIConverter.WriteableOutputStream; |
| import org.eclipse.emf.ecore.util.EcoreUtil; |
| import org.eclipse.emf.ecore.xmi.XMLResource; |
| import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; |
| import org.eclipse.emf.transaction.RecordingCommand; |
| import org.eclipse.emf.transaction.TransactionalEditingDomain; |
| import org.eclipse.jface.dialogs.MessageDialog; |
| import org.eclipse.jface.resource.ImageDescriptor; |
| import org.eclipse.swt.graphics.Color; |
| import org.eclipse.swt.graphics.Image; |
| import org.eclipse.swt.graphics.RGB; |
| import org.eclipse.swt.widgets.Display; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class ApogyCommonEMFUIFacadeCustomImpl extends ApogyCommonEMFUIFacadeImpl { |
| |
| private static final Logger Logger = LoggerFactory.getLogger(ApogyCommonEMFUIFacadeImpl.class); |
| |
| @Override |
| public TreeMap<UnitConverter, String> getUnitConverterMap() { |
| TreeMap<UnitConverter, String> map = super.getUnitConverterMap(); |
| |
| if (map == null || map.isEmpty()) { |
| map = new TreeMap<UnitConverter, String>(new Comparator<UnitConverter>() { |
| @Override |
| public int compare(UnitConverter arg0, UnitConverter arg1) { |
| return (arg0.convert(1) - arg1.convert(1)) < 0 ? 1 : -1; |
| } |
| }); |
| |
| map.put(new MultiplyConverter(1E24), "Y"); |
| map.put(new MultiplyConverter(1E21), "Z"); |
| map.put(new MultiplyConverter(1E18), "E"); |
| map.put(new MultiplyConverter(1E15), "P"); |
| map.put(new MultiplyConverter(1E12), "T"); |
| map.put(new MultiplyConverter(1E9), "G"); |
| map.put(new MultiplyConverter(1E6), "M"); |
| map.put(new MultiplyConverter(1E3), "k"); |
| map.put(new MultiplyConverter(1E2), "h"); |
| map.put(new MultiplyConverter(1E1), "da"); |
| map.put(UnitConverter.IDENTITY, " "); |
| map.put(new MultiplyConverter(1E-1), "d"); |
| map.put(new MultiplyConverter(1E-2), "c"); |
| map.put(new MultiplyConverter(1E-3), "m"); |
| map.put(new MultiplyConverter(1E-6), "μ"); |
| map.put(new MultiplyConverter(1E-9), "n"); |
| map.put(new MultiplyConverter(1E-12), "p"); |
| map.put(new MultiplyConverter(1E-15), "f"); |
| map.put(new MultiplyConverter(1E-18), "a"); |
| map.put(new MultiplyConverter(1E-21), "z"); |
| map.put(new MultiplyConverter(1E-24), "y"); |
| |
| setUnitConverterMap(map); |
| } |
| |
| return super.getUnitConverterMap(); |
| } |
| |
| @Override |
| public Color getColorForRange(Ranges range) { |
| return Activator.getDefault().getRangeColor(range, Display.getDefault()); |
| } |
| |
| |
| @Override |
| public RGB getRGBColorForRange(Ranges range) { |
| return Activator.getDefault().getRangeColor(range); |
| } |
| |
| @Override |
| public Unit<?> getDisplayUnits(ETypedElement eTypedElement) { |
| Unit<?> displayUnit = null; |
| |
| /** try the registry */ |
| UnitsProvider provider = DisplayUnitsRegistry.INSTANCE.getEntriesMap().getEntries().get(eTypedElement); |
| if (provider != null) { |
| displayUnit = provider.getProvidedUnit(null); |
| } |
| |
| /** try the display preferences */ |
| if (displayUnit == null) { |
| displayUnit = Activator.getDefault().getDisplayUnit(eTypedElement); |
| } |
| |
| /** try the model units */ |
| if (displayUnit == null) { |
| displayUnit = ApogyCommonEMFFacade.INSTANCE.getEngineeringUnits(eTypedElement); |
| } |
| |
| return displayUnit; |
| } |
| |
| |
| @Override |
| public Unit<?> getDisplayUnits(EOperation eOperation, EOperationEParametersUnitsProviderParameters parameters) { |
| Unit<?> displayUnit = null; |
| |
| /** try the registry */ |
| UnitsProvider provider = DisplayUnitsRegistry.INSTANCE.getEntriesMap().getEntries().get(eOperation); |
| if (provider != null) { |
| displayUnit = provider.getProvidedUnit(parameters); |
| } |
| |
| /** try the display preferences */ |
| if (displayUnit == null) { |
| displayUnit = getDisplayUnits(parameters.getParam()); |
| } |
| |
| return displayUnit; |
| } |
| |
| |
| @Override |
| public void addUnitsProviderToRegistry(ETypedElement eTypedElement, UnitsProvider provider) { |
| DisplayUnitsRegistry.INSTANCE.getEntriesMap().getEntries().put(eTypedElement, provider); |
| DisplayUnitsRegistry.INSTANCE.save(); |
| } |
| |
| |
| @Override |
| public Number convertToNativeUnits(Number number, Unit<?> nativeUnits, Unit<?> displayUnits, |
| EClassifier numberType) { |
| if (displayUnits != null && displayUnits != nativeUnits) { |
| Double doubleValue = displayUnits.getConverterTo(nativeUnits).convert(number.doubleValue()); |
| |
| /** Convert to the right type */ |
| if (numberType == EcorePackage.Literals.EFLOAT) { |
| return doubleValue.floatValue(); |
| } else if (numberType == EcorePackage.Literals.EBYTE) { |
| return doubleValue.byteValue(); |
| } else if (numberType == EcorePackage.Literals.ESHORT) { |
| return doubleValue.shortValue(); |
| } else if (numberType == EcorePackage.Literals.EINT) { |
| return doubleValue.intValue(); |
| } else if (numberType == EcorePackage.Literals.ELONG) { |
| return doubleValue.longValue(); |
| } else { |
| return doubleValue; |
| } |
| } |
| |
| return number; |
| } |
| |
| |
| @Override |
| public DecimalFormat getDisplayFormat(ETypedElement eTypedElement) { |
| DecimalFormat format = null; |
| |
| /** try the registry */ |
| FormatProvider provider = DecimalFormatRegistry.INSTANCE.getEntriesMap().getEntries().get(eTypedElement); |
| if (provider != null) { |
| format = provider.getProvidedFormat(null); |
| } |
| |
| /** get the format from the type */ |
| if (format == null) { |
| String formatStr = ""; |
| EClassifier type = eTypedElement.getEType(); |
| if (type == EcorePackage.Literals.EFLOAT) { |
| formatStr = Activator.getDefault().getPreferenceStore() |
| .getString(PreferencesConstants.NATIVE_FORMAT_FLOAT_ID); |
| } else if (type == EcorePackage.Literals.EBYTE) { |
| formatStr = Activator.getDefault().getPreferenceStore() |
| .getString(PreferencesConstants.NATIVE_FORMAT_BYTE_ID); |
| } else if (type == EcorePackage.Literals.ESHORT) { |
| formatStr = Activator.getDefault().getPreferenceStore() |
| .getString(PreferencesConstants.NATIVE_FORMAT_SHORT_ID); |
| } else if (type == EcorePackage.Literals.EINT) { |
| formatStr = Activator.getDefault().getPreferenceStore() |
| .getString(PreferencesConstants.NATIVE_FORMAT_INT_ID); |
| } else if (type == EcorePackage.Literals.ELONG) { |
| formatStr = Activator.getDefault().getPreferenceStore() |
| .getString(PreferencesConstants.NATIVE_FORMAT_LONG_ID); |
| } else { |
| formatStr = Activator.getDefault().getPreferenceStore() |
| .getString(PreferencesConstants.NATIVE_FORMAT_DOUBLE_ID); |
| } |
| format = new DecimalFormat(formatStr); |
| } |
| return format; |
| } |
| |
| |
| @Override |
| public DecimalFormat getDisplayFormat(EOperation eOperation, |
| EOperationEParametersFormatProviderParameters parameters) { |
| DecimalFormat format = null; |
| |
| /** try the registry */ |
| FormatProvider provider = DecimalFormatRegistry.INSTANCE.getEntriesMap().getEntries().get(eOperation); |
| if (provider != null) { |
| format = provider.getProvidedFormat(parameters); |
| } |
| |
| /** try the with the eParameter */ |
| if (format == null) { |
| format = getDisplayFormat(parameters.getParam()); |
| } |
| |
| return format; |
| } |
| |
| |
| @Override |
| public void addFormatProviderToRegistry(ETypedElement eTypedElement, FormatProvider provider) { |
| DecimalFormatRegistry.INSTANCE.getEntriesMap().getEntries().put(eTypedElement, provider); |
| DecimalFormatRegistry.INSTANCE.save(); |
| } |
| |
| @Override |
| public void openDeleteNamedDialog(Named named) { |
| List<Named> nameds = new ArrayList<Named>(); |
| nameds.add(named); |
| this.openDeleteNamedDialog(nameds); |
| } |
| |
| @Override |
| public void openDeleteNamedDialog(List<? extends Named> namedList) { |
| String eObjectsToDeleteMessage = ""; |
| |
| eObjectsToDeleteMessage += ApogyCommonEMFFacade.INSTANCE.toString(namedList, ", "); |
| |
| MessageDialog dialog = new MessageDialog(null, "Delete the selected elements", null, |
| "Are you sure to delete these elements: " + eObjectsToDeleteMessage + "?", MessageDialog.QUESTION, |
| new String[] { "Yes", "No" }, 1); |
| int result = dialog.open(); |
| if (result == 0) { |
| for (EObject eObject : namedList) { |
| ApogyCommonTransactionFacade.INSTANCE.basicDelete(eObject.eContainer(), eObject.eContainingFeature(), |
| eObject); |
| } |
| } |
| } |
| |
| @Override |
| public void saveToPersistedState(MPart mPart, String persistedStateKey, EObject eObject, ResourceSet resourceSet) { |
| if (eObject != null) { |
| ApogyCommonTransactionFacade.INSTANCE.removeFromEditingDomain(eObject); |
| |
| Resource childResource = resourceSet |
| .createResource(org.eclipse.emf.common.util.URI.createURI(mPart.getElementId())); |
| TransactionalEditingDomain transactionalEditingDomain = (TransactionalEditingDomain) AdapterFactoryEditingDomain |
| .getEditingDomainFor(resourceSet); |
| |
| RecordingCommand cmd = new RecordingCommand(transactionalEditingDomain) { |
| @Override |
| protected void doExecute() { |
| childResource.getContents().add(eObject); |
| } |
| }; |
| |
| try { |
| transactionalEditingDomain.getCommandStack().execute(cmd); |
| } catch (Throwable t) { |
| } |
| |
| StringWriter stringWriter = new StringWriter(); |
| WriteableOutputStream os = new URIConverter.WriteableOutputStream(stringWriter, |
| XMLResource.OPTION_ENCODING); |
| try { |
| childResource.save(os, Collections.EMPTY_MAP); |
| } catch (Throwable t) { |
| Logger.error("Failed to save EObject to Part <" + mPart.getElementId() + "> Persisted State at key <" |
| + persistedStateKey + ">.", t); |
| } |
| |
| // Saves the XML of the model in the persisted state. |
| mPart.getPersistedState().put(persistedStateKey, stringWriter.getBuffer().toString()); |
| } |
| } |
| |
| @Override |
| public EObject readFromPersistedState(MPart mPart, String persistedStateKey, ResourceSet resourceSet) { |
| EObject eObject = null; |
| String modelString = mPart.getPersistedState().get(persistedStateKey); |
| |
| if (modelString != null) { |
| Resource childResource = resourceSet |
| .createResource(org.eclipse.emf.common.util.URI.createURI(mPart.getElementId())); |
| StringReader reader = new StringReader(modelString); |
| ReadableInputStream is = new URIConverter.ReadableInputStream(reader); |
| |
| try { |
| childResource.load(is, Collections.EMPTY_MAP); |
| |
| if (childResource.getContents().size() > 0) { |
| eObject = childResource.getContents().get(0); |
| } else { |
| Logger.warn("No EObject found in Part <" + mPart.getElementId() + "> Persisted State at key <" |
| + persistedStateKey + ">."); |
| } |
| } catch (Throwable t) { |
| Logger.error("Failed to load EObject from Part <" + mPart.getElementId() + "> Persisted State at key <" |
| + persistedStateKey + ">.", t); |
| } |
| |
| } else { |
| Logger.info("No EObject content found in Part <" + mPart.getElementId() + "> Persisted State at key <" |
| + persistedStateKey + ">."); |
| } |
| |
| return eObject; |
| } |
| |
| |
| @Override |
| public Image getImage(EClass eClass) { |
| final String URL_PREFIX = "platform:/plugin"; |
| Image image = null; |
| String path = new String(URL_PREFIX); |
| |
| /** |
| * Try getting the image from the edit directory. |
| */ |
| // Get the annotation |
| String editDirectory = EcoreUtil.getAnnotation(eClass.getEPackage(), "http://www.eclipse.org/emf/2002/GenModel", |
| "editDirectory"); |
| |
| // If the edit directory was found. |
| if (editDirectory != null) { |
| if (editDirectory.contains("/src-gen")) { |
| editDirectory = editDirectory.substring(0, editDirectory.indexOf("/src-gen")); |
| } |
| path += editDirectory + "/icons/full/obj16/"; |
| } else { |
| // Otherwise try the normal edit directory used in Apogy code. |
| path += "/" + eClass.getEPackage().getNsURI() + ".edit/icons/full/obj16/"; |
| } |
| |
| path += eClass.getName() + ".gif"; |
| try { |
| image = ImageDescriptor.createFromURL(new URL(path)).createImage(); |
| } catch (MalformedURLException e) { |
| Logger.error( |
| " Unable to get the image corresponding to the eClass from the edit directory " + eClass.getName(), |
| e); |
| } |
| |
| return image; |
| } |
| |
| } // ApogyCommonEMFUIFacadeImpl |