Revision: 959
Author: allain.lalonde
Date: Tue Jan 26 08:58:01 2010
Log: Fixing some find bugs warnings and Making PText use an enum for specification of HorizontalAlignment, making a bunch of error checking unnecessary.
http://code.google.com/p/piccolo2d/source/detail?r=959

Modified:
/piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PNode.java /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/activities/PActivityScheduler.java /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/event/PDragSequenceEventHandler.java /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/nodes/PImage.java /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/nodes/PText.java /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PBounds.java /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PDebug.java /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/nodes/PTextTest.java /piccolo2d.java/branches/2.0-spike/extras/src/main/java/org/piccolo2d/extras/util/PFixedWidthStroke.java

=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PNode.java Tue Jan 26 08:27:02 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/PNode.java Tue Jan 26 08:58:01 2010
@@ -1478,10 +1478,8 @@
             return true;
         }
         // Don't put any invalidating code here or else nodes with volatile
-        // bounds will
- // create a soft infinite loop (calling Swing.invokeLater()) when they
-        // validate
-        // their bounds.
+ // bounds will create a soft infinite loop (calling Swing.invokeLater())
+        // when they validate their bounds.
         return false;
     }

=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/activities/PActivityScheduler.java Tue Jan 26 06:08:26 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/activities/PActivityScheduler.java Tue Jan 26 08:58:01 2010
@@ -57,12 +57,14 @@
 public class PActivityScheduler implements Serializable {
     private static final long serialVersionUID = 1L;
     private transient Timer activityTimer = null;
+    private transient final List<PActivity> processingActivities;
     private final PRoot root;
     private final List<PActivity> activities;
+
+
     private boolean activitiesChanged;
     private boolean animating;
-    private final List<PActivity> processingActivities;
-
+
     /**
      * Constructs an instance of PActivityScheduler. All activities it will
      * schedule will take place on children of the rootNode provided.
@@ -171,7 +173,7 @@
         if (!activities.isEmpty()) {
             processingActivities.clear();
             processingActivities.addAll(activities);
-
+
final List<PActivity> clonedActivities = new ArrayList<PActivity>(processingActivities);
             for (PActivity each : reverse(clonedActivities)) {
                 each.processStep(currentTime);
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/event/PDragSequenceEventHandler.java Tue Jan 19 12:39:24 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/event/PDragSequenceEventHandler.java Tue Jan 26 08:58:01 2010
@@ -59,6 +59,7 @@

     /** Constructs a drag sequence event handler instance. */
     public PDragSequenceEventHandler() {
+        dragActivity = null;
     }

     /**
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/nodes/PImage.java Tue Jan 19 12:39:24 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/nodes/PImage.java Tue Jan 26 08:58:01 2010
@@ -34,6 +34,7 @@
 import java.awt.Image;
 import java.awt.MediaTracker;
 import java.awt.Toolkit;
+import java.awt.geom.Rectangle2D;
 import java.awt.image.BufferedImage;
 import java.io.IOException;
 import java.io.ObjectInputStream;
@@ -46,7 +47,6 @@
 import org.piccolo2d.util.PBounds;
 import org.piccolo2d.util.PPaintContext;

-
 /**
* <b>PImage</b> is a wrapper around a java.awt.Image. If this node is copied or * serialized that image will be converted into a BufferedImage if it is not
@@ -75,7 +75,7 @@
* {...@link #getImage getImage}). Both old and new value will be set correctly
      * to Image objects in any property change event.
      */
-
+
     public static final int PROPERTY_CODE_IMAGE = 1 << 15;

     private transient Image image;
@@ -188,21 +188,20 @@
             return;
         }

-        final double iw = image.getWidth(null);
-        final double ih = image.getHeight(null);
+ final Rectangle2D imageRect = new Rectangle2D.Double(0, 0, image.getWidth(null), image.getHeight(null));

         final PBounds b = getBoundsReference();
         final Graphics2D g2 = paintContext.getGraphics();

