Skip to content

Commit

Permalink
[2032] Enhancements to decorator label providers
Browse files Browse the repository at this point in the history
* Decorators can be specified for common superclasses
* Registers decorator factories for datatype/datavalue packages

Change-Id: I2875449ca4a91dd4f1b51c7bbec3283ca5a215f9
Signed-off-by: Felix Dorner <felix.dorner@gmail.com>
  • Loading branch information
felixdo committed May 8, 2018
1 parent 58c9c79 commit 5ade62b
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 35 deletions.
Expand Up @@ -61,6 +61,18 @@
supportedTypes="org.eclipse.emf.edit.provider.IItemLabelProvider"
uri="http://www.polarsys.org/capella/core/information/communication/1.2.0"/>
</extension>
<extension point="org.eclipse.emf.edit.itemProviderAdapterFactories">
<factory
class="org.polarsys.capella.core.data.information.datavalue.provider.DatavalueItemProviderDecoratorAdapterFactory"
supportedTypes="org.eclipse.emf.edit.provider.IItemLabelProvider"
uri="http://www.polarsys.org/capella/core/information/datavalue/1.2.0"/>
</extension>
<extension point="org.eclipse.emf.edit.itemProviderAdapterFactories">
<factory
class="org.polarsys.capella.core.data.information.datatype.provider.DatatypeItemProviderDecoratorAdapterFactory"
supportedTypes="org.eclipse.emf.edit.provider.IItemLabelProvider"
uri="http://www.polarsys.org/capella/core/information/datatype/1.2.0"/>
</extension>
<extension point="org.eclipse.emf.edit.itemProviderAdapterFactories">
<factory
class="org.polarsys.capella.core.data.information.provider.InformationItemProviderDecoratorAdapterFactory"
Expand Down Expand Up @@ -139,6 +151,18 @@
class="org.polarsys.capella.core.data.information.communication.provider.CommunicationItemProviderDecoratorAdapterFactory">
</decoratorAdapterFactory>
</extension>
<extension
point="org.polarsys.capella.core.model.handler.decoratorAdapterFactory">
<decoratorAdapterFactory
class="org.polarsys.capella.core.data.information.datavalue.provider.DatavalueItemProviderDecoratorAdapterFactory">
</decoratorAdapterFactory>
</extension>
<extension
point="org.polarsys.capella.core.model.handler.decoratorAdapterFactory">
<decoratorAdapterFactory
class="org.polarsys.capella.core.data.information.datatype.provider.DatatypeItemProviderDecoratorAdapterFactory">
</decoratorAdapterFactory>
</extension>
<extension
point="org.polarsys.capella.core.model.handler.decoratorAdapterFactory">
<decoratorAdapterFactory
Expand Down
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2006, 2017 THALES GLOBAL SERVICES.
* Copyright (c) 2006, 2018 THALES GLOBAL SERVICES.
* 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
Expand All @@ -11,7 +11,6 @@
package org.polarsys.capella.core.data.gen.edit.decorators;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -21,6 +20,7 @@
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.EMFEditPlugin;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
Expand All @@ -33,41 +33,47 @@
public class ItemProviderAdapterDecorator extends ItemProviderDecorator implements Adapter.Internal {

private List<Notifier> targets;
/** cache to store the contributors to the delegatedDecorators extension point */
private static Map<String, List<IDelegatedDecorator>> _contributors = null;
private static Map<IDelegatedDecorator, String> _contributorsPosition = null;

private static Map<String, List<IDelegatedDecorator>> directContributors;
private static Map<String, List<IDelegatedDecorator>> allContributors;
private static Map<IDelegatedDecorator, String> contributorPositions;

/** constants related to the delegatedDecorators extension point */
private static final String PLUGIN_ID = "org.polarsys.capella.core.data.gen.edit.decorators";
private static final String EXTENSION_POINT_ID = "delegatedDecorator";
private static final String PLUGIN_ID = "org.polarsys.capella.core.data.gen.edit.decorators"; //$NON-NLS-1$
private static final String EXTENSION_POINT_ID = "delegatedDecorator"; //$NON-NLS-1$

protected static final String DECORATOR_POSITION_OVERRIDES = "overrides"; //$NON-NLS-1$
protected static final String DECORATOR_POSITION_PREFIX = "prefix"; //$NON-NLS-1$
protected static final String DECORATOR_POSITION_SUFFIX = "suffix"; //$NON-NLS-1$

protected static final String DECORATOR_ATTRIBUTE_POSITION = "position"; //$NON-NLS-1$
protected static final String DECORATOR_ATTRIBUTE_TYPE = "type"; //$NON-NLS-1$
protected static final String DECORATOR_ATTRIBUTE_CLASS = "class"; //$NON-NLS-1$


protected static final String DECORATOR_POSITION_OVERRIDES = "overrides";
protected static final String DECORATOR_POSITION_PREFIX = "prefix";
protected static final String DECORATOR_POSITION_SUFFIX = "suffix";

public ItemProviderAdapterDecorator(AdapterFactory adapterFactory) {
super(adapterFactory);
}

@Override
public String getText(Object object) {
String text = super.getText(object);
StringBuilder text = new StringBuilder(super.getText(object));

for (IDelegatedDecorator labelProvider : getDelegatedDecorators((EObject) object)) {
if (labelProvider.appliesTo(object)) {
String position = getDecoratorPosition(labelProvider);
if (DECORATOR_POSITION_PREFIX.equals(position)) {
text = labelProvider.getText(object) + text;
text.insert(0, labelProvider.getText(object));
} else if (DECORATOR_POSITION_SUFFIX.equals(position)) {
text = text + labelProvider.getText(object);
text.append(labelProvider.getText(object));
} else if (DECORATOR_POSITION_OVERRIDES.equals(position)) {
text = labelProvider.getText(object);
text.replace(0, text.length(), labelProvider.getText(object));
}
}
}

return text;
return text.toString();
}

/**
Expand All @@ -77,47 +83,75 @@ protected String getDecoratorPosition(IDelegatedDecorator labelProvider) {
if (labelProvider == null) {
return ICommonConstants.EMPTY_STRING;
}
if (_contributorsPosition == null) {
if (contributorPositions == null) {
fillContributorsCache();
}
return _contributorsPosition.get(labelProvider);
return contributorPositions.get(labelProvider);
}

/**
* @param eobject the object from which a delegated decorator is requested
* @param eobject
* the object from which a delegated decorator is requested
* @return a list of {@link IDelegatedDecorator} related to the given eobject
*/
protected List<IDelegatedDecorator> getDelegatedDecorators(EObject eobject) {
if (eobject == null) {
return Collections.<IDelegatedDecorator> emptyList();
}
if (_contributors == null) {
if (directContributors == null) {
fillContributorsCache();
}
List<IDelegatedDecorator> providers = _contributors.get(eobject.eClass().getInstanceClassName());
return providers != null ? providers : Collections.<IDelegatedDecorator> emptyList();

return getContributorsPerType(eobject.eClass());
}

/**
private List<IDelegatedDecorator> getContributorsPerType(EClass clazz) {
if (!allContributors.containsKey(clazz.getInstanceClassName())) {
List<IDelegatedDecorator> result = new ArrayList<IDelegatedDecorator>(2);
List<IDelegatedDecorator> current = directContributors.get(clazz.getInstanceClassName());
if (current != null) {
for (IDelegatedDecorator next : current) {
if (!result.contains(next)) {
result.add(next);
}
}
}
for (EClass superType : clazz.getEAllSuperTypes()) {
for (IDelegatedDecorator next : getContributorsPerType(superType)) {
if (!result.contains(next)) {
result.add(next);
}
}
}
if (result.isEmpty()) {
result = Collections.emptyList();
}
allContributors.put(clazz.getInstanceClassName(), result);
}
return allContributors.get(clazz.getInstanceClassName());
}

/*
* Fills both caches '_contributors' and '_contributorsPosition'
*/
private void fillContributorsCache() {
_contributors = new HashMap<String, List<IDelegatedDecorator>>();
_contributorsPosition = new HashMap<IDelegatedDecorator, String>();
List<IConfigurationElement> attributesProvider =
Arrays.asList(ExtensionPointHelper.getConfigurationElements(PLUGIN_ID, EXTENSION_POINT_ID));
for (IConfigurationElement provider : attributesProvider) {
String type = provider.getAttribute("type");
List<IDelegatedDecorator> labelProviders = _contributors.get(type);
directContributors = new HashMap<String, List<IDelegatedDecorator>>();
contributorPositions = new HashMap<IDelegatedDecorator, String>();
allContributors = new HashMap<String, List<IDelegatedDecorator>>();

for (IConfigurationElement provider : ExtensionPointHelper.getConfigurationElements(PLUGIN_ID, EXTENSION_POINT_ID)) {
String type = provider.getAttribute(DECORATOR_ATTRIBUTE_TYPE);
List<IDelegatedDecorator> labelProviders = directContributors.get(type);
if (labelProviders == null) {
labelProviders = new ArrayList<IDelegatedDecorator>();
_contributors.put(type, labelProviders);
directContributors.put(type, labelProviders);
}
IDelegatedDecorator cls = (IDelegatedDecorator) ExtensionPointHelper.createInstance(provider, "class");
IDelegatedDecorator cls = (IDelegatedDecorator) ExtensionPointHelper.createInstance(provider, DECORATOR_ATTRIBUTE_CLASS);
labelProviders.add(cls);
String position = provider.getAttribute("position");
_contributorsPosition.put(cls, position == null ? ICommonConstants.EMPTY_STRING : position);
String position = provider.getAttribute(DECORATOR_ATTRIBUTE_POSITION);
contributorPositions.put(cls, position == null ? ICommonConstants.EMPTY_STRING : position);
}

}

/**
Expand All @@ -137,7 +171,7 @@ protected Object overlayImage(Object object, Object image) {
if (AdapterFactoryEditingDomain.isControlled(object)) {
List<Object> images = new ArrayList<Object>(2);
images.add(image);
images.add(EMFEditPlugin.INSTANCE.getImage("full/ovr16/ControlledObject"));
images.add(EMFEditPlugin.INSTANCE.getImage("full/ovr16/ControlledObject")); //$NON-NLS-1$
image = new ComposedImage(images);
}
return image;
Expand All @@ -146,6 +180,7 @@ protected Object overlayImage(Object object, Object image) {
/**
* @see org.eclipse.emf.common.notify.Adapter#getTarget()
*/
@Override
public Notifier getTarget() {
if (targets == null || targets.isEmpty()) {
return null;
Expand All @@ -156,6 +191,7 @@ public Notifier getTarget() {
/**
* @see org.eclipse.emf.common.notify.Adapter#setTarget(org.eclipse.emf.common.notify.Notifier)
*/
@Override
public void setTarget(Notifier newTarget) {
if (targets == null) {
targets = new ArrayList<Notifier>();
Expand All @@ -166,6 +202,7 @@ public void setTarget(Notifier newTarget) {
/**
* @see org.eclipse.emf.common.notify.Adapter.Internal#unsetTarget(org.eclipse.emf.common.notify.Notifier)
*/
@Override
public void unsetTarget(Notifier oldTarget) {
if (targets != null) {
targets.remove(oldTarget);
Expand Down
@@ -0,0 +1,27 @@
/*******************************************************************************
* Copyright (c) 2018 THALES GLOBAL SERVICES.
* 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:
* Thales - initial API and implementation
*******************************************************************************/
package org.polarsys.capella.core.data.information.datatype.provider;

import org.eclipse.emf.edit.provider.DecoratorAdapterFactory;
import org.eclipse.emf.edit.provider.IItemProviderDecorator;
import org.polarsys.capella.core.data.gen.edit.decorators.ForwardingItemProviderAdapterDecorator;

public class DatatypeItemProviderDecoratorAdapterFactory extends DecoratorAdapterFactory {

public DatatypeItemProviderDecoratorAdapterFactory() {
super(new DatatypeItemProviderAdapterFactory());
}

@Override
protected IItemProviderDecorator createItemProviderDecorator(Object target, Object Type) {
return new ForwardingItemProviderAdapterDecorator(this);
}
}
@@ -0,0 +1,27 @@
/*******************************************************************************
* Copyright (c) 2018 THALES GLOBAL SERVICES.
* 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:
* Thales - initial API and implementation
*******************************************************************************/
package org.polarsys.capella.core.data.information.datavalue.provider;

import org.eclipse.emf.edit.provider.DecoratorAdapterFactory;
import org.eclipse.emf.edit.provider.IItemProviderDecorator;
import org.polarsys.capella.core.data.gen.edit.decorators.ForwardingItemProviderAdapterDecorator;

public class DatavalueItemProviderDecoratorAdapterFactory extends DecoratorAdapterFactory {

public DatavalueItemProviderDecoratorAdapterFactory() {
super(new DatavalueItemProviderAdapterFactory());
}

@Override
protected IItemProviderDecorator createItemProviderDecorator(Object target, Object Type) {
return new ForwardingItemProviderAdapterDecorator(this);
}
}

0 comments on commit 5ade62b

Please sign in to comment.