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