-        if (b.x != 0 || b.y != 0 || b.width != iw || b.height != ih) {
-            g2.translate(b.x, b.y);
-            g2.scale(b.width / iw, b.height / ih);
+        if (imageRect.equals(b)) {
             g2.drawImage(image, 0, 0, null);
-            g2.scale(iw / b.width, ih / b.height);
-            g2.translate(-b.x, -b.y);
         }
         else {
+            g2.translate(b.x, b.y);
+ g2.scale(b.width / imageRect.getWidth(), b.height / imageRect.getHeight());
             g2.drawImage(image, 0, 0, null);
+ g2.scale(imageRect.getWidth() / b.width, imageRect.getHeight() / b.height);
+            g2.translate(-b.x, -b.y);
         }

     }
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/nodes/PText.java Sat Jan 23 18:48:30 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/nodes/PText.java Tue Jan 26 08:58:01 2010
@@ -160,8 +160,12 @@
     protected double greekThreshold = DEFAULT_GREEK_THRESHOLD;

     /** Horizontal alignment for this text node. */
-    private float horizontalAlignment = DEFAULT_HORIZONTAL_ALIGNMENT;
-
+ private HorizontalAlignment horizontalAlignment = HorizontalAlignment.LEFT;
+
+    public enum HorizontalAlignment {
+        LEFT, CENTER, RIGHT
+    }
+
     /**
* True if this text node should constrain its height to the height of its
      * text.
@@ -205,7 +209,7 @@
      * @since 1.3
      * @return the horizontal alignment for this text node
      */
