tn5250j/src/org/tn5250j/tools AlignLayout.java,1.1.1.1,1.2 FixedCenterLayout.java,1.1,1.2 ENHGridLayout.java,1.1.1.1,1.2

"Kenneth J. Pouncey" <[email protected]> Thu, 16 Jun 2005 04:41:20 +0000
Newsgroups gmane.comp.java.tn5250j.cvs
Message-ID <[email protected]>
Update of /cvsroot/tn5250j/tn5250j/src/org/tn5250j/tools
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24279/src/org/tn5250j/tools

Modified Files:
	AlignLayout.java FixedCenterLayout.java ENHGridLayout.java 
Log Message:
the rest of the story

Index: AlignLayout.java
===================================================================
RCS file: /cvsroot/tn5250j/tn5250j/src/org/tn5250j/tools/AlignLayout.java,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -C2 -d -r1.1.1.1 -r1.2
*** AlignLayout.java	10 Apr 2002 19:46:26 -0000	1.1.1.1
--- AlignLayout.java	16 Jun 2005 04:41:18 -0000	1.2
***************
*** 1,172 ****
! package org.tn5250j.tools;
! /**
!  * Title: tn5250J
!  * Copyright:   Copyright (c) 2001
!  * Company:
!  * @author  Kenneth J. Pouncey
!  * @version 0.1
!  *
!  * Description:
!  *
!  * This program 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.
!  *
!  * This program 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 this software; see the file COPYING.  If not, write to
!  * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
!  * Boston, MA 02111-1307 USA
!  *
!  */
! 
! import java.awt.Container;
! import java.awt.Component;
! import java.util.Hashtable;
! 
! public class AlignLayout extends ENHGridLayout {
! 
!    protected Hashtable alignment;
!    protected Hashtable resize_width, resize_height;
!    public static final int TOP = 1;
!    public static final int MIDDLE = 4;
!    public static final int BOTTOM = 5;
! 
!    /**
!     * Creates an aligner layout with 2 columns, a variable number of rows,
!     * and a gap of 5 pixels.
!     */
!    public AlignLayout() {
!       this(2, 5, 5);
!    }
! 
!    /**
!     * Creates an aligner layout with the specified number of columns and gaps.
!     * @param cols the number of columns (should be a multiple of 2)
!     * @param hgap the horizontal gap variable
!     * @param vgap the vertical gap variable
!     * @exception IllegalArgumentException If the rows and columns are invalid.
!     */
!    public AlignLayout(int cols, int hgap, int vgap) {
!       super(VARIABLE, cols, hgap, vgap);
!    }
! 
!    private int get(Hashtable table, Component comp, int def) {
!       Object v = (table != null) ? table.get(""+comp.hashCode()) : null;
!       return (v != null) ? ((Integer)v).intValue() : def;
!    }
! 
!    private boolean get(Hashtable table, Component comp, boolean def) {
!       Object v = (table != null) ? table.get(""+comp.hashCode()) : null;
!       return (v != null) ? ((Boolean)v).booleanValue() : def;
!    }
! 
!    /**
!     * Gets the vertical position of a label relative to its control.
!     * @see #setLabelVerticalAlignment
!     */
!    public int getLabelVerticalAlignment(Component child) {
!       return get(alignment, child, MIDDLE);
!    }
! 
!    /**
!     * Sets the vertical position of a label relative to its control.
!     * @param align TOP, MIDDLE (default), or BOTTOM.
!     * @exception IllegalArgumentException If an invalid value is set
!     */
!    public void setLabelVerticalAlignment(Component child, int align) {
!       if (alignment == null) alignment = new Hashtable(5);
!       alignment.put(""+child.hashCode(), new Integer(align));
!    }
! 
!    /** Gets the component's RezizeWidth value.
!     * @see #setResizeWidth
!     */
!    public boolean getResizeWidth(Component child) {
!       return get(resize_width, child, false);
!    }
! 
!    /** Sets whether the control should be resized horizontally to its parent's
!     * right edge if it is in the last column (default: false).
!     */
!    public void setResizeWidth(Component child, boolean v) {
!       if (resize_width == null) resize_width = new Hashtable(5);
!       resize_width.put(""+child.hashCode(), new Boolean(v));
!    }
! 
!    /** Gets the component's RezizeHeight value.
!     * @see #setResizeHeight
!     */
!    public boolean getResizeHeight(Component child) {
!       return get(resize_height, child, false);
!    }
! 
!    /** Sets whether the control should be resized vertically to the height of the
!     * largest component in its row (default: false). This value is ignored for
!     * labels (the components in odd columns).
!     */
!    public void setResizeHeight(Component child, boolean v) {
!       if (resize_height == null) resize_height = new Hashtable(5);
!       resize_height.put(""+child.hashCode(), new Boolean(v));
!    }
! 
!    protected boolean isLabel(int col) { return (col % 2) == 0; }
! 
!    /**
!     * Positions the component.
!     * @param pos the component's index in its parents child list
!     * @param row,col component's position
!     */
!    protected void setBounds(int pos, int row, int col, Component comp,
!                      int x, int y, int col_width, int row_height) {
! 
!       int comp_w = col_width, comp_h = row_height;
! 
!       if (isLabel(col) || !getResizeHeight(comp)) {
!          comp_h = comp.getPreferredSize().height;
!       }
! 
!       /* Resize a control to its parent's right edge if its resizeWidth value
!        * is true, and it is in the last column
!        */
!       if (!isLabel(col)) {
!          if (col < col_widths.length-1) {
!          }
!          else if (getResizeWidth(comp)) {
!             Container parent = comp.getParent();
!             comp_w = parent.getSize().width - parent.getInsets().right - x;
!          }
!          else {
!             comp_w = comp.getPreferredSize().width;
!          }
! 
!          comp.setBounds(x, y, comp_w, comp_h);
! 
!          return;
!       }
! 
!       int control_h = row_height;
!       if (pos < comp.getParent().getComponentCount()-1) {
!          Component control = comp.getParent().getComponents()[pos+1];
!          if (control != null && !getResizeHeight(control)) {
!             control_h = control.getPreferredSize().height;
!          }
!       }
! 
!       switch (getLabelVerticalAlignment(comp)) {
!       case MIDDLE:
!          y += (control_h - comp_h) / 2;
!          break;
!       case BOTTOM:
!          y += control_h - comp_h;
!          break;
!       }
!       comp.setBounds(x, y, comp_w, comp_h);
!    }
! 
! }
--- 1,172 ----
! package org.tn5250j.tools;
! /**
!  * Title: tn5250J
!  * Copyright:   Copyright (c) 2001
!  * Company:
!  * @author  Kenneth J. Pouncey
!  * @version 0.1
!  *
!  * Description:
!  *
!  * This program 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.
!  *
!  * This program 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 this software; see the file COPYING.  If not, write to
!  * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
!  * Boston, MA 02111-1307 USA
!  *
!  */
! 
! import java.awt.Container;
! import java.awt.Component;
! import java.util.Hashtable;
! 
! public class AlignLayout extends ENHGridLayout {
! 
!    protected Hashtable alignment;
!    protected Hashtable resize_width, resize_height;
!    public static final int TOP = 1;
!    public static final int MIDDLE = 4;
!    public static final int BOTTOM = 5;
! 
!    /**
!     * Creates an aligner layout with 2 columns, a variable number of rows,
!     * and a gap of 5 pixels.
!     */
!    public AlignLayout() {
!       this(2, 5, 5);
!    }
! 
!    /**
!     * Creates an aligner layout with the specified number of columns and gaps.
!     * @param cols the number of columns (should be a multiple of 2)
!     * @param hgap the horizontal gap variable
!     * @param vgap the vertical gap variable
!     * @exception IllegalArgumentException If the rows and columns are invalid.
!     */
!    public AlignLayout(int cols, int hgap, int vgap) {
!       super(VARIABLE, cols, hgap, vgap);
!    }
! 
!    private int get(Hashtable table, Component comp, int def) {
!       Object v = (table != null) ? table.get(""+comp.hashCode()) : null;
!       return (v != null) ? ((Integer)v).intValue() : def;
!    }
! 
!    private boolean get(Hashtable table, Component comp, boolean def) {
!       Object v = (table != null) ? table.get(""+comp.hashCode()) : null;
!       return (v != null) ? ((Boolean)v).booleanValue() : def;
!    }
! 
!    /**
!     * Gets the vertical position of a label relative to its control.
!     * @see #setLabelVerticalAlignment
!     */
!    public int getLabelVerticalAlignment(Component child) {
!       return get(alignment, child, MIDDLE);
!    }
! 
!    /**
!     * Sets the vertical position of a label relative to its control.
!     * @param align TOP, MIDDLE (default), or BOTTOM.
!     * @exception IllegalArgumentException If an invalid value is set
!     */
!    public void setLabelVerticalAlignment(Component child, int align) {
!       if (alignment == null) alignment = new Hashtable(5);
!       alignment.put(""+child.hashCode(), new Integer(align));
!    }
! 
!    /** Gets the component's RezizeWidth value.
!     * @see #setResizeWidth
!     */
!    public boolean getResizeWidth(Component child) {
!       return get(resize_width, child, false);
!    }
! 
!    /** Sets whether the control should be resized horizontally to its parent's
!     * right edge if it is in the last column (default: false).
!     */
!    public void setResizeWidth(Component child, boolean v) {
!       if (resize_width == null) resize_width = new Hashtable(5);
!       resize_width.put(""+child.hashCode(), new Boolean(v));
!    }
! 
!    /** Gets the component's RezizeHeight value.
!     * @see #setResizeHeight
!     */
!    public boolean getResizeHeight(Component child) {
!       return get(resize_height, child, false);
!    }
! 
!    /** Sets whether the control should be resized vertically to the height of the
!     * largest component in its row (default: false). This value is ignored for
!     * labels (the components in odd columns).
!     */
!    public void setResizeHeight(Component child, boolean v) {
!       if (resize_height == null) resize_height = new Hashtable(5);
!       resize_height.put(""+child.hashCode(), new Boolean(v));
!    }
! 
!    protected boolean isLabel(int col) { return (col % 2) == 0; }
! 
!    /**
!     * Positions the component.
!     * @param pos the component's index in its parents child list
!     * @param row,col component's position
!     */
!    protected void setBounds(int pos, int row, int col, Component comp,
!                      int x, int y, int col_width, int row_height) {
! 
!       int comp_w = col_width, comp_h = row_height;
! 
!       if (isLabel(col) || !getResizeHeight(comp)) {
!          comp_h = comp.getPreferredSize().height;
!       }
! 
!       /* Resize a control to its parent's right edge if its resizeWidth value
!        * is true, and it is in the last column
!        */
!       if (!isLabel(col)) {
!          if (col < col_widths.length-1) {
!          }
!          else if (getResizeWidth(comp)) {
!             Container parent = comp.getParent();
!             comp_w = parent.getSize().width - parent.getInsets().right - x;
!          }
!          else {
!             comp_w = comp.getPreferredSize().width;
!          }
! 
!          comp.setBounds(x, y, comp_w, comp_h);
! 
!          return;
!       }
! 
!       int control_h = row_height;
!       if (pos < comp.getParent().getComponentCount()-1) {
!          Component control = comp.getParent().getComponents()[pos+1];
!          if (control != null && !getResizeHeight(control)) {
!             control_h = control.getPreferredSize().height;
!          }
!       }
! 
!       switch (getLabelVerticalAlignment(comp)) {
!       case MIDDLE:
!          y += (control_h - comp_h) / 2;
!          break;
!       case BOTTOM:
!          y += control_h - comp_h;
!          break;
!       }
!       comp.setBounds(x, y, comp_w, comp_h);
!    }
! 
! }

