1 /*
   2  * $Id$
   3  *
   4  * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.  Oracle designates this
  10  * particular file as subject to the "Classpath" exception as provided
  11  * by Oracle in the LICENSE file that accompanied this code.
  12  *
  13  * This code is distributed in the hope that it will be useful, but WITHOUT
  14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16  * version 2 for more details (a copy is included in the LICENSE file that
  17  * accompanied this code).
  18  *
  19  * You should have received a copy of the GNU General Public License version
  20  * 2 along with this work; if not, write to the Free Software Foundation,
  21  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  22  *
  23  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  24  * or visit www.oracle.com if you need additional information or have any
  25  * questions.
  26  */
  27 package com.sun.javatest.exec;
  28 
  29 import java.awt.Color;
  30 import java.awt.Component;
  31 import java.awt.Container;
  32 import java.awt.Dimension;
  33 import java.awt.FlowLayout;
  34 import java.awt.Graphics;
  35 import java.awt.Insets;
  36 import java.util.ArrayList;
  37 import javax.swing.JComponent;
  38 import javax.swing.JPanel;
  39 import javax.swing.border.BevelBorder;
  40 
  41 /**
  42  * The Panel which holds standard and custom toolbars
  43  */
  44 class ToolBarPanel extends JPanel {
  45 
  46     /**
  47      * Creates a new panel.
  48      */
  49     ToolBarPanel() {
  50         setLayout(theLayout);
  51         setName("toolbarPanel");
  52     }
  53 
  54     /**
  55      * Paints the panel.
  56      * Invoked by Swing to draw components.
  57      * @param g - the Graphics context in which to paint
  58      **/
  59     public void paint(java.awt.Graphics g) {
  60         int indent = 0;
  61         super.paint(g);
  62         int[] separators = theLayout.getRowPositions();
  63         Color oldColor = g.getColor();
  64         for (int i = 0; i < separators.length; i++) {
  65             g.setColor(b.getShadowInnerColor(this));
  66             // x1, y1, x2, y2
  67             g.drawLine(indent, separators[i], getWidth()-indent, separators[i]);
  68             g.setColor(b.getHighlightInnerColor(this));
  69             g.drawLine(indent, separators[i]+1, getWidth()-indent, separators[i]+1);
  70         }
  71 
  72         g.setColor(oldColor);
  73     }
  74 
  75     /**
  76      *ToolBarLayout lays out the panel and calculates positions for
  77      *horisontal separators
  78      **/
  79     class ToolBarLayout extends FlowLayout {
  80 
  81         public ToolBarLayout() {
  82             super(FlowLayout.LEFT, 0, 0);
  83             setHgap(10);
  84         }
  85 
  86         public Dimension preferredLayoutSize(Container target) {
  87             synchronized (target.getTreeLock()) {
  88                 int realW = target.getSize().width;
  89                 int commponentsPerRow = 0;
  90                 ArrayList<Integer> hs = new ArrayList<>();
  91                 if (realW == 0) {
  92                     return super.preferredLayoutSize(target);
  93                 }
  94                 Dimension dim = new Dimension(0, 0);
  95                 int nmembers = target.getComponentCount();
  96                 boolean firstVisibleComponent = true;
  97                 Insets insets = target.getInsets();
  98                 int maxWidth = 0;
  99                 int maxHeight = 0;
 100                 int row = 1;
 101                 for (int i = 0 ; i < nmembers ; i++) {
 102                     Component m = target.getComponent(i);
 103                     Dimension d = m.getPreferredSize();
 104                     if (m.isVisible()) {
 105                         dim.height = Math.max(dim.height, d.height);
 106                         commponentsPerRow++;
 107                         if (firstVisibleComponent) {
 108                             firstVisibleComponent = false;
 109                         } else {
 110                             dim.width += getHgap();
 111                         }
 112                         // width of a current row can be
 113                         int newFullWidth = dim.width + d.width + insets.left + insets.right + getHgap()*2;
 114                         if (newFullWidth < realW || commponentsPerRow == 1) {
 115                             // add to the row
 116                             dim.width += d.width;
 117                             maxHeight = Math.max(maxHeight, d.height);
 118                         } else {
 119                             // start new row
 120                             row++;
 121                             hs.add(new Integer(dim.height));
 122                             dim.height += maxHeight + getVgap();
 123                             maxHeight = 0;
 124                             // this is bug
 125                             // it calculates height of a new row by height of first component
 126                             maxWidth = Math.max(maxWidth, dim.width);
 127                             maxHeight = Math.max(maxHeight, d.height);
 128                             dim.width = d.width;
 129                             firstVisibleComponent = true;
 130                             commponentsPerRow = 1;
 131                         }
 132                         if (m instanceof JComponent) {
 133                             ((JComponent)m).putClientProperty(PB_PROP_NAME, new Boolean(!firstVisibleComponent));
 134                         }
 135                     }
 136                 }
 137                 dim.width = Math.max(maxWidth, dim.width);
 138                 dim.width += insets.left + insets.right + getHgap()*2;
 139                 dim.height += insets.top + insets.bottom + getVgap() *2;
 140                 // fill in separators between rows
 141                 separators = new int[hs.size()];
 142                 for (int i = 0; i < hs.size(); i++) {
 143                     separators[i] = hs.get(i).intValue();
 144                 }
 145                 return dim;
 146             }
 147         }
 148 
 149         synchronized int[] getRowPositions() {
 150             return separators;
 151         }
 152         private int[] separators = new int[0];
 153     }
 154 
 155     private ToolBarLayout theLayout = new ToolBarLayout();
 156     private BevelBorder b = new BevelBorder(BevelBorder.RAISED);
 157     public static final String PB_PROP_NAME = "PAINT_BORDER";
 158 
 159 }
 160 
 161 
 162