Skip to content

Commit

Permalink
#2644 Add quick fix to remove image not found for diagram elements
Browse files Browse the repository at this point in the history
Signed-off-by: Séraphin Costa <seraphin.costa@obeosoft.com>
  • Loading branch information
scosta-obeo authored and pdulth committed Jun 29, 2023
1 parent bb7da0c commit a27e776
Show file tree
Hide file tree
Showing 10 changed files with 373 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ Require-Bundle: org.polarsys.capella.core.validation,
org.polarsys.capella.core.diagram.helpers;bundle-version="7.0.0",
org.polarsys.kitalpha.richtext.widget.ext,
org.polarsys.kitalpha.richtext.widget.tools,
org.apache.commons.lang
org.apache.commons.lang,
org.eclipse.sirius.ext.base,
org.eclipse.emf.common
Export-Package: org.polarsys.capella.core.platform.sirius.sirius.validation.ddiagram,
org.polarsys.capella.core.platform.sirius.sirius.validation.parser.helper
Bundle-ActivationPolicy: lazy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ quickFix.ImagePath_Resolver.label = Select a new image
quickFix.ImagePath_Resolver.desc = Select a new image to replace the current non reachable one

quickFix.ImagePath_RemoveResolver.label = Remove missing image
quickFix.ImagePath_RemoveResolver.desc = Remove missing image from rich text description
quickFix.ImagePath_RemoveResolver.descRichText = Remove missing image from rich text description
quickFix.ImagePath_RemoveResolver.descDiagram = Remove broken reference to image from diagram

