JAL-1683 replace year/version strings with tokens in source
[jalview.git] / src / jalview / gui / JvSwingUtils.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ 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 jalview.util.MessageManager;
24
25 import java.awt.Color;
26 import java.awt.Font;
27 import java.awt.Rectangle;
28 import java.awt.event.ActionListener;
29
30 import javax.swing.AbstractButton;
31 import javax.swing.JButton;
32 import javax.swing.JComponent;
33 import javax.swing.JLabel;
34 import javax.swing.JMenu;
35 import javax.swing.JMenuItem;
36 import javax.swing.JPanel;
37 import javax.swing.SwingConstants;
38
39 /**
40  * useful functions for building Swing GUIs
41  * 
42  * @author JimP
43  * 
44  */
45 public final class JvSwingUtils
46 {
47   /**
48    * wrap a bare html safe string to around 60 characters per line using a
49    * <table width=350>
50    * <tr>
51    * <td></td> field
52  * @param enclose TODO
53  * @param ttext
54    * 
55    * @return
56    */
57   public static String wrapTooltip(boolean enclose, String ttext)
58   {
59     if (ttext.length() < 60)
60     {
61       return enclose ? "<html>" + ttext + "</html>" : ttext;
62     }
63     else
64     {
65       return (enclose ? "<html>" : "")
66               + "<table width=350 border=0><tr><td>" + ttext
67               + "</td></tr></table>" + ((enclose ? "</html>" : ""));
68     }
69   }
70
71   public static JButton makeButton(String label, String tooltip,
72           ActionListener action)
73   {
74     JButton button = new JButton();
75     button.setText(label);
76     // TODO: get the base font metrics for the Jalview gui from somewhere
77     button.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
78     button.setForeground(Color.black);
79     button.setHorizontalAlignment(SwingConstants.CENTER);
80     button.setToolTipText(tooltip);
81     button.addActionListener(action);
82     return button;
83   }
84
85   /**
86    * find or add a submenu with the given title in the given menu
87    * 
88    * @param menu
89    * @param submenu
90    * @return the new or existing submenu
91    */
92   public static JMenu findOrCreateMenu(JMenu menu, String submenu)
93   {
94     JMenu submenuinstance = null;
95     for (int i = 0, iSize = menu.getMenuComponentCount(); i < iSize; i++)
96     {
97       if (menu.getMenuComponent(i) instanceof JMenu
98               && ((JMenu) menu.getMenuComponent(i)).getText().equals(
99                       submenu))
100       {
101         submenuinstance = (JMenu) menu.getMenuComponent(i);
102       }
103     }
104     if (submenuinstance == null)
105     {
106       submenuinstance = new JMenu(submenu);
107       menu.add(submenuinstance);
108     }
109     return submenuinstance;
110
111   }
112
113   /**
114    * 
115    * @param panel
116    * @param tooltip
117    * @param label
118    * @param valBox
119    * @return the GUI element created that was added to the layout so it's
120    *         attributes can be changed.
121    */
122   public static JPanel addtoLayout(JPanel panel, String tooltip,
123           JComponent label, JComponent valBox)
124   {
125     JPanel laypanel = new JPanel(), labPanel = new JPanel(), valPanel = new JPanel();
126     // laypanel.setSize(panel.getPreferredSize());
127     // laypanel.setLayout(null);
128     labPanel.setBounds(new Rectangle(7, 7, 158, 23));
129     valPanel.setBounds(new Rectangle(172, 7, 270, 23));
130     // labPanel.setLayout(new GridLayout(1,1));
131     // valPanel.setLayout(new GridLayout(1,1));
132     labPanel.add(label);
133     valPanel.add(valBox);
134     laypanel.add(labPanel);
135     laypanel.add(valPanel);
136     valPanel.setToolTipText(tooltip);
137     labPanel.setToolTipText(tooltip);
138     valBox.setToolTipText(tooltip);
139     panel.add(laypanel);
140     panel.validate();
141     return laypanel;
142   }
143
144   public static void mgAddtoLayout(JPanel cpanel, String tooltip,
145           JLabel jLabel, JComponent name)
146   {
147     mgAddtoLayout(cpanel, tooltip, jLabel, name, null);
148   }
149
150   public static void mgAddtoLayout(JPanel cpanel, String tooltip,
151           JLabel jLabel, JComponent name, String params)
152   {
153     cpanel.add(jLabel);
154     if (params == null)
155     {
156       cpanel.add(name);
157     }
158     else
159     {
160       cpanel.add(name, params);
161     }
162     name.setToolTipText(tooltip);
163     jLabel.setToolTipText(tooltip);
164   }
165
166   /**
167    * standard font for labels and check boxes in dialog boxes
168    * 
169    * @return
170    */
171
172   public static Font getLabelFont()
173   {
174     return getLabelFont(false, false);
175   }
176
177   public static Font getLabelFont(boolean bold, boolean italic)
178   {
179     return new java.awt.Font("Verdana", (!bold && !italic) ? Font.PLAIN
180             : (bold ? Font.BOLD : 0) + (italic ? Font.ITALIC : 0), 11);
181   }
182
183   /**
184    * standard font for editable text areas
185    * 
186    * @return
187    */
188   public static Font getTextAreaFont()
189   {
190     return getLabelFont(false, false);
191   }
192
193   /**
194    * clean up a swing menu. Removes any empty submenus without selection
195    * listeners.
196    * 
197    * @param webService
198    */
199   public static void cleanMenu(JMenu webService)
200   {
201     for (int i = 0; i < webService.getItemCount();)
202     {
203       JMenuItem item = webService.getItem(i);
204       if (item instanceof JMenu && ((JMenu) item).getItemCount() == 0)
205       {
206         webService.remove(i);
207       }
208       else
209       {
210         i++;
211       }
212     }
213   }
214
215   public static void jvInitComponent(AbstractButton comp, String i18nString)
216   {
217     setColorAndFont(comp);
218     if (i18nString != null && !i18nString.isEmpty())
219     {
220       comp.setText(MessageManager.getString(i18nString));
221     }
222   }
223
224   public static void jvInitComponent(JComponent comp)
225   {
226     setColorAndFont(comp);
227   }
228
229   private static void setColorAndFont(JComponent comp)
230   {
231     comp.setBackground(Color.white);
232     comp.setFont(JvSwingUtils.getLabelFont());
233   }
234
235 }