Skip to content

Commit

Permalink
#164 Adapt import of reqif content with images
Browse files Browse the repository at this point in the history
Import of images now requires to reference images using relative paths.
Ensure that image import only allows to do this import, set the imported
requirement ReqIfText content to reference the relative path image.

Update the import test.

Change-Id: I0000000000000000000000000000000000000000
Signed-off-by: Arnaud Dieumegard <arnaud.dieumegard@obeo.fr>
  • Loading branch information
arnauddieumegard authored and pdulth committed Jul 21, 2022
1 parent 582613c commit e174528
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 199 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,8 @@
package org.polarsys.capella.vp.requirements.importer.transposer.bridge;

public class ImageImporter {
private ImageImportStrategy imgImportStrategy;
private String absPath;
private String relPath;

public ImageImportStrategy getImgImportStrategy() {
return imgImportStrategy;
}

public void setImgImportStrategy(ImageImportStrategy imgImportStrategy) {
this.imgImportStrategy = imgImportStrategy;
}

public String getAbsPath() {
return absPath;
}
public void setAbsPath(String absPath) {
this.absPath = absPath;
}
public String getRelPath() {
return relPath;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 THALES GLOBAL SERVICES.
* Copyright (c) 2020, 2022 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
Expand All @@ -12,6 +12,7 @@
*******************************************************************************/
package org.polarsys.capella.vp.requirements.importer.transposer.bridge;

import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;

Expand All @@ -29,16 +30,13 @@
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ImageImportingDialog extends TitleAreaDialog {

private Text absPathText;
private Button absPathChoosingButton;
private Text relativePathText;
private Button relativePathChoosingButton;
private Button embedImageButton;
private ImageImporter imageImporter;
private IProject currentProject;

Expand All @@ -59,55 +57,15 @@ protected Control createDialogArea(Composite parent) {
GridDataFactory.fillDefaults().grab(true, false).applyTo(importingModesComposite);
GridLayoutFactory.fillDefaults().numColumns(3).equalWidth(false).margins(0, 0).applyTo(importingModesComposite);

setUpAbsPathUI(container, importingModesComposite);

setUpRelPathUI(container, importingModesComposite);

setUpEmbeddedModeUI(importingModesComposite);

return container;
}

protected void setUpEmbeddedModeUI(Composite importingModesComposite) {
embedImageButton = new Button(importingModesComposite, SWT.RADIO);
embedImageButton.setText("Encode image in Base64 and embed it in the text");
embedImageButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
embedImageButton.setSelection(true);
setEnableAbsImagePath(false);
setEnableRelativeImagePath(false);
enableFinishForEmbeddedImg();
}

protected void enableFinishForEmbeddedImg() {
setMessage(Messages.ImageImportingDialog_DefaultMessage + Messages.ImageImportingDialog_EmbeddedMessage);
getButton(IDialogConstants.OK_ID).setEnabled(true);
imageImporter.setImgImportStrategy(ImageImportStrategy.EMBEDDED);
}
});
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).applyTo(embedImageButton);
}

protected void setUpRelPathUI(Composite container, Composite importingModesComposite) {
relativePathChoosingButton = new Button(importingModesComposite, SWT.RADIO);
relativePathChoosingButton.setText("Choose a path to image folder that is relative to the current project:");
relativePathChoosingButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
setEnableRelativeImagePath(true);
setEnableAbsImagePath(false);
embedImageButton.setSelection(false);
String currentProjectLocation = currentProject.getLocation().toString();
Path currentProjectPath = Paths.get(currentProjectLocation);
if (!currentProjectPath.resolve(relativePathText.getText()).toFile().exists()) {
disableFinish();
} else {
enableFinishForRelPath();
}
}
});
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).applyTo(relativePathChoosingButton);
Label relativePathTextChoice = new Label(importingModesComposite, SWT.SINGLE);
relativePathTextChoice.setText("Path to image folder that is relative to the current project:");
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).applyTo(relativePathTextChoice);

