Search in sources :

Example 1 with GraphicalEditorException

use of org.osate.ge.internal.GraphicalEditorException in project osate2 by osate.

the class DiagramElementLayoutUtil method findMidpoint.

private static Point findMidpoint(final List<Point> points) {
    if (points.size() < 2) {
        throw new IllegalArgumentException("At least two points must be specified");
    }
    final double totalLength = length(points);
    double lengthToTarget = totalLength / 2.0;
    for (int i = 1; i < points.size(); i++) {
        final Point p1 = points.get(i - 1);
        final Point p2 = points.get(i);
        final double segmentLength = length(p1, p2);
        if (lengthToTarget > segmentLength) {
            lengthToTarget -= segmentLength;
        } else {
            final double frac = lengthToTarget / segmentLength;
            return new Point(p1.x + (p2.x - p1.x) * frac, p1.y + (p2.y - p1.y) * frac);
        }
    }
    throw new GraphicalEditorException("Unexpected case: midpoint not found");
}
Also used : Point(org.osate.ge.graphics.Point) SizeConstraint(org.eclipse.elk.core.options.SizeConstraint) Point(org.osate.ge.graphics.Point) GraphicalEditorException(org.osate.ge.internal.GraphicalEditorException)

Example 2 with GraphicalEditorException

use of org.osate.ge.internal.GraphicalEditorException in project osate2 by osate.

the class DiagramElementLayoutUtil method layout.

/**
 * Performs a layout of the specified diagram nodes
 * @param actionLabel description of the action
 * @param editor the editor containing the diagram
 * @param diagramNodes the nodes to layout. If null, the entire diagram will be laid out
 * @param options the layout options
 */
public static void layout(final String actionLabel, final IEditorPart editor, final Collection<? extends DiagramNode> diagramNodes, final LayoutOptions options) {
    if (!(editor instanceof InternalDiagramEditor)) {
        throw new GraphicalEditorException("Editor must be an " + InternalDiagramEditor.class.getName());
    }
    final InternalDiagramEditor diagramEditor = ((InternalDiagramEditor) editor);
    final LayoutInfoProvider layoutInfoProvider = Adapters.adapt(diagramEditor, LayoutInfoProvider.class);
    layout(actionLabel, diagramEditor.getDiagram(), diagramNodes, layoutInfoProvider, options);
}
Also used : InternalDiagramEditor(org.osate.ge.internal.ui.editor.InternalDiagramEditor) GraphicalEditorException(org.osate.ge.internal.GraphicalEditorException)

Example 3 with GraphicalEditorException

use of org.osate.ge.internal.GraphicalEditorException in project osate2 by osate.

the class LayoutDebugUtil method saveElkGraphToDebugProject.

/**
 * When {@link #SAVE_GRAPH_ENABLED} is true, saves the ELK graph to a file in the {@link #MAGIC_PROJECT_NAME} project.
 * @param g the graph to safe
 * @param suffix an identifier to add to the name of the file.
 */
public static void saveElkGraphToDebugProject(final ElkNode g, final String suffix) {
    if (SAVE_GRAPH_ENABLED) {
        final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(MAGIC_PROJECT_NAME);
        if (project != null && project.exists()) {
            final URI uri = URI.createPlatformResourceURI(project.getFile("layout_graph_" + suffix + ".elkg").getFullPath().toString(), true);
            // Save the resource
            final ResourceSet rs = new ResourceSetImpl();
            final Resource resource = rs.createResource(uri);
            resource.getContents().add(g);
            try {
                resource.save(Collections.emptyMap());
            } catch (IOException e) {
                throw new GraphicalEditorException(e);
            }
        }
    }
}
Also used : ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IOException(java.io.IOException) URI(org.eclipse.emf.common.util.URI) IProject(org.eclipse.core.resources.IProject) GraphicalEditorException(org.osate.ge.internal.GraphicalEditorException)

Example 4 with GraphicalEditorException

use of org.osate.ge.internal.GraphicalEditorException in project osate2 by osate.

the class DiagramSerialization method readMetaModelDiagram.

/**
 * Loads the serialized diagram
 * @param uri the URI specifying the location of the diagram file
 * @return the serialized diagram
 */