-    public float getHorizontalAlignment() {
+    public HorizontalAlignment getHorizontalAlignment() {
         return horizontalAlignment;
     }

@@ -219,30 +223,9 @@
      *            <code>Component.CENTER_ALIGNMENT</code>, or
      *            <code>Component.RIGHT_ALIGNMENT</code>
      */
-    public void setHorizontalAlignment(final float horizontalAlignment) {
-        if (!validHorizontalAlignment(horizontalAlignment)) {
- throw new IllegalArgumentException("horizontalAlignment must be one of Component.LEFT_ALIGNMENT, " - + "Component.CENTER_ALIGNMENT, or Component.RIGHT_ALIGNMENT");
-        }
+ public void setHorizontalAlignment(final HorizontalAlignment horizontalAlignment) {
         this.horizontalAlignment = horizontalAlignment;
     }
-
-    /**
-     * Return true if the specified horizontal alignment is one of
-     * <code>Component.LEFT_ALIGNMENT</code>,
-     * <code>Component.CENTER_ALIGNMENT</code>, or
-     * <code>Component.RIGHT_ALIGNMENT</code>.
-     *
-     * @param horizontalAlignment horizontal alignment
-     * @return true if the specified horizontal alignment is one of
-     *         <code>Component.LEFT_ALIGNMENT</code>,
-     *         <code>Component.CENTER_ALIGNMENT</code>, or
-     *         <code>Component.RIGHT_ALIGNMENT</code>
-     */
- private static boolean validHorizontalAlignment(final float horizontalAlignment) { - return Component.LEFT_ALIGNMENT == horizontalAlignment || Component.CENTER_ALIGNMENT == horizontalAlignment
-                || Component.RIGHT_ALIGNMENT == horizontalAlignment;
-    }

     /**
      * Return the paint used to paint this node's text.
@@ -544,7 +527,17 @@
                 return;
             }

- final float offset = (float) (getWidth() - tl.getAdvance()) * horizontalAlignment;
+            final float offset;
+            switch (horizontalAlignment) {
+                case LEFT:
+                    offset = 0;
+                    break;
+                case RIGHT:
+                    offset = (float) (getWidth() - tl.getAdvance());
+                    break;
+                default: // CENTER
+                    offset = (float) (getWidth() - tl.getAdvance()) * 0.5f;
+            }

             tl.draw(g2, x + offset, y);

=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PBounds.java Tue Jan 26 08:27:02 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PBounds.java Tue Jan 26 08:58:01 2010
@@ -50,7 +50,7 @@
  * @version 1.0
  * @author Jesse Grosjean
  */
-public class PBounds extends Rectangle2D.Double implements Serializable {
+public class PBounds extends Rectangle2D.Double {
     /**
      * Allows for future serialization code to understand versioned binary
      * formats.
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PDebug.java Sun Jan 24 19:06:03 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/main/java/org/piccolo2d/util/PDebug.java Tue Jan 26 08:58:01 2010
@@ -30,7 +30,7 @@

 import java.awt.Color;
 import java.awt.Graphics;
-import java.awt.Graphics2D;
+import java.awt.geom.Rectangle2D;

 import javax.swing.SwingUtilities;

@@ -156,9 +156,9 @@
         }

         if (PDebug.debugRegionManagement) {
-            final Graphics2D g2 = (Graphics2D) g;
-            g2.setColor(PDebug.getDebugPaintColor());
-            g2.fill(g.getClipBounds().getBounds2D());
+            g.setColor(PDebug.getDebugPaintColor());
+            Rectangle2D b = g.getClipBounds().getBounds2D();
+ g.fillRect((int) b.getX(), (int) b.getY(), (int) b.getWidth(), (int) b.getHeight());
         }

         processingOutput = false;
=======================================
--- /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/nodes/PTextTest.java Tue Jan 19 12:49:37 2010 +++ /piccolo2d.java/branches/2.0-spike/core/src/test/java/org/piccolo2d/nodes/PTextTest.java Tue Jan 26 08:58:01 2010
@@ -29,15 +29,13 @@
 package org.piccolo2d.nodes;

 import java.awt.Color;
-import java.awt.Component;
 import java.awt.Font;

-import org.piccolo2d.MockPropertyChangeListener;
-import org.piccolo2d.nodes.PText;
-
-
 import junit.framework.TestCase;

+import org.piccolo2d.MockPropertyChangeListener;
+import org.piccolo2d.nodes.PText.HorizontalAlignment;
+
 /**
  * Unit test for PText.
  */
@@ -103,47 +101,17 @@
     }

     public void testHorizontalAlignmentIsLeftByDefault() {
- assertEquals(Component.LEFT_ALIGNMENT, textNode.getHorizontalAlignment(), 0.000001); + assertEquals(HorizontalAlignment.LEFT, textNode.getHorizontalAlignment());
     }

     public void testSetHorizontalAlignmentPersists() {
-        textNode.setHorizontalAlignment(Component.RIGHT_ALIGNMENT);
- assertEquals(Component.RIGHT_ALIGNMENT, textNode.getHorizontalAlignment(), 0.000001);
+        textNode.setHorizontalAlignment(HorizontalAlignment.RIGHT);
+ assertEquals(HorizontalAlignment.RIGHT, textNode.getHorizontalAlignment());
     }

-    public void testSetHorizontalAlignmentInvalidValues() {
+    public void testSetHorizontalAlignmentMayNotBeNull() {
         try {
-            textNode.setHorizontalAlignment(-2.0f);
-        }
-        catch (final IllegalArgumentException e) {
-            // expected
-        }
-        try {
-            textNode.setHorizontalAlignment(2.0f);
-        }
-        catch (final IllegalArgumentException e) {
-            // expected
-        }
-        try {
-            textNode.setHorizontalAlignment(-Float.MAX_VALUE);
-        }
-        catch (final IllegalArgumentException e) {
-            // expected
-        }
-        try {
-            textNode.setHorizontalAlignment(Float.MAX_VALUE);
-        }
-        catch (final IllegalArgumentException e) {
-            // expected
-        }
-        try {
-            textNode.setHorizontalAlignment(-1.00f);
-        }
-        catch (final IllegalArgumentException e) {
-            // expected
-        }
-        try {
-            textNode.setHorizontalAlignment(1.00f);
+            textNode.setHorizontalAlignment(null);
         }
         catch (final IllegalArgumentException e) {
             // expected
=======================================
--- /piccolo2d.java/branches/2.0-spike/extras/src/main/java/org/piccolo2d/extras/util/PFixedWidthStroke.java Tue Jan 26 08:27:02 2010 +++ /piccolo2d.java/branches/2.0-spike/extras/src/main/java/org/piccolo2d/extras/util/PFixedWidthStroke.java Tue Jan 26 08:58:01 2010
@@ -29,7 +29,6 @@
 package org.piccolo2d.extras.util;

 import java.awt.BasicStroke;
-import java.awt.Shape;
 import java.awt.Stroke;
 import java.io.ObjectStreamException;
 import java.io.Serializable;

--
Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en

Reply via email to