Skip to content

Commit

Permalink
Fixed #502 Provide a way to override the 'Size of exported images' Si…
Browse files Browse the repository at this point in the history
…rius preference.
  • Loading branch information
ylussaud committed Sep 2, 2024
1 parent 050e69d commit b2d2a0f
Show file tree
Hide file tree
Showing 27 changed files with 3,492 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/*******************************************************************************
* Copyright (c) 2017, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.obeonetwork.m2doc.sirius;

/**
Expand All @@ -22,6 +32,16 @@ public final class M2DocSiriusUtils {
*/
public static final String SIRIUS_FORCE_REFRESH = "SiriusForceRefresh";

/**
* The Sirius scale policy option.
*/
public static final String SIRIUS_SCALING_POLICY = "SiriusScalingPolicy";

/**
* The Sirius scale level option.
*/
public static final String SIRIUS_SCALE_LEVEL = "SiriusScaleLevel";

/**
* Constructor.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.eclipse.sirius.table.metamodel.table.description.TableDescription;
import org.eclipse.sirius.ui.business.api.dialect.DialectUIManager;
import org.eclipse.sirius.ui.business.api.dialect.ExportFormat;
import org.eclipse.sirius.ui.business.api.dialect.ExportFormat.ScalingPolicy;
import org.eclipse.sirius.ui.tools.api.actions.export.SizeTooLargeException;
import org.eclipse.sirius.viewpoint.DRepresentation;
import org.eclipse.sirius.viewpoint.DRepresentationDescriptor;
Expand Down Expand Up @@ -73,6 +74,9 @@ public class M2DocSiriusServices {
*/
private static final String JPG = "JPG";

/**
* The {@link Set} of valid {@link ImageFileFormat} {@link ImageFileFormat#getName() names}.
*/
private static final Set<String> VALID_IMAGE_FORMATS = initializeValidImageFormats();

/**
Expand Down Expand Up @@ -131,6 +135,16 @@ public void clean(Object key) {
*/
private final boolean forceRefresh;

/**
* The {@link ScalingPolicy} for diagram image export.
*/
private final ScalingPolicy scalingPolicy;

/**
* The scale level for the diagram image export.
*/
private final Integer scaleLevel;

