You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2017/10/20 16:29:34 UTC

svn commit: r1812761 - in /pivot/trunk/wtk: src/org/apache/pivot/wtk/Dimensions.java src/org/apache/pivot/wtk/Insets.java test/org/apache/pivot/wtk/test/DimensionsTest.java test/org/apache/pivot/wtk/test/InsetsTest.java

Author: rwhitcomb
Date: Fri Oct 20 16:29:34 2017
New Revision: 1812761

URL: http://svn.apache.org/viewvc?rev=1812761&view=rev
Log:
Some enhancements to Insets and Dimensions:
* Create a "square" Dimensions from a single integer that has
  that as both the width and height.
* Create an Insets from a total width and height; divide the
  total into left/right and top/bottom, leaving any (odd) extra
  value on the right or bottom.
* Create an Insets, then, from a Dimensions (using the new
  width/height constructor).
* Create a "getSize()" method for Insets that returns the
  Dimensions value containing both width and height.

Test all these changes in the DimensionsTest and InsetsTest classes.

Modified:
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Dimensions.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Insets.java
    pivot/trunk/wtk/test/org/apache/pivot/wtk/test/DimensionsTest.java
    pivot/trunk/wtk/test/org/apache/pivot/wtk/test/InsetsTest.java

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Dimensions.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Dimensions.java?rev=1812761&r1=1812760&r2=1812761&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Dimensions.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Dimensions.java Fri Oct 20 16:29:34 2017
@@ -36,7 +36,17 @@ public final class Dimensions implements
     public static final String WIDTH_KEY = "width";
     public static final String HEIGHT_KEY = "height";
 
-    public static final Dimensions ZERO = new Dimensions(0, 0);
+    public static final Dimensions ZERO = new Dimensions(0);
+
+    /**
+     * Construct a "square" dimensions that has the same width
+     * as height.
+     *
+     * @param side The width and height of this dimensions.
+     */
+    public Dimensions(int side) {
+        this.width = this.height = side;
+    }
 
     public Dimensions(int width, int height) {
         this.width = width;

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Insets.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Insets.java?rev=1812761&r1=1812760&r2=1812761&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Insets.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Insets.java Fri Oct 20 16:29:34 2017
@@ -70,6 +70,43 @@ public final class Insets implements Ser
         this.right = right;
     }
 
+    /**
+     * Construct an <tt>Insets</tt> value given the total
+     * height and width values to produce.
+     * <p> This will assign half the height to each of the top
+     * and bottom, and half the width each to the left and right.
+     * <p> Any excess (for odd values) will be assigned to the bottom
+     * and right respectively.
+     *
+     * @param height  The total height to assign to this Insets value.
+     * @param width   The total width to assign.
+     * @see #getHeight
+     * @see #getWidth
+     */
+    public Insets(int height, int width) {
+        this.top = height / 2;
+        // For odd height, assign the excess to the bottom
+        this.bottom = height - this.top;
+        this.left = width / 2;
+        // Ditto for width, excess on the right
+        this.right = width - this.left;
+    }
+
+    /**
+     * Construct an <tt>Insets</tt> value given the total
+     * dimensions of the value to produce.
+     * <p> This will assign half the dimensions height to each of the top
+     * and bottom, and half the dimensions width each to the left and right.
+     * <p> Any excess (for odd values) will be assigned to the right
+     * and bottom respectively.
+     *
+     * @param size The total size (height and width) to assign.
+     * @see #getSize
+     */
+    public Insets(Dimensions size) {
+        this(size.height, size.width);
+    }
+
     public Insets(Insets insets) {
         Utils.checkNull(insets, "padding/margin");
 
@@ -82,10 +119,10 @@ public final class Insets implements Ser
     public Insets(Dictionary<String, ?> insets) {
         Utils.checkNull(insets, "padding/margin");
 
-        top = insets.getInt(TOP_KEY);
-        left = insets.getInt(LEFT_KEY);
-        bottom = insets.getInt(BOTTOM_KEY);
-        right = insets.getInt(RIGHT_KEY);
+        this.top = insets.getInt(TOP_KEY);
+        this.left = insets.getInt(LEFT_KEY);
+        this.bottom = insets.getInt(BOTTOM_KEY);
+        this.right = insets.getInt(RIGHT_KEY);
 
     }
 
@@ -105,6 +142,19 @@ public final class Insets implements Ser
         return top + bottom;
     }
 
