You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by gb...@apache.org on 2009/04/21 02:11:15 UTC

svn commit: r766924 - in /incubator/pivot/trunk/wtk/src/pivot/wtk/media: Drawing.java Image.java ImageListener.java Picture.java drawing/Group.java drawing/Shape.java drawing/ShapeListener.java

Author: gbrown
Date: Tue Apr 21 00:11:15 2009
New Revision: 766924

URL: http://svn.apache.org/viewvc?rev=766924&view=rev
Log:
Continue to stub out drawing APIs.

Added:
    incubator/pivot/trunk/wtk/src/pivot/wtk/media/ImageListener.java
Modified:
    incubator/pivot/trunk/wtk/src/pivot/wtk/media/Drawing.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/media/Image.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/media/Picture.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/media/drawing/Group.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/media/drawing/Shape.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/media/drawing/ShapeListener.java

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/media/Drawing.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/media/Drawing.java?rev=766924&r1=766923&r2=766924&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/media/Drawing.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/media/Drawing.java Tue Apr 21 00:11:15 2009
@@ -19,31 +19,62 @@
 import java.awt.Graphics2D;
 import pivot.wtk.Bounds;
 import pivot.wtk.media.drawing.Group;
+import pivot.wtk.media.drawing.Shape;
+import pivot.wtk.media.drawing.ShapeListener;
 
 /**
  * Image representing a vector drawing.
+ * <p>
+ * TODO If developer retains a reference to the root group but not the drawing,
+ * the drawing will never be garbage collected. Do we need a way to decouple
+ * it?
  *
  * @author gbrown
  */
 public class Drawing extends Image {
-    private Group shapes = new Group();
+    private Group root;
+    private ShapeListener rootListener = new ShapeListener.Adapter() {
+        @Override
+        public void boundsChanged(Shape shape, int previousX, int previousY,
+            int previousWidth, int previousHeight) {
+            // TODO Need to clip this to 0, 0
+            imageListeners.sizeChanged(Drawing.this, previousWidth, previousHeight);
+        }
+
+        public void regionInvalidated(Shape shape, int x, int y, int width, int height) {
+            // TODO Need to clip this to 0, 0
+            imageListeners.regionInvalidated(Drawing.this, x, y, width, height);
+        }
+    };
+
+    public Drawing() {
+        this(new Group());
+    }
+
+    public Drawing(Group root) {
+        this.root = root;
+
+        root.getShapeListeners().add(rootListener);
+    }
+
+    public Group getRoot() {
+        return root;
+    }
 
     public int getWidth() {
-        Bounds bounds = shapes.getBounds();
+        // TODO Need to clip this to 0, 0
+        Bounds bounds = root.getBounds();
         return bounds.width + bounds.x;
     }
 
     public int getHeight() {
-        Bounds bounds = shapes.getBounds();
+        // TODO Need to clip this to 0, 0
+        Bounds bounds = root.getBounds();
         return bounds.height + bounds.y;
     }
 
     public void paint(Graphics2D graphics) {
         graphics.clipRect(0, 0, getWidth(), getHeight());
-        shapes.paint(graphics);
-    }
-
-    public Group getShapes() {
-        return shapes;
+        root.paint(graphics);
     }
 }

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/media/Image.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/media/Image.java?rev=766924&r1=766923&r2=766924&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/media/Image.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/media/Image.java Tue Apr 21 00:11:15 2009
@@ -22,6 +22,7 @@
 import java.net.URL;
 
 import pivot.io.IOTask;
+import pivot.util.ListenerList;
 import pivot.util.concurrent.Dispatcher;
 import pivot.util.concurrent.TaskListener;
 import pivot.util.concurrent.TaskExecutionException;
