Skip to content

Commit

Permalink
Fixes and refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
hallvard committed Apr 5, 2021
1 parent d4d1270 commit aff43fb
Show file tree
Hide file tree
Showing 39 changed files with 468 additions and 344 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,12 @@ protected IStatus run(final IProgressMonitor monitor) {

protected String ensureDiagram(String diagramText) {
if (! diagramText.startsWith("@")) {
diagramText = PlantumlConstants.START_UML + "\n" + diagramText + "\n" + PlantumlConstants.END_UML;
diagramText = PlantumlConstants.START_UML + "\n" + diagramText;
String suffix = PlantumlConstants.END_UML;
if (! diagramText.endsWith("\n")) {
suffix = "\n" + suffix;
}
diagramText += suffix;
}
return diagramText;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

public class DiagramSourceView extends AbstractDiagramSourceView {

private String diagramText;
private Text diagramTextView;

@Override
Expand All @@ -22,10 +23,11 @@ public void createPartControl(final Composite parent) {
protected void updateDiagramText(final String textDiagram, final DiagramIntent diagramIntent, final IProgressMonitor progressMonitor) {
if (isValidControl(diagramTextView)) {
setDiagramViewStatus(ViewStatus.DIAGRAM_VIEW_TEXT, textDiagram);
this.diagramText = textDiagram;
setDiagramViewStatus(ViewStatus.DIAGRAM_VIEW_DATA, textDiagram);
asyncExec(() -> {
if (isValidControl(diagramTextView)) {
final String text = (textDiagram == null ? "" : textDiagram);
setDiagramViewStatus(ViewStatus.DIAGRAM_VIEW_DATA, textDiagram);
diagramTextView.setText(text);
setDiagramViewStatus(ViewStatus.DIAGRAM_VIEW, text);
}
Expand All @@ -35,7 +37,7 @@ protected void updateDiagramText(final String textDiagram, final DiagramIntent d

@Override
public String getDiagramText() {
return (isValidControl(diagramTextView) ? diagramTextView.getText() : null);
return diagramText;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,24 +156,51 @@ private static void parseImageMapString(final String cMapData, final Collection<
// areaElement = areaElement.substring(0, areaElement.length() - 1);
// }
// }
final LinkData link = new LinkData();
link.href = getAttributeValue(areaElement, "href");
link.title = getAttributeValue(areaElement, "title");
link.altText = getAttributeValue(areaElement, "alt");
final String coords = getAttributeValue(areaElement, "coords");
if (coords != null) {
final LinkData link = new LinkData();
link.href = getAttributeValue(areaElement, "href");
link.title = getAttributeValue(areaElement, "title");
link.altText = getAttributeValue(areaElement, "alt");
final String[] ints = coords.split(",");
if (ints.length == 4) {
try {
final int x1 = Integer.valueOf(ints[0]), y1 = Integer.valueOf(ints[1]), x2 = Integer.valueOf(ints[2]), y2 = Integer.valueOf(ints[3]);
link.rect = new Rectangle(x1, y1, x2 - x1, y2 - y1);
links.add(link);
} catch (final NumberFormatException e) {
}
}
}
links.add(link);
}
if (links != null && links.size() > 0) {
scaleLinks(links);
}
}

// assumes image is scaled, but the margin is the same
private static void scaleLinks(final Collection<LinkData> links) {
int minX = Integer.MAX_VALUE, minY = Integer.MAX_VALUE;
for (final LinkData link : links) {
if (link.rect.x < minX) {
minX = link.rect.x;
}
if (link.rect.y < minY) {
minY = link.rect.y;
}
}
// empirically identified
final int marginX = 7, marginY = 7;
final int sx = minX / marginX, sy = minY / marginY;
System.out.println("Scaling with " + sx + "," + sy);
for (final LinkData link : links) {
link.rect.x /= sx;
link.rect.y /= sy;
link.rect.width /= sx;
link.rect.height /= sy;
}
}

/*
@startuml
actor Bob [[http://plantuml.com/sequence]]
Expand Down
14 changes: 3 additions & 11 deletions bundles/net.sourceforge.plantuml.ecore/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,11 @@
point="net.sourceforge.plantuml.eclipse.diagramIntentProvider">

<diagramIntentProvider
id="net.sourceforge.plantuml.ecore.EcoreClassDiagramIntentProvider"
providerClass="net.sourceforge.plantuml.ecore.EcoreClassDiagramIntentProvider"
label="Class diagrams generated from ecore models"
id="net.sourceforge.plantuml.ecore.EcoreDiagramIntentProvider"
providerClass="net.sourceforge.plantuml.ecore.EcoreDiagramIntentProvider"
label="Class and object diagrams generated from ecore models and instances"
>
</diagramIntentProvider>

<diagramIntentProvider
id="net.sourceforge.plantuml.ecore.EcoreObjectDiagramIntentProvider"
providerClass="net.sourceforge.plantuml.ecore.EcoreObjectDiagramIntentProvider"
priority="default"
label="Object diagrams generated from ecore model instances"
>
</diagramIntentProvider>
</extension>

</plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ protected AbstractEcoreDiagramIntentProvider(final Class<?> editorType) {
}

@Override
public boolean supportsSelection(final ISelection selection) {
public Boolean supportsSelection(final ISelection selection) {
if (selection instanceof IStructuredSelection) {
final Object object = ((IStructuredSelection) selection).getFirstElement();
if (object instanceof EObject) {
return supportsEObject((EObject) object);
} else if (object instanceof Resource) {
for (final EObject eObject : ((Resource) object).getContents()) {
if (supportsEObject(eObject)) {
return true;
}
}
}
}
return false;
Expand All @@ -43,41 +49,41 @@ public boolean supportsSelection(final ISelection selection) {
//

@Override
protected Collection<DiagramIntent> getDiagramInfos(final WorkbenchEditorPartDiagramIntentProviderContext context) {
protected Collection<? extends DiagramIntent> getDiagramInfos(final WorkbenchEditorPartDiagramIntentProviderContext context) {
if (context.getSelection() instanceof IStructuredSelection) {
final Object selection = ((IStructuredSelection) context.getSelection()).getFirstElement();
EObject root = null;
if (selection instanceof Resource) {
final Resource resource = (Resource) selection;
if (resource.getContents().size() > 0) {
root = resource.getContents().get(0);
for (final EObject eObject : ((Resource) selection).getContents()) {
if (supportsEObject(eObject)) {
final Collection<? extends DiagramIntent> diagramInfos = getDiagramInfos(eObject);
if (diagramInfos != null) {
return diagramInfos;
}
}
}
} else if (selection instanceof EObject) {
root = (EObject) selection;
}
if (supportsEObject(root)) {
return getDiagramInfos(root);
} else if (selection instanceof EObject && supportsEObject((EObject) selection)) {
return getDiagramInfos((EObject) selection);
}
}
return null;
}

protected abstract Collection<DiagramIntent> getDiagramInfos(final EObject root);
protected abstract Collection<? extends DiagramIntent> getDiagramInfos(final EObject root);

@Override
protected boolean supportsPath(final IPath path) {
protected Boolean supportsPath(final IPath path) {
return "xmi".equals(path.getFileExtension());
}

@Override
protected Collection<DiagramIntent> getDiagramInfos(final WorkspaceDiagramIntentProviderContext context) {
protected Collection<? extends DiagramIntent> getDiagramInfos(final WorkspaceDiagramIntentProviderContext context) {
final URI uri = URI.createPlatformResourceURI(context.getPath().toString(), false);
final ResourceSet resSet = new ResourceSetImpl();
resSet.getResource(uri, true);
return getDiagramInfos(resSet);
}

protected Collection<DiagramIntent> getDiagramInfos(final ResourceSet resourceSet) {
protected Collection<? extends DiagramIntent> getDiagramInfos(final ResourceSet resourceSet) {
for (final Resource resource : resourceSet.getResources()) {
for (final EObject eObject : resource.getContents()) {
if (supportsEObject(eObject)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,23 @@ protected void appendClass(final EClass eClass, final int genFlags, final String
}

protected void appendDataType(final EDataType dataType, final int genFlags, final StringBuilder buffer) {
appendClassStart(null, "class", dataType.getName(), getClassifierColor(dataType), buffer);
String link = null;
if (includes(genFlags, GEN_CLASS_HYPERLINKS)) {
link = diagramHelper.getEObjectHyperlink(dataType);
}
appendClassStart(null, "class", dataType.getName(), link, getClassifierColor(dataType), buffer);
if (dataType.getInstanceClassName() != null) {
appendAttribute(null, null, null, dataType.getInstanceClassName(), buffer);
}
appendClassEnd(buffer);
}

protected void appendEnum(final EEnum eEnum, final int genFlags, final StringBuilder buffer) {
appendClassStart(null, "enum", eEnum.getName(), getClassifierColor(eEnum), buffer);
String link = null;
if (includes(genFlags, GEN_CLASS_HYPERLINKS)) {
link = diagramHelper.getEObjectHyperlink(eEnum);
}
appendClassStart(null, "enum", eEnum.getName(), link, getClassifierColor(eEnum), buffer);
for (final EEnumLiteral literal : eEnum.getELiterals()) {
appendAttribute(null, null, literal.getName(), literal.getLiteral(), buffer);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package net.sourceforge.plantuml.ecore;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import org.eclipse.core.runtime.IPath;
import org.eclipse.emf.ecore.EModelElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;

import net.sourceforge.plantuml.util.AbstractDiagramIntent;
import net.sourceforge.plantuml.util.DiagramIntent;

public class EcoreDiagramIntentProvider extends AbstractEcoreDiagramIntentProvider {

public EcoreDiagramIntentProvider() {
super();
}

protected EcoreDiagramIntentProvider(final Class<?> editorType) {
super(editorType);
}

@Override
protected Boolean supportsPath(final IPath path) {
return "ecore".equals(path.getFileExtension()) || "xmi".equals(path.getFileExtension());
}

@Override
protected boolean supportsEObject(final EObject object) {
return true;
}

protected final EcoreDiagramHelper diagramHelper = new EcoreDiagramHelper();

protected EPackage getEPackage(final EObject selection) {
return diagramHelper.getAncestor(selection, EPackage.class);
}

public static boolean isEcoreClassDiagramObject(final Object object) {
return object instanceof EModelElement;
}

@Override
protected Collection<? extends DiagramIntent> getDiagramInfos(final EObject eObject) {
final Collection<AbstractDiagramIntent<?>> diagrams = new ArrayList<>();
final boolean isEcoreClassDiagram = isEcoreClassDiagramObject(eObject);
final EPackage pack = getEPackage(isEcoreClassDiagram ? eObject : eObject.eClass());
if (! isEcoreClassDiagram) {
diagrams.add(new EcoreObjectDiagramIntent(eObject));
}
if (pack != null) {
final EcoreClassDiagramIntent classDiagramIntent = new EcoreClassDiagramIntent(Collections.singletonList(pack));
if (! isEcoreClassDiagram) {
classDiagramIntent.setPriority(-1);
}
diagrams.add(classDiagramIntent);
}
return diagrams;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected Collection<DiagramIntent> getDiagramInfos(final WorkbenchEditorPartDia
}

@Override
protected boolean supportsPath(final IPath path) {
protected Boolean supportsPath(final IPath path) {
return "java".equals(path.getFileExtension());
}

Expand All @@ -54,7 +54,7 @@ private Collection<DiagramIntent> getDiagramInfos(final IFile file) {

@Override
protected Collection<DiagramIntent> getDiagramInfos(final WorkspaceDiagramIntentProviderContext context) {
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(context.getPath());
final IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(context.getPath());
return getDiagramInfos(file);
}
}
Loading

0 comments on commit aff43fb

Please sign in to comment.