JAL-1627 bug fix to remove wierd space in Sequence Features/Sequence Group tooltip...
[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.BorderLayout;
26 import java.awt.Color;
27 import java.awt.Font;
28 import java.awt.GridLayout;
29 import java.awt.Rectangle;
30 import java.awt.event.ActionListener;
31
32 import javax.swing.AbstractButton;
33 import javax.swing.JButton;
34 import javax.swing.JComponent;
35 import javax.swing.JLabel;
36 import javax.swing.JMenu;
37 import javax.swing.JMenuItem;
38 import javax.swing.JPanel;
39 import javax.swing.JScrollBar;
40 import javax.swing.SwingConstants;
41
42 /**
43  * useful functions for building Swing GUIs
44  * 
45  * @author JimP
46  * 
47  */
48 public final class JvSwingUtils
49 {
50   /**
51    * wrap a bare html safe string to around 60 characters per line using a
52    * <table width=350>
53    * <tr>
54    * <td></td> field
55  * @param enclose TODO
56  * @param ttext
57    * 
58    * @return
59    */
60   public static String wrapTooltip(boolean enclose, String ttext)
61   {
62     ttext = ttext.trim();
63     if (ttext.length() < 60)
64     {
65       return enclose ? "<html>" + ttext + "</html>" : ttext;
66     }
67     else
68     {
69       return (enclose ? "<html>" : "")
70               + "<table width=350 border=0><tr><td>" + ttext
71               + "</td></tr></table>" + ((enclose ? "</html>" : ""));
72     }
73   }
74
75   public static JButton makeButton(String label, String tooltip,
76           ActionListener action)
77   {
78     JButton button = new JButton();
79     button.setText(label);
80     // TODO: get the base font metrics for the Jalview gui from somewhere
81     button.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
82     button.setForeground(Color.black);
83     button.setHorizontalAlignment(SwingConstants.CENTER);
84     button.setToolTipText(tooltip);
85     button.addActionListener(action);
86     return button;
87   }
88
89   /**
90    * find or add a submenu with the given title in the given menu
91    * 
92    * @param menu
93    * @param submenu
94    * @return the new or existing submenu
95    */
96   public static JMenu findOrCreateMenu(JMenu menu, String submenu)
97   {
98     JMenu submenuinstance = null;
99     for (int i = 0, iSize = menu.getMenuComponentCount(); i < iSize; i++)
100     {
101       if (menu.getMenuComponent(i) instanceof JMenu
102               && ((JMenu) menu.getMenuComponent(i)).getText().equals(
103                       submenu))
104       {
105         submenuinstance = (JMenu) menu.getMenuComponent(i);
106       }
107     }
108     if (submenuinstance == null)
109     {
110       submenuinstance = new JMenu(submenu);
111       menu.add(submenuinstance);
112     }
113     return submenuinstance;
114
115   }
116
117   /**
118    * 
119    * @param panel
120    * @param tooltip
121    * @param label
122    * @param valBox
123    * @return the GUI element created that was added to the layout so it's
124    *         attributes can be changed.
125    */
126   public static JPanel addtoLayout(JPanel panel, String tooltip,
127           JComponent label, JComponent valBox)
128   {
129     JPanel laypanel = new JPanel(new GridLayout(1, 2));
130     JPanel labPanel = new JPanel(new BorderLayout());
131     JPanel valPanel = new JPanel();
132     // laypanel.setSize(panel.getPreferredSize());
133     // laypanel.setLayout(null);
134     labPanel.setBounds(new Rectangle(7, 7, 158, 23));
135     valPanel.setBounds(new Rectangle(172, 7, 270, 23));
136     // labPanel.setLayout(new GridLayout(1,1));
137     // valPanel.setLayout(new GridLayout(1,1));
138     labPanel.add(label, BorderLayout.WEST);
139     valPanel.add(valBox);
140     laypanel.add(labPanel);
141     laypanel.add(valPanel);
142     valPanel.setToolTipText(tooltip);
143     labPanel.setToolTipText(tooltip);
144     valBox.setToolTipText(tooltip);
145     panel.add(laypanel);
146     panel.validate();
147     return laypanel;
148   }
149
150   public static void mgAddtoLayout(JPanel cpanel, String tooltip,
151           JLabel jLabel, JComponent name)
152   {
153     mgAddtoLayout(cpanel, tooltip, jLabel, name, null);
154   }
155
156   public static void mgAddtoLayout(JPanel cpanel, String tooltip,
157           JLabel jLabel, JComponent name, String params)
158   {
159     cpanel.add(jLabel);
160     if (params == null)
161     {
162       cpanel.add(name);
163     }
164     else
165     {
166       cpanel.add(name, params);
167     }
168     name.setToolTipText(tooltip);
169     jLabel.setToolTipText(tooltip);
170   }
171
172   /**
173    * standard font for labels and check boxes in dialog boxes
174    * 
175    * @return
176    */
177
178   public static Font getLabelFont()
179   {
180     return getLabelFont(false, false);
181   }
182
183   public static Font getLabelFont(boolean bold, boolean italic)
184   {
185     return new java.awt.Font("Verdana", (!bold && !italic) ? Font.PLAIN
186             : (bold ? Font.BOLD : 0) + (italic ? Font.ITALIC : 0), 11);
187   }
188
189   /**
190    * standard font for editable text areas
191    * 
192    * @return
193    */
194   public static Font getTextAreaFont()
195   {
196     return getLabelFont(false, false);
197   }
198
199   /**
200    * clean up a swing menu. Removes any empty submenus without selection
201    * listeners.
202    * 
203    * @param webService
204    */
205   public static void cleanMenu(JMenu webService)
206   {
207     for (int i = 0; i < webService.getItemCount();)
208     {
209       JMenuItem item = webService.getItem(i);
210       if (item instanceof JMenu && ((JMenu) item).getItemCount() == 0)
211       {
212         webService.remove(i);
213       }
214       else
215       {
216         i++;
217       }
218     }
219   }
220
221   /**
222    * Returns the proportion of its range that a scrollbar's position represents,
223    * as a value between 0 and 1. For example if the whole range is from 0 to
224    * 200, then a position of 40 gives proportion = 0.2.
225    * 
226    * @see http://www.javalobby.org/java/forums/t33050.html#91885334
227    * 
228    * @param scroll
229    * @return
230    */
231   public static float getScrollBarProportion(JScrollBar scroll)
232   {
233     /*
234      * The extent (scroll handle width) deduction gives the true operating range
235      * of possible positions.
236      */
237     int possibleRange = scroll.getMaximum() - scroll.getMinimum()
238             - scroll.getModel().getExtent();
239     float valueInRange = scroll.getValue()
240             - (scroll.getModel().getExtent() / 2f);
241     float proportion = valueInRange / possibleRange;
242     return proportion;
243   }
244
245   /**
246    * Returns the scroll bar position in its range that would match the given
247    * proportion (between 0 and 1) of the whole. For example if the whole range
248    * is from 0 to 200, then a proportion of 0.25 gives position 50.
249    * 
250    * @param scrollbar
251    * @param proportion
252    * @return
253    */
254   public static int getScrollValueForProportion(JScrollBar scrollbar,
255           float proportion)
256   {
257     /*
258      * The extent (scroll handle width) deduction gives the true operating range
259      * of possible positions.
260      */
261     float fraction = proportion
262             * (scrollbar.getMaximum() - scrollbar.getMinimum() - scrollbar
263                     .getModel().getExtent())
264             + (scrollbar.getModel().getExtent() / 2f);
265     return Math.min(Math.round(fraction), scrollbar.getMaximum());
266   }
267
268   public static void jvInitComponent(AbstractButton comp, String i18nString)
269   {
270     setColorAndFont(comp);
271     if (i18nString != null && !i18nString.isEmpty())
272     {
273       comp.setText(MessageManager.getString(i18nString));
274     }
275   }
276
277   public static void jvInitComponent(JComponent comp)
278   {
279     setColorAndFont(comp);
280   }
281
282   private static void setColorAndFont(JComponent comp)
283   {
284     comp.setBackground(Color.white);
285     comp.setFont(JvSwingUtils.getLabelFont());
286   }
287
288 }