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; ...@@ -31,6 +31,7 @@ import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EObject;
import org.eclipse.etrice.core.room.ActorClass; import org.eclipse.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.room.RoomModel; import org.eclipse.etrice.core.room.RoomModel;
...@@ -40,6 +41,7 @@ import org.eclipse.etrice.ui.commands.RoomOpeningHelper; ...@@ -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.UIBaseActivator;
import org.eclipse.etrice.ui.common.base.export.IBulkDiagramExporter; 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.preferences.UIBasePreferenceConstants;
import org.eclipse.etrice.ui.common.base.support.DiagramAccessBase;
import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelection;
...@@ -145,18 +147,27 @@ public class ExportDiagramsHandler extends AbstractHandler { ...@@ -145,18 +147,27 @@ public class ExportDiagramsHandler extends AbstractHandler {
if (folder.exists()) { if (folder.exists()) {
String folderPath = folder.getLocation().toOSString(); String folderPath = folder.getLocation().toOSString();
IBulkDiagramExporter behaviorExporter = RoomOpeningHelper.getBehaviorDiagramAccess().getDiagramExporter(); DiagramAccessBase behaviorDiagramAccess = RoomOpeningHelper.getBehaviorDiagramAccess();
IBulkDiagramExporter structureExporter = RoomOpeningHelper.getStructureDiagramAccess().getDiagramExporter(); DiagramAccessBase structureDiagramAccess = RoomOpeningHelper.getStructureDiagramAccess();
model.getRoomClasses().forEach(rc -> { model.getRoomClasses().forEach(rc -> {
if(rc instanceof ActorClass) { try {
ActorClass ac = (ActorClass) rc; if(rc instanceof ActorClass) {
if (ac.getStateMachine() != null) { ActorClass ac = (ActorClass) rc;
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);
}
}
else if(rc instanceof SubSystemClass) {
if (structureDiagramAccess.diagramExists(rc)) {
structureDiagramAccess.getDiagramExporter().export((SubSystemClass) rc, folderPath);
}
} }
structureExporter.export(ac, folderPath);
} }
else if(rc instanceof SubSystemClass) { catch (Throwable t) {
structureExporter.export((SubSystemClass) rc, folderPath); Platform.getLog(ExportDiagramsHandler.class).error("uncaught error in ExportDiagramsHandler for RoomClass '" + rc.getName() + "'", t);
} }
}); });
......
...@@ -21,6 +21,7 @@ import java.util.Collections; ...@@ -21,6 +21,7 @@ import java.util.Collections;
import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.command.Command; import org.eclipse.emf.common.command.Command;
...@@ -79,41 +80,8 @@ public abstract class DiagramAccessBase { ...@@ -79,41 +80,8 @@ public abstract class DiagramAccessBase {
* Load diagram based on rootObjects resource URI into given resourceSet * Load diagram based on rootObjects resource URI into given resourceSet
*/ */
public Diagram getDiagram(EObject rootObject, ResourceSet rs) { public Diagram getDiagram(EObject rootObject, ResourceSet rs) {
Resource resource = rootObject.eResource(); if (diagramExists(rootObject)) {
if (resource==null) Resource diagRes = rs.getResource(getDiagramURI(rootObject), true);
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 (diagRes.getContents().isEmpty()) if (diagRes.getContents().isEmpty())
return null; return null;
if (diagRes.getContents().get(0) instanceof Diagram) { if (diagRes.getContents().get(0) instanceof Diagram) {
...@@ -123,7 +91,7 @@ public abstract class DiagramAccessBase { ...@@ -123,7 +91,7 @@ public abstract class DiagramAccessBase {
} }
} }
else { else {
Resource diagRes = rs.createResource(diagURI); Resource diagRes = rs.createResource(getDiagramURI(rootObject));
IPreferenceStore store = UIBaseActivator.getDefault().getPreferenceStore(); IPreferenceStore store = UIBaseActivator.getDefault().getPreferenceStore();
boolean snap = store.getBoolean(UIBasePreferenceConstants.SNAP_TO_GRID); boolean snap = store.getBoolean(UIBasePreferenceConstants.SNAP_TO_GRID);
...@@ -152,6 +120,55 @@ public abstract class DiagramAccessBase { ...@@ -152,6 +120,55 @@ public abstract class DiagramAccessBase {
return null; 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) { private ResourceSet newResourceSet(URI uri) {
if (uri.isPlatformResource()) { if (uri.isPlatformResource()) {
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(uri.toPlatformString(true))); IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(uri.toPlatformString(true)));
...@@ -219,6 +236,14 @@ public abstract class DiagramAccessBase { ...@@ -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) { public DiagramEditorBase findDiagramEditor(EObject rootObject) {
IFileEditorInput input = getEditorInput(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