Added fix for new relation creation.

This commit is contained in:
Andrew Lalis 2021-02-23 09:33:43 +01:00
parent 657aece1ac
commit 1cb513cb51
5 changed files with 31 additions and 5 deletions

View File

@ -6,7 +6,7 @@
<groupId>nl.andrewlalis</groupId> <groupId>nl.andrewlalis</groupId>
<artifactId>EntityRelationMappingEditor</artifactId> <artifactId>EntityRelationMappingEditor</artifactId>
<version>1.3.0</version> <version>1.3.1</version>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>

View File

@ -7,7 +7,7 @@ import nl.andrewlalis.erme.view.EditorFrame;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
public class EntityRelationMappingEditor { public class EntityRelationMappingEditor {
public static final String VERSION = "1.3.0"; public static final String VERSION = "1.3.1";
public static void main(String[] args) { public static void main(String[] args) {
if (!FlatLightLaf.install()) { if (!FlatLightLaf.install()) {

View File

@ -3,6 +3,7 @@ package nl.andrewlalis.erme.control.actions.edits;
import lombok.Setter; import lombok.Setter;
import nl.andrewlalis.erme.model.MappingModel; import nl.andrewlalis.erme.model.MappingModel;
import nl.andrewlalis.erme.model.Relation; import nl.andrewlalis.erme.model.Relation;
import nl.andrewlalis.erme.view.DiagramPanel;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -23,6 +24,9 @@ public class AddRelationAction extends AbstractAction {
@Setter @Setter
private MappingModel model; private MappingModel model;
@Setter
private DiagramPanel diagramPanel;
public AddRelationAction() { public AddRelationAction() {
super("Add Relation"); super("Add Relation");
this.putValue(SHORT_DESCRIPTION, "Add a new relation to the diagram."); this.putValue(SHORT_DESCRIPTION, "Add a new relation to the diagram.");
@ -39,9 +43,23 @@ public class AddRelationAction extends AbstractAction {
JOptionPane.PLAIN_MESSAGE JOptionPane.PLAIN_MESSAGE
); );
if (name != null) { if (name != null) {
final boolean isFirstRelation = this.model.getRelations().isEmpty();
Point p;
if (isFirstRelation) {
p = new Point(100, 100);
} else {
Rectangle bounds = this.model.getRelationBounds(); Rectangle bounds = this.model.getRelationBounds();
Point center = new Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2); p = new Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
this.model.addRelation(new Relation(this.model, center, name)); }
Relation r = new Relation(this.model, p, name);
this.model.getSelectedRelations().forEach(rl -> rl.setSelected(false));
r.setSelected(true);
this.model.addRelation(r);
if (isFirstRelation) {
this.model.normalizeRelationPositions();
this.diagramPanel.centerModel();
this.diagramPanel.repaint();
}
} }
} }
} }

View File

@ -73,6 +73,9 @@ public class MappingModel implements Serializable, Viewable {
} }
public Rectangle getRelationBounds() { public Rectangle getRelationBounds() {
if (this.getRelations().isEmpty()) {
return new Rectangle(0, 0, 0, 0);
}
int minX = Integer.MAX_VALUE; int minX = Integer.MAX_VALUE;
int minY = Integer.MAX_VALUE; int minY = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE; int maxX = Integer.MIN_VALUE;
@ -109,6 +112,10 @@ public class MappingModel implements Serializable, Viewable {
minX = Math.min(minX, r.getPosition().x); minX = Math.min(minX, r.getPosition().x);
minY = Math.min(minY, r.getPosition().y); minY = Math.min(minY, r.getPosition().y);
} }
if (this.getRelations().isEmpty()) {
minX = 0;
minY = 0;
}
for (Relation r : this.getRelations()) { for (Relation r : this.getRelations()) {
final Point current = r.getPosition(); final Point current = r.getPosition();
r.setPosition(new Point(current.x - minX, current.y - minY)); r.setPosition(new Point(current.x - minX, current.y - minY));

View File

@ -123,6 +123,7 @@ public class DiagramPanel extends JPanel implements ModelChangeListener {
LoadAction.getInstance().setDiagramPanel(this); LoadAction.getInstance().setDiagramPanel(this);
ExportToImageAction.getInstance().setModel(this.model); ExportToImageAction.getInstance().setModel(this.model);
AddRelationAction.getInstance().setModel(this.model); AddRelationAction.getInstance().setModel(this.model);
AddRelationAction.getInstance().setDiagramPanel(this);
RemoveRelationAction.getInstance().setModel(this.model); RemoveRelationAction.getInstance().setModel(this.model);
AddAttributeAction.getInstance().setModel(this.model); AddAttributeAction.getInstance().setModel(this.model);
RemoveAttributeAction.getInstance().setModel(this.model); RemoveAttributeAction.getInstance().setModel(this.model);