Commit 50b6c714 authored by Eyrak Paen-Rochlitz's avatar Eyrak Paen-Rochlitz
Browse files

[ui.exporter] Only export diagram images if diagram model file exists

parent 77bbfd5a
Pipeline #4535 passed with stage
in 0 seconds
......@@ -31,6 +31,7 @@ import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.room.RoomModel;
......@@ -40,6 +41,7 @@ import org.eclipse.etrice.ui.commands.RoomOpeningHelper;
import org.eclipse.etrice.ui.common.base.UIBaseActivator;
import org.eclipse.etrice.ui.common.base.export.IBulkDiagramExporter;
import org.eclipse.etrice.ui.common.base.preferences.UIBasePreferenceConstants;
import org.eclipse.etrice.ui.common.base.support.DiagramAccessBase;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.ISelection;
......@@ -145,18 +147,27 @@ public class ExportDiagramsHandler extends AbstractHandler {
if (folder.exists()) {
String folderPath = folder.getLocation().toOSString();
IBulkDiagramExporter behaviorExporter = RoomOpeningHelper.getBehaviorDiagramAccess().getDiagramExporter();
IBulkDiagramExporter structureExporter = RoomOpeningHelper.getStructureDiagramAccess().getDiagramExporter();
DiagramAccessBase behaviorDiagramAccess = RoomOpeningHelper.getBehaviorDiagramAccess();
DiagramAccessBase structureDiagramAccess = RoomOpeningHelper.getStructureDiagramAccess();
model.getRoomClasses().forEach(rc -> {
try {
if(rc instanceof ActorClass) {
ActorClass ac = (ActorClass) rc;
if (ac.getStateMachine() != null) {
behaviorExporter.export(ac, folderPath);
if (ac.getStateMachine() != null && behaviorDiagramAccess.diagramExists(ac)) {
behaviorDiagramAccess.getDiagramExporter().export(ac, folderPath);
}
if (structureDiagramAccess.diagramExists(ac)) {
structureDiagramAccess.getDiagramExporter().export(ac, folderPath);
}
structureExporter.export(ac, folderPath);
}
else if(rc instanceof SubSystemClass) {
structureExporter.export((SubSystemClass) rc, folderPath);
if (structureDiagramAccess.diagramExists(rc)) {
structureDiagramAccess.getDiagramExporter().export((SubSystemClass) rc, folderPath);
}
}
}
catch (Throwable t) {
Platform.getLog(ExportDiagramsHandler.class).error("uncaught error in ExportDiagramsHandler for RoomClass '" + rc.getName() + "'", t);
}
});
......
......@@ -21,6 +21,7 @@ import java.util.Collections;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.command.Command;
......@@ -79,41 +80,8 @@ public abstract class DiagramAccessBase {
* Load diagram based on rootObjects resource URI into given resourceSet
*/
public Diagram getDiagram(EObject rootObject, ResourceSet rs) {
Resource resource = rootObject.eResource();
if (resource==null)
return null;
URI uri = resource.getURI();
URI diagURI = null;
boolean exists = false;
if (uri.isPlatformResource()) {
uri = uri.trimSegments(1);
IFolder diagramFolder = null;
if (uri.segmentCount()==2) {
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(uri.lastSegment());
diagramFolder = project.getFolder(DIAGRAMS_FOLDER_NAME);
}
else {
IFolder parentFolder = ResourcesPlugin.getWorkspace().getRoot().getFolder(new Path(uri.toPlatformString(true)));
diagramFolder = parentFolder.getFolder(DIAGRAMS_FOLDER_NAME);
}
IFile diagramFile = diagramFolder.getFile(getDigramFileName(rootObject));
diagURI = URI.createPlatformResourceURI(diagramFile.getFullPath().toString(), true);
exists = diagramFile.exists();
}
else {
File diagramFile = new File(uri.toFileString());
diagramFile = new File(diagramFile.getParent()
+File.separator+DIAGRAMS_FOLDER_NAME
+File.separator+getDigramFileName(rootObject));
diagURI = URI.createFileURI(diagramFile.getPath());
exists = diagramFile.exists();
}
if (exists) {
Resource diagRes = rs.getResource(diagURI, true);
if (diagramExists(rootObject)) {
Resource diagRes = rs.getResource(getDiagramURI(rootObject), true);
if (diagRes.getContents().isEmpty())
return null;
if (diagRes.getContents().get(0) instanceof Diagram) {
......@@ -123,7 +91,7 @@ public abstract class DiagramAccessBase {
}
}
else {
Resource diagRes = rs.createResource(diagURI);
Resource diagRes = rs.createResource(getDiagramURI(rootObject));
IPreferenceStore store = UIBaseActivator.getDefault().getPreferenceStore();
boolean snap = store.getBoolean(UIBasePreferenceConstants.SNAP_TO_GRID);
......@@ -152,6 +120,55 @@ public abstract class DiagramAccessBase {
return null;
}
public URI getDiagramURI(EObject rootObject) {
Resource resource = rootObject.eResource();
if (resource==null)
return null;
URI uri = resource.getURI();
URI diagURI = null;
if (uri.isPlatformResource()) {
uri = uri.trimSegments(1);
IFolder diagramFolder = null;
if (uri.segmentCount()==2) {
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(uri.lastSegment());
diagramFolder = project.getFolder(DIAGRAMS_FOLDER_NAME);
}
else {
IFolder parentFolder = ResourcesPlugin.getWorkspace().getRoot().getFolder(new Path(uri.toPlatformString(true)));
diagramFolder = parentFolder.getFolder(DIAGRAMS_FOLDER_NAME);
}
IFile diagramFile = diagramFolder.getFile(getDigramFileName(rootObject));
diagURI = URI.createPlatformResourceURI(diagramFile.getFullPath().toString(), true);
}
else {
File diagramFile = new File(uri.toFileString());
diagramFile = new File(diagramFile.getParent()
+File.separator+DIAGRAMS_FOLDER_NAME
+File.separator+getDigramFileName(rootObject));
diagURI = URI.createFileURI(diagramFile.getPath());
}
return diagURI;
}
public boolean diagramExists(EObject rootObject) {
URI diagURI = getDiagramURI(rootObject);
if (diagURI == null) {
return false;
}
boolean exists = false;
if (diagURI.isPlatformResource()) {
IResource diagramFile = ResourcesPlugin.getWorkspace().getRoot().findMember(diagURI.toPlatformString(true));
exists = (diagramFile != null);
} else {
File diagramFile = new File(diagURI.toFileString());
exists = diagramFile.exists();
}
return exists;
}
private ResourceSet newResourceSet(URI uri) {
if (uri.isPlatformResource()) {
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(uri.toPlatformString(true)));
......@@ -219,6 +236,14 @@ public abstract class DiagramAccessBase {
}
}
/**
* Returns the diagram instance of the corresponding <code>rootObject</code> if the diagram is open in the Workbench.
*
* <p>IMPORTANT: as an additional side effect, if the diagram model does not exist then it will
* automatically be created regardless.</p>
* @param rootObject the model element for the desired diagram
* @return the corresponding diagram instance if it is currently open, or null if it is not open
*/
public DiagramEditorBase findDiagramEditor(EObject rootObject) {
IFileEditorInput input = getEditorInput(rootObject);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment