diff options
| author | Pierre-Charles David | 2016-02-02 15:07:04 +0000 |
|---|---|---|
| committer | Pierre-Charles David | 2016-02-03 14:57:58 +0000 |
| commit | ea1660fa321305fa8b0d02dca159eced3236ce96 (patch) | |
| tree | 02083d86701d01f6ccb38b2d55f8e0468d3dec42 | |
| parent | c1efa7982a432a2ee2d0047be7b70690c444a9fa (diff) | |
| download | org.eclipse.sirius-ea1660fa321305fa8b0d02dca159eced3236ce96.tar.gz org.eclipse.sirius-ea1660fa321305fa8b0d02dca159eced3236ce96.tar.xz org.eclipse.sirius-ea1660fa321305fa8b0d02dca159eced3236ce96.zip | |
[482528] Replace SiriusElementFinder by the more general SiriusContext
SiriusContext can do more and without explicitly depending on GEF of GMF
types. While not finished, SiriusContext does enough to replace
SemanticElementFinder.
Bug: 482528
Change-Id: I2733c0206101cf70b95f4fe93168f8f6fef554ec
Signed-off-by: Pierre-Charles David <pierre-charles.david@obeo.fr>
4 files changed, 233 insertions, 61 deletions
diff --git a/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/SemanticElementFinder.java b/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/SemanticElementFinder.java deleted file mode 100644 index 6445fc22d1..0000000000 --- a/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/SemanticElementFinder.java +++ /dev/null @@ -1,55 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2015, 2016 Obeo. - * 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: - * Obeo - initial API and implementation - *******************************************************************************/ -package org.eclipse.sirius.ui.properties.internal; - -import org.eclipse.emf.ecore.EObject; -import org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart; -import org.eclipse.gmf.runtime.diagram.ui.editparts.GraphicalEditPart; -import org.eclipse.gmf.runtime.notation.Edge; -import org.eclipse.sirius.viewpoint.DSemanticDecorator; - -/** - * Helper to identify the semantic element associated to a Sirius representation - * element. - * - * @author pcdavid - */ -public final class SemanticElementFinder { - /** - * The constructor. - */ - private SemanticElementFinder() { - // Prevent instantiation - } - - /** - * Returns the semantic element associated with the given selection. - * - * @param selection - * The selection - * @return The semantic element found or <code>null</code> otherwise - */ - public static EObject getAssociatedSemanticElement(Object selection) { - EObject semanticElement = null; - if (selection instanceof GraphicalEditPart) { - semanticElement = ((GraphicalEditPart) selection).resolveSemanticElement(); - } else if (selection instanceof ConnectionEditPart) { - semanticElement = ((Edge) ((ConnectionEditPart) selection).getModel()).getElement(); - } else if (selection instanceof DSemanticDecorator) { - semanticElement = ((DSemanticDecorator) selection).getTarget(); - } - if (semanticElement instanceof DSemanticDecorator) { - return ((DSemanticDecorator) semanticElement).getTarget(); - } else { - return semanticElement; - } - } -} diff --git a/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/SiriusContext.java b/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/SiriusContext.java new file mode 100644 index 0000000000..3a957859bf --- /dev/null +++ b/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/SiriusContext.java @@ -0,0 +1,228 @@ +/******************************************************************************* + * Copyright (c) 2016 Obeo. + * 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: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.ui.properties.internal; + +import java.util.Collection; + +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.sirius.business.api.query.EObjectQuery; +import org.eclipse.sirius.business.api.session.Session; +import org.eclipse.sirius.business.api.session.SessionManager; +import org.eclipse.sirius.ext.base.Option; +import org.eclipse.sirius.ext.base.Options; +import org.eclipse.sirius.viewpoint.DRepresentation; +import org.eclipse.sirius.viewpoint.DRepresentationElement; +import org.eclipse.sirius.viewpoint.DSemanticDecorator; + +/** + * Provides contextual information to clients about where a given element exists + * in terms of Sirius elements. Depending on the "input" element this context + * refers to, any of the context fields may be absent. + * + * @author pcdavid + * @since 4.0 + */ +public final class SiriusContext { + /** + * The original element from which the context information was computed. + */ + private Object input; + + /** + * The Sirius Session of which the input is part of, if it could be + * determined unambiguously. + */ + private final Session session; + + /** + * The Sirius representation of which the input is part of, if it could be + * determined unambiguously. + */ + private final DRepresentation representation; + + /** + * The DSemanticDecorator associated witht the input, which only makes sense + * if the input element itself is a DSemanticDecorator. + */ + private final DSemanticDecorator semanticDecorator; + + /** + * The main semantic element associated with the input, if it could be + * determined unambiguously. + */ + private final EObject mainSemanticElement; + + private SiriusContext(Object input, Session session, DRepresentation representation, DSemanticDecorator semanticDecorator, EObject mainSemanticElement) { + this.input = input; + this.session = session; + this.representation = representation; + this.semanticDecorator = semanticDecorator; + this.mainSemanticElement = mainSemanticElement; + } + + /** + * Static factory method. + * + * @param input + * the input element. + * @return the computed context. + */ + public static SiriusContext from(Object input) { + SiriusContext result = new SiriusContext(input, null, null, null, null); + if (input instanceof Session) { + result = fromSession((Session) input); + } else if (input instanceof DRepresentation) { + result = fromDRepresentation((DRepresentation) input); + } else if (input instanceof DRepresentationElement) { + result = fromDRepresentationElement((DRepresentationElement) input); + } else if (input instanceof EObject) { + result = fromUnknownEObject((EObject) input); + } else if (input instanceof IAdaptable) { + Object adapted = ((IAdaptable) input).getAdapter(EObject.class); + if (adapted instanceof EObject) { + result = from((EObject) adapted); + // Remember the original unadapted input. + result.input = input; + } + } + return result; + } + + /** + * From a session as a starting point, we can not deduce anything more + * precise. + */ + private static SiriusContext fromSession(Session session) { + return new SiriusContext(session, session, null, null, null); + } + + /** + * From a DRepresentation, we can deduce the containing Session (if we are + * inside an active one). Some representations (in practice, all current + * cases) are also DSemanticDecorator and thus have an associated semantic + * element. + */ + private static SiriusContext fromDRepresentation(DRepresentation repr) { + Session session = new EObjectQuery(repr).getSession(); + DSemanticDecorator decorator = null; + if (repr instanceof DSemanticDecorator) { + decorator = (DSemanticDecorator) repr; + } + EObject mainSemanticElement = null; + if (decorator != null) { + mainSemanticElement = decorator.getTarget(); + } + return new SiriusContext(repr, session, repr, decorator, mainSemanticElement); + } + + /** + * From a DRepresentationElement we can deduce everything, both "up" to the + * enclosing session and "down" to the associated semantic elements. + */ + private static SiriusContext fromDRepresentationElement(DRepresentationElement dre) { + Session session = new EObjectQuery(dre).getSession(); + Option<DRepresentation> repr = new EObjectQuery(dre).getRepresentation(); + return new SiriusContext(dre, session, repr.get(), dre, dre.getTarget()); + } + + /** + * From an unkown EObject, we van only deduce the session (if it is part of + * one), and maybe the semantic element of the object itself is inside one + * of the session's semantic resources. This should only be called as a last + * resort, if all other specific types of input about which we can know more + * have been exhausted. + */ + private static SiriusContext fromUnknownEObject(EObject obj) { + Session session = SessionManager.INSTANCE.getSession(obj); + // Session.getSemanticResources() only returns non-controlled resources, + // so we need to identify the top-level Resource containing the element. + Resource res = EcoreUtil.getRootContainer(obj).eResource(); + EObject semanticElement = null; + if (session != null && res != null && session.getSemanticResources().contains(res)) { + semanticElement = obj; + } + return new SiriusContext(obj, session, null, null, semanticElement); + } + + /** + * The original input element for which this context was created. + * + * @return the original input element. + */ + public Object getInput() { + return input; + } + + /** + * Returns the session to which the input element is associated. May be + * absent if the input element does not exist in the context of a Sirius + * session. + * + * @return the session to which the input element is associated. + */ + public Option<Session> getSession() { + return Options.fromNullable(session); + } + + /** + * Returns the Sirius representation to which the input element is + * associated. May be absent if the input element does not exist in the + * context of a single Sirius representation. Note for that semantic + * elements, even if they are currently represented on only one + * representation in their session, are not considered associated to it. + * + * @return the Sirius representation to which the input element is + * associated. + */ + public Option<DRepresentation> getDRepresentation() { + return Options.fromNullable(representation); + } + + /** + * Returns the {@link DSemanticDecorator} associated to the input, which is + * only available if the input is itself a {@link DSemanticDecorator}. + * + * @return the {@link DSemanticDecorator} associated to the input. + */ + public DSemanticDecorator getSemanticDecorator() { + return semanticDecorator; + } + + /** + * Returns the main semantic element associated to the input. May be absent. + * + * @return the main semantic element associated to the input. + */ + public Option<EObject> getMainSemanticElement() { + if (semanticDecorator != null) { + return Options.newSome(semanticDecorator.getTarget()); + } else { + return Options.newSome(mainSemanticElement); + } + } + + /** + * Returns the additional semantic elements associated to the input. May be + * absent. + * + * @return the additional semantic elements associated to the input. + */ + public Option<Collection<EObject>> getAdditionalSemanticElements() { + if (semanticDecorator instanceof DRepresentationElement) { + return Options.newSome((Collection<EObject>) ((DRepresentationElement) semanticDecorator).getSemanticElements()); + } else { + return Options.newNone(); + } + } +} diff --git a/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/SiriusSemanticAdapter.java b/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/SiriusSemanticAdapter.java index 402b2c0ba0..05956863b0 100644 --- a/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/SiriusSemanticAdapter.java +++ b/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/SiriusSemanticAdapter.java @@ -22,7 +22,7 @@ public class SiriusSemanticAdapter implements IAdapterFactory { @Override public Object getAdapter(final Object adaptableObject, @SuppressWarnings("rawtypes") final Class adapterType) { if (adapterType != null) { - return SemanticElementFinder.getAssociatedSemanticElement(adaptableObject); + return SiriusContext.from(adaptableObject).getMainSemanticElement().get(); } else { return null; } diff --git a/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/tabprovider/SiriusTabDescriptorProvider.java b/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/tabprovider/SiriusTabDescriptorProvider.java index d76c3a9481..72b68686c8 100644 --- a/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/tabprovider/SiriusTabDescriptorProvider.java +++ b/incubation/org.eclipse.sirius.ui.properties/src/org/eclipse/sirius/ui/properties/internal/tabprovider/SiriusTabDescriptorProvider.java @@ -37,7 +37,7 @@ import org.eclipse.sirius.ext.base.Option; import org.eclipse.sirius.properties.PageDescription; import org.eclipse.sirius.properties.ViewExtensionDescription; import org.eclipse.sirius.ui.properties.internal.Messages; -import org.eclipse.sirius.ui.properties.internal.SemanticElementFinder; +import org.eclipse.sirius.ui.properties.internal.SiriusContext; import org.eclipse.sirius.ui.properties.internal.SiriusInterpreter; import org.eclipse.sirius.ui.properties.internal.SiriusUIPropertiesPlugin; import org.eclipse.sirius.viewpoint.description.DescriptionPackage; @@ -73,11 +73,10 @@ public class SiriusTabDescriptorProvider implements IEEFTabDescriptorProvider { if (objects.length > 1) { SiriusUIPropertiesPlugin.getPlugin().warning(Messages.SiriusTabDescriptorProvider_UnsupportedMultipleSelection, null); } - - EObject semanticElement = SemanticElementFinder.getAssociatedSemanticElement(objects[0]); - if (semanticElement != null) { + SiriusContext ctx = SiriusContext.from(objects[0]); + if (ctx.getMainSemanticElement().some()) { // Let's find out the description of the view - return this.getTabDescriptors(semanticElement); + return this.getTabDescriptors(ctx.getMainSemanticElement().get()); } else { SiriusUIPropertiesPlugin.getPlugin().error(Messages.SiriusTabDescriptorProvider_UndefinedSemanticElement, null); } |