Index: ENHGridLayout.java
===================================================================
RCS file: /cvsroot/tn5250j/tn5250j/src/org/tn5250j/tools/ENHGridLayout.java,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -C2 -d -r1.1.1.1 -r1.2
*** ENHGridLayout.java	10 Apr 2002 19:46:24 -0000	1.1.1.1
--- ENHGridLayout.java	16 Jun 2005 04:41:18 -0000	1.2
***************
*** 1,229 ****
! package org.tn5250j.tools;
! /**
!  * Title: tn5250J
!  * Copyright:   Copyright (c) 2001
!  * Company:
!  * @author  Kenneth J. Pouncey
!  * @version 0.1
!  *
!  * Description:
!  *
!  * This program 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.
!  *
!  * This program 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 this software; see the file COPYING.  If not, write to
!  * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
!  * Boston, MA 02111-1307 USA
!  *
!  */
! import java.awt.GridLayout;
! import java.awt.Container;
! import java.awt.Component;
! import java.awt.Dimension;
! import java.awt.Insets;
! 
! /**
!  * <code>ENHGridLayout</code> is an improved subclass of <code>GridLayout</code>.
!  * It lays out a grid of rows and columns based on the attributes of the
!  * individual rows and columns. <code>ENHGridLayout</code>
!  * uses the widest element in a column to set the width of that
!  * column, and the tallest element in a row to set the height of
!  * that row.
!  */
! public class ENHGridLayout extends GridLayout {
! 
!    /** The horiztonal gap between items. */
!    protected int hgap;
! 
!    /** The vertical gap between items. */
!    protected int vgap;
! 
!    /** The number of rows in the layout, as set by the user.
!     * This number may not correspond exactly to the number of
!     * rows in the layout.
!     */
!    protected int rows;
! 
!    /** The number of columns in the layout, as set by the user.
!     * This number may not correspond exactly to the number of
!     * columns in the layout.
!     */
!    protected int cols;
! 
!    /** Array of row heights.
!     * It is accurate only after a call to getGridSizes()
!     */
!    protected int row_heights[] = new int[0];
! 
!    /** Array of column widths.
!     * It is accurate only after a call to getGridSizes()
!     */
!    protected int col_widths[] = new int[0];
! 
!    public final static int VARIABLE = 0;
! 
!    /**
!     * Creates a grid layout with the specified number of rows and columns.
!     * @param rows the number of rows in the layout
!     * @param cols the number of columns in the layout
!     */
!    public ENHGridLayout(int rows, int cols) {
!       this(rows, cols, 0, 0);
!    }
! 
!    /**
!     * Creates a grid layout with the specified rows, columns,
!     * horizontal gap, and vertical gap.
!     * @param rows the rows; VARIABLE (0) means 'any number.'
!     * @param cols the columns; VARIABLE (0) means 'any number.'
!     * Only one of 'rows' and 'cols' can be VARIABLE, not both.
!     * @param hgap the horizontal gap variable
!     * @param vgap the vertical gap variable
!     * @exception IllegalArgumentException If the rows and columns are invalid.
!     */
!    public ENHGridLayout(int rows, int cols, int hgap, int vgap) {
!       super(rows, cols, hgap, vgap);
!       this.rows = rows;
!       this.cols = cols;
!       this.hgap = hgap;
!       this.vgap = vgap;
!    }
! 
!    /**
!     * Traverses the children and determines row heights and column widths.
!     * @param parent the component which needs to be laid out
!     * @param min if true, the minimum size is used. Otherwise, the preferred size
!     * is used.
!     */
!    protected void getGridSizes(Container parent, boolean min) {
!       int ncomponents = parent.getComponentCount();
!       if (ncomponents == 0) return;
!       int nrows = rows, ncols = cols;
!       if (nrows > 0)
!           ncols = (ncomponents + nrows - 1) / nrows;
!       else
!           nrows = (ncomponents + ncols - 1) / ncols;
! 
!       row_heights = new int[nrows];
!       col_widths = new int[ncols];
! 
!       for (int i = 0; i < ncomponents; i++) {
!           Component comp = parent.getComponent(i);
!          Dimension d = min ? comp.getMinimumSize() :
!                       comp.getPreferredSize();
! 
!          int row = i / ncols;
!          if (d.height > row_heights[row])
!             row_heights[row] = d.height;
! 
!          int col = i % ncols;
!          if (d.width > col_widths[col])
!             col_widths[col] = d.width;
!       }
!    }
! 
!    /**
!     * Sums the items of an array
!     */
!    final int sum(int[] array) {
!       if (array == null) return 0;
!       int s = 0;
!       for (int i = 0; i < array.length; i++)
!          s += array[i];
!       return s;
!    }
! 
!    /**
!     * Calculates the preferred size for this layout.
!     * @param parent the component which needs to be laid out
!     */
!    public Dimension preferredLayoutSize(Container parent) {
! 
!       Insets insets = parent.getInsets();
!       getGridSizes(parent, false);
!       return new Dimension(insets.left + insets.right + sum(col_widths)
!                       + (col_widths.length+1)*hgap,
!                       insets.top + insets.bottom + sum(row_heights)
!                       + (row_heights.length+1)*vgap);
!    }
! 
!    /**
!     * Returns the minimum dimensions needed to layout the components
!     * contained in the specified panel.
!     * @param parent the component which needs to be laid out
!     */
!    public Dimension minimumLayoutSize(Container parent) {
! 
!       Insets insets = parent.getInsets();
!       getGridSizes(parent, true);
!       return new Dimension(insets.left + insets.right + sum(col_widths)
!                       + (col_widths.length+1)*hgap,
!                       insets.top + insets.bottom + sum(row_heights)
!                       + (row_heights.length+1)*vgap);
!    }
! 
!    /**
!     * Positions the component.
!     * @param pos the component's index in its parents child list
!     * @param row,col component's position
!     */
!    protected void setBounds(int pos, int row, int col,
!                      Component comp, int x, int y, int w, int h) {
! 
!       comp.setBounds(x, y, w, h);
! 
!    }
! 
!    /**
!     * Performs the layout of the children.
!     * It calculates the number of actual rows and columns
!     * based on the user's settings, retrieves row height and column
!     * width information, then moves all the children to the appropriate places.
!     * @param parent the specified component being laid out
!     * @see #reshape
!     */
!    public void layoutContainer(Container parent) {
!       int ncomponents = parent.getComponentCount();
!       if (ncomponents == 0) {
!          return;
!       }
!       Insets insets = parent.getInsets();
! 
!       getGridSizes(parent, false);
!       int nrows = rows, ncols = cols;
! 
!       if (nrows > 0)
!           ncols = (ncomponents + nrows - 1) / nrows;
!       else
!           nrows = (ncomponents + ncols - 1) / ncols;
! 
!       Dimension psize = parent.getSize();
!       for (int col = 0, x = insets.left+hgap; col < ncols; col++) {
!           for (int row = 0, y = insets.top+vgap; row < nrows; row++) {
!             int i = row*ncols + col;
!             if (i < ncomponents) {
!                int w = Math.max(0, Math.min(col_widths[col],
!                                      psize.width-insets.right-x));
!                int h = Math.max(0, Math.min(row_heights[row],
!                                      psize.height-insets.bottom-y));
!                setBounds(i, row, col, parent.getComponent(i), x, y, w, h);
!             }
!             y += row_heights[row] + vgap;
!           }
!          x += col_widths[col] + hgap;
!       }
!    }
! 
! }
! 
! 
! 
! 
--- 1,229 ----
! package org.tn5250j.tools;
! /**
!  * Title: tn5250J
!  * Copyright:   Copyright (c) 2001
!  * Company:
!  * @author  Kenneth J. Pouncey
!  * @version 0.1
!  *
!  * Description:
!  *
!  * This program 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.
!  *
!  * This program 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 this software; see the file COPYING.  If not, write to
!  * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
!  * Boston, MA 02111-1307 USA
!  *
!  */
! import java.awt.GridLayout;
! import java.awt.Container;
! import java.awt.Component;
! import java.awt.Dimension;
! import java.awt.Insets;
! 
! /**
!  * <code>ENHGridLayout</code> is an improved subclass of <code>GridLayout</code>.
!  * It lays out a grid of rows and columns based on the attributes of the
!  * individual rows and columns. <code>ENHGridLayout</code>
!  * uses the widest element in a column to set the width of that
!  * column, and the tallest element in a row to set the height of
!  * that row.
!  */
! public class ENHGridLayout extends GridLayout {
! 
!    /** The horiztonal gap between items. */
!    protected int hgap;
! 
!    /** The vertical gap between items. */
!    protected int vgap;
! 
!    /** The number of rows in the layout, as set by the user.
!     * This number may not correspond exactly to the number of
!     * rows in the layout.
!     */
!    protected int rows;
! 
!    /** The number of columns in the layout, as set by the user.
!     * This number may not correspond exactly to the number of
!     * columns in the layout.
!     */
!    protected int cols;
! 
!    /** Array of row heights.
!     * It is accurate only after a call to getGridSizes()
!     */
!    protected int row_heights[] = new int[0];
! 
!    /** Array of column widths.
!     * It is accurate only after a call to getGridSizes()
!     */
!    protected int col_widths[] = new int[0];
! 
!    public final static int VARIABLE = 0;
! 
!    /**
!     * Creates a grid layout with the specified number of rows and columns.
!     * @param rows the number of rows in the layout
!     * @param cols the number of columns in the layout
!     */
!    public ENHGridLayout(int rows, int cols) {
!       this(rows, cols, 0, 0);
!    }
! 
!    /**
!     * Creates a grid layout with the specified rows, columns,
!     * horizontal gap, and vertical gap.
!     * @param rows the rows; VARIABLE (0) means 'any number.'
!     * @param cols the columns; VARIABLE (0) means 'any number.'
!     * Only one of 'rows' and 'cols' can be VARIABLE, not both.
!     * @param hgap the horizontal gap variable
!     * @param vgap the vertical gap variable
!     * @exception IllegalArgumentException If the rows and columns are invalid.
!     */
!    public ENHGridLayout(int rows, int cols, int hgap, int vgap) {
!       super(rows, cols, hgap, vgap);
!       this.rows = rows;
!       this.cols = cols;
!       this.hgap = hgap;
!       this.vgap = vgap;
!    }
! 
!    /**
!     * Traverses the children and determines row heights and column widths.
!     * @param parent the component which needs to be laid out
!     * @param min if true, the minimum size is used. Otherwise, the preferred size
!     * is used.
!     */
!    protected void getGridSizes(Container parent, boolean min) {
!       int ncomponents = parent.getComponentCount();
!       if (ncomponents == 0) return;
!       int nrows = rows, ncols = cols;
!       if (nrows > 0)
!           ncols = (ncomponents + nrows - 1) / nrows;
!       else
!           nrows = (ncomponents + ncols - 1) / ncols;
! 
!       row_heights = new int[nrows];
!       col_widths = new int[ncols];
! 
!       for (int i = 0; i < ncomponents; i++) {
!           Component comp = parent.getComponent(i);
!          Dimension d = min ? comp.getMinimumSize() :
!                       comp.getPreferredSize();
! 
!          int row = i / ncols;
!          if (d.height > row_heights[row])
!             row_heights[row] = d.height;
! 
!          int col = i % ncols;
!          if (d.width > col_widths[col])
!             col_widths[col] = d.width;
!       }
!    }
! 
!    /**
!     * Sums the items of an array
!     */
!    final int sum(int[] array) {
!       if (array == null) return 0;
!       int s = 0;
!       for (int i = 0; i < array.length; i++)
!          s += array[i];
!       return s;
!    }
! 
!    /**
!     * Calculates the preferred size for this layout.
!     * @param parent the component which needs to be laid out
!     */
!    public Dimension preferredLayoutSize(Container parent) {
! 
!       Insets insets = parent.getInsets();
!       getGridSizes(parent, false);
!       return new Dimension(insets.left + insets.right + sum(col_widths)
!                       + (col_widths.length+1)*hgap,
!                       insets.top + insets.bottom + sum(row_heights)
!                       + (row_heights.length+1)*vgap);
!    }
! 
!    /**
!     * Returns the minimum dimensions needed to layout the components
!     * contained in the specified panel.
!     * @param parent the component which needs to be laid out
!     */
!    public Dimension minimumLayoutSize(Container parent) {
! 
!       Insets insets = parent.getInsets();
!       getGridSizes(parent, true);
!       return new Dimension(insets.left + insets.right + sum(col_widths)
!                       + (col_widths.length+1)*hgap,
!                       insets.top + insets.bottom + sum(row_heights)
!                       + (row_heights.length+1)*vgap);
!    }
! 
!    /**
!     * Positions the component.
!     * @param pos the component's index in its parents child list
!     * @param row,col component's position
!     */
!    protected void setBounds(int pos, int row, int col,
!                      Component comp, int x, int y, int w, int h) {
! 
!       comp.setBounds(x, y, w, h);
! 
!    }
! 
!    /**
!     * Performs the layout of the children.
!     * It calculates the number of actual rows and columns
!     * based on the user's settings, retrieves row height and column
!     * width information, then moves all the children to the appropriate places.
!     * @param parent the specified component being laid out
!     * @see #reshape
!     */
!    public void layoutContainer(Container parent) {
!       int ncomponents = parent.getComponentCount();
!       if (ncomponents == 0) {
!          return;
!       }
!       Insets insets = parent.getInsets();
! 
!       getGridSizes(parent, false);
!       int nrows = rows, ncols = cols;
! 
!       if (nrows > 0)
!           ncols = (ncomponents + nrows - 1) / nrows;
!       else
!           nrows = (ncomponents + ncols - 1) / ncols;
! 
!       Dimension psize = parent.getSize();
!       for (int col = 0, x = insets.left+hgap; col < ncols; col++) {
!           for (int row = 0, y = insets.top+vgap; row < nrows; row++) {
!             int i = row*ncols + col;
!             if (i < ncomponents) {
!                int w = Math.max(0, Math.min(col_widths[col],
!                                      psize.width-insets.right-x));
!                int h = Math.max(0, Math.min(row_heights[row],
!                                      psize.height-insets.bottom-y));
!                setBounds(i, row, col, parent.getComponent(i), x, y, w, h);
!             }
!             y += row_heights[row] + vgap;
!           }
!          x += col_widths[col] + hgap;
!       }
!    }
! 
! }
! 
! 
! 
! 