quickFix.ImagePath_MassResolver.label = Change all image paths
quickFix.ImagePath_MassResolver.desc = Replace the first segments of image paths by new ones for diagram nodes and rich text description.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,15 @@
</resolver>
<resolver
class="org.polarsys.capella.core.platform.sirius.sirius.validation.ddiagram.ImagePathRemoveResolver"
desc="%quickFix.ImagePath_RemoveResolver.desc"
desc="%quickFix.ImagePath_RemoveResolver.descDiagram"
label="%quickFix.ImagePath_RemoveResolver.label">
<rules
ruleId="I_46">
</rules>
</resolver>
<resolver
class="org.polarsys.capella.core.platform.sirius.sirius.validation.ddiagram.ImagePathRemoveResolver"
desc="%quickFix.ImagePath_RemoveResolver.descRichText"
label="%quickFix.ImagePath_RemoveResolver.label">
<rules
ruleId="I_47">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*******************************************************************************
* Copyright (c) 2022, 2023 THALES GLOBAL SERVICES.
*
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
*
* SPDX-License-Identifier: EPL-2.0
*
*
* Contributors:
* Thales - initial API and implementation
*******************************************************************************/
Expand All @@ -18,15 +18,24 @@
import org.apache.log4j.Logger;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.sirius.business.api.query.EObjectQuery;
import org.eclipse.sirius.business.api.session.Session;
import org.eclipse.sirius.common.tools.api.resource.FileProvider;
import org.eclipse.sirius.diagram.DDiagram;
import org.eclipse.sirius.diagram.DDiagramElement;
import org.eclipse.sirius.diagram.tools.internal.validation.constraints.ImagePathWrappingStatus;
import org.eclipse.sirius.diagram.tools.internal.validation.constraints.ImagePathWrappingStatus.ImagePathTarget;
import org.eclipse.sirius.diagram.ui.internal.quickfix.ImageMarkerRemoveResolution;
import org.eclipse.sirius.ext.base.Option;
import org.eclipse.sirius.ui.business.api.dialect.DialectUIManager;
import org.eclipse.sirius.viewpoint.DRepresentation;
import org.eclipse.sirius.viewpoint.DSemanticDecorator;
import org.polarsys.capella.common.helpers.validation.ConstraintStatusDiagnostic;
import org.polarsys.capella.common.tools.report.appenders.reportlogview.MarkerViewHelper;
import org.polarsys.capella.common.tools.report.config.registry.ReportManagerRegistry;
Expand All @@ -36,65 +45,50 @@
@SuppressWarnings("restriction")
public class ImagePathRemoveResolver extends AbstractCapellaMarkerResolution {

protected Logger _logger = ReportManagerRegistry.getInstance().subscribe(IReportManagerDefaultComponents.VALIDATION);
protected Logger _logger = ReportManagerRegistry.getInstance().subscribe(IReportManagerDefaultComponents.VALIDATION);

public boolean removeImagePathInRichText(EObject eObject, String featurename, String imagePath,
TransactionalEditingDomain ted) {
boolean fixSucceeded = false;
Optional<EAttribute> eAttributeOpt = eObject.eClass().getEAllAttributes().stream()
.filter(attr -> featurename.equals(attr.getName())).findFirst();
if (eAttributeOpt.isPresent()) {
EAttribute eAttribute = eAttributeOpt.get();
Object stringObj = eObject.eGet(eAttribute);
if (stringObj instanceof String) {
String htmlText = (String) stringObj;
String imgRegex = "<img\\s+[^>]*?src\\s*=\\s*(\"|')" + imagePath + "(\"|')[^>]*?>"; //$NON-NLS-1$
ted.getCommandStack().execute(new RecordingCommand(ted) {
@Override
protected void doExecute() {
String newHtmlText = htmlText.replaceAll(imgRegex, "");
eObject.eSet(eAttribute, newHtmlText);
}
});
fixSucceeded = true;
}
}
;
return fixSucceeded;
}
@Override
public void run(IMarker marker_p) {
final List<EObject> modelElements = getModelElements(marker_p);
if (modelElements.size() < 1)
return;
EObject target = modelElements.get(0);
Optional<Session> sessionOpt = Session.of(target);

@Override
public void run(IMarker marker_p) {
final List<EObject> modelElements = getModelElements(marker_p);
if (modelElements.size() < 1)
return;
EObject target = modelElements.get(0);
Optional<Session> sessionOpt = Session.of(target);
Diagnostic diagnostic = MarkerViewHelper.getDiagnostic(marker_p);
ImagePathWrappingStatus imagePathStatus = null;
if (diagnostic instanceof ConstraintStatusDiagnostic && ((ConstraintStatusDiagnostic) diagnostic).getConstraintStatus() instanceof ImagePathWrappingStatus) {
imagePathStatus = (ImagePathWrappingStatus) ((ConstraintStatusDiagnostic) diagnostic).getConstraintStatus();
}

Diagnostic diagnostic = MarkerViewHelper.getDiagnostic(marker_p);
ImagePathWrappingStatus imagePathStatus = null;
if (diagnostic instanceof ConstraintStatusDiagnostic
&& ((ConstraintStatusDiagnostic) diagnostic).getConstraintStatus() instanceof ImagePathWrappingStatus) {
imagePathStatus = (ImagePathWrappingStatus) ((ConstraintStatusDiagnostic) diagnostic).getConstraintStatus();
}
if (imagePathStatus != null && sessionOpt.isPresent()) {
String notReachableImagePath = imagePathStatus.getNotReachableImagePath();
TransactionalEditingDomain ted = sessionOpt.get().getTransactionalEditingDomain();
boolean fixSucceeded = FileProvider.getDefault().exists(new Path(notReachableImagePath), sessionOpt.get());

if (imagePathStatus != null && sessionOpt.isPresent()) {
String notReachableImagePath = imagePathStatus.getNotReachableImagePath();
TransactionalEditingDomain ted = sessionOpt.get().getTransactionalEditingDomain();
boolean fixSucceeded = FileProvider.getDefault().exists(new Path(notReachableImagePath), sessionOpt.get());
if (!fixSucceeded) {
ImagePathTarget imagePathTarget = imagePathStatus.getImagePathTarget();
EAttribute eAttribute = imagePathStatus.getEAttribute();

if (!fixSucceeded) {
EAttribute eAttribute = imagePathStatus.getEAttribute();
fixSucceeded = removeImagePathInRichText(target, eAttribute.getName(), notReachableImagePath, ted);
}
if (imagePathTarget.equals(ImagePathTarget.WORKSPACE_IMAGE) && target instanceof DSemanticDecorator) {
Option<DRepresentation> dRepresention = new EObjectQuery(target).getRepresentation();
if (dRepresention.some() && dRepresention.get() instanceof DDiagram) {
DDiagram dDiagram = (DDiagram) dRepresention.get();
DialectUIManager.INSTANCE.openEditor(sessionOpt.get(), dDiagram, new NullProgressMonitor());
fixSucceeded = ImageMarkerRemoveResolution.removeImageInDiagram(dDiagram, (DDiagramElement) target, ted);
}
} else {
fixSucceeded = ImageMarkerRemoveResolution.removeImagePathInRichText(target, eAttribute.getName(), notReachableImagePath, ted);
}
}

if (fixSucceeded) {
try {
marker_p.delete();
} catch (CoreException exception_p) {
_logger.error("Exception while deleting marker : " + exception_p.toString()); //$NON-NLS-1$
if (fixSucceeded) {
try {
marker_p.delete();
} catch (CoreException exception_p) {
_logger.error("Exception while deleting marker : " + exception_p.toString()); //$NON-NLS-1$
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,11 @@ <h1 id="Integrity">Integrity</h1>
<img title="WARNING" alt="WARNING" border="0" src="../../Images/warning.gif"/> This rule applies on diagrams elements of diagram associated to the Capella element and will load the diagram.
<p>It is recommended to uncheck this rule by default.</p>
<p>This rule ensures that images, used as background of diagram elements, can be found.</p>
<p>Tip: If some cases are detected (validation markers), it is recommended to check the presence of the image and move it in the right folder according to the used expected image path location. Once done, you can restart the validation. If you have still validation markers, then use the "Quick fix" tool to select a new image.</p>
<p>Tip: If some cases are detected (validation markers), it is recommended to check the presence of the image and move it in the right folder according to the used expected image path location. Once done, you can restart the validation. If you have still validation markers, then, you can use the "Quick fix" tool to:
<ul>
<li>Select a new image</li>
<li>Remove broken image link from diagram element or restore default image of diagram element</li>
</ul></p>
</td>
</tr>
</table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,11 @@ It is recommended to uncheck this rule by default.

This rule ensures that images, used as background of diagram elements, can be found.

Tip: If some cases are detected (validation markers), it is recommended to check the presence of the image and move it in the right folder according to the used expected image path location. Once done, you can restart the validation. If you have still validation markers, then use the "Quick fix" tool to select a new image.
Tip: If some cases are detected (validation markers), it is recommended to check the presence of the image and move it in the right folder according to the used expected image path location. Once done, you can restart the validation. If you have still validation markers, then, you can use the "Quick fix" tool to:
<ul>
<li>Select a new image</li>
<li>Remove broken image link from diagram element or restore default image of diagram element</li>
</ul>
|}
<br>
{| class="VALIDATION-RULE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ Require-Bundle: org.eclipse.core.runtime,
org.polarsys.capella.core.transition.system.topdown,
org.polarsys.capella.core.platform.sirius.ui.perspective,
org.polarsys.capella.common.flexibility.wizards,
org.polarsys.capella.core.platform.sirius.sirius.validation,
org.polarsys.kitalpha.richtext.widget.tools
Bundle-RequiredExecutionEnvironment: JavaSE-17
org.polarsys.capella.core.platform.sirius.sirius.validation,
org.polarsys.kitalpha.richtext.widget.tools,
org.eclipse.sirius.diagram.ui
Bundle-RequiredExecutionEnvironment: JavaSE-17
Bundle-ActivationPolicy: lazy
Export-Package: org.polarsys.capella.test.validation.rules.ju,
org.polarsys.capella.test.validation.rules.ju.testcases,
Expand Down

0 comments on commit a27e776

Please sign in to comment.