Jal-3357 ISSUE JAL-3253-applet tooltip for PHOSPHORYLATION missing
[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.Component;
28 import java.awt.Font;
29 import java.awt.GridLayout;
30 import java.awt.Rectangle;
31 import java.awt.event.ActionListener;
32 import java.awt.event.MouseAdapter;
33 import java.awt.event.MouseEvent;
34 import java.util.List;
35 import java.util.Objects;
36
37 import javax.swing.AbstractButton;
38 import javax.swing.BorderFactory;
39 import javax.swing.JButton;
40 import javax.swing.JComboBox;
41 import javax.swing.JComponent;
42 import javax.swing.JLabel;
43 import javax.swing.JMenu;
44 import javax.swing.JMenuItem;
45 import javax.swing.JPanel;
46 import javax.swing.JScrollBar;
47 import javax.swing.SwingConstants;
48 import javax.swing.border.Border;
49 import javax.swing.border.TitledBorder;
50
51 /**
52  * useful functions for building Swing GUIs
53  * 
54  * @author JimP
55  * 
56  */
57 public final class JvSwingUtils
58 {
59   /**
60    * wrap a bare html safe string to around 60 characters per line using a CSS
61    * style class specifying word-wrap and break-word
62    * 
63    * @param enclose
64    *          if true, add &lt;html&gt; wrapper tags (currently false for only
65    *          two references -- both in Jws2Discoverer --
66    * @param ttext
67    * 
68    * @return
69    */
70   public static String wrapTooltip(boolean enclose, String ttext)
71   {
72     Objects.requireNonNull(ttext,
73             "Tootip text to format must not be null!");
74     ttext = ttext.trim();
75     boolean maxLengthExceeded = false;
76
77     if (ttext.contains("<br>"))
78     {
79       String[] htmllines = ttext.split("<br>");
80       for (String line : htmllines)
81       {
82         maxLengthExceeded = line.length() > 60;
83         if (maxLengthExceeded)
84         {
85           break;
86         }
87       }
88     }
89     else
90     {
91       maxLengthExceeded = ttext.length() > 60;
92     }
93
94     if (!maxLengthExceeded)
95     {
96       return enclose ? "<html>" + ttext + "</html>" : ttext;
97     }
98
99     return (enclose ? "<html><div style='width:350px'>"
100             
101             // BH 2018,2019
102             
103             + "<style> div.ttip {width:350px;white-space:pre-wrap;padding:2px;overflow-wrap:break-word;}</style><div class=\"ttip\">"
104             + ttext
105             + " </div></div>"
106             + "</html>" : ttext);
107
108   }
109
110   public static JButton makeButton(String label, String tooltip,
111           ActionListener action)
112   {
113     JButton button = new JButton();
114     button.setText(label);
115     // TODO: get the base font metrics for the Jalview gui from somewhere
116     button.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
117     button.setForeground(Color.black);
118     button.setHorizontalAlignment(SwingConstants.CENTER);
119     button.setToolTipText(tooltip);
120     button.addActionListener(action);
121     return button;
122   }
123
124   /**
125    * find or add a submenu with the given title in the given menu
126    * 
127    * @param menu
128    * @param submenu
129    * @return the new or existing submenu
130    */
131   public static JMenu findOrCreateMenu(JMenu menu, String submenu)
132   {
133     JMenu submenuinstance = null;
134     for (int i = 0, iSize = menu.getMenuComponentCount(); i < iSize; i++)
135     {
136       if (menu.getMenuComponent(i) instanceof JMenu
137               && ((JMenu) menu.getMenuComponent(i)).getText()
138                       .equals(submenu))
139       {
140         submenuinstance = (JMenu) menu.getMenuComponent(i);
141       }
142     }
143     if (submenuinstance == null)
144     {
145       submenuinstance = new JMenu(submenu);
146       menu.add(submenuinstance);
147     }
148     return submenuinstance;
149
150   }
151
152   /**
153    * 
154    * @param panel
155    * @param tooltip
156    * @param label
157    * @param valBox
158    * @return the GUI element created that was added to the layout so it's
159    *         attributes can be changed.
160    */
161   public static JPanel addtoLayout(JPanel panel, String tooltip,
162           JComponent label, JComponent valBox)
163   {
164     JPanel laypanel = new JPanel(new GridLayout(1, 2));
165     JPanel labPanel = new JPanel(new BorderLayout());
166     JPanel valPanel = new JPanel();
167     labPanel.setBounds(new Rectangle(7, 7, 158, 23));
168     valPanel.setBounds(new Rectangle(172, 7, 270, 23));
169     labPanel.add(label, BorderLayout.WEST);
170     valPanel.add(valBox);
171     laypanel.add(labPanel);
172     laypanel.add(valPanel);
173     valPanel.setToolTipText(tooltip);
174     labPanel.setToolTipText(tooltip);
175     valBox.setToolTipText(tooltip);
176     panel.add(laypanel);
177     panel.validate();
178     return laypanel;
179   }
180
181   public static void mgAddtoLayout(JPanel cpanel, String tooltip,
182           JLabel jLabel, JComponent name)
183   {
184     mgAddtoLayout(cpanel, tooltip, jLabel, name, null);
185   }
186
187   public static void mgAddtoLayout(JPanel cpanel, String tooltip,
188           JLabel jLabel, JComponent name, String params)
189   {
190     cpanel.add(jLabel);
191     if (params == null)
192     {
193       cpanel.add(name);
194     }
195     else
196     {
197       cpanel.add(name, params);
198     }
199     name.setToolTipText(tooltip);
200     jLabel.setToolTipText(tooltip);
201   }
202
203   /**
204    * standard font for labels and check boxes in dialog boxes
205    * 
206    * @return
207    */
208
209   public static Font getLabelFont()
210   {
211     return getLabelFont(false, false);
212   }
213
214   public static Font getLabelFont(boolean bold, boolean italic)
215   {
216     return new java.awt.Font("Verdana",
217             (!bold && !italic) ? Font.PLAIN
218                     : (bold ? Font.BOLD : 0) + (italic ? Font.ITALIC : 0),
219             11);
220   }
221
222   /**
223    * standard font for editable text areas
224    * 
225    * @return
226    */
227   public static Font getTextAreaFont()
228   {
229     return getLabelFont(false, false);
230   }
231
232   /**
233    * clean up a swing menu. Removes any empty submenus without selection
234    * listeners.
235    * 
236    * @param webService
237    */
238   public static void cleanMenu(JMenu webService)
239   {
240     for (int i = 0; i < webService.getItemCount();)
241     {
242       JMenuItem item = webService.getItem(i);
243       if (item instanceof JMenu && ((JMenu) item).getItemCount() == 0)
244       {
245         webService.remove(i);
246       }
247       else
248       {
249         i++;
250       }
251     }
252   }
253
254   /**
255    * Returns the proportion of its range that a scrollbar's position represents,
256    * as a value between 0 and 1. For example if the whole range is from 0 to
257    * 200, then a position of 40 gives proportion = 0.2.
258    * 
259    * @see http://www.javalobby.org/java/forums/t33050.html#91885334
260    * 
261    * @param scroll
262    * @return
263    */
264   public static float getScrollBarProportion(JScrollBar scroll)
265   {
266     /*
267      * The extent (scroll handle width) deduction gives the true operating range
268      * of possible positions.
269      */
270     int possibleRange = scroll.getMaximum() - scroll.getMinimum()
271             - scroll.getModel().getExtent();
272     float valueInRange = scroll.getValue()
273             - (scroll.getModel().getExtent() / 2f);
274     float proportion = valueInRange / possibleRange;
275     return proportion;
276   }
277
278   /**
279    * Returns the scroll bar position in its range that would match the given
280    * proportion (between 0 and 1) of the whole. For example if the whole range
281    * is from 0 to 200, then a proportion of 0.25 gives position 50.
282    * 
283    * @param scrollbar
284    * @param proportion
285    * @return
286    */
287   public static int getScrollValueForProportion(JScrollBar scrollbar,
288           float proportion)
289   {
290     /*
291      * The extent (scroll handle width) deduction gives the true operating range
292      * of possible positions.
293      */
294     float fraction = proportion
295             * (scrollbar.getMaximum() - scrollbar.getMinimum()
296                     - scrollbar.getModel().getExtent())
297             + (scrollbar.getModel().getExtent() / 2f);
298     return Math.min(Math.round(fraction), scrollbar.getMaximum());
299   }
300
301   public static void jvInitComponent(AbstractButton comp, String i18nString)
302   {
303     setColorAndFont(comp);
304     if (i18nString != null && !i18nString.isEmpty())
305     {
306       comp.setText(MessageManager.getString(i18nString));
307     }
308   }
309
310   public static void jvInitComponent(JComponent comp)
311   {
312     setColorAndFont(comp);
313   }
314
315   private static void setColorAndFont(JComponent comp)
316   {
317     comp.setBackground(Color.white);
318     comp.setFont(JvSwingUtils.getLabelFont());
319   }
320
321   /**
322    * A helper method to build a drop-down choice of values, with tooltips for
323    * the entries
324    * 
325    * @param entries
326    * @param tooltips
327    */
328   public static JComboBox<String> buildComboWithTooltips(
329           List<String> entries, List<String> tooltips)
330   {
331     JComboBox<String> combo = new JComboBox<>();
332     final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
333     combo.setRenderer(renderer);
334     for (String attName : entries)
335     {
336       combo.addItem(attName);
337     }
338     renderer.setTooltips(tooltips);
339     final MouseAdapter mouseListener = new MouseAdapter()
340     {
341       @Override
342       public void mouseEntered(MouseEvent e)
343       {
344         int j = combo.getSelectedIndex();
345         if (j > -1)
346         {
347           combo.setToolTipText(tooltips.get(j));
348         }
349       }
350       @Override
351       public void mouseExited(MouseEvent e)
352       {
353         combo.setToolTipText(null);
354       }
355     };
356     for (Component c : combo.getComponents())
357     {
358       c.addMouseListener(mouseListener);
359     }
360     return combo;
361   }
362
363   /**
364    * Adds a titled border to the component in the default font and position (top
365    * left), optionally witht italic text
366    * 
367    * @param comp
368    * @param title
369    * @param italic
370    */
371   public static TitledBorder createTitledBorder(JComponent comp,
372           String title, boolean italic)
373   {
374     Font font = comp.getFont();
375     if (italic)
376     {
377       font = new Font(font.getName(), Font.ITALIC, font.getSize());
378     }
379     Border border = BorderFactory.createTitledBorder("");
380     TitledBorder titledBorder = BorderFactory.createTitledBorder(border,
381             title, TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION,
382             font);
383     comp.setBorder(titledBorder);
384
385     return titledBorder;
386   }
387
388 }