Skip to content

Commit

Permalink
Add TestBuilder Sort by Num Points
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-jts committed May 28, 2024
1 parent 3d75eb1 commit c1c8310
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class GeometryTreeModel implements TreeModel
public static Comparator<GeometricObjectNode> SORT_AREA_DESC = new AreaComparator(true);
public static Comparator<GeometricObjectNode> SORT_LEN_ASC = new LengthComparator(false);
public static Comparator<GeometricObjectNode> SORT_LEN_DESC = new LengthComparator(true);
public static Comparator<GeometricObjectNode> SORT_NUMPTS_ASC = new NumPointsComparator(false);
public static Comparator<GeometricObjectNode> SORT_NUMPTS_DESC = new NumPointsComparator(true);

private Vector<TreeModelListener> treeModelListeners = new Vector<TreeModelListener>();

Expand Down Expand Up @@ -153,6 +155,21 @@ public int compare(GeometricObjectNode o1, GeometricObjectNode o2) {
return dirFactor * Double.compare(area1, area2);
}
}
public static class NumPointsComparator implements Comparator<GeometricObjectNode> {

private int dirFactor;

public NumPointsComparator(boolean direction) {
this.dirFactor = direction ? 1 : -1;
}

@Override
public int compare(GeometricObjectNode o1, GeometricObjectNode o2) {
int num1 = o1.getGeometry().getNumPoints();
int num2 = o2.getGeometry().getNumPoints();
return dirFactor * Integer.compare(num1, num2);
}
}
}

abstract class GeometricObjectNode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public class InspectorPanel extends TestBuilderPanel {
private Geometry geometry;

private Comparator<GeometricObjectNode> sorterArea;

private Comparator<GeometricObjectNode> sorterLen;
private Comparator<GeometricObjectNode> sorterNumPoints;

public InspectorPanel() {
this(true);
Expand Down Expand Up @@ -128,6 +128,11 @@ public void actionPerformed(ActionEvent e) {
sortByLen();
}
});
JButton btnSortByNumPts = SwingUtil.createButton(AppIcons.ICON_POINT, "Sort by Num Points (Asc/Desc)", new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
sortByNumPoints();
}
});

JPanel btn2Panel = new JPanel();
btn2Panel.setLayout(new BoxLayout(btn2Panel, BoxLayout.PAGE_AXIS));
Expand All @@ -148,6 +153,7 @@ public void actionPerformed(ActionEvent e)

btn2Panel.add(Box.createRigidArea(new Dimension(0, 10)));
btn2Panel.add(new JLabel("Sort"));
btn2Panel.add(btnSortByNumPts);
btn2Panel.add(btnSortByLen);
btn2Panel.add(btnSortByArea);
btn2Panel.add(btnSortNone);
Expand Down Expand Up @@ -214,12 +220,14 @@ public void sortNone()
{
sorterLen = null;
sorterArea = null;
sorterNumPoints = null;
geomTreePanel.populate(geometry, source);
}

public void sortByArea()
{
sorterLen = null;
sorterNumPoints = null;

if (sorterArea == GeometryTreeModel.SORT_AREA_ASC) {
sorterArea = GeometryTreeModel.SORT_AREA_DESC;
Expand All @@ -233,6 +241,8 @@ public void sortByArea()
public void sortByLen()
{
sorterArea = null;
sorterNumPoints = null;

if (sorterLen == GeometryTreeModel.SORT_LEN_ASC) {
sorterLen = GeometryTreeModel.SORT_LEN_DESC;
}
Expand All @@ -242,6 +252,18 @@ public void sortByLen()
geomTreePanel.populate(geometry, source, sorterLen);
}


public void sortByNumPoints()
{
sorterArea = null;
sorterLen = null;

if (sorterNumPoints == GeometryTreeModel.SORT_NUMPTS_ASC) {
sorterNumPoints = GeometryTreeModel.SORT_NUMPTS_DESC;
}
else {
sorterNumPoints = GeometryTreeModel.SORT_NUMPTS_ASC;
}
geomTreePanel.populate(geometry, source, sorterNumPoints);
}

}

0 comments on commit c1c8310

Please sign in to comment.