@@ -35,6 +36,21 @@
  * @author gbrown
  */
 public abstract class Image implements Visual {
+    protected class ImageListenerList extends ListenerList<ImageListener>
+        implements ImageListener {
+        public void sizeChanged(Image image, int previousWidth, int previousHeight) {
+            for (ImageListener listener : this) {
+                listener.sizeChanged(image, previousWidth, previousHeight);
+            }
+        }
+
+        public void regionInvalidated(Image image, int x, int y, int width, int height) {
+            for (ImageListener listener : this) {
+                listener.regionInvalidated(image, x, y, width, height);
+            }
+        }
+    }
+
     /**
      * Task that executes an image load operation.
      *
@@ -90,10 +106,16 @@
         }
     }
 
+    protected ImageListenerList imageListeners = new ImageListenerList();
+
     public Dimensions getSize() {
         return new Dimensions(getWidth(), getHeight());
     }
 
+    public ListenerList<ImageListener> getImageListeners() {
+        return imageListeners;
+    }
+
     public static Image load(URL url) {
         LoadTask loadTask = new LoadTask(url);
 

Added: incubator/pivot/trunk/wtk/src/pivot/wtk/media/ImageListener.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/media/ImageListener.java?rev=766924&view=auto
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/media/ImageListener.java (added)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/media/ImageListener.java Tue Apr 21 00:11:15 2009
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package pivot.wtk.media;
+
+/**
+ * Image listener interface.
+ *
+ * @author gbrown
+ */
+public interface ImageListener {
+    /**
+     * Called when an image's size has changed.
+     *
+     * @param image
+     * @param previousWidth
+     * @param previousHeight
+     */
+    public void sizeChanged(Image image, int previousWidth, int previousHeight);
+
+    /**
+     * Called when a region within an image needs to be repainted.
+     *
+     * @param image
+     * @param x
+     * @param y
+     * @param width
+     * @param height
+     */
+    public void regionInvalidated(Image image, int x, int y, int width, int height);
+}

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/media/Picture.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/media/Picture.java?rev=766924&r1=766923&r2=766924&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/media/Picture.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/media/Picture.java Tue Apr 21 00:11:15 2009
@@ -21,6 +21,8 @@
 
 /**
  * Image representing a bitmapped picture.
+ * <p>
+ * TODO Add a resize() method that will scale the image?
  *
  * @author gbrown
  */

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/media/drawing/Group.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/media/drawing/Group.java?rev=766924&r1=766923&r2=766924&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/media/drawing/Group.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/media/drawing/Group.java Tue Apr 21 00:11:15 2009
@@ -42,7 +42,8 @@
     }
 
     public void paint(Graphics2D graphics) {
-        // TODO
+        // TODO Paint each sub-shape, first applying the transform based on
+        // the sub-shape's attributes
     }
 
     public int add(Shape shape) {
@@ -101,7 +102,8 @@
 
     @Override
     public boolean contains(int x, int y) {
-        // TODO
+        // TODO Ask each shape if it contains the point (translate to sub-shape
+        // space first)
         return false;
     }
 

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/media/drawing/Shape.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/media/drawing/Shape.java?rev=766924&r1=766923&r2=766924&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/media/drawing/Shape.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/media/drawing/Shape.java Tue Apr 21 00:11:15 2009
@@ -20,6 +20,7 @@
 import java.awt.Paint;
 import java.awt.geom.AffineTransform;
 
+import pivot.util.ListenerList;
 import pivot.wtk.Bounds;
 import pivot.wtk.Point;
 import pivot.wtk.Visual;
@@ -30,6 +31,46 @@
  * @author gbrown
  */
 public abstract class Shape implements Visual {
+    private class ShapeListenerList extends ListenerList<ShapeListener>
+        implements ShapeListener {
+        public void originChanged(Shape shape, int previousX, int previousY) {
+            for (ShapeListener listener : this) {
+                listener.originChanged(shape, previousX, previousY);
+            }
+        }
+
+        public void boundsChanged(Shape shape, int previousX, int previousY,
+            int previousWidth, int previousHeight) {
+            for (ShapeListener listener : this) {
+                listener.boundsChanged(shape, previousX, previousY, previousWidth, previousHeight);
+            }
+        }
+
+        public void strokeChanged(Shape shape, Paint previousStroke) {
+            for (ShapeListener listener : this) {
+                listener.strokeChanged(shape, previousStroke);
+            }
+        }
+
+        public void strokeThicknessChanged(Shape shape, int previousStrokeThickness) {
+            for (ShapeListener listener : this) {
+                listener.strokeThicknessChanged(shape, previousStrokeThickness);
+            }
+        }
+
+        public void fillChanged(Shape shape, Paint previousFill) {
+            for (ShapeListener listener : this) {
+                listener.fillChanged(shape, previousFill);
+            }
+        }
+
+        public void regionInvalidated(Shape shape, int x, int y, int width, int height) {
+            for (ShapeListener listener : this) {
+                listener.regionInvalidated(shape, x, y, width, height);
+            }
+        }
+    }
+
     private Group parent = null;
 
     private int x = 0;
@@ -47,6 +88,8 @@
     private double translateX = 0;
     private double translateY = 0;
 
+    private ShapeListenerList shapeListeners = new ShapeListenerList();
+
     public Group getParent() {
         return parent;
     }
@@ -205,7 +248,7 @@
     }
 
     public AffineTransform getTransform() {
-        // TODO
+        // TODO Calculate transform based on properties
         return null;
     }
 
@@ -213,11 +256,19 @@
         if (parent != null) {
             parent.invalidateRegion(this);
         }
+
+        // TODO Fire event
     }
 
     protected void invalidateBounds() {
         if (parent != null) {
             parent.invalidateBounds(this);
         }
+
+        // TODO Fire event when bounds are set
+    }
+
+    public ListenerList<ShapeListener> getShapeListeners() {
+        return shapeListeners;
     }
 }

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/media/drawing/ShapeListener.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/media/drawing/ShapeListener.java?rev=766924&r1=766923&r2=766924&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/media/drawing/ShapeListener.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/media/drawing/ShapeListener.java Tue Apr 21 00:11:15 2009
@@ -16,12 +16,91 @@
  */
 package pivot.wtk.media.drawing;
 
