View Javadoc

1   /*
2   
3       dsh-layout  Layout managers for lightweight components.
4       Copyright (c) 2003-2011 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  
43      /**
44       * Create a new button panel.
45       */
46      public ButtonPanel()
47      {
48          super();
49          setLayout(new ButtonLayout(this));
50      }
51  
52  
53      /**
54       * Add spacing.
55       */
56      private void addSpacing()
57      {
58          int buttonCount = 0;
59          //for (Component component : getComponents())
60          for (int i = 0, size = getComponentCount(); i < size; i++)
61          {
62              Component component = getComponent(i);
63              if (component instanceof JButton)
64              {
65                  buttonCount++;
66              }
67          }
68          if (buttonCount == 0)
69          {
70              super.add(Box.createHorizontalGlue());
71              super.add(Box.createHorizontalGlue());
72          }
73          else
74          {
75              int buttonSpacing = 10; // todo:  look up in L&F docs
76              super.add(Box.createHorizontalStrut(buttonSpacing));
77          }
78      }
79  
80      /**
81       * Add the specified button to this button panel.
82       *
83       * @param button button to add
84       */
85      public JButton add(final JButton button)
86      {
87          addSpacing();
88          return (JButton) super.add(button);
89      }
90  
91      /**
92       * Create a new JButton for the specified action and add it to this button panel.
93       *
94       * @param action action
95       * @return a new JButton for the specified action
96       */
97      public JButton add(final Action action)
98      {
99          addSpacing();
100         JButton button = new JButton(action);
101         return (JButton) super.add(button);
102     }
103 
104     // other considerations:
105     // minimum button width
106     // set the default button
107     // adjust left-to-right orientation of default button based on L&F
108 }