c420dc0682cb596bd1669dd92393e7146467f423
[jalview.git] / src / jalview / gui / JvSwingUtils.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3  * Copyright (C) 2014 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import java.awt.Color;
24 import java.awt.Font;
25 import java.awt.Rectangle;
26 import java.awt.event.ActionListener;
27
28 import javax.swing.JButton;
29 import javax.swing.JComponent;
30 import javax.swing.JLabel;
31 import javax.swing.JMenu;
32 import javax.swing.JMenuItem;
33 import javax.swing.JPanel;
34 import javax.swing.SwingConstants;
35
36 /**
37  * useful functions for building Swing GUIs
38  * 
39  * @author JimP
40  * 
41  */
42 public final class JvSwingUtils
43 {
44   /**
45    * wrap a bare html safe string to around 60 characters per line using a
46    * <table width=350>
47    * <tr>
48    * <td></td> field
49    * 
50    * @param ttext
51    * @return
52    */
53   public static String wrapTooltip(String ttext)
54   {
55     if (ttext.length() < 60)
56     {
57       return ttext;
58     }
59     else
60     {
61       return "<table width=350 border=0><tr><td>" + ttext
62               + "</td></tr></table>";
63     }
64   }  
65   public static JButton makeButton(String label, String tooltip,
66           ActionListener action)
67   {
68     JButton button = new JButton();
69     button.setText(label);
70     // TODO: get the base font metrics for the Jalview gui from somewhere
71     button.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
72     button.setForeground(Color.black);
73     button.setHorizontalAlignment(SwingConstants.CENTER);
74     button.setToolTipText(tooltip);
75     button.addActionListener(action);
76     return button;
77   }
78
79   /**
80    * find or add a submenu with the given title in the given menu
81    * 
82    * @param menu
83    * @param submenu
84    * @return the new or existing submenu
85    */
86   public static JMenu findOrCreateMenu(JMenu menu, String submenu)
87   {
88     JMenu submenuinstance = null;
89     for (int i = 0, iSize = menu.getMenuComponentCount(); i < iSize; i++)
90     {
91       if (menu.getMenuComponent(i) instanceof JMenu
92               && ((JMenu) menu.getMenuComponent(i)).getText().equals(
93                       submenu))
94       {
95         submenuinstance = (JMenu) menu.getMenuComponent(i);
96       }
97     }
98     if (submenuinstance == null)
99     {
100       submenuinstance = new JMenu(submenu);
101       menu.add(submenuinstance);
102     }
103     return submenuinstance;
104
105   }
106
107   /**
108    * 
109    * @param panel
110    * @param tooltip
111    * @param label
112    * @param valBox
113    * @return the GUI element created that was added to the layout so it's
114    *         attributes can be changed.
115    */
116   public static JPanel addtoLayout(JPanel panel, String tooltip,
117           JComponent label, JComponent valBox)
118   {
119     JPanel laypanel = new JPanel(), labPanel = new JPanel(), valPanel = new JPanel();
120     // laypanel.setSize(panel.getPreferredSize());
121     // laypanel.setLayout(null);
122     labPanel.setBounds(new Rectangle(7, 7, 158, 23));
123     valPanel.setBounds(new Rectangle(172, 7, 270, 23));
124     // labPanel.setLayout(new GridLayout(1,1));
125     // valPanel.setLayout(new GridLayout(1,1));
126     labPanel.add(label);
127     valPanel.add(valBox);
128     laypanel.add(labPanel);
129     laypanel.add(valPanel);
130     valPanel.setToolTipText(tooltip);
131     labPanel.setToolTipText(tooltip);
132     valBox.setToolTipText(tooltip);
133     panel.add(laypanel);
134     panel.validate();
135     return laypanel;
136   }
137
138   public static void mgAddtoLayout(JPanel cpanel, String tooltip,
139           JLabel jLabel, JComponent name)
140   {
141     mgAddtoLayout(cpanel, tooltip, jLabel, name, null);
142   }
143
144   public static void mgAddtoLayout(JPanel cpanel, String tooltip,
145           JLabel jLabel, JComponent name, String params)
146   {
147     cpanel.add(jLabel);
148     if (params == null)
149     {
150       cpanel.add(name);
151     }
152     else
153     {
154       cpanel.add(name, params);
155     }
156     name.setToolTipText(tooltip);
157     jLabel.setToolTipText(tooltip);
158   }
159
160   /**
161    * standard font for labels and check boxes in dialog boxes
162    * 
163    * @return
164    */
165
166   public static Font getLabelFont()
167   {
168     return getLabelFont(false, false);
169   }
170
171   public static Font getLabelFont(boolean bold, boolean italic)
172   {
173     return new java.awt.Font("Verdana", (!bold && !italic) ? Font.PLAIN
174             : (bold ? Font.BOLD : 0) + (italic ? Font.ITALIC : 0), 11);
175   }
176
177   /**
178    * standard font for editable text areas
179    * 
180    * @return
181    */
182   public static Font getTextAreaFont()
183   {
184     return getLabelFont(false, false);
185   }
186
187   /**
188    * clean up a swing menu. Removes any empty submenus without selection
189    * listeners.
190    * 
191    * @param webService
192    */
193   public static void cleanMenu(JMenu webService)
194   {
195     for (int i = 0; i < webService.getItemCount();)
196     {
197       JMenuItem item = webService.getItem(i);
198       if (item instanceof JMenu && ((JMenu) item).getItemCount() == 0)
199       {
200         webService.remove(i);
201       }
202       else
203       {
204         i++;
205       }
206     }
207   }
208
209 }