Coverage Report - org.dishevelled.layout.ButtonPanel
 
Classes in this File Line Coverage Branch Coverage Complexity
ButtonPanel
100%
19/19
100%
6/6
1.75
 
 1  
 /*
 2  
 
 3  
     dsh-layout  Layout managers for lightweight components.
 4  
     Copyright (c) 2003-2012 held jointly by the individual authors.
 5  
 
 6  
     This library is free software; you can redistribute it and/or modify it
 7  
     under the terms of the GNU Lesser General Public License as published
 8  
     by the Free Software Foundation; either version 3 of the License, or (at
 9  
     your option) any later version.
 10  
 
 11  
     This library is distributed in the hope that it will be useful, but WITHOUT
 12  
     ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
 13  
     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 14  
     License for more details.
 15  
 
 16  
     You should have received a copy of the GNU Lesser General Public License
 17  
     along with this library;  if not, write to the Free Software Foundation,
 18  
     Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
 19  
 
 20  
     > http://www.fsf.org/licensing/licenses/lgpl.html
 21  
     > http://www.opensource.org/licenses/lgpl-license.php
 22  
 
 23  
 */
 24  
 package org.dishevelled.layout;
 25  
 
 26  
 import java.awt.Component;
 27  
 
 28  
 import javax.swing.Action;
 29  
 import javax.swing.Box;
 30  
 import javax.swing.JButton;
 31  
 import javax.swing.JPanel;
 32  
 
 33  
 /**
 34  
  * Button panel.
 35  
  *
 36  
  * @author  Michael Heuer
 37  
  * @version $Revision$ $Date$
 38  
  */
 39  
 public final class ButtonPanel
 40  
     extends JPanel
 41  
 {
 42  
     /** Default button spacing, <code>10</code>. */
 43  
     private static final int DEFAULT_BUTTON_SPACING = 10;
 44  
 
 45  
 
 46  
     /**
 47  
      * Create a new button panel.
 48  
      */
 49  
     public ButtonPanel()
 50  
     {
 51  4
         super();
 52  4
         setLayout(new ButtonLayout(this));
 53  4
     }
 54  
 
 55  
 
 56  
     /**
 57  
      * Add spacing.
 58  
      */
 59  
     private void addSpacing()
 60  
     {
 61  6
         int buttonCount = 0;
 62  
         //for (Component component : getComponents())
 63  22
         for (int i = 0, size = getComponentCount(); i < size; i++)
 64  
         {
 65  16
             Component component = getComponent(i);
 66  16
             if (component instanceof JButton)
 67  
             {
 68  6
                 buttonCount++;
 69  
             }
 70  
         }
 71  6
         if (buttonCount == 0)
 72  
         {
 73  2
             super.add(Box.createHorizontalGlue());
 74  2
             super.add(Box.createHorizontalGlue());
 75  
         }
 76  
         else
 77  
         {
 78  4
             int buttonSpacing = DEFAULT_BUTTON_SPACING; // todo:  look up in L&F docs
 79  4
             super.add(Box.createHorizontalStrut(buttonSpacing));
 80  
         }
 81  6
     }
 82  
 
 83  
     /**
 84  
      * Add the specified button to this button panel.
 85  
      *
 86  
      * @param button button to add
 87  
      * @return a reference to the added button
 88  
      */
 89  
     public JButton add(final JButton button)
 90  
     {
 91  3
         addSpacing();
 92  3
         return (JButton) super.add(button);
 93  
     }
 94  
 
 95  
     /**
 96  
      * Create a new JButton for the specified action and add it to this button panel.
 97  
      *
 98  
      * @param action action
 99  
      * @return a new JButton for the specified action
 100  
      */
 101  
     public JButton add(final Action action)
 102  
     {
 103  3
         addSpacing();
 104  3
         JButton button = new JButton(action);
 105  3
         return (JButton) super.add(button);
 106  
     }
 107  
 
 108  
     // other considerations:
 109  
     // minimum button width
 110  
     // set the default button
 111  
     // adjust left-to-right orientation of default button based on L&F
 112  
 }