View Javadoc

1   /*
2    * $Id: IconButtonHelper.java,v 1.1 2004/04/15 16:37:06 mch Exp $
3    *
4    * (C) Copyright Astrogrid...
5    */
6   
7   package org.astrogrid.ui;
8   
9   
10  /***
11   * Some convenience routines for making and arranging icon buttons
12   *
13   * @author M Hill
14   */
15  
16  import java.awt.Dimension;
17  import javax.swing.BorderFactory;
18  import javax.swing.Icon;
19  import javax.swing.JButton;
20  import org.astrogrid.ui.IconFactory;
21  
22  public class IconButtonHelper
23  {
24     /*** Helper conveience method for making the little icon buttons on top right */
25     public  static JButton makeIconButton(String title)
26     {
27        return makeIconButton(title, title, null);
28     }
29  
30     /*** Helper conveience method for making the little icon buttons on top right */
31     public  static JButton makeIconButton(String title, String iconname, String tooltip)
32     {
33        JButton b = null;
34        Icon i = IconFactory.getIcon(iconname, IconFactory.MEDIUM);
35        if (i == null)
36        {
37           b = new JButton(title);
38        }
39        else
40        {
41           b = new JButton(i);
42           b.setBorder(BorderFactory.createEmptyBorder(1,1,1,1));
43           int size = Math.max(i.getIconWidth(), i.getIconHeight());
44           b.setPreferredSize(new Dimension(size+2, size+2));
45        }
46        
47        b.setToolTipText(tooltip);
48        return b;
49     }
50  
51     /*** Runs through making sure they are all the (height) of the largest, and
52      * non-text ones are made the same width
53      * */
54     public static void sizeButtons(JButton[] buttons)
55     {
56        //find largest size
57        //NB often at creation time the sizes are not set
58        int size = 0;
59        for (int b=0;b<buttons.length;b++)
60        {
61           if (buttons[b].getIcon() != null) {
62              size = Math.max(size, buttons[b].getIcon().getIconHeight());
63           }
64           if (buttons[b].getText() == null) {
65              if (size<buttons[b].getWidth())  size = buttons[b].getWidth();
66           }
67        }
68  
69        for (int b=0;b<buttons.length;b++)
70        {
71           if (buttons[b].getText() == null) { //icon button
72              buttons[b].setPreferredSize(new Dimension(size+2, size+2));
73           } else { //text button - just set height
74              buttons[b].setSize(buttons[b].getWidth(), size+2);
75           }
76        }
77     }
78     
79  }
80  
81  /*
82  $Log: IconButtonHelper.java,v $
83  Revision 1.1  2004/04/15 16:37:06  mch
84  More tidying
85  
86  Revision 1.1  2004/03/03 17:40:58  mch
87  Moved ui package
88  
89  Revision 1.1  2004/02/17 16:04:06  mch
90  New Desktop GUI
91  
92  Revision 1.1  2004/02/17 03:53:43  mch
93  Nughtily large number of fixes for demo
94  
95   */
96