/* TruncFlowLayout Copyright Peter Chatterton 1998-2004 This custom layout manager lays out components in a row but provides spacing and end-of-line control by means of specially defined labels e.g. mainPanel.add( new Label( TruncFlowLayout.END_OF_LINE )); Components get their preferred sizes (until they run off the line). END_OF_LINE is required. */ package xmlagency; import java.awt.*; public class TruncFlowLayout implements LayoutManager2 { protected int margin_height; protected int margin_width; protected int hGap = 0; protected int vGap = 0; // need if all text? protected int alignment; protected int maxLineHeight; // from tallest component in a line. public static final String END_OF_LINE = "0END_OF_LINE"; public static final String H_SPACE10 = "0H_SPACE10"; public static final String H_SPACE20 = "0H_SPACE20"; public static final String V_SPACE10 = "0V_SPACE10"; public static final String V_SPACE20 = "0V_SPACE20"; public static final int PREF_SIZE = 1; public static final int MIN_SIZE = 2; public static final int MAX_SIZE = 3; TruncFlowLayout( ) { } TruncFlowLayout( int inHGap) { hGap = inHGap; } TruncFlowLayout( int inHGap, int inVGap ) { hGap = inHGap; vGap = inVGap; } // * // * layoutContainer() // * Layout the components by calling their getBounds(). // * public void layoutContainer( Container parent ) { Util.printlnLayout( "TruncFlowLayout:layoutContainer." ); Insets insets = parent.getInsets(); Dimension parent_size = parent.getSize(); Component child; int numbChildren = parent.getComponentCount(); Util.printlnLayout( "TruncFlowLayout:layoutContainer. numbChildren = " + numbChildren ); int x = insets.left + margin_width; int x0 = x; int y = insets.top + margin_height; for( int i = 0; i < numbChildren; i++ ) { child = parent.getComponent(i); if( !child.isVisible() ) continue; Dimension pref = child.getPreferredSize(); // if( child instanceof Label ) Util.printlnLayout( "TruncFlowLayout:layoutContainer, Label = " + ((Label)child).getText() ); else Util.printlnLayout( "TruncFlowLayout:layoutContainer, Not a Label, name = " + child.getName() ); // if( child instanceof Label ) { String labelText = ((Label)child).getText(); if( labelText.equals( H_SPACE10 ) ) { Util.printlnLayout( "TruncFlowLayout:layoutContainer, H_SPACE10" ); x += 10; continue; } else if( labelText.equals( H_SPACE20 ) ) { Util.printlnLayout( "TruncFlowLayout:layoutContainer, H_SPACE20" ); x += 20; continue; } else if( labelText.equals( V_SPACE10 ) ) { Util.printlnLayout( "TruncFlowLayout:layoutContainer, V_SPACE10" ); y += 10; continue; } else if( labelText.equals( V_SPACE20 ) ) { Util.printlnLayout( "TruncFlowLayout:layoutContainer, V_SPACE20" ); y += 20; continue; } else if( labelText.equals( END_OF_LINE ) ) { Util.printlnLayout( "TruncFlowLayout:layoutContainer, END_OF_LINE Label = " ); y += maxLineHeight + vGap; maxLineHeight = 0; x = x0; continue; // NEED THESE! } } // end if( child instanceof Label ) Util.printlnLayout( "TruncFlowLayout:layoutContainer, pref = " + pref ); maxLineHeight = ( pref.height > maxLineHeight ) ? pref.height : maxLineHeight; Util.printlnLayout( "TruncFlowLayout:layoutContainer, maxLineHeight = " + maxLineHeight ); child.setBounds( x, y, pref.width, pref.height ); x += pref.width + hGap; } // end for loop. } // end of layoutContainer. public Dimension preferredLayoutSize( Container parent ) { Util.printlnLayout( "TruncFlowLayout:preferredLayoutSize." ); return layoutSize( parent, PREF_SIZE ); } public Dimension minimumLayoutSize( Container parent ) { Util.printlnLayout( "TruncFlowLayout:minimumLayoutSize." ); return layoutSize( parent, MIN_SIZE ); } public Dimension maximumLayoutSize( Container parent ) { Util.printlnLayout( "TruncFlowLayout:maximumLayoutSize." ); return layoutSize( parent, MAX_SIZE ); } // * // * layoutSize() // * Compute maximum width and total height of all visible children // * This is a local method, supporting the above three, // * which are entered after setVisible. // * protected Dimension layoutSize( Container parent, int sizetype ) { Util.printlnLayout( "TruncFlowLayout:layoutSize." ); int maxWidth=0, width=0, maxHeight=0, totalHeight=0; int numbChildren = parent.getComponentCount(); Util.printlnLayout( "TruncFlowLayout:layoutSize. numbChildren = " + numbChildren ); Dimension size = new Dimension( 0,0 ); Insets insets = parent.getInsets(); int numbVisibleChildren = 0; for( int i = 0; i < numbChildren; i++ ) { Component child = parent.getComponent( i ); if( child instanceof Label ) Util.printlnLayout( "TruncFlowLayout:layoutSize, Label = " + ((Label)child).getText() ); else Util.printlnLayout( "TruncFlowLayout:layoutSize, Not a Label, name = " + child.getName() ); if ( !child.isVisible() ) continue; numbVisibleChildren++; Dimension d; if( sizetype == PREF_SIZE ) d = child.getPreferredSize(); else if( sizetype == MIN_SIZE ) d = child.getMinimumSize(); else d = child.getMaximumSize(); Util.printlnLayout( "TruncFlowLayout:layoutSize, child size = " + d ); // * // * Accumulate each component's width. // * Save the max height. // * First test for special labels (ignore them). // * String text; if( child instanceof Label && ( text = ((Label)child).getText() ).length() > 0 && text.charAt( 0 ) == '0' && !text.equals( END_OF_LINE ) && !text.equals( H_SPACE10 ) && !text.equals( H_SPACE20 ) && !text.equals( V_SPACE10 ) && !text.equals( V_SPACE20 ) ) //&& (((Label)child).getText().charAt( 0 ) == '0' ) { Util.printlnLayout( "TruncFlowLayout:layoutSize, got a special label." ); } else { width += d.width; if( d.height > maxHeight ) maxHeight = d.height; Util.printlnLayout( "TruncFlowLayout:layoutSize(1), width = " + width + ", maxHeight = " + maxHeight ); } // * // * At end of line, // * accumulate heights, // * save the max width. // * if( child instanceof Label && ((Label)child).getText().equals( END_OF_LINE ) ) { Util.printlnLayout( "TruncFlowLayout:layoutSize, end of line processing." ); totalHeight += maxHeight; maxHeight = 0; if( width > maxWidth ) { maxWidth = width; width = 0; } } } // end for. size.width = maxWidth; size.height = totalHeight; Util.printlnLayout( "TruncFlowLayout:layoutSize(2), size = " + size ); // Now add in margins and stuff size.width += insets.left + insets.right + 2*margin_width; size.height += insets.top + insets.bottom + 2*margin_height; Util.printlnLayout( "TruncFlowLayout:layoutSize(3), size = " + size ); return size; } // * // * Other LayoutManager(2) methods that are unused by this class // * public void addLayoutComponent(String constraint, Component comp) {} public void addLayoutComponent(Component comp, Object constraint) {} public void removeLayoutComponent(Component comp) {} public void invalidateLayout(Container parent) {} public float getLayoutAlignmentX(Container parent) { return 0.5f; } public float getLayoutAlignmentY(Container parent) { return 0.5f; } } // end class.