relativePathText = new Text(importingModesComposite, SWT.SINGLE | SWT.BORDER);
relativePathText.addModifyListener(e -> {
Expand All @@ -132,87 +90,30 @@ public void widgetSelected(SelectionEvent e) {
dialog.setFilterPath(currentProjectLocation);
String result = dialog.open();
if (result != null) {
Path resultPath = Paths.get(result);
Path currentProjectPath = Paths.get(currentProjectLocation);
if (resultPath.startsWith(currentProjectPath)) {
Path relativizedPath = currentProjectPath.relativize(resultPath);
relativePathText.setText(relativizedPath.toString());
URI resultPath = Paths.get(result).toUri();
URI currentProjectPath = Paths.get(currentProjectLocation).toUri();
if (resultPath.toString().startsWith(currentProjectPath.toString())) {
URI relativizedPath = currentProjectPath.relativize(resultPath);
relativePathText.setText(relativizedPath.getPath());
} else {
relativePathText.setText("");
}
}
}
});
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).applyTo(relativePathBrowseButton);
setEnableRelativeImagePath(false);
}

protected void setUpAbsPathUI(Composite container, Composite importingModesComposite) {
absPathChoosingButton = new Button(importingModesComposite, SWT.RADIO);
absPathChoosingButton.setText("Choose an absolute path to image folder:");
absPathChoosingButton.setSelection(true);
absPathChoosingButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
setEnableAbsImagePath(true);
setEnableRelativeImagePath(false);
embedImageButton.setSelection(false);
if (Paths.get(absPathText.getText()).toFile().exists() && Paths.get(absPathText.getText()).isAbsolute()) {
enableFinishForAbsPath();
} else {
setMessage(Messages.ImageImportingDialog_DefaultMessage + Messages.ImageImportingDialog_AbsPathMessage);
disableFinish();
}
}
});
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).applyTo(absPathChoosingButton);

absPathText = new Text(importingModesComposite, SWT.SINGLE | SWT.BORDER);
absPathText.addModifyListener(e -> {
if (Paths.get(absPathText.getText()).toFile().exists() && Paths.get(absPathText.getText()).isAbsolute()) {
enableFinishForAbsPath();
} else {
setErrorMessage("The absolute path does not point to a valid folder.");
disableFinish();
}
});
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(absPathText);

Button absPathBrowseButton = new Button(importingModesComposite, SWT.PUSH);
absPathBrowseButton.setText("Browse...");
absPathBrowseButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
DirectoryDialog dialog = new DirectoryDialog(container.getShell());
String result = dialog.open();
if (result != null) {
absPathText.setText(result);
}
}
});
GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).applyTo(absPathBrowseButton);
}

protected void enableFinishForAbsPath() {
setMessage(Messages.ImageImportingDialog_DefaultMessage + Messages.ImageImportingDialog_AbsPathMessage);
setErrorMessage(null);
getButton(IDialogConstants.OK_ID).setEnabled(true);
imageImporter.setImgImportStrategy(ImageImportStrategy.ABS_PATH);
imageImporter.setAbsPath(absPathText.getText());
setEnableRelativeImagePath(true);
}

protected void enableFinishForRelPath() {
setMessage(Messages.ImageImportingDialog_DefaultMessage + Messages.ImageImportingDialog_RelPathMessage);
setErrorMessage(null);
getButton(IDialogConstants.OK_ID).setEnabled(true);
imageImporter.setImgImportStrategy(ImageImportStrategy.REL_PATH);
imageImporter.setRelPath(relativePathText.getText());
}

protected void disableFinish() {
getButton(IDialogConstants.OK_ID).setEnabled(false);
imageImporter.setImgImportStrategy(null);
imageImporter.setAbsPath(null);
}

