Implemented rudimentary auto-positioning feature using alphabetic sorting of relations
Now going to be working on custom sorting
This commit is contained in:
parent
3ba2f1982c
commit
af00918502
|
@ -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<Relation> 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)));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,7 +15,7 @@ import java.util.stream.Collectors;
|
||||||
* Represents a single "relation" or table in the diagram.
|
* Represents a single "relation" or table in the diagram.
|
||||||
*/
|
*/
|
||||||
@Getter
|
@Getter
|
||||||
public class Relation implements Serializable, Viewable {
|
public class Relation implements Serializable, Viewable, Comparable<Relation> {
|
||||||
private final MappingModel model;
|
private final MappingModel model;
|
||||||
private Point position;
|
private Point position;
|
||||||
private String name;
|
private String name;
|
||||||
|
@ -97,4 +97,9 @@ public class Relation implements Serializable, Viewable {
|
||||||
this.getAttributes().forEach(a -> c.addAttribute(a.copy(c)));
|
this.getAttributes().forEach(a -> c.addAttribute(a.copy(c)));
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Relation relation) {
|
||||||
|
return this.name.compareTo(relation.name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -129,6 +129,8 @@ public class DiagramPanel extends JPanel implements ModelChangeListener {
|
||||||
RemoveAttributeAction.getInstance().setModel(this.model);
|
RemoveAttributeAction.getInstance().setModel(this.model);
|
||||||
LoadSampleModelAction.getInstance().setDiagramPanel(this);
|
LoadSampleModelAction.getInstance().setDiagramPanel(this);
|
||||||
LolcatAction.getInstance().setDiagramPanel(this);
|
LolcatAction.getInstance().setDiagramPanel(this);
|
||||||
|
AutoPositionAction.getInstance().setDiagramPanel(this);
|
||||||
|
AutoPositionAction.getInstance().setModel(this.model);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void prepareGraphics(Graphics2D g) {
|
public static void prepareGraphics(Graphics2D g) {
|
||||||
|
|
|
@ -40,6 +40,7 @@ public class EditorMenuBar extends JMenuBar {
|
||||||
menu.add(AddAttributeAction.getInstance());
|
menu.add(AddAttributeAction.getInstance());
|
||||||
menu.add(RemoveAttributeAction.getInstance());
|
menu.add(RemoveAttributeAction.getInstance());
|
||||||
menu.add(new JCheckBoxMenuItem(LolcatAction.getInstance()));
|
menu.add(new JCheckBoxMenuItem(LolcatAction.getInstance()));
|
||||||
|
menu.add(AutoPositionAction.getInstance());
|
||||||
menu.addSeparator();
|
menu.addSeparator();
|
||||||
menu.add(UndoAction.getInstance());
|
menu.add(UndoAction.getInstance());
|
||||||
menu.add(RedoAction.getInstance());
|
menu.add(RedoAction.getInstance());
|
||||||
|
|
|
@ -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<Relation> list;
|
||||||
|
|
||||||
|
public void moveUp(int index) {
|
||||||
|
if (index > 0) {
|
||||||
|
Collections.swap(list, index, index - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue