classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] FYI: javax.swing Box and BoxLayout implementations merged


From: Mark Wielaard
Subject: [cp-patches] FYI: javax.swing Box and BoxLayout implementations merged
Date: Sun, 01 Aug 2004 00:33:05 +0200

Hi,

This merges in the work of Roman on the libgcj gui branch for Box and
BoxLayout. With this Swing menubars look a lot nicer!

2004-07-31  Roman Kennke  <address@hidden>

        * javax/swing/Box.java:
        (createGlue): Implemented
        (createHorizontalGlue): Implemented
        (createHorizontalStrut): Implemented
        (createVerticalGlue): Implemented
        (createVerticalStrut): Implemented

2004-07-31  Roman Kennke  <address@hidden>

        * javax/swing/BoxLayout.java: Reimplement.

Committed.

Mark
Index: javax/swing/Box.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/Box.java,v
retrieving revision 1.9
diff -u -r1.9 Box.java
--- javax/swing/Box.java        8 Jul 2004 19:26:23 -0000       1.9
+++ javax/swing/Box.java        31 Jul 2004 22:24:15 -0000
@@ -47,7 +47,11 @@
 import java.awt.AWTError;
 
 /**
- * Needs some work I guess....
+ * A component that uses a address@hidden BoxLayout} as Layout Manager.
+ *
+ * In addition to that, this class provides a set of static methods for
+ * creating some filler components ('struts' and 'glue') for use in
+ * containers that are laid out using BoxLayout.
  *
  * @author Ronald Veldema (address@hidden)
  */
@@ -69,7 +73,10 @@
       return null;
     }
   }
-  
+
+  /**
+   * A component that servers as a filler in BoxLayout controlled containers.
+   */
   public static class Filler extends JComponent implements Accessible
   {
     private static final long serialVersionUID = -1204263191910183998L;
@@ -93,11 +100,25 @@
     
     private transient Dimension min, pref, max;
     
+    /**
+     * Creates a new instance of Filler.
+     *
+     * @param min the minimum size of the filler.
+     * @param pref the preferred size of the filler.
+     * @param max the maximum size of the filler.
+     */
     public Filler(Dimension min, Dimension pref, Dimension max)
     {
       changeShape(min, pref, max);
     }
     
+    /**
+     * Changes the dimensions of this Filler.
+     *
+     * @param min the new minimum size of the filler.
+     * @param pref the new preferred size of the filler.
+     * @param max the new maximum size of the filler.
+     */
     public void changeShape(Dimension min, Dimension pref, Dimension max)
     {
       this.min = min;
@@ -113,30 +134,66 @@
       return accessibleContext;
     }
     
+    /**
+     * Returns the maximum size of this Filler.
+     *
+     * @return the maximum size of this Filler.
+     */
     public Dimension getMaximumSize()
     {
       return max;
     }
     
+    /**
+     * Returns the minimum size of this Filler.
+     *
+     * @return the minimum size of this Filler.
+     */
     public Dimension getMinimumSize()
     {
       return min;
     }
     
+    /**
+     * Returns the preferred size of this Filler.
+     *
+     * @return the preferred size of this Filler.
+     */
     public Dimension getPreferredSize()
     {
       return pref;
     }
   }
   
+  /**
+   * Creates a new Box component, that lays out its children according
+   * to the <code>axis</code> parameter.
+   *
+   * @param axis the orientation of the BoxLayout.
+   *
+   * @see BoxLayout#X_AXIS
+   * @see BoxLayout#Y_AXIS
+   * @see BoxLayout#LINE_AXIS
+   * @see BoxLayout#PAGE_AXIS
+   */
   public Box(int axis)
   {
     setLayout(new BoxLayout(this, axis));      
   }
   
+  /**
+   * Creates a filler component which acts as glue between components.
+   * It does not take space unless some extra space is available. If extra
+   * space is available, this component can expand in both X and Y directions.
+   *
+   * @return a glue-like filler component.
+   */
   public static Component createGlue()
   {
-    return null;
+    Filler glue = new Filler(new Dimension(0,0), new Dimension(0,0),
+                             new Dimension(Integer.MAX_VALUE,Integer.MAX_VALUE)
+                             );
+    return glue;
   }
   
   public static Box createHorizontalBox()
@@ -144,14 +201,32 @@
     return null;
   }
   
+  /**
+   * Creates a filler component which acts as glue between components.
+   * It does not take space unless some extra space is available. If extra
+   * space is available, this component can expand in the X direction.
+   *
+   * @return a glue-like filler component.
+   */
   public static Component createHorizontalGlue()
   {
-    return null;
+    return createGlue();
   }
   
+  /**
+   * Creates a filler component which acts as strut between components.
+   * It will fill exactly the specified horizontal size.
+   *
+   * @param width the width of this strut in pixels.
+   *
+   * @return a strut-like filler component.
+   */
   public static Component createHorizontalStrut(int width)
   {
-    return null;
+    Filler strut = new Filler(new Dimension(width, 0),
+                              new Dimension(width, 0),
+                              new Dimension(width, Integer.MAX_VALUE));
+    return strut;
   }
   
   public static Component createRigidArea(Dimension d)
@@ -164,14 +239,32 @@
     return null;
   }
   
+  /**
+   * Creates a filler component which acts as glue between components.
+   * It does not take space unless some extra space is available. If extra
+   * space is available, this component can expand in the Y direction.
+   *
+   * @return a glue-like filler component.
+   */
   public static Component createVerticalGlue()
   {
-    return null;
+    return createGlue();
   }
   
+  /**
+   * Creates a filler component which acts as strut between components.
+   * It will fill exactly the specified vertical size.
+   *
+   * @param height the height of this strut in pixels.
+   *
+   * @return a strut-like filler component.
+   */
   public static Component createVerticalStrut(int height)
   {
-    return null;
+    Filler strut = new Filler(new Dimension(0, height),
+                              new Dimension(0, height),
+                              new Dimension(Integer.MAX_VALUE, height));
+    return strut;
   }
   
   public void setLayout(LayoutManager l)
Index: javax/swing/BoxLayout.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/BoxLayout.java,v
retrieving revision 1.5
diff -u -r1.5 BoxLayout.java
--- javax/swing/BoxLayout.java  26 Nov 2003 21:12:01 -0000      1.5
+++ javax/swing/BoxLayout.java  31 Jul 2004 22:24:15 -0000
@@ -42,15 +42,11 @@
 import java.awt.ComponentOrientation;
 import java.awt.Container;
 import java.awt.Dimension;
-import java.awt.GridLayout;
 import java.awt.LayoutManager2;
 import java.io.Serializable;
 
-
 /**
  * A layout for swing components.
- * This implementation delegates its methods to
- * java.awt.GridLayout to do its work.
  *
  * @author Ronald Veldema (address@hidden)
  */
@@ -87,11 +83,6 @@
   private Container container;
   
   /*
-   * Internal layout.
-   */
-  private GridLayout grid;
-
-  /*
    * Current type of component layouting. Defaults to X_AXIS.
    */
   private int way = X_AXIS;
@@ -108,75 +99,41 @@
   {
     int width = 0;
     int height = 0;
-    ComponentOrientation orientation = container.getComponentOrientation();
-
     this.container = container;
     this.way = way;
-
-    switch (way)
-      {
-      case X_AXIS:
-       width = 1;
-       break;
-      case Y_AXIS:
-       height = 1;
-       break;
-      case LINE_AXIS:
-       if (orientation.isHorizontal())
-          height = 1;
-       else
-         width = 1;
-       break;
-      case PAGE_AXIS:
-       if (!orientation.isHorizontal())
-          height = 1;
-       else
-         width = 1;
-       break;
-      default:
-       throw new AWTError("Invalid value for way");
-      }
-
-    grid = new GridLayout(width, height);
   }
 
   /**
-   * Adds a component to the layout.
+   * Adds a component to the layout. Not used in BoxLayout.
    *
    * @param name The name of the component to add.
    * @param component the component to add to the layout.
    */
   public void addLayoutComponent(String name, Component component)
   {
-    if (way == X_AXIS
-        || (way == LINE_AXIS
-            && component.getComponentOrientation().isHorizontal())
-        || (way == PAGE_AXIS
-            && !component.getComponentOrientation().isHorizontal()))
-      grid.setColumns(grid.getColumns() + 1);
-    else
-      grid.setRows(grid.getRows() + 1);
   }
 
   /**
-   * Removes a component from the layout.
+   * Removes a component from the layout. Not used in BoxLayout.
    *
    * @param component The component to remove from the layout.
    */
   public void removeLayoutComponent(Component component)
   {
-    grid.removeLayoutComponent(component);
+  }
 
-    if (way == X_AXIS
-        || (way == LINE_AXIS
-            && component.getComponentOrientation().isHorizontal())
-        || (way == PAGE_AXIS
-            && !component.getComponentOrientation().isHorizontal()))
-      grid.setColumns(grid.getColumns() - 1);
-    else
-      grid.setRows(grid.getRows() - 1);
+  private boolean isHorizontalIn(Container parent)
+  {
+    ComponentOrientation orientation = parent.getComponentOrientation();
+    return this.way == X_AXIS 
+      || (this.way == LINE_AXIS 
+          && orientation.isHorizontal())
+      || (this.way == PAGE_AXIS
+          && (!orientation.isHorizontal()));
   }
 