+    /**
+     * Return the total size of this insets value as a single
+     * <tt>Dimensions</tt> value.
+     *
+     * @return The total width and height of this object.
+     * @see #getWidth
+     * @see #getHeight
+     * @see #Insets(Dimensions)
+     */
+    public Dimensions getSize() {
+        return new Dimensions(left + right, top + bottom);
+    }
+
     @Override
     public boolean equals(Object object) {
         boolean equals = false;

Modified: pivot/trunk/wtk/test/org/apache/pivot/wtk/test/DimensionsTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/test/org/apache/pivot/wtk/test/DimensionsTest.java?rev=1812761&r1=1812760&r2=1812761&view=diff
==============================================================================
--- pivot/trunk/wtk/test/org/apache/pivot/wtk/test/DimensionsTest.java (original)
+++ pivot/trunk/wtk/test/org/apache/pivot/wtk/test/DimensionsTest.java Fri Oct 20 16:29:34 2017
@@ -34,9 +34,15 @@ public class DimensionsTest {
     public void test() {
         Dimensions zero = Dimensions.ZERO;
         Dimensions zero_a = new Dimensions(0, 0);
+        Dimensions zero_b = new Dimensions(0);
         Dimensions one = new Dimensions(1, 1);
         Dimensions one_a = zero.expand(1);
-        Dimensions zero_b = one_a.expand(-1, -1);
+        Dimensions zero_c = one_a.expand(-1, -1);
+
+        Dimensions seven = new Dimensions(7);
+        Dimensions seven_a = new Dimensions(7, 7);
+        Dimensions seven_b = zero.expand(7);
+        Dimensions seven_c = zero_a.expand(7, 7);
 
         Dimensions a = Dimensions.decode("2 x 3");
         Dimensions a_1 = new Dimensions(2, 3);
@@ -55,7 +61,12 @@ public class DimensionsTest {
 
         assertEquals(zero, zero_a);
         assertEquals(one, one_a);
-        assertEquals(zero, zero_b);
+        assertEquals(zero_a, zero_b);
+        assertEquals(zero, zero_c);
+
+        assertEquals(seven, seven_a);
+        assertEquals(seven, seven_b);
+        assertEquals(seven_b, seven_c);
 
         assertEquals(a, a_1);
         assertEquals(b, b_1);

Modified: pivot/trunk/wtk/test/org/apache/pivot/wtk/test/InsetsTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/test/org/apache/pivot/wtk/test/InsetsTest.java?rev=1812761&r1=1812760&r2=1812761&view=diff
==============================================================================
--- pivot/trunk/wtk/test/org/apache/pivot/wtk/test/InsetsTest.java (original)
+++ pivot/trunk/wtk/test/org/apache/pivot/wtk/test/InsetsTest.java Fri Oct 20 16:29:34 2017
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertTru
 
 import org.junit.Test;
 
+import org.apache.pivot.wtk.Dimensions;
 import org.apache.pivot.wtk.Insets;
 
 
@@ -47,6 +48,12 @@ public class InsetsTest {
         Insets i4 = new Insets(5, 6, 7, 8);
         Insets i4a = Insets.decode("5, 6; 7, 8");
 
+        Dimensions dim5 = new Dimensions(5);
+        Insets i5 = new Insets(dim5);
+        Insets i5a = new Insets(2, 2, 3, 3);
+        Dimensions dim5a = i5.getSize();
+        Dimensions dim5b = i5a.getSize();
+
         assertEquals(i0, i0a);
         assertEquals(i1, i1a);
         assertEquals(i2, i2a);
@@ -64,6 +71,11 @@ public class InsetsTest {
         assertEquals(i4a.getWidth(), 14);
         assertEquals(i4a.getHeight(), 12);
         assertEquals(i4a.toString(), "Insets [5, 6, 7, 8]");
+
+        assertEquals(i5, i5a);
+        assertEquals(dim5, dim5a);
+        assertEquals(dim5a, dim5b);
+        assertEquals(i5.toString(), "Insets [2, 2, 3, 3]");
     }
 
 }