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