+  
+
   /**
    * Returns the preferred size of the layout.
    *
@@ -188,8 +145,38 @@
   {
     if (parent != container)
       throw new AWTError("invalid parent");
+
+    int x = 0;
+    int y = 0;
+
+    Component[] children = parent.getComponents();
+
+    if (isHorizontalIn(parent))
+      {        
+        // sum up preferred widths of components, find maximum of preferred
+        // heights
+        for (int index = 0; index < children.length; index++)
+          {
+            Component comp = children[index];
+            Dimension sz = comp.getPreferredSize();
+            x += sz.width;
+            y = Math.max(y, sz.height);
+          }
+      } 
+    else 
+      {        
+        // sum up preferred heights of components, find maximum of
+        //  preferred widths
+        for (int index = 0; index < children.length; index++)
+          {
+            Component comp = children[index];
+            Dimension sz = comp.getPreferredSize();
+            y += sz.height;
+            x = Math.max(x, sz.width);
+          }
+      }
     
-    return grid.preferredLayoutSize(parent);
+    return new Dimension(x, y);
   }
 
   /**
@@ -203,8 +190,38 @@
   {
     if (parent != container)
       throw new AWTError("invalid parent");
+
+    int x = 0;
+    int y = 0;
+
+    Component[] children = parent.getComponents();
+
+    if (isHorizontalIn(parent))
+      {
+        // sum up preferred widths of components, find maximum of preferred
+        // heights
+        for (int index = 0; index < children.length; index++)
+          {
+            Component comp = children[index];
+            Dimension sz = comp.getMinimumSize();
+            x += sz.width;
+            y = Math.max(y, sz.height);
+          }
+      }
+    else
+      {
+        // sum up preferred heights of components, find maximum of
+        //  preferred widths
+        for (int index = 0; index < children.length; index++)
+          {
+            Component comp = children[index];
+            Dimension sz = comp.getMinimumSize();
+            y += sz.height;
+            x = Math.max(x, sz.width);
+          }
+      }
     
-    return grid.minimumLayoutSize(parent);
+    return new Dimension(x, y);
   }
 
   /**
@@ -216,19 +233,69 @@
   {
     if (parent != container)
       throw new AWTError("invalid parent");
-    
-    grid.layoutContainer(parent);
-  }
 
+    Dimension size = parent.getSize();
+
+    Component[] children = parent.getComponents();
+
+    if (isHorizontalIn(parent))
+      {
+        int x = 0;
+        for (int index = 0; index < children.length; index++)
+          {
+            Component comp = children[index];
+            Dimension sz = comp.getPreferredSize();
+            int width = sz.width;
+            int height = sz.height;
+            int cy = 0;
+            if (height > size.height)
+              {
+                height = size.height;
+              }
+            else
+              {
+                cy = (int) ((size.height - height) * comp.getAlignmentY());
+              }
+            
+            comp.setSize(width, height);
+            comp.setLocation(x, cy);
+            x = x + width;            
+          }
+      }
+    else
+      {
+        int y = 0;        
+        for (int index = 0; index < children.length; index++)
+          {
+            Component comp = children[index];
+            Dimension sz = comp.getPreferredSize();
+            int width = sz.width;
+            int height = sz.height;
+            int cx = 0;
+            if (width > size.width)
+              {
+                width = size.width;
+              }
+            else
+              {
+                cx = (int) ((size.width - width) * comp.getAlignmentX());
+              }
+            
+            comp.setSize(width, height);
+            comp.setLocation(cx, y);
+            y = y + height;            
+          }
+      }    
+  }
+  
   /**
-   * Adds a component to the layout.
+   * Adds a component to the layout. Not used in BoxLayout
    *
    * @param child The component to add to the layout.
    * @param constraints The constraints for the component in the layout.
    */
   public void addLayoutComponent(Component child, Object constraints)
   {
-    addLayoutComponent("", child);
   }
 
   /**
@@ -284,7 +351,37 @@
   {
     if (parent != container)
       throw new AWTError("invalid parent");
-    
-    return preferredLayoutSize(parent);
+
+    int x = 0;
+    int y = 0;
+
+    Component[] children = parent.getComponents();
+
+    if (isHorizontalIn(parent))
+      {
+        
+        // sum up preferred widths of components, find maximum of preferred
+        // heights
+        for (int index = 0; index < children.length; index++)
+          {
+            Component comp = children[index];
+            Dimension sz = comp.getMaximumSize();
+            x += sz.width;
+            y = Math.max(y, sz.height);
+          }
+      }
+    else
+      {
+        // sum up preferred heights of components, find maximum of
+        //  preferred widths
+        for (int index = 0; index < children.length; index++)
+          {
+            Component comp = children[index];
+            Dimension sz = comp.getMaximumSize();
+            y += sz.height;
+            x = Math.max(x, sz.width);
+          }
+      } 
+    return new Dimension(x, y);
   }
 }

Attachment: signature.asc
Description: This is a digitally signed message part


reply via email to

[Prev in Thread] Current Thread [Next in Thread]