@Override
Expand All @@ -222,25 +123,20 @@ protected boolean isResizable() {

@Override
protected Point getInitialSize() {
return new Point(1000, 320);
return new Point(600, 200);
}

@Override
public void create() {
super.create();
setTitle("Image importing options");
setMessage(Messages.ImageImportingDialog_DefaultMessage + Messages.ImageImportingDialog_AbsPathMessage,
IMessageProvider.NONE);
getButton(IDialogConstants.OK_ID).setEnabled(false);
}

private void setEnableAbsImagePath(boolean enable) {
absPathChoosingButton.setSelection(enable);
absPathText.setEnabled(enable);
setTitle("Image importing parameters");
setMessage(Messages.ImageImportingDialog_DefaultMessage + Messages.ImageImportingDialog_RelPathMessage,
IMessageProvider.NONE);
// Default path will be the current project so it should be active
getButton(IDialogConstants.OK_ID).setEnabled(true);
}

private void setEnableRelativeImagePath(boolean enable) {
relativePathChoosingButton.setSelection(enable);
relativePathText.setEnabled(enable);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 THALES GLOBAL SERVICES.
* Copyright (c) 2020, 2022 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
Expand Down Expand Up @@ -236,34 +236,25 @@ protected void convertImgElement(AttributeOwner owner, Node child) {
dialog.open();
});
}
if (imageImporter.getImgImportStrategy() != null) {
if (imageImporter.getRelPath() != null) {
convertImgElement(imgElement, owner);
} else {
LOGGER.log(Level.ERROR, "No valid image import strategy. Images won't be imported");
LOGGER.log(Level.ERROR, "No valid path to import. Images won't be imported");
}
}

/**
* Convert the img element in ReqIF format into Capella description format according to the chosen image importing
* strategy. We suppose that the src of the imgElement before being converted is always the name of the exported
* image.
* Convert the img element in ReqIF format into Capella description format. We suppose that the src of the
* imgElement before being converted is always the name of the exported image.
*
* @param imgElement
* the img element to convert
*/
protected void convertImgElement(Element imgElement, AttributeOwner owner) {
String imgName = imgElement.getAttribute("src");
if (imageImporter.getImgImportStrategy() == ImageImportStrategy.ABS_PATH) {
imgElement.setAttribute("src", (new File(imageImporter.getAbsPath(), imgName)).toURI().toString());
storeFileToCopy(imgName, getReqIFFolder(), imageImporter.getAbsPath(), owner);
} else if (imageImporter.getImgImportStrategy() == ImageImportStrategy.REL_PATH) {
imgElement.setAttribute("src", Paths.get(imageImporter.getRelPath(), imgName).toString());
storeFileToCopy(imgName, getReqIFFolder(),
(new File(getCurrentProject().getLocation().toString(), imageImporter.getRelPath())).getPath(), owner);
} else if (imageImporter.getImgImportStrategy() == ImageImportStrategy.EMBEDDED) {
String base64Image = encode((new File(getReqIFFolder(), imgName)).getPath());
imgElement.setAttribute("src", base64Image);
}
imgElement.setAttribute("src", getCurrentProject().getName() + "/" + imageImporter.getRelPath() + imgName);
storeFileToCopy(imgName, getReqIFFolder(),
(new File(getCurrentProject().getLocation().toString(), imageImporter.getRelPath())).getPath(), owner);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ CategoryFormat_Name={0}
CategoryFormat_Description=Handle all differences manipulating {0} elements
ImageImportingDialog_DefaultMessage= The imported ReqIf file references images. Please indicate below how to import these images into your model.\n
ImageImportingDialog_AbsPathMessage= In this mode, images will be in an absolute path folder. This is the recommended mode.
ImageImportingDialog_RelPathMessage= In this mode, images will be in a relative path folder. If the text is empty, the current project will be chosen.
ImageImportingDialog_RelPathMessage= Images will be located in a relative path folder. If the text is empty, the current project will be chosen.
ImageImportingDialog_EmbeddedMessage= In this mode, images will be encoded in Base64 and embed in the text. Be aware that this will add up the image to your model size.

0 comments on commit e174528

Please sign in to comment.