diff options
author | Christian W. Damus | 2014-07-22 20:12:05 +0000 |
---|---|---|
committer | Christian W. Damus | 2014-07-23 18:42:50 +0000 |
commit | 2e48dff78cf32463061f9367cebd0759684f32e6 (patch) | |
tree | 8546eccb61077c841e7f2d4868bcd1ebac862018 /plugins | |
parent | 88826611b46aebc0632e3a9e7bf07aeee2f7840f (diff) | |
download | org.eclipse.papyrus-2e48dff78cf32463061f9367cebd0759684f32e6.tar.gz org.eclipse.papyrus-2e48dff78cf32463061f9367cebd0759684f32e6.tar.xz org.eclipse.papyrus-2e48dff78cf32463061f9367cebd0759684f32e6.zip |
425270: [model explorer] Relationships in model explorer need default names
https://bugs.eclipse.org/bugs/show_bug.cgi?id=425270
Updated the label providers that contribute names to the Model Explorer to derive implicit labels for unnamed elements from the UML2 item providers.
Diffstat (limited to 'plugins')
5 files changed, 292 insertions, 70 deletions
diff --git a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/Activator.java b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/Activator.java index bf3f32ef234..3095bf96516 100644 --- a/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/Activator.java +++ b/plugins/infra/gmfdiag/org.eclipse.papyrus.infra.gmfdiag.common/src/org/eclipse/papyrus/infra/gmfdiag/common/Activator.java @@ -10,6 +10,7 @@ * Contributors:
* Cedric Dumoulin Cedric.dumoulin@lifl.fr - Initial API and implementation
* Christian W. Damus (CEA) - bug 410346
+ * Christian W. Damus (CEA) - bug 425270
*
*****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common;
@@ -20,7 +21,6 @@ import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.notify.AdapterFactory;
-import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint;
@@ -38,8 +38,6 @@ public class Activator extends AbstractUIPlugin { private static Activator instance;
- private ComposedAdapterFactory adapterFactory;
-
public Activator() {
}
@@ -53,13 +51,10 @@ public class Activator extends AbstractUIPlugin { // register the login helper
log = new LogHelper(this);
PreferencesHint.registerPreferenceStore(DIAGRAM_PREFERENCES_HINT, getPreferenceStore());
- adapterFactory = createAdapterFactory();
}
@Override
public void stop(BundleContext context) throws Exception {
- adapterFactory.dispose();
- adapterFactory = null;
log = null;
instance = null;
super.stop(context);
@@ -69,16 +64,12 @@ public class Activator extends AbstractUIPlugin { return instance;
}
- protected ComposedAdapterFactory createAdapterFactory() {
- return new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
- }
-
public AdapterFactory getItemProvidersAdapterFactory() {
- return adapterFactory;
+ return org.eclipse.papyrus.uml.tools.Activator.getDefault().getItemProviderAdapterFactory();
}
public ImageDescriptor getItemImageDescriptor(Object item) {
- IItemLabelProvider labelProvider = (IItemLabelProvider)adapterFactory.adapt(item, IItemLabelProvider.class);
+ IItemLabelProvider labelProvider = (IItemLabelProvider)getItemProvidersAdapterFactory().adapt(item, IItemLabelProvider.class);
if(labelProvider != null) {
return ExtendedImageRegistry.getInstance().getImageDescriptor(labelProvider.getImage(item));
}
diff --git a/plugins/uml/modelexplorer/org.eclipse.papyrus.uml.modelexplorer/src/org/eclipse/papyrus/uml/modelexplorer/queries/GetComplexName.java b/plugins/uml/modelexplorer/org.eclipse.papyrus.uml.modelexplorer/src/org/eclipse/papyrus/uml/modelexplorer/queries/GetComplexName.java index 4623ad5a8c1..aaacd45a304 100644 --- a/plugins/uml/modelexplorer/org.eclipse.papyrus.uml.modelexplorer/src/org/eclipse/papyrus/uml/modelexplorer/queries/GetComplexName.java +++ b/plugins/uml/modelexplorer/org.eclipse.papyrus.uml.modelexplorer/src/org/eclipse/papyrus/uml/modelexplorer/queries/GetComplexName.java @@ -1,46 +1,56 @@ -/* - * - */ +/***************************************************************************** + * Copyright (c) 2010, 2014 CEA LIST and others. + * + * 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: + * CEA LIST - Initial API and implementation + * Christian W. Damus (CEA) - bug 425270 + * + /*****************************************************************************/ package org.eclipse.papyrus.uml.modelexplorer.queries; -import java.util.List; - import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.edit.provider.IItemLabelProvider; import org.eclipse.papyrus.emf.facet.efacet.core.IFacetManager; import org.eclipse.papyrus.emf.facet.efacet.core.exception.DerivedTypedElementException; import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.ParameterValue; import org.eclipse.papyrus.emf.facet.query.java.core.IJavaQuery2; import org.eclipse.papyrus.emf.facet.query.java.core.IParameterValueList2; +import org.eclipse.papyrus.uml.tools.providers.DelegatingItemLabelProvider; import org.eclipse.uml2.uml.NamedElement; -import org.eclipse.uml2.uml.Stereotype; /** get the name + the list of applied stereotypes */ public class GetComplexName implements IJavaQuery2<NamedElement, String> { - /** left Stereotype delimiters ('Guillemets francais'). */ - public static String ST_LEFT = String.valueOf("\u00AB"); //$NON-NLS-1$ - /** Right Stereotype delimiters ('Guillemets francais'). */ - public static String ST_RIGHT = String.valueOf("\u00BB"); //$NON-NLS-1$ + /** + * Left Stereotype delimiters ('Guillemets francais'). + * + * @deprecated No longer used. + */ + @Deprecated + public static String ST_LEFT = "\u00AB"; //$NON-NLS-1$ + + /** + * Right Stereotype delimiters ('Guillemets francais'). + * + * @deprecated No longer used. + */ + @Deprecated + public static String ST_RIGHT = "\u00BB"; //$NON-NLS-1$ + private static final IItemLabelProvider labelProvider = new DelegatingItemLabelProvider(); public String evaluate(NamedElement source, IParameterValueList2 parameterValues, IFacetManager facetManager) throws DerivedTypedElementException { - ParameterValue parameterValue= (ParameterValue)parameterValues.getParameterValueByName("eObject"); - if(parameterValue.getValue() instanceof EStructuralFeature){ + ParameterValue parameterValue = (ParameterValue)parameterValues.getParameterValueByName("eObject"); //$NON-NLS-1$ + if(parameterValue.getValue() instanceof EStructuralFeature) { return ((EStructuralFeature)parameterValue.getValue()).getName(); } - String txt = ""; //$NON-NLS-1$ - List<Stereotype> stereoList = source.getAppliedStereotypes(); - if (stereoList.size() == 0) { - return source.getName(); - } else { - for (int i = 0; i < stereoList.size(); i++) { - txt = txt + stereoList.get(i).getName(); - if (i < stereoList.size() - 1) { - txt = txt + ", "; //$NON-NLS-1$ - } - } - txt = ST_LEFT + txt + ST_RIGHT + " " + source.getName(); //$NON-NLS-1$ - return txt; - } + + // Delegate to UML2 Edit providers to get localized and inferred names where applicable + return labelProvider.getText(source); } } diff --git a/plugins/uml/tools/org.eclipse.papyrus.uml.tools/src/org/eclipse/papyrus/uml/tools/Activator.java b/plugins/uml/tools/org.eclipse.papyrus.uml.tools/src/org/eclipse/papyrus/uml/tools/Activator.java index db4e491288d..aba3b3aa8fa 100644 --- a/plugins/uml/tools/org.eclipse.papyrus.uml.tools/src/org/eclipse/papyrus/uml/tools/Activator.java +++ b/plugins/uml/tools/org.eclipse.papyrus.uml.tools/src/org/eclipse/papyrus/uml/tools/Activator.java @@ -1,5 +1,5 @@ /*****************************************************************************
- * Copyright (c) 2014 CEA LIST.
+ * Copyright (c) 2014 CEA LIST and others.
*
*
* All rights reserved. This program and the accompanying materials
@@ -9,6 +9,8 @@ *
* Contributors:
* Patrick Tessier (CEA LIST) - Initial API and implementation
+ * Christian W. Damus (CEA) - bug 425270
+ *
/*****************************************************************************/
package org.eclipse.papyrus.uml.tools;
@@ -16,7 +18,9 @@ import java.util.ArrayList; import java.util.Collection;
import java.util.List;
+import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.papyrus.infra.core.log.LogHelper;
@@ -47,6 +51,8 @@ public class Activator extends AbstractUIPlugin { */
public static LogHelper log;
+ private ComposedAdapterFactory adapterFactory;
+
/**
* The constructor
*/
@@ -63,6 +69,7 @@ public class Activator extends AbstractUIPlugin { super.start(context);
plugin = this;
log = new LogHelper(this);
+ adapterFactory = createAdapterFactory();
}
/*
@@ -72,6 +79,9 @@ public class Activator extends AbstractUIPlugin { */
@Override
public void stop(BundleContext context) throws Exception {
+ adapterFactory.dispose();
+ adapterFactory = null;
+ log = null;
plugin = null;
super.stop(context);
}
@@ -85,6 +95,14 @@ public class Activator extends AbstractUIPlugin { return plugin;
}
+ protected ComposedAdapterFactory createAdapterFactory() {
+ return new ComposedAdapterFactory(ComposedAdapterFactory.Descriptor.Registry.INSTANCE);
+ }
+
+ public AdapterFactory getItemProviderAdapterFactory() {
+ return adapterFactory;
+ }
+
diff --git a/plugins/uml/tools/org.eclipse.papyrus.uml.tools/src/org/eclipse/papyrus/uml/tools/providers/DelegatingItemLabelProvider.java b/plugins/uml/tools/org.eclipse.papyrus.uml.tools/src/org/eclipse/papyrus/uml/tools/providers/DelegatingItemLabelProvider.java new file mode 100644 index 00000000000..65050c33f21 --- /dev/null +++ b/plugins/uml/tools/org.eclipse.papyrus.uml.tools/src/org/eclipse/papyrus/uml/tools/providers/DelegatingItemLabelProvider.java @@ -0,0 +1,221 @@ +/***************************************************************************** + * Copyright (c) 2014 CEA LIST and others. + * + * 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: + * CEA LIST - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.uml.tools.providers; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.eclipse.emf.common.notify.AdapterFactory; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.edit.provider.IItemLabelProvider; +import org.eclipse.papyrus.emf.facet.util.emf.core.ModelUtils; +import org.eclipse.papyrus.infra.emf.utils.EMFHelper; +import org.eclipse.papyrus.uml.tools.Activator; +import org.eclipse.uml2.common.util.UML2Util; +import org.eclipse.uml2.uml.Association; +import org.eclipse.uml2.uml.Relationship; +import org.eclipse.uml2.uml.TemplateParameter; +import org.eclipse.uml2.uml.TemplateSignature; +import org.eclipse.uml2.uml.util.UMLSwitch; + + +/** + * An item label provider that delegates to an adapter factory and rewrites the result for UML element labels. + * If the adapter factory needs to be disposed (which is usually the case for item-provider factories), then + * this is the responsibility of the client that creates the delegating label provider. + */ +public class DelegatingItemLabelProvider implements IItemLabelProvider { + + public static final int SHOW_LABEL = 0x1; + + public static final int SHOW_METACLASS = 0x2; + + public static final int SHOW_STEREOTYPES = 0x4; + + /** left Stereotype delimiters ('Guillemets francais'). */ + private static String ST_LEFT = "\u00AB"; //$NON-NLS-1$ + + /** Right Stereotype delimiters ('Guillemets francais'). */ + private static String ST_RIGHT = "\u00BB"; //$NON-NLS-1$ + + /** + * The pattern of UML2 Edit labels: + * <ol> + * <li>keywords (ad hoc and stereotypes), if any, followed by</li> + * <li>an optional metaclass specifier, followed by</li> + * <li>metaclass-specific decorations (such as derived mark {@code "/"} for properties)</li> + * <li>the element name, possibly localized or inferred if omitted by the modeler</li> + * <li>metaclass-specific decorations (such as type and multiplicity {@code ": String [1..*]"} for properties)</li> + * </ol> + */ + private static final Pattern UML2_LABEL_PATTERN = Pattern.compile("(?:<<(.+?)>>)?\\s*(<[^<>]+>)?\\s*(.*)"); + + private final UMLSwitch<Boolean> showMetaclassSwitch = createShowMetaclassSwitch(); + + private final AdapterFactory itemAdapterFactory; + + private final int style; + + /** + * Initializes me with my delegate factory and a bit-mask of which label components to allow. + * + * @param itemAdapterFactory + * my delegate factory + * @param style + * mask of {@linkplain #SHOW_LABEL style bits} indicating which components of the label to allow + */ + public DelegatingItemLabelProvider(AdapterFactory itemAdapterFactory, int style) { + super(); + + this.itemAdapterFactory = itemAdapterFactory; + this.style = style; + } + + /** + * Initializes me with my delegate factory. I do not suppress any components of the label. + * + * @param itemAdapterFactory + * my delegate factory + */ + public DelegatingItemLabelProvider(AdapterFactory itemAdapterFactory) { + this(itemAdapterFactory, SHOW_LABEL | SHOW_METACLASS | SHOW_STEREOTYPES); + } + + /** + * Initializes me with my plug-in's {@linkplain Activator#getItemProviderAdapterFactory() shared adapter factory} as my delegate factory and a + * bit-mask of which label components to allow. <em>Note</em> that the shared adapter factory does not need to be disposed. + * + * @param style + * mask of {@linkplain #SHOW_LABEL style bits} indicating which components of the label to allow + */ + public DelegatingItemLabelProvider(int style) { + this(Activator.getDefault().getItemProviderAdapterFactory(), style); + } + + /** + * Initializes me with my plug-in's {@linkplain Activator#getItemProviderAdapterFactory() shared adapter factory} as my delegate factory. I do not + * suppress any components of the label. <em>Note</em> that the shared adapter factory does not need to be disposed. + */ + public DelegatingItemLabelProvider() { + this(Activator.getDefault().getItemProviderAdapterFactory()); + } + + public String getText(Object object) { + EObject element = EMFHelper.getEObject(object); + if(element == null) { + return null; + } + + // Delegate to UML2 Edit providers to get localized and inferred names where applicable + IItemLabelProvider provider = getDelegate(element); + String result = (provider == null) ? ModelUtils.getDefaultName(element) : provider.getText(element); + + if(result != null) { + // Rewrite the stereotype list and strip the metaclass qualifier, unless there is no label, in which + // case we leave the metaclass qualifier + Matcher m = UML2_LABEL_PATTERN.matcher(result); + if(m.matches()) { + StringBuilder buf = new StringBuilder(); + + String keywords = m.group(1); + if(isShowStereotypes() && (keywords != null)) { + buf.append(ST_LEFT).append(keywords).append(ST_RIGHT); + } + + final String label = m.group(3); + if(isShowMetaclass() && (UML2Util.isEmpty(label) || shouldShowMetaclass(element))) { + // Use the metaclass qualifier + if(buf.length() > 0) { + buf.append(' '); + } + buf.append(m.group(2)); + } + + if(isShowLabel() && !UML2Util.isEmpty(label)) { + if(buf.length() > 0) { + buf.append(' '); + } + buf.append(label); + } + + result = buf.toString(); + } + } + + return result; + } + + public Object getImage(Object object) { + EObject element = EMFHelper.getEObject(object); + if(element == null) { + return null; + } + + // Delegate to UML2 Edit providers to get localized and inferred names where applicable + IItemLabelProvider provider = getDelegate(element); + return (provider == null) ? null : provider.getImage(element); + } + + public final boolean isShowLabel() { + return (style & SHOW_LABEL) == SHOW_LABEL; + } + + public final boolean isShowMetaclass() { + return (style & SHOW_METACLASS) == SHOW_METACLASS; + } + + public final boolean isShowStereotypes() { + return (style & SHOW_STEREOTYPES) == SHOW_STEREOTYPES; + } + + protected IItemLabelProvider getDelegate(EObject object) { + return (IItemLabelProvider)itemAdapterFactory.adapt(object, IItemLabelProvider.class); + } + + protected boolean shouldShowMetaclass(EObject object) { + return showMetaclassSwitch.doSwitch(object); + } + + protected UMLSwitch<Boolean> createShowMetaclassSwitch() { + return new UMLSwitch<Boolean>() { + + @Override + public Boolean defaultCase(EObject object) { + return false; + } + + // By default, show the metaclass for relationships + @Override + public Boolean caseRelationship(Relationship object) { + return true; + } + + // Except associations + @Override + public Boolean caseAssociation(Association object) { + return false; + } + + @Override + public Boolean caseTemplateParameter(TemplateParameter object) { + return true; + } + + @Override + public Boolean caseTemplateSignature(TemplateSignature object) { + return true; + } + }; + } +} diff --git a/plugins/uml/tools/org.eclipse.papyrus.uml.tools/src/org/eclipse/papyrus/uml/tools/providers/UMLLabelProvider.java b/plugins/uml/tools/org.eclipse.papyrus.uml.tools/src/org/eclipse/papyrus/uml/tools/providers/UMLLabelProvider.java index 09eb9ee93ec..a0b55905904 100644 --- a/plugins/uml/tools/org.eclipse.papyrus.uml.tools/src/org/eclipse/papyrus/uml/tools/providers/UMLLabelProvider.java +++ b/plugins/uml/tools/org.eclipse.papyrus.uml.tools/src/org/eclipse/papyrus/uml/tools/providers/UMLLabelProvider.java @@ -1,6 +1,5 @@ /*****************************************************************************
- * Copyright (c) 2014 CEA LIST.
- *
+ * Copyright (c) 2014 CEA LIST and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -9,12 +8,15 @@ *
* Contributors:
* Patrick Tessier (CEA LIST) - Initial API and implementation
+ * Christian W. Damus (CEA) - bug 425270
+ *
/*****************************************************************************/
package org.eclipse.papyrus.uml.tools.providers;
import java.util.Iterator;
import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.papyrus.infra.emf.providers.EMFLabelProvider;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
@@ -34,7 +36,6 @@ import org.eclipse.uml2.uml.LiteralString; import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Operation;
import org.eclipse.uml2.uml.OperationTemplateParameter;
-import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageImport;
import org.eclipse.uml2.uml.PackageMerge;
import org.eclipse.uml2.uml.Parameter;
@@ -54,6 +55,8 @@ public class UMLLabelProvider extends EMFLabelProvider implements ILabelProvider /** icon for metaclass */
public static final String ICON_METACLASS = "/icons/Metaclass.gif";//$NON-NLS-1$
+ private IItemLabelProvider labelProvider = new DelegatingItemLabelProvider(DelegatingItemLabelProvider.SHOW_LABEL | DelegatingItemLabelProvider.SHOW_METACLASS);
+
/**
*
* @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
@@ -172,19 +175,11 @@ public class UMLLabelProvider extends EMFLabelProvider implements ILabelProvider return imageName + " : " + location; //$NON-NLS-1$
} else if(element instanceof PackageImport) {
- Package importedPackage = ((PackageImport)element).getImportedPackage();
- if(importedPackage == null) {
- return "<Package Import>";
- } else {
- return "<Package Import> " + importedPackage.getName();
- }
+ return labelProvider.getText(element);
} else if(element instanceof ElementImport) {
- NamedElement importedElement = ((ElementImport)element).getImportedElement();
- if(importedElement == null) {
- return "<Element Import>";
- } else {
- return "<Element Import> " + importedElement.getName();
- }
+ return labelProvider.getText(element);
+ } else if(element instanceof PackageMerge) {
+ return labelProvider.getText(element);
} else if(element instanceof NamedElement) {
if(element instanceof ValueSpecification) { // Format : [name=]value
String value = null;
@@ -215,24 +210,13 @@ public class UMLLabelProvider extends EMFLabelProvider implements ILabelProvider }
}
} else {
- return ((NamedElement)element).getName();
+ return labelProvider.getText(element);
}
} else if(element instanceof Comment) {
Comment comment = (Comment)element;
return getText(comment);
} else if(element instanceof PackageMerge) {
- String label = "PackageMerge";
- PackageMerge packageMerge = (PackageMerge)element;
- Package mergedPackage = packageMerge.getMergedPackage();
-
- if(mergedPackage != null) {
- if(mergedPackage.eIsProxy()) {
- label += " <<Package not found>>";
- } else {
- label += " " + mergedPackage.getName(); //$NON-NLS-1$
- }
- }
- return label;
+ return labelProvider.getText(element);
}
// TODO: Temporary solution for template parameters
// Note: In the class diagram, for template parameters,
@@ -290,9 +274,7 @@ public class UMLLabelProvider extends EMFLabelProvider implements ILabelProvider }
// END TODO
else if(element instanceof Element) {
- // when the element is not a NamedElement, we return its Type name
- String className = element.eClass().getName();
- return className;
+ return labelProvider.getText(element);
}
return super.getText(element);
|