Hi list,

I've implemented javax.swing.SpringLayout and javax.swing.Spring. I have
not yet signed the FSF copyright agreement, so I cannot get this into
Classpath immediatly. Nevertheless I post it here for review for anybody
interested. There's a good chance that this is buggy, so I would be glad
about testing.
The patch also contains a little addition to javax.swing.ImageIcon. (A
ImageIcon(java.awt.Image) constructor).

I've also started to write some Mauve tests for it, the first test ist
also attached as diff against the Mauve tree.

Suggestions are welcome.

Cheers, Roman

diff -rNPu classpath/javax/swing/ImageIcon.java classpath-roman/javax/swing/ImageIcon.java
--- classpath/javax/swing/ImageIcon.java	2004-04-29 09:00:34.000000000 +0200
+++ classpath-roman/javax/swing/ImageIcon.java	2004-06-03 15:47:56.000000000 +0200
@@ -70,6 +70,14 @@
         //loadImage(image);
     }
 
+    public ImageIcon(Image image) {
+        this.image = image;
+        file = "";
+        descr = "";
+        return;
+    }
+
+
     // not in SUN's spec !!!
     public void setParent(Component p)
     {
diff -rNPu classpath/javax/swing/Spring.java classpath-roman/javax/swing/Spring.java
--- classpath/javax/swing/Spring.java	1970-01-01 01:00:00.000000000 +0100
+++ classpath-roman/javax/swing/Spring.java	2004-06-04 09:49:24.000000000 +0200
@@ -0,0 +1,340 @@
+/* Spring.java -- 
+   Copyright (C) 2004  Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+package javax.swing;
+
+/**
+ * DOCUMENT ME!
+ */
+public abstract class Spring
+{
+
+  public static final int UNSET = -2147483648;
+
+  protected Spring()
+  {
+  }
+
+  public static Spring constant(int pref)
+  {
+    return new SimpleSpring(pref, pref, pref);
+  }
+
+  public static Spring constant(int min, int pref, int max)
+  {
+    return new SimpleSpring(min, pref, max);
+  }
+
+  public abstract int getMaximumValue();
+
+  public abstract int getMinimumValue();
+
+  public abstract int getPreferredValue();
+
+  public abstract int getValue();
+
+  public static Spring max(Spring s1, Spring s2)
+  {
+    return new MaxSpring(s1, s2);
+  }
+
+  public static Spring minus(Spring s)
+  {
+    return new MinusSpring(s);
+  }
+
+  public abstract void setValue(int value);
+
+  public static Spring sum(Spring s1, Spring s2)
+  {
+    return new AddSpring(s1, s2);
+  }
+
+
+
+}
+
+class SimpleSpring extends Spring
+  {
+
+    private int min, pref, max, value;
+
+    public SimpleSpring(int min, int pref, int max)
+    {
+      this.min = min;
+      this.pref = pref;
+      this.max = max;
+      this.value = Spring.UNSET;
+      return;
+    }
+
+    public int getMaximumValue()
+    {
+      return max;
+    }
+
+    public int getMinimumValue()
+    {
+      return min;
+    }
+
+    public int getPreferredValue()
+    {
+      return pref;
+    }
+
+    public int getValue() {
+        if (value == Spring.UNSET)
+            {
+                value = pref;
+            }
+
+        return value;
+    }
+
+    public void setValue(int val) {
+
+        if (val > max)
+          {
+            value = max;
+          }
+        else if (val < min)
+          {
+            value = min;
+          }
+        else
+          {
+            value = val;
+          }
+
+        return;
+    }
+
+  }
+
+
+
+class AddSpring extends Spring {
+
+    private Spring s1, s2;
+    private int value = Spring.UNSET;
+
+    protected AddSpring(Spring s1, Spring s2)
+    {
+      super();
+      this.s1 = s1;
+      this.s2 = s2;
+      return;
+    }
+
+
+    public int getMaximumValue()
+    {
+      int max1 = s1.getMaximumValue();
+      int max2 = s2.getMaximumValue();
+      return max1 + max2;
+    }
+
+    public int getMinimumValue()
+    {
+      int min1 = s1.getMinimumValue();
+      int min2 = s2.getMinimumValue();
+      return min1 + min2;
+    }
+
+    public int getPreferredValue()
+    {
+      int pref1 = s1.getPreferredValue();
+      int pref2 = s2.getPreferredValue();
+      return pref1 + pref2;
+    }
+
+    public int getValue()
+    {
+      if (value == Spring.UNSET)
+        {
+          int val1 = s1.getValue();
+          int val2 = s2.getValue();
+          value = val1 + val2;
+        }
+      return value;
+    }
+
+    public void setValue(int val) {
+
+        if (val > getMaximumValue())
+          {
+            value = getMaximumValue();
+          }
+        else if (val < getMinimumValue())
+          {
+            value = getMinimumValue();
+          }
+        else
+          {
+            value = val;
+          }
+
+        return;
+    }
+
+  }
+
+
+class MinusSpring extends Spring {
+
+    private Spring s;
+    private int value = Spring.UNSET;
+
+    protected MinusSpring(Spring s)
+    {
+      super();
+      this.s = s;
+      return;
+    }
+
+
+    public int getMaximumValue()
+    {
+      return -s.getMinimumValue();
+    }
+
+    public int getMinimumValue()
+    {
+      return -s.getMaximumValue();
+    }
+
+    public int getPreferredValue()
+    {
+      return -s.getPreferredValue();
+    }
+
+    public int getValue()
+    {
+      if (value == Spring.UNSET)
+        {
+          value = -s.getValue();
+        }
+      return value;
+    }
+
+    public void setValue(int val) {
+
+        if (val > getMaximumValue())
+          {
+            value = getMaximumValue();
+          }
+        else if (val < getMinimumValue())
+          {
+            value = getMinimumValue();
+          }
+        else
+          {
+            value = val;
+          }
+
+        return;
+    }
+
+  }
+
+
+class MaxSpring extends Spring {
+
+    private Spring s1, s2;
+    private int value = Spring.UNSET;
+
+    protected MaxSpring(Spring s1, Spring s2)
+    {
+      super();
+      this.s1 = s1;
+      this.s2 = s2;
+      return;
+    }
+
+
+    public int getMaximumValue()
+    {
+      int max1 = s1.getMaximumValue();
+      int max2 = s2.getMaximumValue();
+      return Math.max(max1, max2);
+    }
+
+    public int getMinimumValue()
+    {
+      int min1 = s1.getMinimumValue();
+      int min2 = s2.getMinimumValue();
+      return Math.max(min1, min2);
+    }
+
+    public int getPreferredValue()
+    {
+      int pref1 = s1.getPreferredValue();
+      int pref2 = s2.getPreferredValue();
+      return Math.max(pref1, pref2);
+    }
+
+    public int getValue()
+    {
+      if (value == Spring.UNSET)
+        {
+          int val1 = s1.getValue();
+          int val2 = s2.getValue();
+          value = Math.max(val1, val2);
+        }
+      return value;
+    }
+
+    public void setValue(int val) {
+
+        if (val > getMaximumValue())
+          {
+            value = getMaximumValue();
+          }
+        else if (val < getMinimumValue())
+          {
+            value = getMinimumValue();
+          }
+        else
+          {
+            value = val;
+          }
+
+        return;
+    }
+
+  }
diff -rNPu classpath/javax/swing/SpringLayout.java classpath-roman/javax/swing/SpringLayout.java
--- classpath/javax/swing/SpringLayout.java	1970-01-01 01:00:00.000000000 +0100
+++ classpath-roman/javax/swing/SpringLayout.java	2004-06-04 01:58:36.000000000 +0200
@@ -0,0 +1,430 @@
+/* SpringLayout.java -- 
+   Copyright (C) 2004  Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+package javax.swing;
+
+import java.awt.Component;
+import java.awt.Container;
+import java.awt.Dimension;
+import java.awt.LayoutManager2;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.swing.Spring;
+
+/**
+ * DOCUMENT ME!
+ */
+public class SpringLayout implements LayoutManager2
+{
+
+  public static final String EAST = "East";
+  public static final String NORTH = "North";
+  public static final String SOUTH = "South";
+  public static final String WEST = "West";
+
+
+  private Map constraintsMap;
+
+  static class Constraints
+  {
+
+      private Spring x, y, height, width, east, south;
+
+      public Constraints()
+      {
+          x = y = height = width = east = south = null;
+      }
+
+      public Constraints(Spring x, Spring y)
+      {
+          this.x = x;
+          this.y = y;
+          width = height = east = south = null;
+          return;
+      }
+
+      public Constraints(Spring x, Spring y, Spring width, Spring height)
+      {
+          this.x = x;
+          this.y = y;
+          this.width = width;
+          this.height = height;
+          east = south = null;
+          return;
+      }
+
+      public Spring getConstraint(String edgeName)
+      {
+          Spring retVal = null;
+          if (edgeName.equals(SpringLayout.NORTH))
+              retVal = y;
+          else if (edgeName.equals(SpringLayout.WEST))
+              retVal = x;
+          else if (edgeName.equals(SpringLayout.SOUTH))
+              {
+                  retVal = south;
+                  if ((retVal == null) && (y != null) && (height != null))
+                      retVal = Spring.sum(y, height);
+              }
+          else if (edgeName.equals(SpringLayout.EAST))
+              {
+                  retVal = east;
+                  if ((retVal == null) && (x != null) && (width != null))
+                      retVal = Spring.sum(x, width);
+              }
+
+          return retVal;
+      }
+
+      public Spring getHeight()
+      {
+          Spring retVal = height;
+          if ((retVal == null) && (y != null) && (south != null)) {
+              retVal = Spring.sum(south, Spring.minus(y));
+          }
+          return retVal;
+      }
+
+      public Spring getWidth()
+      {
+          Spring retVal = width;
+          if ((retVal == null) && (x != null) && (east != null)) {
+              retVal = Spring.sum(east, Spring.minus(x));
+          }
+          return retVal;
+      }
+
+      public Spring getX()
+      {
+          Spring retVal = x;
+          if ((retVal == null) && (width != null) && (east != null)) {
+              retVal = Spring.sum(east, Spring.minus(width));
+          }
+          return retVal;
+      }
+
+      public Spring getY()
+      {
+          Spring retVal = y;
+          if ((retVal == null) && (height != null) && (south != null)) {
+              retVal = Spring.sum(south, Spring.minus(height));
+          }
+          return retVal;
+      }
+
+      public void setConstraint(String edgeName, Spring s) {
+
+          if (edgeName.equals(SpringLayout.WEST))
+              {
+                  x = s;
+                  if ((width != null) && (east != null))
+                      width = Spring.sum(east, Spring.minus(x));
+              }
+          else if (edgeName.equals(SpringLayout.NORTH))
+              {
+                  y = s;
+                  if ((height != null) && (south != null))
+                      height = Spring.sum(south, Spring.minus(y));
+              }
+          else if (edgeName.equals(SpringLayout.EAST))
+              {
+                  east = s;
+                  if ((x != null) && (width != null))
+                      x = Spring.sum(east, Spring.minus(width));
+              }
+          else if (edgeName.equals(SpringLayout.SOUTH))
+              {
+                  south = s;
+                  if ((height != null) && (y != null))
+                      y = Spring.sum(south, Spring.minus(height));
+              }
+
+          return;
+      }
+
+
+      public void setHeight(Spring s)
+      {
+          height = s;
+          if ((south != null) && (y != null))
+              south = Spring.sum(y, height);
+
+          return;
+      }
+
+      public void setWidth(Spring s)
+      {
+          width = s;
+          if ((east != null) && (x != null))
+              east = Spring.sum(x, width);
+
+          return;
+
+      }
+
+      public void setX(Spring s)
+      {
+          x = s;
+          if ((width != null) && (east != null))
+              width = Spring.sum(east, Spring.minus(x));
+
+          return;
+
+      }
+
+      public void setY(Spring s)
+      {
+          y = s;
+          if ((height != null) && (south != null))
+              height = Spring.sum(south, Spring.minus(y));
+
+          return;
+
+      }
+  }
+
+  public SpringLayout()
+  {
+
+    constraintsMap = new HashMap();
+    return;
+  }
+
+  public void addLayoutComponent(Component component, Object constraint)
+  {
+    constraintsMap.put(component, constraint);
+    return;
+  }
+
+  public void addLayoutComponent(String name, Component c)
+  {
+      // do nothing here.
+      return;
+  }
+
+  public Spring getConstraint(String edgeName, Component c)
+  {
+      Constraints constraints = getConstraints(c);
+      return constraints.getConstraint(edgeName);
+  }
+
+  public SpringLayout.Constraints getConstraints(Component c)
+  {
+      Constraints constraints = (Constraints) constraintsMap.get(c);
+      if (constraints == null)
+          {
+              Container parent = c.getParent();
+              constraints = new Constraints();
+              constraints.setX
+                  (Spring.constant(parent.getInsets().left));
+              constraints.setY
+                  (Spring.constant(parent.getInsets().top));
+              constraints.setWidth
+                  (Spring.constant(c.getMinimumSize().width,
+                                   c.getPreferredSize().width,
+                                   c.getMaximumSize().width));
+              constraints.setHeight
+                  (Spring.constant(c.getMinimumSize().height,
+                                   c.getPreferredSize().height,
+                                   c.getMaximumSize().height));
+
+              constraintsMap.put(c, constraints);
+
+          }
+
+      return constraints;
+  }
+
+  public float getLayoutAlignmentX(Container p)
+  {
+      return 0.5F;
+  }
+
+  public float getLayoutAlignmentY(Container p)
+  {
+      return 0.5F;
+  }
+
+  public void invalidateLayout(Container p)
+  {
+      // nothing to do here yet
+      return;
+  }
+
+  public void layoutContainer(Container p)
+  {
+
+      int offsetX = p.getInsets().left;
+      int offsetY = p.getInsets().right;
+
+      Component[] components = p.getComponents();
+      for (int index = 0; index < components.length; index++)
+          {
+              Component c = components[index];
+              Constraints constraints = getConstraints(c);
+              int x = constraints.getX().getValue();
+              int y = constraints.getY().getValue();
+              int width = constraints.getWidth().getValue();
+              int height = constraints.getHeight().getValue();
+
+              c.setLocation(x + offsetX, y + offsetY);
+              c.setSize(width, height);
+
+          }
+
+      return;
+  }
+
+  public Dimension maximumLayoutSize(Container p)
+  {
+      int maxX = 0;
+      int maxY = 0;
+
+      int offsetX = p.getInsets().left;
+      int offsetY = p.getInsets().right;
+
+      Component[] components = p.getComponents();
+      for (int index = 0; index < components.length; index++)
+          {
+              Component c = components[index];
+              Constraints constraints = getConstraints(c);
+              int x = constraints.getX().getMaximumValue();
+              int y = constraints.getY().getMaximumValue();
+              int width = constraints.getWidth().getMaximumValue();
+              int height = constraints.getHeight().getMaximumValue();
+
+              int rightEdge = offsetX + x + width;
+              if (rightEdge > maxX)
+                  maxX = rightEdge;
+              int bottomEdge = offsetY + y + height;
+              if (bottomEdge > maxY)
+                  maxY = bottomEdge;
+          }
+
+      return new Dimension(maxX, maxY);
+  }
+
+  public Dimension minimumLayoutSize(Container p)
+  {
+      int maxX = 0;
+      int maxY = 0;
+
+      int offsetX = p.getInsets().left;
+      int offsetY = p.getInsets().right;
+
+      Component[] components = p.getComponents();
+      for (int index = 0; index < components.length; index++)
+          {
+              Component c = components[index];
+              Constraints constraints = getConstraints(c);
+              int x = constraints.getX().getMinimumValue();
+              int y = constraints.getY().getMinimumValue();
+              int width = constraints.getWidth().getMinimumValue();
+              int height = constraints.getHeight().getMinimumValue();
+
+              int rightEdge = offsetX + x + width;
+              if (rightEdge > maxX)
+                  maxX = rightEdge;
+              int bottomEdge = offsetY + y + height;
+              if (bottomEdge > maxY)
+                  maxY = bottomEdge;
+          }
+
+      return new Dimension(maxX, maxY);
+  }
+
+  public Dimension preferredLayoutSize(Container p)
+  {
+      int maxX = 0;
+      int maxY = 0;
+
+      int offsetX = p.getInsets().left;
+      int offsetY = p.getInsets().right;
+
+      Component[] components = p.getComponents();
+      for (int index = 0; index < components.length; index++)
+          {
+              Component c = components[index];
+              Constraints constraints = getConstraints(c);
+              int x = constraints.getX().getPreferredValue();
+              int y = constraints.getY().getPreferredValue();
+              int width = constraints.getWidth().getPreferredValue();
+              int height = constraints.getHeight().getPreferredValue();
+
+              int rightEdge = offsetX + x + width;
+              if (rightEdge > maxX)
+                  maxX = rightEdge;
+              int bottomEdge = offsetY + y + height;
+              if (bottomEdge > maxY)
+                  maxY = bottomEdge;
+          }
+
+      return new Dimension(maxX, maxY);
+  }
+
+  public void putConstraint(String e1, Component c1, int pad, String e2, 
+                            Component c2)
+  {
+      Constraints constraints1 = getConstraints(c1);
+      Constraints constraints2 = getConstraints(c2);
+
+      Spring strut = Spring.constant(pad);
+      Spring otherEdge = constraints2.getConstraint(e2);
+      constraints1.setConstraint(e1, Spring.sum(strut, otherEdge));
+
+      return;
+  }
+
+  public void putConstraint(String e1, Component c1, Spring s, String e2, 
+                            Component c2)
+  {
+      Constraints constraints1 = getConstraints(c1);
+      Constraints constraints2 = getConstraints(c2);
+
+      Spring otherEdge = constraints2.getConstraint(e2);
+      constraints1.setConstraint(e1, Spring.sum(s, otherEdge));
+
+      return;
+  }
+
+  public void removeLayoutComponent(Component c)
+  {
+  }
+
+}
diff -rPNu mauve/gnu/testlet/javax/swing/SpringLayout/Unconstrained.java mauve-roman/gnu/testlet/javax/swing/SpringLayout/Unconstrained.java
--- mauve/gnu/testlet/javax/swing/SpringLayout/Unconstrained.java	1970-01-01 01:00:00.000000000 +0100
+++ mauve-roman/gnu/testlet/javax/swing/SpringLayout/Unconstrained.java	2004-06-04 09:51:26.000000000 +0200
@@ -0,0 +1,92 @@
+// Tags: JDK1.4
+
+// Copyright (C) 2004 Roman Kennke <[EMAIL PROTECTED]>
+
+// This file is part of Mauve.
+
+// Mauve is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// Mauve is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Mauve; see the file COPYING.  If not, write to
+// the Free Software Foundation, 59 Temple Place - Suite 330,
+// Boston, MA 02111-1307, USA.
+
+package gnu.testlet.javax.swing.SpringLayout;
+
+import gnu.testlet.Testlet;
+import gnu.testlet.TestHarness;
+import java.awt.*;
+import javax.swing.*;
+
+/**
+ * Tests the layout of unconstrained components.
+ * 
+ */
+public class Unconstrained implements Testlet
+{
+
+
+    public void test(TestHarness harness)
+    {
+
+        System.err.println("test called");
+        Container con = new Container();
+        TestComponent comp = new TestComponent();
+        comp.setMinimumSize(new Dimension(10, 10));
+        comp.setPreferredSize(new Dimension(20, 20));
+        comp.setMaximumSize(new Dimension(30, 30));
+
+        SpringLayout layout = new SpringLayout();
+        con.setLayout(layout);
+        con.add(comp);
+        layout.layoutContainer(con);
+
+        int width = comp.getSize().width;
+        int height = comp.getSize().height;
+        int x = comp.getLocation().x;
+        int y = comp.getLocation().y;
+
+        harness.check(x, con.getInsets().left);
+        harness.check(y, con.getInsets().top);
+        harness.check(width, 20);
+        harness.check(height, 20);
+
+        return;
+    }
+
+    class TestComponent extends Component
+    {
+
+        private Dimension maxSize, prefSize, minSize;
+
+        public void setMaximumSize(Dimension size)
+        {
+            maxSize = size;
+        }
+
+        public Dimension getMaximumSize()
+        {
+            return maxSize;
+        }
+
+        public void setPreferredSize(Dimension size)
+        {
+            prefSize = size;
+        }
+
+        public Dimension getPreferredSize()
+        {
+            return prefSize;
+        }
+
+        public void setMinimumSize(Dimension size)
+        {
+            minSize = size;
+        }
+
+        public Dimension getMinimumSize()
+        {
+            return minSize;
+        }
+
+    }
+}

Attachment: signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil

_______________________________________________
Classpath mailing list
[EMAIL PROTECTED]
http://lists.gnu.org/mailman/listinfo/classpath

Reply via email to