Index: FixedCenterLayout.java
===================================================================
RCS file: /cvsroot/tn5250j/tn5250j/src/org/tn5250j/tools/FixedCenterLayout.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** FixedCenterLayout.java	30 Nov 2003 10:16:01 -0000	1.1
--- FixedCenterLayout.java	16 Jun 2005 04:41:18 -0000	1.2
***************
*** 1,242 ****
! /**
!  * Title: tn5250J
!  * Copyright:   Copyright (c) 2001
!  * Company:
!  * @author  Kenneth J. Pouncey
!  * @version 0.5
!  *
!  * Description:
!  *
!  * This program 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.
!  *
!  * This program 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 this software; see the file COPYING.  If not, write to
!  * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
!  * Boston, MA 02111-1307 USA
!  *
!  */
! 
! package org.tn5250j.tools;
! 
! import java.awt.LayoutManager2;
! import java.io.Serializable;
! import java.awt.Component;
! import java.awt.Dimension;
! import java.awt.Container;
! import java.awt.BorderLayout;
! import java.awt.Insets;
! 
! /**
!  * Fixed Center layout.
!  */
! public class FixedCenterLayout implements LayoutManager2, Serializable {
! 
!     protected int hgap;
!     protected Component west;
!     protected Component east;
!     protected Component center;
! 
!     /**
!      * Constructs a new layout with no gap between components.
!      */
!     public FixedCenterLayout() {
!         this(0);
!     }
! 
!     /**
!      * Constructs a layout with the specified gaps between components.
!      */
!     public FixedCenterLayout(int hgap) {
!         this.hgap = hgap;
!     }
! 
!     /**
!      * Returns the horizontal gap between components.
!      */
!     public int getHgap() {
!         return hgap;
!     }
! 
!     /**
!      * Sets the horizontal gap between components.
!      */
!     public void setHgap(int hgap) {
!         this.hgap = hgap;
!     }
! 
!     /**
!      * Adds the specified component to the layout.
!      */
!     public void addLayoutComponent(Component comp, Object constraints) {
!         synchronized (comp.getTreeLock()) {
!             if ((constraints == null) || (constraints instanceof String)) {
!                 addLayoutComponent((String)constraints, comp);
!             } else {
!                 throw new IllegalArgumentException("Cannot add to layout: constraint must be a string or null");
!             }
!         }
!     }
! 
!     /**
!      * We are forced to support it by <code>LayoutManager</code>.
!      */
!     public void addLayoutComponent(String name, Component comp) {
!         synchronized (comp.getTreeLock()) {
!             if (name == null || BorderLayout.CENTER.equals(name)) {
!                 center = comp;
!             } else if (BorderLayout.EAST.equals(name)) {
!                 east = comp;
!             } else if (BorderLayout.WEST.equals(name)) {
!                 west = comp;
!             } else {
!                 throw new IllegalArgumentException("cannot add to layout: unknown constraint: " + name);
!             }
!         }
!     }
! 
!     /**
!      * Removes the specified component from this layout.
!      */
!     public void removeLayoutComponent(Component component) {
!         synchronized (component.getTreeLock()) {
!             if (component == center) {
!                 center = null;
!             } else if (component == east) {
!                 east = null;
!             } else if (component == west) {
!                 west = null;
!             }
!         }
!     }
! 
!     /**
!      * Determines the minimum size of the target.
!      */
!     public Dimension minimumLayoutSize(Container target) {
!         synchronized (target.getTreeLock()) {
!             Dimension d = new Dimension(0, 0);
! 
!             addMinimumSize(d, east);
!             addMinimumSize(d, west);
!             addMinimumSize(d, center);
! 
!             Insets insets = target.getInsets();
!             d.width += insets.left + insets.right;
!             d.height += insets.top + insets.bottom;
! 
!             return d;
!         }
!     }
! 
!     /**
!      * Determines the preferred size of the target.
!      */
!     public Dimension preferredLayoutSize(Container target) {
!         synchronized (target.getTreeLock()) {
!             Dimension d = new Dimension(0, 0);
! 
!             addPreferredSize(d, east);
!             addPreferredSize(d, west);
!             addPreferredSize(d, center);
! 
!             Insets insets = target.getInsets();
!             d.width += insets.left + insets.right;
!             d.height += insets.top + insets.bottom;
! 
!             return d;
!         }
!     }
! 
!     /**
!      * Determines the maximum size of the target.
!      */
!     public Dimension maximumLayoutSize(Container target) {
!         return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
!     }
! 
!     /**
!      * Returns the alignment along the x axis.
!      */
!     public float getLayoutAlignmentX(Container parent) {
!         return 0.5f;
!     }
! 
!     /**
!      * Returns the alignment along the y axis.
!      */
!     public float getLayoutAlignmentY(Container parent) {
!         return 0.5f;
!     }
! 
!     /**
!      * Invalidates the layout, indicating that if the layout manager
!      * has cached information it should be discarded.
!      */
!     public void invalidateLayout(Container target) {
!     }
! 
!     /**
!      * Lays out the target argument using this layout.
!      */
!     public void layoutContainer(Container target) {
!         synchronized (target.getTreeLock()) {
!             Insets insets = target.getInsets();
!             int top = insets.top;
!             //	int bottom = target.getHeight() - insets.bottom;
!             int bottom = target.getBounds().height - insets.bottom;
!             int left = insets.left;
!             //	int right = target.getWidth() - insets.right;
!             int right = target.getBounds().width - insets.right;
! 
!             int leftCenter = (right-left)/2;
!             int rightCenter = leftCenter;
! 
!             if (center != null) {
!                 Dimension d = center.getPreferredSize();
!                 leftCenter = (right-left-d.width)/2;
!                 rightCenter = leftCenter+d.width;
!                 center.setBounds(leftCenter, top, d.width, bottom-top);
!             }
!             if (west != null) {
!                 west.setBounds(left, top, leftCenter-left-hgap, bottom-top);
!             }
!             if (east != null) {
!                 east.setBounds(rightCenter+hgap, top, right-rightCenter-2*hgap, bottom-top);
!             }
!         }
!     }
! 
!     private void addMinimumSize(Dimension d, Component c) {
!         if (c != null) {
!             addSize(d, c.getMinimumSize());
!         }
!     }
! 
!     private void addPreferredSize(Dimension d, Component c) {
!         if (c != null) {
!             addSize(d, c.getPreferredSize());
!         }
!     }
! 
!     private void addSize(Dimension d, Dimension size) {
!         d.width += size.width + hgap;
!         d.height = Math.max(size.height, d.height);
!     }
! 
!     /**
!      * Returns a string representation of the state of this layout.
!      */
!     public String toString() {
!         return getClass().getName() + "[hgap=" + hgap + "]";
! 
!     }
! 
! } // FixedCenterLayout
--- 1,242 ----
! /**
!  * Title: tn5250J
!  * Copyright:   Copyright (c) 2001
!  * Company:
!  * @author  Kenneth J. Pouncey
!  * @version 0.5
!  *
!  * Description:
!  *
!  * This program 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.
!  *
!  * This program 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 this software; see the file COPYING.  If not, write to
!  * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
!  * Boston, MA 02111-1307 USA
!  *
!  */
! 
! package org.tn5250j.tools;
! 
! import java.awt.LayoutManager2;
! import java.io.Serializable;
! import java.awt.Component;
! import java.awt.Dimension;
! import java.awt.Container;
! import java.awt.BorderLayout;
! import java.awt.Insets;
! 
! /**
!  * Fixed Center layout.
!  */
! public class FixedCenterLayout implements LayoutManager2, Serializable {
! 
!     protected int hgap;
!     protected Component west;
!     protected Component east;
!     protected Component center;
! 
!     /**
!      * Constructs a new layout with no gap between components.
!      */
!     public FixedCenterLayout() {
!         this(0);
!     }
! 
!     /**
!      * Constructs a layout with the specified gaps between components.
!      */
!     public FixedCenterLayout(int hgap) {
!         this.hgap = hgap;
!     }
! 
!     /**
!      * Returns the horizontal gap between components.
!      */
!     public int getHgap() {
!         return hgap;
!     }
! 
!     /**
!      * Sets the horizontal gap between components.
!      */
!     public void setHgap(int hgap) {
!         this.hgap = hgap;
!     }
! 
!     /**
!      * Adds the specified component to the layout.
!      */
!     public void addLayoutComponent(Component comp, Object constraints) {
!         synchronized (comp.getTreeLock()) {
!             if ((constraints == null) || (constraints instanceof String)) {
!                 addLayoutComponent((String)constraints, comp);
!             } else {
!                 throw new IllegalArgumentException("Cannot add to layout: constraint must be a string or null");
!             }
!         }
!     }
! 
!     /**
!      * We are forced to support it by <code>LayoutManager</code>.
!      */
!     public void addLayoutComponent(String name, Component comp) {
!         synchronized (comp.getTreeLock()) {
!             if (name == null || BorderLayout.CENTER.equals(name)) {
!                 center = comp;
!             } else if (BorderLayout.EAST.equals(name)) {
!                 east = comp;
!             } else if (BorderLayout.WEST.equals(name)) {
!                 west = comp;
!             } else {
!                 throw new IllegalArgumentException("cannot add to layout: unknown constraint: " + name);
!             }
!         }
!     }
! 
!     /**
!      * Removes the specified component from this layout.
!      */
!     public void removeLayoutComponent(Component component) {
!         synchronized (component.getTreeLock()) {
!             if (component == center) {
!                 center = null;
!             } else if (component == east) {
!                 east = null;
!             } else if (component == west) {
!                 west = null;
!             }
!         }
!     }
! 
!     /**
!      * Determines the minimum size of the target.
!      */
!     public Dimension minimumLayoutSize(Container target) {
!         synchronized (target.getTreeLock()) {
!             Dimension d = new Dimension(0, 0);
! 
!             addMinimumSize(d, east);
!             addMinimumSize(d, west);
!             addMinimumSize(d, center);
! 
!             Insets insets = target.getInsets();
!             d.width += insets.left + insets.right;
!             d.height += insets.top + insets.bottom;
! 
!             return d;
!         }
!     }
! 
!     /**
!      * Determines the preferred size of the target.
!      */
!     public Dimension preferredLayoutSize(Container target) {
!         synchronized (target.getTreeLock()) {
!             Dimension d = new Dimension(0, 0);
! 
!             addPreferredSize(d, east);
!             addPreferredSize(d, west);
!             addPreferredSize(d, center);
! 
!             Insets insets = target.getInsets();
!             d.width += insets.left + insets.right;
!             d.height += insets.top + insets.bottom;
! 
!             return d;
!         }
!     }
! 
!     /**
!      * Determines the maximum size of the target.
!      */
!     public Dimension maximumLayoutSize(Container target) {
!         return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
!     }
! 
!     /**
!      * Returns the alignment along the x axis.
!      */
!     public float getLayoutAlignmentX(Container parent) {
!         return 0.5f;
!     }
! 
!     /**
!      * Returns the alignment along the y axis.
!      */
!     public float getLayoutAlignmentY(Container parent) {
!         return 0.5f;
!     }
! 
!     /**
!      * Invalidates the layout, indicating that if the layout manager
!      * has cached information it should be discarded.
!      */
!     public void invalidateLayout(Container target) {
!     }
! 
!     /**
!      * Lays out the target argument using this layout.
!      */
!     public void layoutContainer(Container target) {
!         synchronized (target.getTreeLock()) {
!             Insets insets = target.getInsets();
!             int top = insets.top;
!             //	int bottom = target.getHeight() - insets.bottom;
!             int bottom = target.getBounds().height - insets.bottom;
!             int left = insets.left;
!             //	int right = target.getWidth() - insets.right;
!             int right = target.getBounds().width - insets.right;
! 
!             int leftCenter = (right-left)/2;
!             int rightCenter = leftCenter;
! 
!             if (center != null) {
!                 Dimension d = center.getPreferredSize();
!                 leftCenter = (right-left-d.width)/2;
!                 rightCenter = leftCenter+d.width;
!                 center.setBounds(leftCenter, top, d.width, bottom-top);
!             }
!             if (west != null) {
!                 west.setBounds(left, top, leftCenter-left-hgap, bottom-top);
!             }
!             if (east != null) {
!                 east.setBounds(rightCenter+hgap, top, right-rightCenter-2*hgap, bottom-top);
!             }
!         }
!     }
! 
!     private void addMinimumSize(Dimension d, Component c) {
!         if (c != null) {
!             addSize(d, c.getMinimumSize());
!         }
!     }
! 
!     private void addPreferredSize(Dimension d, Component c) {
!         if (c != null) {
!             addSize(d, c.getPreferredSize());
!         }
!     }
! 
!     private void addSize(Dimension d, Dimension size) {
!         d.width += size.width + hgap;
!         d.height = Math.max(size.height, d.height);
!     }
! 
!     /**
!      * Returns a string representation of the state of this layout.
!      */
!     public String toString() {
!         return getClass().getName() + "[hgap=" + hgap + "]";
! 
!     }
! 
! } // FixedCenterLayout



-------------------------------------------------------
SF.Net email is sponsored by: Discover Easy Linux Migration Strategies
from IBM. Find simple to follow Roadmaps, straightforward articles,
informative Webcasts and more! Get everything you need to get up to
speed, fast. http://ads.osdn.com/?ad_id=7477&alloc_id=16492&op=click