public static org.osate.ge.diagram.Diagram readMetaModelDiagram(final URI uri) {
    Objects.requireNonNull(uri, "uri must not be null");
    // Load the resource
    final ResourceSet rs = new ResourceSetImpl();
    final Resource resource = rs.createResource(uri);
    try {
        resource.load(Collections.singletonMap(XMLResource.OPTION_RECORD_UNKNOWN_FEATURE, true));
        if (resource.getContents().size() == 0 || !(resource.getContents().get(0) instanceof org.osate.ge.diagram.Diagram)) {
            throw new GraphicalEditorException("Unable to load diagram.");
        }
        final org.osate.ge.diagram.Diagram mmDiagram = (org.osate.ge.diagram.Diagram) resource.getContents().get(0);
        return mmDiagram;
    } catch (final IOException e) {
        throw new GraphicalEditorException(e);
    }
}
Also used : ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) Resource(org.eclipse.emf.ecore.resource.Resource) Diagram(org.osate.ge.diagram.Diagram) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IOException(java.io.IOException) GraphicalEditorException(org.osate.ge.internal.GraphicalEditorException) Diagram(org.osate.ge.diagram.Diagram)

Example 5 with GraphicalEditorException

use of org.osate.ge.internal.GraphicalEditorException in project osate2 by osate.

the class DiagramSerialization method write.

/**
 * Serialized the specified runtime diagram and writes is to a file.
 * @param project the project in which the diagram is contained.
 * @param diagram the runtime diagram to serialize
 * @param uri the URI specifying the file to which to write the serialized diagram
 */
public static void write(final IProject project, final AgeDiagram diagram, final URI uri) {
    // Convert from the runtime format to the metamodel format which is stored
    final org.osate.ge.diagram.Diagram mmDiagram = new Diagram();
    mmDiagram.setFormatVersion(FORMAT_VERSION);
    final org.osate.ge.diagram.DiagramConfiguration mmConfig = new org.osate.ge.diagram.DiagramConfiguration();
    mmDiagram.setConfig(mmConfig);
    // Populate the diagram configuration
    final DiagramConfiguration config = diagram.getConfiguration();
    mmConfig.setType(config.getDiagramType().getId());
    mmConfig.setContext(config.getContextBoReference() == null ? null : config.getContextBoReference().toMetamodel());
    mmConfig.setConnectionPrimaryLabelsVisible(config.getConnectionPrimaryLabelsVisible());
    final org.osate.ge.diagram.AadlPropertiesSet enabledProperties = new org.osate.ge.diagram.AadlPropertiesSet();
    mmConfig.setEnabledAadlProperties(enabledProperties);
    for (final String enabledPropertyName : config.getEnabledAadlPropertyNames()) {
        enabledProperties.getProperty().add(enabledPropertyName);
    }
    convertElementsToMetamodel(project, mmDiagram, diagram.getChildren());
    // Save the resource
    final ResourceSet rs = new ResourceSetImpl();
    final Resource resource = rs.createResource(uri);
    resource.getContents().add(mmDiagram);
    try {
        resource.save(Collections.emptyMap());
    } catch (IOException e) {
        throw new GraphicalEditorException(e);
    }
}
Also used : ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) Diagram(org.osate.ge.diagram.Diagram) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IOException(java.io.IOException) Diagram(org.osate.ge.diagram.Diagram) GraphicalEditorException(org.osate.ge.internal.GraphicalEditorException)

Aggregations

GraphicalEditorException (org.osate.ge.internal.GraphicalEditorException)15 IOException (java.io.IOException)5 EObject (org.eclipse.emf.ecore.EObject)4 Resource (org.eclipse.emf.ecore.resource.Resource)4 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)4 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)4 HashSet (java.util.HashSet)3 IProject (org.eclipse.core.resources.IProject)3 CoreException (org.eclipse.core.runtime.CoreException)3 URI (org.eclipse.emf.common.util.URI)3 Collection (java.util.Collection)2 Collections (java.util.Collections)2 List (java.util.List)2 Entry (java.util.Map.Entry)2 Objects (java.util.Objects)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 IFile (org.eclipse.core.resources.IFile)2 IResource (org.eclipse.core.resources.IResource)2 IStatus (org.eclipse.core.runtime.IStatus)2