From af00918502c1f4d3df270a243fe136bea30c4c33 Mon Sep 17 00:00:00 2001 From: Bjorn Pijnacker Date: Fri, 26 Feb 2021 20:23:42 +0100 Subject: [PATCH] Implemented rudimentary auto-positioning feature using alphabetic sorting of relations Now going to be working on custom sorting --- .../control/actions/AutoPositionAction.java | 56 +++++++++++++++++++ .../nl/andrewlalis/erme/model/Relation.java | 7 ++- .../andrewlalis/erme/view/DiagramPanel.java | 2 + .../andrewlalis/erme/view/EditorMenuBar.java | 1 + .../erme/view/OrderableListModel.java | 16 ++++++ 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/main/java/nl/andrewlalis/erme/control/actions/AutoPositionAction.java create mode 100644 src/main/java/nl/andrewlalis/erme/view/OrderableListModel.java diff --git a/src/main/java/nl/andrewlalis/erme/control/actions/AutoPositionAction.java b/src/main/java/nl/andrewlalis/erme/control/actions/AutoPositionAction.java new file mode 100644 index 0000000..3b46629 --- /dev/null +++ b/src/main/java/nl/andrewlalis/erme/control/actions/AutoPositionAction.java @@ -0,0 +1,56 @@ +package nl.andrewlalis.erme.control.actions; + +import lombok.Getter; +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.*; +import java.awt.event.ActionEvent; +import java.util.ArrayList; +import java.util.Collections; +import java.util.concurrent.atomic.AtomicInteger; + +public class AutoPositionAction extends AbstractAction { + private static AutoPositionAction instance; + private final static int MARGIN = 10; + private final static int PADDING = 10; + + public static AutoPositionAction getInstance() { + if (instance == null) { + instance = new AutoPositionAction(); + } + return instance; + } + + @Setter + private DiagramPanel diagramPanel; + @Setter + private MappingModel model; + + public AutoPositionAction() { + super("Position Relations"); + this.putValue(SHORT_DESCRIPTION, "Automatically Position Relations"); + } + + @Override + public void actionPerformed(ActionEvent actionEvent) { + this.positionRelations(); + diagramPanel.repaint(); + } + + private void positionRelations() { + diagramPanel.resetTranslation(); + ArrayList relationList = new ArrayList<>(model.getRelations()); + Collections.sort(relationList); + if (relationList.isEmpty()) return; + + int vertSpace = (int) relationList.get(0).getViewModel().getBounds(diagramPanel.getGraphics2D()).getHeight() + PADDING; + AtomicInteger vertPos = new AtomicInteger(MARGIN); + relationList.forEach(r -> { + r.setPosition(new Point(MARGIN, vertPos.getAndAdd(vertSpace))); + }); + } +} diff --git a/src/main/java/nl/andrewlalis/erme/model/Relation.java b/src/main/java/nl/andrewlalis/erme/model/Relation.java index 05c0e66..8393002 100644 --- a/src/main/java/nl/andrewlalis/erme/model/Relation.java +++ b/src/main/java/nl/andrewlalis/erme/model/Relation.java @@ -15,7 +15,7 @@ import java.util.stream.Collectors; * Represents a single "relation" or table in the diagram. */ @Getter -public class Relation implements Serializable, Viewable { +public class Relation implements Serializable, Viewable, Comparable { private final MappingModel model; private Point position; private String name; @@ -97,4 +97,9 @@ public class Relation implements Serializable, Viewable { this.getAttributes().forEach(a -> c.addAttribute(a.copy(c))); return c; } + + @Override + public int compareTo(Relation relation) { + return this.name.compareTo(relation.name); + } } diff --git a/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java b/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java index 54bc65e..ba57981 100644 --- a/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java +++ b/src/main/java/nl/andrewlalis/erme/view/DiagramPanel.java @@ -129,6 +129,8 @@ public class DiagramPanel extends JPanel implements ModelChangeListener { RemoveAttributeAction.getInstance().setModel(this.model); LoadSampleModelAction.getInstance().setDiagramPanel(this); LolcatAction.getInstance().setDiagramPanel(this); + AutoPositionAction.getInstance().setDiagramPanel(this); + AutoPositionAction.getInstance().setModel(this.model); } public static void prepareGraphics(Graphics2D g) { diff --git a/src/main/java/nl/andrewlalis/erme/view/EditorMenuBar.java b/src/main/java/nl/andrewlalis/erme/view/EditorMenuBar.java index 8710c3d..afd76fa 100644 --- a/src/main/java/nl/andrewlalis/erme/view/EditorMenuBar.java +++ b/src/main/java/nl/andrewlalis/erme/view/EditorMenuBar.java @@ -40,6 +40,7 @@ public class EditorMenuBar extends JMenuBar { menu.add(AddAttributeAction.getInstance()); menu.add(RemoveAttributeAction.getInstance()); menu.add(new JCheckBoxMenuItem(LolcatAction.getInstance())); + menu.add(AutoPositionAction.getInstance()); menu.addSeparator(); menu.add(UndoAction.getInstance()); menu.add(RedoAction.getInstance()); diff --git a/src/main/java/nl/andrewlalis/erme/view/OrderableListModel.java b/src/main/java/nl/andrewlalis/erme/view/OrderableListModel.java new file mode 100644 index 0000000..c3d29c9 --- /dev/null +++ b/src/main/java/nl/andrewlalis/erme/view/OrderableListModel.java @@ -0,0 +1,16 @@ +package nl.andrewlalis.erme.view; + +import nl.andrewlalis.erme.model.Relation; + +import java.util.ArrayList; +import java.util.Collections; + +public class OrderableListModel { + private ArrayList list; + + public void moveUp(int index) { + if (index > 0) { + Collections.swap(list, index, index - 1); + } + } +}