diff --git a/pom.xml b/pom.xml
index 5cb56d7..b8ae243 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
nl.andrewlalis
EntityRelationMappingEditor
- 1.3.0
+ 1.3.1
diff --git a/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java b/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java
index d85cf52..6099536 100644
--- a/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java
+++ b/src/main/java/nl/andrewlalis/erme/EntityRelationMappingEditor.java
@@ -7,7 +7,7 @@ import nl.andrewlalis.erme.view.EditorFrame;
import java.nio.charset.StandardCharsets;
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) {
if (!FlatLightLaf.install()) {
diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/edits/AddRelationAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/edits/AddRelationAction.java
index 6e92623..5dfd813 100644
--- a/src/main/java/nl/andrewlalis/erme/control/actions/edits/AddRelationAction.java
+++ b/src/main/java/nl/andrewlalis/erme/control/actions/edits/AddRelationAction.java
@@ -3,6 +3,7 @@ package nl.andrewlalis.erme.control.actions.edits;
import lombok.Setter;
import nl.andrewlalis.erme.model.MappingModel;
import nl.andrewlalis.erme.model.Relation;
+import nl.andrewlalis.erme.view.DiagramPanel;
import javax.swing.*;
import java.awt.*;
@@ -23,6 +24,9 @@ public class AddRelationAction extends AbstractAction {
@Setter
private MappingModel model;
+ @Setter
+ private DiagramPanel diagramPanel;
+
public AddRelationAction() {
super("Add Relation");
this.putValue(SHORT_DESCRIPTION, "Add a new relation to the diagram.");
@@ -39,9 +43,23 @@ public class AddRelationAction extends AbstractAction {
JOptionPane.PLAIN_MESSAGE
);
if (name != null) {
- Rectangle bounds = this.model.getRelationBounds();
- Point center = new Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
- this.model.addRelation(new Relation(this.model, center, name));
+ final boolean isFirstRelation = this.model.getRelations().isEmpty();
+ Point p;
+ if (isFirstRelation) {
+ p = new Point(100, 100);
+ } else {
+ Rectangle bounds = this.model.getRelationBounds();
+ p = new Point(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
+ }
+ 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();
+ }
}
}
}
diff --git a/src/main/java/nl/andrewlalis/erme/model/MappingModel.java b/src/main/java/nl/andrewlalis/erme/model/MappingModel.java
index 3a37643..b7ed500 100644
--- a/src/main/java/nl/andrewlalis/erme/model/MappingModel.java
+++ b/src/main/java/nl/andrewlalis/erme/model/MappingModel.java
@@ -73,6 +73,9 @@ public class MappingModel implements Serializable, Viewable {
}
public Rectangle getRelationBounds() {
+ if (this.getRelations().isEmpty()) {
+ return new Rectangle(0, 0, 0, 0);
+ }
int minX = Integer.MAX_VALUE;
int minY = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE;
@@ -109,6 +112,10 @@ public class MappingModel implements Serializable, Viewable {
minX = Math.min(minX, r.getPosition().x);
minY = Math.min(minY, r.getPosition().y);
}
+ if (this.getRelations().isEmpty()) {
+ minX = 0;
+ minY = 0;
+ }
for (Relation r : this.getRelations()) {
final Point current = r.getPosition();
r.setPosition(new Point(current.x - minX, current.y - minY));
diff --git a/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java b/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java
index 8e805e2..8210b1e 100644
--- a/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java
+++ b/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java
@@ -123,6 +123,7 @@ public class DiagramPanel extends JPanel implements ModelChangeListener {
LoadAction.getInstance().setDiagramPanel(this);
ExportToImageAction.getInstance().setModel(this.model);
AddRelationAction.getInstance().setModel(this.model);
+ AddRelationAction.getInstance().setDiagramPanel(this);
RemoveRelationAction.getInstance().setModel(this.model);
AddAttributeAction.getInstance().setModel(this.model);
RemoveAttributeAction.getInstance().setModel(this.model);