Implemented custom order alignment
- Implemented a custom model for a JList that allows for reordering elements using JButtons - Ask the user whether to sort alphabetically or using an order they decide - Ensured that the list in the OrderableListPanel keeps its order between multiple sorting times (this saves the user the work of re-sorting every time)
This commit is contained in:
parent
af00918502
commit
faadc40d26
|
@ -1,10 +1,10 @@
|
||||||
package nl.andrewlalis.erme.control.actions;
|
package nl.andrewlalis.erme.control.actions;
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
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 nl.andrewlalis.erme.view.DiagramPanel;
|
||||||
|
import nl.andrewlalis.erme.view.OrderableListPanel;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
@ -13,10 +13,22 @@ import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A class that implements an AutoPositionAction. This automatically (vertically) positions all relations in the model
|
||||||
|
* based either on alphabetic ordering (by name) or an order that's set by the user
|
||||||
|
*/
|
||||||
public class AutoPositionAction extends AbstractAction {
|
public class AutoPositionAction extends AbstractAction {
|
||||||
private static AutoPositionAction instance;
|
|
||||||
private final static int MARGIN = 10;
|
private final static int MARGIN = 10;
|
||||||
private final static int PADDING = 10;
|
private final static int PADDING = 10;
|
||||||
|
private static AutoPositionAction instance;
|
||||||
|
@Setter
|
||||||
|
private DiagramPanel diagramPanel;
|
||||||
|
@Setter
|
||||||
|
private MappingModel model;
|
||||||
|
public AutoPositionAction() {
|
||||||
|
super("Position Relations");
|
||||||
|
this.putValue(SHORT_DESCRIPTION, "Automatically Position Relations");
|
||||||
|
}
|
||||||
|
|
||||||
public static AutoPositionAction getInstance() {
|
public static AutoPositionAction getInstance() {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
|
@ -25,32 +37,63 @@ public class AutoPositionAction extends AbstractAction {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Setter
|
|
||||||
private DiagramPanel diagramPanel;
|
|
||||||
@Setter
|
|
||||||
private MappingModel model;
|
|
||||||
|
|
||||||
public AutoPositionAction() {
|
|
||||||
super("Position Relations");
|
|
||||||
this.putValue(SHORT_DESCRIPTION, "Automatically Position Relations");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent actionEvent) {
|
public void actionPerformed(ActionEvent actionEvent) {
|
||||||
this.positionRelations();
|
if (model.getRelations().size() == 0) {
|
||||||
|
JOptionPane.showMessageDialog(
|
||||||
|
null,
|
||||||
|
"Cannot position all relations when there are no relations present",
|
||||||
|
"Relations Required",
|
||||||
|
JOptionPane.WARNING_MESSAGE
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String[] choices = new String[]{"Alphabeticaly", "Custom Order"};
|
||||||
|
String choice = (String) JOptionPane.showInputDialog(
|
||||||
|
null,
|
||||||
|
"Select how to sort the relations",
|
||||||
|
"Position Relations",
|
||||||
|
JOptionPane.PLAIN_MESSAGE,
|
||||||
|
null,
|
||||||
|
choices,
|
||||||
|
0);
|
||||||
|
if (choice == null) return;
|
||||||
|
if (choice.equals(choices[0])) {
|
||||||
|
positionRelation();
|
||||||
|
} else if (choice.equals(choices[1])) {
|
||||||
|
JOptionPane.showConfirmDialog(
|
||||||
|
null,
|
||||||
|
OrderableListPanel.getInstance(),
|
||||||
|
"teststring",
|
||||||
|
JOptionPane.OK_CANCEL_OPTION,
|
||||||
|
JOptionPane.PLAIN_MESSAGE);
|
||||||
|
this.positionRelations(OrderableListPanel.getInstance().getOrderList());
|
||||||
|
}
|
||||||
diagramPanel.repaint();
|
diagramPanel.repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void positionRelations() {
|
/**
|
||||||
diagramPanel.resetTranslation();
|
* Sets the location on all relations in the orderList to be vertically aligned
|
||||||
ArrayList<Relation> relationList = new ArrayList<>(model.getRelations());
|
* @param orderList The ordered list of relations to be aligned
|
||||||
Collections.sort(relationList);
|
*/
|
||||||
if (relationList.isEmpty()) return;
|
private void positionRelations(ArrayList<Relation> orderList) {
|
||||||
|
if (orderList.isEmpty()) return;
|
||||||
|
|
||||||
int vertSpace = (int) relationList.get(0).getViewModel().getBounds(diagramPanel.getGraphics2D()).getHeight() + PADDING;
|
int vertSpace = (int) orderList.get(0).getViewModel().getBounds(diagramPanel.getGraphics2D()).getHeight() + PADDING;
|
||||||
AtomicInteger vertPos = new AtomicInteger(MARGIN);
|
AtomicInteger vertPos = new AtomicInteger(MARGIN);
|
||||||
relationList.forEach(r -> {
|
orderList.forEach(r -> {
|
||||||
r.setPosition(new Point(MARGIN, vertPos.getAndAdd(vertSpace)));
|
r.setPosition(new Point(MARGIN, vertPos.getAndAdd(vertSpace)));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
diagramPanel.centerModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an orderList by grabbing all relations and sorting them
|
||||||
|
*/
|
||||||
|
private void positionRelation() {
|
||||||
|
ArrayList<Relation> relationList = new ArrayList<>(model.getRelations());
|
||||||
|
Collections.sort(relationList);
|
||||||
|
positionRelations(relationList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package nl.andrewlalis.erme.model;
|
package nl.andrewlalis.erme.model;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import nl.andrewlalis.erme.view.OrderableListPanel;
|
||||||
import nl.andrewlalis.erme.view.view_models.MappingModelViewModel;
|
import nl.andrewlalis.erme.view.view_models.MappingModelViewModel;
|
||||||
import nl.andrewlalis.erme.view.view_models.ViewModel;
|
import nl.andrewlalis.erme.view.view_models.ViewModel;
|
||||||
|
|
||||||
|
@ -27,6 +28,7 @@ public class MappingModel implements Serializable, Viewable {
|
||||||
public MappingModel() {
|
public MappingModel() {
|
||||||
this.relations = new HashSet<>();
|
this.relations = new HashSet<>();
|
||||||
this.changeListeners = new HashSet<>();
|
this.changeListeners = new HashSet<>();
|
||||||
|
this.addChangeListener(OrderableListPanel.getInstance());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addRelation(Relation r) {
|
public void addRelation(Relation r) {
|
||||||
|
|
|
@ -131,6 +131,7 @@ public class DiagramPanel extends JPanel implements ModelChangeListener {
|
||||||
LolcatAction.getInstance().setDiagramPanel(this);
|
LolcatAction.getInstance().setDiagramPanel(this);
|
||||||
AutoPositionAction.getInstance().setDiagramPanel(this);
|
AutoPositionAction.getInstance().setDiagramPanel(this);
|
||||||
AutoPositionAction.getInstance().setModel(this.model);
|
AutoPositionAction.getInstance().setModel(this.model);
|
||||||
|
OrderableListPanel.getInstance().setModel(this.model);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void prepareGraphics(Graphics2D g) {
|
public static void prepareGraphics(Graphics2D g) {
|
||||||
|
|
|
@ -1,16 +1,63 @@
|
||||||
package nl.andrewlalis.erme.view;
|
package nl.andrewlalis.erme.view;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
import nl.andrewlalis.erme.model.Relation;
|
import nl.andrewlalis.erme.model.Relation;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
public class OrderableListModel {
|
/**
|
||||||
private ArrayList<Relation> list;
|
* A custom model for a ListModel that supports ordering by publically accessable methods
|
||||||
|
*/
|
||||||
|
public class OrderableListModel extends AbstractListModel<Relation> {
|
||||||
|
@Getter
|
||||||
|
private final ArrayList<Relation> list = new ArrayList<>();
|
||||||
|
|
||||||
|
public OrderableListModel() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addAll(Collection<Relation> relations) {
|
||||||
|
list.addAll(relations);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeAll(Collection<Relation> relations) {
|
||||||
|
list.removeAll(relations);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(Relation relation) {
|
||||||
|
list.add(relation);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void remove(Relation relation) {
|
||||||
|
list.remove(relation);
|
||||||
|
}
|
||||||
|
|
||||||
public void moveUp(int index) {
|
public void moveUp(int index) {
|
||||||
if (index > 0) {
|
if (index > 0) {
|
||||||
Collections.swap(list, index, index - 1);
|
Collections.swap(list, index, index - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.fireContentsChanged(this, index, index - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void moveDown(int index) {
|
||||||
|
if (index < this.getSize() - 1) {
|
||||||
|
Collections.swap(list, index, index + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.fireContentsChanged(this, index, index + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getSize() {
|
||||||
|
return list.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Relation getElementAt(int i) {
|
||||||
|
return list.get(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,82 @@
|
||||||
|
package nl.andrewlalis.erme.view;
|
||||||
|
|
||||||
|
import lombok.Setter;
|
||||||
|
import nl.andrewlalis.erme.model.MappingModel;
|
||||||
|
import nl.andrewlalis.erme.model.ModelChangeListener;
|
||||||
|
import nl.andrewlalis.erme.model.Relation;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A panel to be used in a popup that has a OrderableListModel implemented. Implements ModelChangeListener to be able
|
||||||
|
* to update the list of relations when new ones are added or ones are removed.
|
||||||
|
*/
|
||||||
|
public class OrderableListPanel extends JPanel implements ModelChangeListener {
|
||||||
|
private static OrderableListPanel instance;
|
||||||
|
private final JList<Relation> list;
|
||||||
|
private final OrderableListModel listModel;
|
||||||
|
@Setter
|
||||||
|
private MappingModel model;
|
||||||
|
|
||||||
|
private OrderableListPanel() {
|
||||||
|
list = new JList<>();
|
||||||
|
listModel = new OrderableListModel();
|
||||||
|
list.setModel(listModel);
|
||||||
|
|
||||||
|
JButton up = new JButton(new AbstractAction() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent actionEvent) {
|
||||||
|
if (list.isSelectionEmpty()) return;
|
||||||
|
listModel.moveUp(list.getSelectedIndex());
|
||||||
|
list.setSelectedIndex(list.getSelectedIndex() - 1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
up.setText("\u2227");
|
||||||
|
JButton down = new JButton(new AbstractAction() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent actionEvent) {
|
||||||
|
if (list.isSelectionEmpty()) return;
|
||||||
|
listModel.moveDown(list.getSelectedIndex());
|
||||||
|
list.setSelectedIndex(list.getSelectedIndex() + 1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
down.setText("\u2228");
|
||||||
|
|
||||||
|
this.add(list);
|
||||||
|
this.add(up);
|
||||||
|
this.add(down);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static OrderableListPanel getInstance() {
|
||||||
|
if (instance == null) {
|
||||||
|
instance = new OrderableListPanel();
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates removed and new relations in the listModel. Does it in a special way to preserve existing ordering in the
|
||||||
|
* list, so user has to do minimal re-sorting.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onModelChanged() {
|
||||||
|
if (this.model == null) return;
|
||||||
|
Set<Relation> newRelations = new HashSet<>(model.getRelations());
|
||||||
|
newRelations.removeAll(listModel.getList());
|
||||||
|
|
||||||
|
Set<Relation> removedRelations = new HashSet<>(listModel.getList());
|
||||||
|
removedRelations.removeAll(model.getRelations());
|
||||||
|
|
||||||
|
listModel.removeAll(removedRelations);
|
||||||
|
listModel.addAll(newRelations);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Relation> getOrderList() {
|
||||||
|
return new ArrayList<>(listModel.getList());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue