/*******************************************************************************
* Copyright (c) 2011-2016 EclipseSource Muenchen GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eugen Neufeld - initial API and implementation
******************************************************************************/
package org.eclipse.emfforms.internal.core.services.label;
import java.text.MessageFormat;
import java.util.Map;
import java.util.Map.Entry;
import java.util.WeakHashMap;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.WritableValue;
import org.eclipse.core.databinding.property.value.IValueProperty;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecp.common.spi.asserts.Assert;
import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
import org.eclipse.emfforms.spi.common.BundleResolver;
import org.eclipse.emfforms.spi.common.BundleResolver.NoBundleFoundException;
import org.eclipse.emfforms.spi.common.BundleResolverFactory;
import org.eclipse.emfforms.spi.common.locale.EMFFormsLocaleChangeListener;
import org.eclipse.emfforms.spi.common.locale.EMFFormsLocaleProvider;
import org.eclipse.emfforms.spi.common.report.ReportService;
import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedReport;
import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
import org.eclipse.emfforms.spi.core.services.databinding.emf.EMFFormsDatabindingEMF;
import org.eclipse.emfforms.spi.core.services.label.EMFFormsLabelProvider;
import org.eclipse.emfforms.spi.core.services.label.NoLabelFoundException;
import org.eclipse.emfforms.spi.localization.EMFFormsLocalizationService;
import org.osgi.framework.Bundle;
/**
* Implementation of {@link EMFFormsLabelProvider}. It provides a label service that delivers the display name and
* description for a domain model reference and optionally an EObject.
*
* @author Eugen Neufeld
*
*/
public class EMFFormsLabelProviderImpl implements EMFFormsLabelProvider, EMFFormsLocaleChangeListener {
/**
* Key for the map of description observables.
*
* @author Eugen Neufeld
*/
private static class DescriptionKey {
private final String eClassName;
private final String featureName;
private final Bundle bundle;
DescriptionKey(String eClassName, String featureName, Bundle bundle) {
super();
this.eClassName = eClassName;
this.featureName = featureName;
this.bundle = bundle;
}
Bundle getBundle() {
return bundle;
}
String geteClassName() {
return eClassName;
}
String getFeatureName() {
return featureName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (bundle == null ? 0 : bundle.hashCode());
result = prime * result + (eClassName == null ? 0 : eClassName.hashCode());
result = prime * result + (featureName == null ? 0 : featureName.hashCode());
return result;
}
// BEGIN COMPLEX CODE
// path complexity check does not take returns into account
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof DescriptionKey)) {
return false;
}
final DescriptionKey other = (DescriptionKey) obj;
if (bundle == null) {
if (other.bundle != null) {
return false;
}
} else if (!bundle.equals(other.bundle)) {
return false;
}
if (eClassName == null) {
if (other.eClassName != null) {
return false;
}
} else if (!eClassName.equals(other.eClassName)) {
return false;
}
if (featureName == null) {
if (other.featureName != null) {
return false;
}
} else if (!featureName.equals(other.featureName)) {
return false;
}
return true;
}
// END COMPLEX CODE
}
private static final String DISPLAY_NAME = "_UI_%1$s_%2$s_feature"; //$NON-NLS-1$
private static final String DESCRIPTION = "_UI_%1$s_%2$s_description"; //$NON-NLS-1$
private static final String DESCRIPTION_COMPOSITE = "_UI_PropertyDescriptor_description"; //$NON-NLS-1$
private static final String TYPE = "_UI_%1$s_type"; //$NON-NLS-1$
private EMFFormsDatabindingEMF emfFormsDatabinding;
private EMFFormsLocalizationService localizationService;
private ReportService reportService;
private BundleResolver bundleResolver = BundleResolverFactory.createBundleResolver();
private final Map<WritableValue, BundleKeyWrapper> displayKeyObservableMap = new WeakHashMap<WritableValue, BundleKeyWrapper>();
private final Map<WritableValue, DescriptionKey> descriptionKeyObservableMap = new WeakHashMap<WritableValue, DescriptionKey>();