/**
* The {@link CleaningJobRegistry}.
*/
Expand Down Expand Up @@ -168,8 +182,26 @@ public void clean(Object key) {
*
* @param session
* the Sirius {@link Session}
* @param forceRefresh
* for the refresh of diagarms before image export
*/
public M2DocSiriusServices(Session session, boolean forceRefresh) {
this(session, forceRefresh, ScalingPolicy.WORKSPACE_DEFAULT, null);
}

/**
* Constructor.
*
* @param session
* the Sirius {@link Session}
* @param forceRefresh
* for the refresh of diagarms before image export
* @param scalingPolicy
* the {@link ScalingPolicy} for diagram image export
* @param scaleLevel
* the scale level for diagram image export
*/
public M2DocSiriusServices(Session session, boolean forceRefresh, ScalingPolicy scalingPolicy, Integer scaleLevel) {
this.session = session;
this.uriConverter = session.getTransactionalEditingDomain().getResourceSet().getURIConverter();
this.forceRefresh = forceRefresh;
Expand All @@ -179,6 +211,8 @@ public M2DocSiriusServices(Session session, boolean forceRefresh) {
} else {
shouldCloseSession = false;
}
this.scalingPolicy = scalingPolicy;
this.scaleLevel = scaleLevel;
}

private static Set<String> initializeValidImageFormats() {
Expand Down Expand Up @@ -492,7 +526,7 @@ protected MImage internalAsImage(final DRepresentation representation, String fo
throw new IllegalArgumentException(format + " is not a valide format: " + validFormatString);
}
final ExportFormat exportFormat = new ExportFormat(ExportFormat.ExportDocumentFormat.NONE,
ImageFileFormat.resolveImageFormat(format));
ImageFileFormat.resolveImageFormat(format), scalingPolicy, scaleLevel);
final File tmpFile = File.createTempFile(sanitize(representation.getName()) + "-m2doc",
"." + format.toLowerCase());
tmpFiles.add(tmpFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017, 2023 Obeo.
* Copyright (c) 2017, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand All @@ -11,6 +11,7 @@
package org.obeonetwork.m2doc.sirius.services.configurator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -43,6 +44,7 @@
import org.eclipse.sirius.business.api.session.Session;
import org.eclipse.sirius.business.api.session.SessionManager;
import org.eclipse.sirius.business.internal.session.SessionTransientAttachment;
import org.eclipse.sirius.ui.business.api.dialect.ExportFormat.ScalingPolicy;
import org.eclipse.ui.PlatformUI;
import org.obeonetwork.m2doc.genconf.GenconfUtils;
import org.obeonetwork.m2doc.services.configurator.IServicesConfigurator;
Expand Down Expand Up @@ -92,6 +94,8 @@ private static List<String> initOptions() {

res.add(M2DocSiriusUtils.SIRIUS_SESSION_OPTION);
res.add(M2DocSiriusUtils.SIRIUS_FORCE_REFRESH);
res.add(M2DocSiriusUtils.SIRIUS_SCALING_POLICY);
res.add(M2DocSiriusUtils.SIRIUS_SCALE_LEVEL);

return res;
}
Expand Down Expand Up @@ -239,7 +243,20 @@ public Set<IService> getServices(IReadOnlyQueryEnvironment queryEnvironment, Res
if (URIConverter.INSTANCE.exists(sessionURI, Collections.emptyMap())) {
final Session session = SessionManager.INSTANCE.getSession(sessionURI, new NullProgressMonitor());
final boolean forceRefresh = Boolean.valueOf(options.get(M2DocSiriusUtils.SIRIUS_FORCE_REFRESH));
final M2DocSiriusServices serviceInstance = new M2DocSiriusServices(session, forceRefresh);
final ScalingPolicy scalingPolicy;
if (options.containsKey(M2DocSiriusUtils.SIRIUS_SCALING_POLICY)) {
scalingPolicy = ScalingPolicy.valueOf(options.get(M2DocSiriusUtils.SIRIUS_SCALING_POLICY));
} else {
scalingPolicy = ScalingPolicy.WORKSPACE_DEFAULT;
}
final Integer scaleLevel;
if (options.containsKey(M2DocSiriusUtils.SIRIUS_SCALE_LEVEL)) {
scaleLevel = Integer.valueOf(options.get(M2DocSiriusUtils.SIRIUS_SCALE_LEVEL));
} else {
scaleLevel = null;
}
final M2DocSiriusServices serviceInstance = new M2DocSiriusServices(session, forceRefresh,
scalingPolicy, scaleLevel);
res.addAll(ServiceUtils.getServices(queryEnvironment, serviceInstance));
services.put(queryEnvironment, serviceInstance);
}
Expand Down Expand Up @@ -285,7 +302,7 @@ public Map<String, List<Diagnostic>> validate(IReadOnlyQueryEnvironment queryEnv
"The Sirius session doesn't exist: " + sessionURI.toString(), new Object[] {sessionURI}));
}
}
final String forceRefreshStr = options.get(M2DocSiriusUtils.SIRIUS_SESSION_OPTION);
final String forceRefreshStr = options.get(M2DocSiriusUtils.SIRIUS_FORCE_REFRESH);
if (forceRefreshStr != null) {
final List<Diagnostic> diagnostics = new ArrayList<>();
res.put(M2DocSiriusUtils.SIRIUS_FORCE_REFRESH, diagnostics);
Expand All @@ -296,6 +313,44 @@ public Map<String, List<Diagnostic>> validate(IReadOnlyQueryEnvironment queryEnv
new Object[] {forceRefreshStr}));
}
}
final String scalePolicyStr = options.get(M2DocSiriusUtils.SIRIUS_SCALING_POLICY);
if (!isValidScalePolicy(scalePolicyStr)) {
final List<Diagnostic> diagnostics = new ArrayList<>();
res.put(M2DocSiriusUtils.SIRIUS_SCALING_POLICY, diagnostics);
diagnostics.add(new BasicDiagnostic(
Diagnostic.ERROR, M2DocSiriusUtils.PLUGIN_ID, 0, "The Sirius scale policy must be one of "
+ Arrays.toString(ScalingPolicy.values()) + ": " + scalePolicyStr,
new Object[] {scalePolicyStr}));
}
final String scaleLevelStr = options.get(M2DocSiriusUtils.SIRIUS_SCALE_LEVEL);
try {
Integer.valueOf(scaleLevelStr);
} catch (NumberFormatException e) {
final List<Diagnostic> diagnostics = new ArrayList<>();
res.put(M2DocSiriusUtils.SIRIUS_SCALE_LEVEL, diagnostics);
diagnostics.add(new BasicDiagnostic(Diagnostic.ERROR, M2DocSiriusUtils.PLUGIN_ID, 0,
"The Sirius scale level must be an integer: " + scalePolicyStr, new Object[] {scaleLevelStr}));
}

