blob: e3f1ad92a24db35ae15c5e63e75f2382366aa63f [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2011, 2014 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:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.diagram.ui.business.internal.dialect.description;
import java.util.Collection;
import java.util.Map;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.sirius.business.api.dialect.description.AbstractInterpretedExpressionQuery;
import org.eclipse.sirius.business.api.dialect.description.DefaultInterpretedExpressionTargetSwitch;
import org.eclipse.sirius.business.api.dialect.description.IInterpretedExpressionQuery;
import org.eclipse.sirius.business.api.dialect.description.IInterpretedExpressionTargetSwitch;
import org.eclipse.sirius.diagram.business.api.diagramtype.DiagramTypeDescriptorRegistry;
import org.eclipse.sirius.diagram.business.api.diagramtype.IDiagramTypeDescriptor;
import org.eclipse.sirius.diagram.description.DiagramExtensionDescription;
import org.eclipse.sirius.diagram.description.EdgeMappingImport;
import org.eclipse.sirius.diagram.description.tool.DirectEditLabel;
import org.eclipse.sirius.ext.base.Option;
import org.eclipse.sirius.ext.base.Options;
import org.eclipse.sirius.viewpoint.description.RepresentationDescription;
import org.eclipse.sirius.viewpoint.description.RepresentationElementMapping;
import org.eclipse.sirius.viewpoint.description.tool.EditMaskVariables;
import com.google.common.base.Function;
import com.google.common.collect.Sets;
/**
* Query allowing to get the target domain classes and available packages for a
* given Interpreted expression. This diagram query will treat all generic
* description elements and those related to the diagram concept.
*
* @author <a href="mailto:alex.lagarde@obeo.fr">Alex Lagarde</a>
*
*/
public class DiagramInterpretedExpressionQuery extends AbstractInterpretedExpressionQuery implements IInterpretedExpressionQuery {
/**
* Default constructor.
*
* @param target
* the target containing the InterpretedExpression (NodeMapping,
* ModelOperation...)
* @param feature
* the feature corresponding to the InterpretedExpression to
* evaluate ( NodeMapping.semanticCandidatesExpression...)
*/
public DiagramInterpretedExpressionQuery(EObject target, EStructuralFeature feature) {
super(target, feature);
}
/**
*
* {@inheritDoc}
*
* @see org.eclipse.sirius.business.api.dialect.description.AbstractInterpretedExpressionQuery#initializeTargetSwitch()
*/
@Override
protected void initializeTargetSwitch() {
targetSwitch = new DiagramGlobalInterpretedTargetSwitch();
}
/**
* {@inheritDoc}
*/
@Override
protected void appendAllLocalVariableDefinitions(Map<String, Collection<String>> definitions, EObject context) {
super.appendAllLocalVariableDefinitions(definitions, context);
// Direct edit defines numbered variables based on their mask.
if (context instanceof DirectEditLabel && ((DirectEditLabel) context).getMask() != null) {
EditMaskVariables emv = ((DirectEditLabel) context).getMask();
appendEditMaskVariables(emv, definitions);
}
}
/**
* An {@link IInterpretedExpressionTargetSwitch} that delegates to the
* defaultSwitch or the diagram specific switch, according to the package of
* the considered element.
*
* @author <a href="mailto:alex.lagarde@obeo.fr">Alex Lagarde</a>
*
*/
private class DiagramGlobalInterpretedTargetSwitch implements IInterpretedExpressionTargetSwitch {
private final Function<EObject, EObject> diagramFirstRelevantContainerFinder = new FirstRelevantContainerDiagramFunction();
private DefaultInterpretedExpressionTargetSwitch defaultSwitch = new DefaultInterpretedExpressionTargetSwitch(feature, this, diagramFirstRelevantContainerFinder);
private DiagramInterpretedExpressionTargetSwitch diagramDescriptionSwitch = new DiagramInterpretedExpressionTargetSwitch(feature, this);
private DiagramStyleInterpretedExpressionTargetSwitch diagramStyleSwitch = new DiagramStyleInterpretedExpressionTargetSwitch(feature, this);
private DiagramToolInterpretedExpressionTargetSwitch diagramToolSwitch = new DiagramToolInterpretedExpressionTargetSwitch(feature, this);
private FilterInterpretedExpressionTargetSwitch diagramFilterSwitch = new FilterInterpretedExpressionTargetSwitch(feature, this);
/**
*
* {@inheritDoc}
*
* @see org.eclipse.sirius.business.api.dialect.description.IInterpretedExpressionTargetSwitch#doSwitch(org.eclipse.emf.ecore.EObject)
*/
public Option<Collection<String>> doSwitch(EObject target, boolean considerFeature) {
Collection<String> targetTypes = Sets.newLinkedHashSet();
Option<Collection<String>> expressionTarget = Options.newSome(targetTypes);
if (target != null) {
// Step 1: trying to apply any contributed switch that matches
// the given target's EPackage
for (final IDiagramTypeDescriptor diagramTypeDescriptor : DiagramTypeDescriptorRegistry.getInstance().getAllDiagramTypeDescriptors()) {
if (diagramTypeDescriptor.getDiagramDescriptionProvider().handles(target.eClass().getEPackage())) {
IInterpretedExpressionTargetSwitch contributedSwitch = diagramTypeDescriptor.getDiagramDescriptionProvider().createInterpretedExpressionSwitch(feature, this);
if (contributedSwitch != null) {
expressionTarget = contributedSwitch.doSwitch(target, considerFeature);
}
}
}
// If no result has been found
if (expressionTarget.some() && expressionTarget.get().isEmpty()) {
// Step 2: apply the Diagram description specific switch
diagramDescriptionSwitch.setConsiderFeature(considerFeature);
expressionTarget = diagramDescriptionSwitch.doSwitch(target);
}
// If no result has been found
if (expressionTarget.some() && expressionTarget.get().isEmpty()) {
// Step 3: apply the Diagram style specific switch
diagramStyleSwitch.setConsiderFeature(considerFeature);
expressionTarget = diagramStyleSwitch.doSwitch(target);
}
// If no result has been found
if (expressionTarget.some() && expressionTarget.get().isEmpty()) {
// Step 4: apply the Diagram tool specific switch
diagramToolSwitch.setConsiderFeature(considerFeature);
expressionTarget = diagramToolSwitch.doSwitch(target);
}
// If no result has been found
if (expressionTarget.some() && expressionTarget.get().isEmpty()) {
// Step 5: apply the Diagram filter specific switch
diagramFilterSwitch.setConsiderFeature(considerFeature);
expressionTarget = diagramFilterSwitch.doSwitch(target);
}
// If no result has been found
if (expressionTarget.some() && expressionTarget.get().isEmpty()) {
// Step 7 : we use the default switch
expressionTarget = defaultSwitch.doSwitch(target, considerFeature);
}
}
return expressionTarget;
}
@Override
public Function<EObject, EObject> getFirstRelevantContainerFinder() {
return diagramFirstRelevantContainerFinder;
}
}
/**
* Returns the first relevant for the given EObject, i.e. the first
* container from which a domain class can be determined.
* <p>
* For example, for a given NodeMapping will return the first
* ContainerMapping or DiagramRepresentationDescription that contains this
* mapping.
* </p>
*/
private static class FirstRelevantContainerDiagramFunction implements Function<EObject, EObject> {
/**
* {@inheritDoc}
*/
@Override
public EObject apply(EObject input) {
EObject container = input.eContainer();
while ((!(container instanceof RepresentationDescription)) && (!(container instanceof RepresentationElementMapping)) && (!(container instanceof EdgeMappingImport))
&& (!(container instanceof DiagramExtensionDescription))) {
container = container.eContainer();
}
return container;
}
}
}