Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 16:11:35 GMT
  2. Package jalview.gui

File JvSwingUtils.java

 

Coverage histogram

../../img/srcFileCovDistChart7.png
30% of files have more coverage

Code metrics

38
88
19
1
383
241
42
0.48
4.63
19
2.21

Classes

Class Line # Actions
JvSwingUtils 56 88 42
0.675862167.6%
 

Contributing tests

This file is covered by 408 tests. .

Source view

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