1 /*
   2  * Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.swing.colorchooser;
  27 
  28 import javax.swing.*;
  29 import java.awt.*;
  30 import java.awt.event.*;
  31 import javax.swing.event.*;
  32 import javax.swing.text.*;
  33 import java.io.Serializable;
  34 
  35 
  36 /**
  37   * A better GridLayout class
  38   *
  39   * @author Steve Wilson
  40   */
  41 class SmartGridLayout implements LayoutManager, Serializable {
  42 
  43   int rows = 2;
  44   int columns = 2;
  45   int xGap = 2;
  46   int yGap = 2;
  47   int componentCount = 0;
  48   Component[][] layoutGrid;
  49 
  50 
  51   public SmartGridLayout(int numColumns, int numRows) {
  52     rows = numRows;
  53     columns = numColumns;
  54     layoutGrid = new Component[numColumns][numRows];
  55 
  56   }
  57 
  58 
  59   public void layoutContainer(Container c) {
  60 
  61     buildLayoutGrid(c);
  62 
  63     int[] rowHeights = new int[rows];
  64     int[] columnWidths = new int[columns];
  65 
  66     for (int row = 0; row < rows; row++) {
  67         rowHeights[row] = computeRowHeight(row);
  68     }
  69 
  70     for (int column = 0; column < columns; column++) {
  71         columnWidths[column] = computeColumnWidth(column);
  72     }
  73 
  74 
  75     Insets insets = c.getInsets();
  76 
  77     if (c.getComponentOrientation().isLeftToRight()) {
  78         int horizLoc = insets.left;
  79         for (int column = 0; column < columns; column++) {
  80           int vertLoc = insets.top;
  81 
  82           for (int row = 0; row < rows; row++) {
  83             Component current = layoutGrid[column][row];
  84 
  85             current.setBounds(horizLoc, vertLoc, columnWidths[column], rowHeights[row]);
  86             //  System.out.println(current.getBounds());
  87             vertLoc += (rowHeights[row] + yGap);
  88           }
  89           horizLoc += (columnWidths[column] + xGap );
  90         }
  91     } else {
  92         int horizLoc = c.getWidth() - insets.right;
  93         for (int column = 0; column < columns; column++) {
  94           int vertLoc = insets.top;
  95           horizLoc -= columnWidths[column];
  96 
  97           for (int row = 0; row < rows; row++) {
  98             Component current = layoutGrid[column][row];
  99 
 100             current.setBounds(horizLoc, vertLoc, columnWidths[column], rowHeights[row]);
 101             //  System.out.println(current.getBounds());
 102             vertLoc += (rowHeights[row] + yGap);
 103           }
 104           horizLoc -= xGap;
 105         }
 106     }
 107 
 108 
 109 
 110   }
 111 
 112   public Dimension minimumLayoutSize(Container c) {
 113 
 114     buildLayoutGrid(c);
 115     Insets insets = c.getInsets();
 116 
 117 
 118 
 119     int height = 0;
 120     int width = 0;
 121 
 122     for (int row = 0; row < rows; row++) {
 123         height += computeRowHeight(row);
 124     }
 125 
 126     for (int column = 0; column < columns; column++) {
 127         width += computeColumnWidth(column);
 128     }
 129 
 130     height += (yGap * (rows - 1)) + insets.top + insets.bottom;
 131     width += (xGap * (columns - 1)) + insets.right + insets.left;
 132 
 133     return new Dimension(width, height);
 134 
 135 
 136   }
 137 
 138   public Dimension preferredLayoutSize(Container c) {
 139       return minimumLayoutSize(c);
 140   }
 141 
 142 
 143   public void addLayoutComponent(String s, Component c) {}
 144 
 145   public void removeLayoutComponent(Component c) {}
 146 
 147 
 148   private void buildLayoutGrid(Container c) {
 149 
 150       Component[] children = c.getComponents();
 151 
 152       for (int componentCount = 0; componentCount < children.length; componentCount++) {
 153         //      System.out.println("Children: " +componentCount);
 154         int row = 0;
 155         int column = 0;
 156 
 157         if (componentCount != 0) {
 158           column = componentCount % columns;
 159           row = (componentCount - column) / columns;
 160         }
 161 
 162         //      System.out.println("inserting into: "+ column +  " " + row);
 163 
 164         layoutGrid[column][row] = children[componentCount];
 165       }
 166   }
 167 
 168   private int computeColumnWidth(int columnNum) {
 169     int maxWidth = 1;
 170     for (int row = 0; row < rows; row++) {
 171       int width = layoutGrid[columnNum][row].getPreferredSize().width;
 172       if (width > maxWidth) {
 173         maxWidth = width;
 174       }
 175     }
 176     return maxWidth;
 177   }
 178 
 179   private int computeRowHeight(int rowNum) {
 180     int maxHeight = 1;
 181     for (int column = 0; column < columns; column++) {
 182       int height = layoutGrid[column][rowNum].getPreferredSize().height;
 183       if (height > maxHeight) {
 184         maxHeight = height;
 185       }
 186     }
 187     return maxHeight;
 188   }
 189 
 190 }