return res;
}

/**
* Tells if the given scale policy {@link String} is a valid {@link ScalingPolicy}.
*
* @param scalePolicyStr
* the scale policy {@link String}
* @return <code>true</code> if the given scale policy {@link String} is a valid {@link ScalingPolicy}, <code>false</code> otherwise
*/
private boolean isValidScalePolicy(String scalePolicyStr) {
boolean res = false;

for (ScalingPolicy policy : ScalingPolicy.values()) {
if (policy.name().equals(scalePolicyStr)) {
res = true;
break;
}
}

return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Bundle-Vendor: %providerName
Bundle-Localization: plugin
Require-Bundle: org.junit,
org.obeonetwork.m2doc.sirius;bundle-version="[3.0.0,4.0.0)",
org.eclipse.sirius.ui;bundle-version="[6.5.1,7.5.0)",
org.obeonetwork.m2doc;bundle-version="[3.0.0,4.0.0)",
org.eclipse.core.runtime,
org.obeonetwork.m2doc.genconf;bundle-version="[3.0.0,4.0.0)",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="anydsl" nsURI="http://www.eclipse.org/acceleo/anydsl" nsPrefix="anydsl">
<eClassifiers xsi:type="ecore:EClass" name="World">
<eAnnotations source="http://www.obeo.fr/dsl/dnc/archetype">
<details key="archetype" value="MomentInterval"/>
</eAnnotations>
<eStructuralFeatures xsi:type="ecore:EReference" name="companies" upperBound="-1"
eType="#//Company" containment="true"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="foods" upperBound="-1"
eType="#//Food" containment="true"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="sources" upperBound="-1"
eType="#//Source" containment="true"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="MultiNamedElement" abstract="true" interface="true">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="name" upperBound="-1" eType="#//SingleString"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="NamedElement" abstract="true" interface="true">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="#//SingleString"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Producer" eSuperTypes="#//NamedElement">
<eStructuralFeatures xsi:type="ecore:EReference" name="adress" eType="#//Adress"
containment="true"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="company" eType="#//Company"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="foods" upperBound="-1"
eType="#//Food" eOpposite="#//Food/producers"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Adress">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="zipCode" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="city" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="country" eType="#//CountryData"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Company" abstract="true" interface="true"
eSuperTypes="#//NamedElement">
<eStructuralFeatures xsi:type="ecore:EReference" name="adress" eType="#//Adress"
containment="true"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="world" eType="#//World"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="ProductionCompany" eSuperTypes="#//Company">
<eStructuralFeatures xsi:type="ecore:EReference" name="producers" upperBound="-1"
eType="#//Producer" containment="true"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Restaurant" eSuperTypes="#//Company">
<eStructuralFeatures xsi:type="ecore:EReference" name="chefs" upperBound="-1"
eType="#//Chef" containment="true"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="menu" upperBound="-1" eType="#//EStringToRecipeMap"
containment="true"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Chef" eSuperTypes="#//NamedElement">
<eStructuralFeatures xsi:type="ecore:EReference" name="adress" eType="#//Adress"
containment="true"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="recipes" upperBound="-1"
eType="#//Recipe" containment="true"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Recipe" eSuperTypes="#//NamedElement">
<eStructuralFeatures xsi:type="ecore:EReference" name="ingredients" upperBound="-1"
eType="#//Food"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Food" eSuperTypes="#//NamedElement">
<eOperations name="ripen" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EBoolean">
<eParameters name="color" eType="#//Color"/>
</eOperations>
<eOperations name="preferredColor" eType="#//Color"/>
<eOperations name="newFood" eType="#//Food"/>
<eOperations name="setColor">
<eParameters name="food" eType="#//Food"/>
<eParameters name="newColor" eType="#//Color"/>
</eOperations>
<eOperations name="setCaliber">
<eParameters name="food" eType="#//Food"/>
<eParameters name="newCaliber" upperBound="-1" eType="#//Caliber"/>
</eOperations>
<eOperations name="acceptedCaliber" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EBoolean">
<eParameters name="caliber" eType="#//Caliber"/>
</eOperations>
<eOperations name="label">
<eParameters name="text" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eOperations>
<eOperations name="preferredLabel" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString">
<eParameters name="text" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eOperations>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="color" upperBound="-1"
eType="#//Color"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="caliber" eType="#//Caliber"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="relatedFoods" upperBound="-1"
eType="#//Food"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="group" eType="#//Group"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="label" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="source" eType="#//Source"
eOpposite="#//Source/foods"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="producers" upperBound="-1"
eType="#//Producer" eOpposite="#//Producer/foods"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Source" abstract="true" interface="true"
eSuperTypes="#//MultiNamedElement">
<eStructuralFeatures xsi:type="ecore:EReference" name="foods" upperBound="-1"
eType="#//Food" eOpposite="#//Food/source"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="origin" upperBound="-1"
eType="#//CountryData"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Plant" eSuperTypes="#//Source">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="kind" eType="#//Kind"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Animal" eSuperTypes="#//Source">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="part" upperBound="-1" eType="#//Part"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EEnum" name="Color">
<eLiterals name="black"/>
<eLiterals name="red" value="1"/>
<eLiterals name="green" value="2"/>
<eLiterals name="yellow" value="3"/>
<eLiterals name="orange" value="4"/>
<eLiterals name="brown" value="5"/>
<eLiterals name="pink" value="6"/>
<eLiterals name="palPink" value="7" literal="palPink"/>
<eLiterals name="veryYellow" value="8"/>
<eLiterals name="white" value="9"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EEnum" name="Caliber">
<eLiterals name="S"/>
<eLiterals name="M" value="1"/>
<eLiterals name="L" value="2"/>
<eLiterals name="XL" value="3"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EEnum" name="Group">
<eLiterals name="Water"/>
<eLiterals name="Dairy" value="1"/>
<eLiterals name="Fruit" value="2"/>
<eLiterals name="Grain" value="3"/>
<eLiterals name="Protein" value="4"/>
<eLiterals name="Sweet" value="5"/>
<eLiterals name="Vegetable" value="6"/>
<eLiterals name="Alcohol" value="7"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EEnum" name="Continent">
<eLiterals name="Europe"/>
<eLiterals name="Asia" value="1"/>
<eLiterals name="Africa" value="2"/>
<eLiterals name="America" value="3"/>
<eLiterals name="Australia" value="4"/>
<eLiterals name="Antarctica" value="5"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EEnum" name="Kind">
<eLiterals name="Other"/>
<eLiterals name="Seed" value="1"/>
<eLiterals name="Oilseed" value="2"/>
<eLiterals name="Tree" value="3"/>
<eLiterals name="Root" value="4"/>
<eLiterals name="Bulb" value="5"/>
<eLiterals name="Leaf" value="6"/>
<eLiterals name="Stem" value="7"/>
<eLiterals name="Flower" value="8"/>
<eLiterals name="Inflorescence" value="9"/>
<eLiterals name="Spice" value="10"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EEnum" name="Part">
<eLiterals name="Other"/>
<eLiterals name="Muscle" value="1"/>
<eLiterals name="Organ" value="2"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EDataType" name="CountryData" instanceClassName="anydsl.Country"/>
<eClassifiers xsi:type="ecore:EDataType" name="SingleString" instanceClassName="java.lang.String"/>
<eClassifiers xsi:type="ecore:EClass" name="EStringToRecipeMap" instanceClassName="java.util.Map$Entry">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="key" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
<eStructuralFeatures xsi:type="ecore:EReference" name="value" eType="#//Recipe"/>
</eClassifiers>
</ecore:EPackage>
Loading

0 comments on commit b2d2a0f

Please sign in to comment.