From 0fa8c0806c5f7383531fec7d0d44ed5c0a96aadf Mon Sep 17 00:00:00 2001 From: Bob Jacobsen Date: Sat, 25 Apr 2020 12:07:28 -0700 Subject: [PATCH] remove access to int SHAPE_POINT_n, add tests --- .../display/layoutEditor/LayoutEditor.java | 69 +++++++++++++- .../display/layoutEditor/LayoutShape.java | 15 +-- .../display/layoutEditor/LayoutTrack.java | 3 - .../layoutEditor/LayoutEditorTest.java | 93 ++++++++++++++++++- 4 files changed, 160 insertions(+), 20 deletions(-) diff --git a/java/src/jmri/jmrit/display/layoutEditor/LayoutEditor.java b/java/src/jmri/jmrit/display/layoutEditor/LayoutEditor.java index cfe1181ece1..ae07e757ad7 100644 --- a/java/src/jmri/jmrit/display/layoutEditor/LayoutEditor.java +++ b/java/src/jmri/jmrit/display/layoutEditor/LayoutEditor.java @@ -380,6 +380,10 @@ protected static boolean isPopupHitType(HitPointType hitType) { return result; } // isPopupHitType + // ***************************************************************** + // Turntable Ray support + // ***************************************************************** + /** * Find the 0-63 index with respect to TURNTABLE_RAY_0 * of a given enum entry. Throws {@link IllegalArgumentException} if @@ -391,7 +395,8 @@ protected static boolean isPopupHitType(HitPointType hitType) { */ protected int turntableTrackIndex() { int result = this.xmlValue - HitPointType.TURNTABLE_RAY_0.xmlValue; - if (result < 0) throw new IllegalArgumentException(this.toString()+ "is not a valid TURNTABLE_RAY"); + if (result < 0) throw new IllegalArgumentException(this.toString()+ "is not a valid TURNTABLE_RAY"); + if (result > 63) throw new IllegalArgumentException(this.toString()+ "is not a valid TURNTABLE_RAY"); return result; } @@ -427,6 +432,60 @@ protected static HitPointType[] turntableValues() { TURNTABLE_RAY_56, TURNTABLE_RAY_57, TURNTABLE_RAY_58, TURNTABLE_RAY_59, TURNTABLE_RAY_60, TURNTABLE_RAY_61, TURNTABLE_RAY_62, TURNTABLE_RAY_63 }; } + + // ***************************************************************** + // Shape Point support + // ***************************************************************** + + /** + * Find the 0-9 index with respect to SHAPE_POINT_0 + * of a given enum entry. Throws {@link IllegalArgumentException} if + * the given enum value isn't one of the SHAPE_POINT_0 entries. + *