+import java.awt.Paint;
+
 /**
  * Shape listener interface.
  *
  * @author gbrown
  */
 public interface ShapeListener {
+    /**
+     * Shape listener adapter.
+     *
+     * @author gbrown
+     */
+    public static class Adapter implements ShapeListener {
+        public void originChanged(Shape shape, int previousX, int previousY) {
+        }
+
+        public void boundsChanged(Shape shape, int previousX, int previousY,
+            int previousWidth, int previousHeight) {
+        }
+
+        public void strokeChanged(Shape shape, Paint previousStroke) {
+        }
+
+        public void strokeThicknessChanged(Shape shape, int previousStrokeThickness) {
+        }
+
+        public void fillChanged(Shape shape, Paint previousFill) {
+        }
+
+        public void regionInvalidated(Shape shape, int x, int y, int width, int height) {
+        }
+    }
+
+    /**
+     * Called when a shape's origin has changed.
+     *
+     * @param shape
+     * @param previousX
+     * @param previousY
+     */
     public void originChanged(Shape shape, int previousX, int previousY);
-    public void sizeChanged(Shape shape, int previousWidth, int previousHeight);
+
+    /**
+     * Called when a shape's bounds have changed.
+     *
+     * @param shape
+     * @param previousWidth
+     * @param previousHeight
+     */
+    public void boundsChanged(Shape shape, int previousX, int previousY,
+        int previousWidth, int previousHeight);
+
+    /**
+     * Called when a shape's stroke has changed.
+     *
+     * @param shape
+     * @param previousStroke
+     */
+    public void strokeChanged(Shape shape, Paint previousStroke);
+
+    /**
+     * Called when a shape's stroke thickness has changed.
+     *
+     * @param shape
+     * @param previousStrokeThickness
+     */
+    public void strokeThicknessChanged(Shape shape, int previousStrokeThickness);
+
+    /**
+     * Called when a shape's fill has changed.
+     *
+     * @param shape
+     * @param previousFill
+     */
+    public void fillChanged(Shape shape, Paint previousFill);
+
+    /**
+     * Called when a region within a shape needs to be repainted.
+     *
+     * @param shape
+     * @param x
+     * @param y
+     * @param width
+     * @param height
+     */
+    public void regionInvalidated(Shape shape, int x, int y, int width, int height);
 }