2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import java.awt.Color;
24 import java.awt.Component;
25 import java.awt.Container;
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;
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;
47 import jalview.util.MessageManager;
48 import jalview.util.Platform;
51 * useful functions for building Swing GUIs
56 public final class JvSwingUtils
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;\">"
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
67 * if true, add <html> wrapper tags (currently false for only
68 * two references -- both in Jws2Discoverer --
73 public static String wrapTooltip(boolean enclose, String ttext)
75 Objects.requireNonNull(ttext,
76 "Tootip text to format must not be null!");
77 ttext = ttext.trim().replaceAll("<br/>", "<br>");
78 boolean maxLengthExceeded = false;
80 boolean isHTML = ttext.startsWith("<html>");
83 ttext = ttext.substring(6);
85 if (ttext.endsWith("</html>"))
88 ttext = ttext.substring(0, ttext.length() - 7);
90 boolean hasBR = ttext.contains("<br>");
91 enclose |= isHTML || hasBR;
94 int pt = -1, ptlast = -4;
95 while ((pt = ttext.indexOf("<br>", pt + 1)) >= 0) {
96 if (pt - ptlast - 4 > 60) {
97 maxLengthExceeded = true;
104 maxLengthExceeded = ttext.length() > 60;
107 String ret = (!enclose ? ttext : maxLengthExceeded ? HTML_PREFIX + ttext + "</div></html>" :
108 "<html>" + ttext + "</html>");
109 //System.out.println("JvSwUtil " + enclose + " " + maxLengthExceeded + " " + ret);
113 public static JButton makeButton(String label, String tooltip,
114 ActionListener action)
116 JButton button = new JButton();
117 button.setText(label);
118 // TODO: get the base font metrics for the Jalview gui from somewhere
119 button.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
120 button.setForeground(Color.black);
121 button.setHorizontalAlignment(SwingConstants.CENTER);
122 button.setToolTipText(tooltip);
123 button.addActionListener(action);
128 * find or add a submenu with the given title in the given menu
132 * @return the new or existing submenu
134 public static JMenu findOrCreateMenu(JMenu menu, String submenu)
136 JMenu submenuinstance = null;
137 for (int i = 0, iSize = menu.getMenuComponentCount(); i < iSize; i++)
139 if (menu.getMenuComponent(i) instanceof JMenu
140 && ((JMenu) menu.getMenuComponent(i)).getText()
143 submenuinstance = (JMenu) menu.getMenuComponent(i);
146 if (submenuinstance == null)
148 submenuinstance = new JMenu(submenu);
149 menu.add(submenuinstance);
151 return submenuinstance;
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)
166 public static void addtoLayout(Container container, String tooltip,
167 JComponent label, JComponent comp, String constraints)
169 container.add(label);
170 container.add(comp, constraints);
171 comp.setToolTipText(tooltip); // this doesn't seem to show?
172 label.setToolTipText(tooltip);
176 public static void mgAddtoLayout(JPanel cpanel, String tooltip,
177 JLabel jLabel, JComponent name)
179 mgAddtoLayout(cpanel, tooltip, jLabel, name, null);
182 public static void mgAddtoLayout(JPanel cpanel, String tooltip,
183 JLabel jLabel, JComponent name, String params)
192 cpanel.add(name, params);
194 name.setToolTipText(tooltip);
195 jLabel.setToolTipText(tooltip);
199 * standard font for labels and check boxes in dialog boxes
204 public static Font getLabelFont()
206 return getLabelFont(false, false);
209 public static Font getLabelFont(boolean bold, boolean italic)
211 return new java.awt.Font("Verdana",
212 (!bold && !italic) ? Font.PLAIN
213 : (bold ? Font.BOLD : 0) + (italic ? Font.ITALIC : 0),
218 * standard font for editable text areas
222 public static Font getTextAreaFont()
224 return getLabelFont(false, false);
228 * clean up a swing menu. Removes any empty submenus without selection
233 public static void cleanMenu(JMenu webService)
235 for (int i = 0; i < webService.getItemCount();)
237 JMenuItem item = webService.getItem(i);
238 if (item instanceof JMenu && ((JMenu) item).getItemCount() == 0)
240 webService.remove(i);
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.
254 * @see http://www.javalobby.org/java/forums/t33050.html#91885334
259 public static float getScrollBarProportion(JScrollBar scroll)
262 * The extent (scroll handle width) deduction gives the true operating range
263 * of possible positions.
265 int possibleRange = scroll.getMaximum() - scroll.getMinimum()
266 - scroll.getModel().getExtent();
267 float valueInRange = scroll.getValue()
268 - (scroll.getModel().getExtent() / 2f);
269 float proportion = valueInRange / possibleRange;
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.
282 public static int getScrollValueForProportion(JScrollBar scrollbar,
286 * The extent (scroll handle width) deduction gives the true operating range
287 * of possible positions.
289 float fraction = proportion
290 * (scrollbar.getMaximum() - scrollbar.getMinimum()
291 - scrollbar.getModel().getExtent())
292 + (scrollbar.getModel().getExtent() / 2f);
293 return Math.min(Math.round(fraction), scrollbar.getMaximum());
296 public static void jvInitComponent(AbstractButton comp, String i18nString)
298 setColorAndFont(comp);
299 if (i18nString != null && !i18nString.isEmpty())
301 comp.setText(MessageManager.getString(i18nString));
305 public static void jvInitComponent(JComponent comp)
307 setColorAndFont(comp);
310 private static void setColorAndFont(JComponent comp)
312 comp.setBackground(Color.white);
313 comp.setFont(JvSwingUtils.getLabelFont());
317 * A helper method to build a drop-down choice of values, with tooltips for
323 public static JComboBox<Object> buildComboWithTooltips(
324 List<Object> entries, List<String> tooltips)
326 JComboBox<Object> combo = new JComboBox<>();
327 final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
328 combo.setRenderer(renderer);
329 for (Object attName : entries)
331 combo.addItem(attName);
333 renderer.setTooltips(tooltips);
334 final MouseAdapter mouseListener = new MouseAdapter()
337 public void mouseEntered(MouseEvent e)
339 int j = combo.getSelectedIndex();
342 combo.setToolTipText(tooltips.get(j));
346 public void mouseExited(MouseEvent e)
348 combo.setToolTipText(null);
351 for (Component c : combo.getComponents())
353 c.addMouseListener(mouseListener);
359 * Adds a titled border to the component in the default font and position (top
360 * left), optionally with italic text
366 public static TitledBorder createTitledBorder(JComponent comp,
367 String title, boolean italic)
369 Font font = comp.getFont();
372 font = new Font(font.getName(), Font.ITALIC, font.getSize());
374 Border border = BorderFactory.createTitledBorder("");
375 TitledBorder titledBorder = BorderFactory.createTitledBorder(border,
376 title, TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION,
378 comp.setBorder(titledBorder);