+ * Ideally, this would be replaced by shape code that works + * directly with the enum values as a step toward using objects + * to implement hit points. + */ + protected int shapePointIndex() { + int result = this.xmlValue - HitPointType.SHAPE_POINT_0.xmlValue; + if (result < 0) throw new IllegalArgumentException(this.toString()+ "is not a valid SHAPE_POINT"); + if (result > 9) throw new IllegalArgumentException(this.toString()+ "is not a valid SHAPE_POINT"); + return result; + } + + /** + * Return a specific SHAPE_POINT from its 0-9 index. + * Throws {@link IllegalArgumentException} if + * the given index value isn't valid for the SHAPE_POINT entries. + *

+ * Ideally, this would be replaced by shape code that works + * directly with the enum values as a step toward using objects + * to implement hit points. + */ + protected static HitPointType shapePointIndexedValue(int i) { + if (i<0 || i>9 ) throw new IllegalArgumentException(i+ "is not a valid SHAPE_POINT index"); + return getValue(SHAPE_POINT_0.xmlValue+i); + } + + + /** + * Return an array of the valid SHAPE_POINT enum values. + * Meant for interations over the set of points. Order is + * from 0 to 9. + */ + protected static HitPointType[] shapePointValues() { + return new HitPointType[]{ + SHAPE_POINT_0, SHAPE_POINT_1, SHAPE_POINT_2, SHAPE_POINT_3, SHAPE_POINT_4, SHAPE_POINT_5, SHAPE_POINT_6, SHAPE_POINT_7, SHAPE_POINT_8, SHAPE_POINT_9 + }; + } + + protected static boolean isShapePointOffsetHitPointType(LayoutEditor.HitPointType t) { + return ((t.compareTo(SHAPE_POINT_0) >= 0) + && (t.compareTo(SHAPE_POINT_9) <= 0)); + } + + + } @@ -3269,7 +3328,7 @@ public void mousePressed(MouseEvent event) { selectedObject = null; // assume we're adding... for (LayoutShape ls : layoutShapes) { selectedHitPointType = ls.findHitPointType(dLoc, true); - if (LayoutShape.isShapePointOffsetHitPointType(selectedHitPointType)) { + if (LayoutEditor.HitPointType.isShapePointOffsetHitPointType(selectedHitPointType)) { //log.warn("extend selectedObject: ", lt); selectedObject = ls; // nope, we're extending beginLocation.setLocation(dLoc); @@ -3675,7 +3734,7 @@ public void mouseReleased(MouseEvent event) { _targetPanel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } else { LayoutShape ls = (LayoutShape) selectedObject; - ls.addPoint(currentPoint, selectedHitPointType.getXmlValue() - HitPointType.SHAPE_POINT_0.getXmlValue()); + ls.addPoint(currentPoint, selectedHitPointType.shapePointIndex()); } } else if (leToolBarPanel.signalMastButton.isSelected()) { addSignalMast(); @@ -4999,8 +5058,8 @@ public void mouseDragged(@Nonnull MouseEvent event) { ((TrackSegment) selectedObject).setBezierControlPoint(currentPoint, index); } else if ((selectedHitPointType == HitPointType.SHAPE_CENTER)) { ((LayoutShape) selectedObject).setCoordsCenter(currentPoint); - } else if (LayoutShape.isShapePointOffsetHitPointType(selectedHitPointType)) { - int index = selectedHitPointType.getXmlValue() - HitPointType.SHAPE_POINT_0.getXmlValue(); + } else if (LayoutEditor.HitPointType.isShapePointOffsetHitPointType(selectedHitPointType)) { + int index = selectedHitPointType.shapePointIndex(); ((LayoutShape) selectedObject).setPoint(index, currentPoint); } else if (HitPointType.isTurntableRayHitType(selectedHitPointType)) { LayoutTurntable turn = (LayoutTurntable) selectedObject; diff --git a/java/src/jmri/jmrit/display/layoutEditor/LayoutShape.java b/java/src/jmri/jmrit/display/layoutEditor/LayoutShape.java index 76ccd7db2b2..0a3f43f7a58 100644 --- a/java/src/jmri/jmrit/display/layoutEditor/LayoutShape.java +++ b/java/src/jmri/jmrit/display/layoutEditor/LayoutShape.java @@ -292,7 +292,7 @@ public int getNumberPoints() { * @return the maximum number of points */ public int getMaxNumberPoints() { - return LayoutEditor.HitPointType.SHAPE_POINT_9.getXmlValue() - LayoutEditor.HitPointType.SHAPE_POINT_0.getXmlValue() + 1; + return LayoutEditor.HitPointType.shapePointValues().length; } /** @@ -330,7 +330,7 @@ protected LayoutEditor.HitPointType findHitPointType(@Nonnull Point2D hitPoint, } for (int idx = 0; idx < shapePoints.size(); idx++) { if (r.contains(shapePoints.get(idx).getPoint())) { - result = LayoutEditor.HitPointType.getValue(LayoutEditor.HitPointType.SHAPE_POINT_0.getXmlValue() + idx); + result = LayoutEditor.HitPointType.shapePointIndexedValue(idx); break; } } @@ -340,7 +340,7 @@ protected LayoutEditor.HitPointType findHitPointType(@Nonnull Point2D hitPoint, distance = MathUtil.distance(shapePoints.get(idx).getPoint(), hitPoint); if (distance < minDistance) { minDistance = distance; - result = LayoutEditor.HitPointType.getValue(LayoutEditor.HitPointType.SHAPE_POINT_0.getXmlValue() + idx); + result = LayoutEditor.HitPointType.shapePointIndexedValue(idx); } } } @@ -349,12 +349,7 @@ protected LayoutEditor.HitPointType findHitPointType(@Nonnull Point2D hitPoint, public static boolean isShapeHitPointType(LayoutEditor.HitPointType t) { return ((t == LayoutEditor.HitPointType.SHAPE_CENTER) - || isShapePointOffsetHitPointType(t)); - } - - public static boolean isShapePointOffsetHitPointType(LayoutEditor.HitPointType t) { - return ((t.compareTo(LayoutEditor.HitPointType.SHAPE_POINT_0) >= 0) - && (t.compareTo(LayoutEditor.HitPointType.SHAPE_POINT_9) <= 0)); + || LayoutEditor.HitPointType.isShapePointOffsetHitPointType(t)); } /** @@ -428,7 +423,7 @@ protected JPopupMenu showShapePopUp(@CheckForNull MouseEvent mouseEvent, LayoutE popup = new JPopupMenu(); } if (layoutEditor.isEditable()) { - int pointIndex = hitPointType.getXmlValue() - LayoutEditor.HitPointType.SHAPE_POINT_0.getXmlValue(); + int pointIndex = hitPointType.shapePointIndex(); //JMenuItem jmi = popup.add(Bundle.getMessage("MakeLabel", Bundle.getMessage("LayoutShape")) + getName()); JMenuItem jmi = popup.add(Bundle.getMessage("ShapeNameMenuItemTitle", getName())); diff --git a/java/src/jmri/jmrit/display/layoutEditor/LayoutTrack.java b/java/src/jmri/jmrit/display/layoutEditor/LayoutTrack.java index 1daf6dd227e..ce104ad67f7 100644 --- a/java/src/jmri/jmrit/display/layoutEditor/LayoutTrack.java +++ b/java/src/jmri/jmrit/display/layoutEditor/LayoutTrack.java @@ -49,9 +49,6 @@ public abstract class LayoutTrack { // public static final int BEZIER_CONTROL_POINT_OFFSET_MIN = 30; // offset for TrackSegment Bezier control points (minimum) // public static final int BEZIER_CONTROL_POINT_OFFSET_MAX = 38; // offset for TrackSegment Bezier control points (maximum) // public static final int SHAPE_CENTER = 39; -// public static final int SHAPE_POINT_OFFSET_MIN = 40; // offset for Shape points (minimum) -// public static final int SHAPE_POINT_OFFSET_MAX = 49; // offset for Shape points (maximum) -// public static final int TURNTABLE_RAY_OFFSET = 50; // offset for turntable connection points // operational instance variables (not saved between sessions) protected LayoutEditor layoutEditor = null; protected String ident = ""; diff --git a/java/test/jmri/jmrit/display/layoutEditor/LayoutEditorTest.java b/java/test/jmri/jmrit/display/layoutEditor/LayoutEditorTest.java index 34a8c5f9436..e8b48c85ead 100644 --- a/java/test/jmri/jmrit/display/layoutEditor/LayoutEditorTest.java +++ b/java/test/jmri/jmrit/display/layoutEditor/LayoutEditorTest.java @@ -72,6 +72,10 @@ public void testDefaultCtor() { JUnitUtil.dispose(e); } + // ***************************************************************** + // Turntable Ray support + // ***************************************************************** + @Test public void testHPTturntableTrackIndexOK() { Assert.assertEquals(0, LayoutEditor.HitPointType.TURNTABLE_RAY_0.turntableTrackIndex()); @@ -114,7 +118,7 @@ public void testHPTturntableTrackIndexedValueBad2() { } @Test - public void turntableValues() { + public void testHPTturntableValues() { LayoutEditor.HitPointType[] array = LayoutEditor.HitPointType.turntableValues(); Assert.assertEquals(64, array.length); Assert.assertEquals(LayoutEditor.HitPointType.TURNTABLE_RAY_0, array[0]); @@ -124,11 +128,96 @@ public void turntableValues() { Assert.assertEquals(LayoutEditor.HitPointType.TURNTABLE_RAY_63, array[63]); // while available, use the indexing operations to test content - for (int i = 0; i<=63; i++) { + for (int i = 0; i