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 jalview.util.MessageManager;
25 import java.awt.Color;
26 import java.awt.Component;
27 import java.awt.Container;
29 import java.awt.event.ActionListener;
30 import java.awt.event.MouseAdapter;
31 import java.awt.event.MouseEvent;
32 import java.util.List;
33 import java.util.Objects;
35 import javax.swing.AbstractButton;
36 import javax.swing.BorderFactory;
37 import javax.swing.JButton;
38 import javax.swing.JComboBox;
39 import javax.swing.JComponent;
40 import javax.swing.JMenu;
41 import javax.swing.JMenuItem;
42 import javax.swing.JScrollBar;
43 import javax.swing.SwingConstants;
44 import javax.swing.border.Border;
45 import javax.swing.border.TitledBorder;
48 * useful functions for building Swing GUIs
53 public final class JvSwingUtils
56 * wrap a bare html safe string to around 60 characters per line using a CSS
57 * style class specifying word-wrap and break-word
60 * if true, add <html> wrapper tags
65 public static String wrapTooltip(boolean enclose, String ttext)
67 Objects.requireNonNull(ttext,
68 "Tootip text to format must not be null!");
70 boolean maxLengthExceeded = false;
72 if (ttext.contains("<br>"))
74 String[] htmllines = ttext.split("<br>");
75 for (String line : htmllines)
77 maxLengthExceeded = line.length() > 60;
78 if (maxLengthExceeded)
86 maxLengthExceeded = ttext.length() > 60;
89 if (!maxLengthExceeded)
91 return enclose ? "<html>" + ttext + "</html>" : ttext;
94 return (enclose ? "<html>" : "")
95 + "<style> p.ttip {width: 350; text-align: justify; word-wrap: break-word;}</style><p class=\"ttip\">"
96 + ttext + "</p>" + ((enclose ? "</html>" : ""));
100 public static JButton makeButton(String label, String tooltip,
101 ActionListener action)
103 JButton button = new JButton();
104 button.setText(label);
105 // TODO: get the base font metrics for the Jalview gui from somewhere
106 button.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
107 button.setForeground(Color.black);
108 button.setHorizontalAlignment(SwingConstants.CENTER);
109 button.setToolTipText(tooltip);
110 button.addActionListener(action);
115 * find or add a submenu with the given title in the given menu
119 * @return the new or existing submenu
121 public static JMenu findOrCreateMenu(JMenu menu, String submenu)
123 JMenu submenuinstance = null;
124 for (int i = 0, iSize = menu.getMenuComponentCount(); i < iSize; i++)
126 if (menu.getMenuComponent(i) instanceof JMenu
127 && ((JMenu) menu.getMenuComponent(i)).getText()
130 submenuinstance = (JMenu) menu.getMenuComponent(i);
133 if (submenuinstance == null)
135 submenuinstance = new JMenu(submenu);
136 menu.add(submenuinstance);
138 return submenuinstance;
143 * A convenience method that that adds a component with label to a container,
144 * sets a tooltip on both component and label, and optionally specifies layout
145 * constraints for the added component (but not the label)
153 public static void addtoLayout(Container container, String tooltip,
154 JComponent label, JComponent comp, String constraints)
156 container.add(label);
157 container.add(comp, constraints);
158 comp.setToolTipText(tooltip); // this doesn't seem to show?
159 label.setToolTipText(tooltip);
163 * standard font for labels and check boxes in dialog boxes
168 public static Font getLabelFont()
170 return getLabelFont(false, false);
173 public static Font getLabelFont(boolean bold, boolean italic)
175 return new java.awt.Font("Verdana",
176 (!bold && !italic) ? Font.PLAIN
177 : (bold ? Font.BOLD : 0) + (italic ? Font.ITALIC : 0),
182 * standard font for editable text areas
186 public static Font getTextAreaFont()
188 return getLabelFont(false, false);
192 * clean up a swing menu. Removes any empty submenus without selection
197 public static void cleanMenu(JMenu webService)
199 for (int i = 0; i < webService.getItemCount();)
201 JMenuItem item = webService.getItem(i);
202 if (item instanceof JMenu && ((JMenu) item).getItemCount() == 0)
204 webService.remove(i);
214 * Returns the proportion of its range that a scrollbar's position represents,
215 * as a value between 0 and 1. For example if the whole range is from 0 to
216 * 200, then a position of 40 gives proportion = 0.2.
218 * @see http://www.javalobby.org/java/forums/t33050.html#91885334
223 public static float getScrollBarProportion(JScrollBar scroll)
226 * The extent (scroll handle width) deduction gives the true operating range
227 * of possible positions.
229 int possibleRange = scroll.getMaximum() - scroll.getMinimum()
230 - scroll.getModel().getExtent();
231 float valueInRange = scroll.getValue()
232 - (scroll.getModel().getExtent() / 2f);
233 float proportion = valueInRange / possibleRange;
238 * Returns the scroll bar position in its range that would match the given
239 * proportion (between 0 and 1) of the whole. For example if the whole range
240 * is from 0 to 200, then a proportion of 0.25 gives position 50.
246 public static int getScrollValueForProportion(JScrollBar scrollbar,
250 * The extent (scroll handle width) deduction gives the true operating range
251 * of possible positions.
253 float fraction = proportion
254 * (scrollbar.getMaximum() - scrollbar.getMinimum()
255 - scrollbar.getModel().getExtent())
256 + (scrollbar.getModel().getExtent() / 2f);
257 return Math.min(Math.round(fraction), scrollbar.getMaximum());
260 public static void jvInitComponent(AbstractButton comp, String i18nString)
262 setColorAndFont(comp);
263 if (i18nString != null && !i18nString.isEmpty())
265 comp.setText(MessageManager.getString(i18nString));
269 public static void jvInitComponent(JComponent comp)
271 setColorAndFont(comp);
274 private static void setColorAndFont(JComponent comp)
276 comp.setBackground(Color.white);
277 comp.setFont(JvSwingUtils.getLabelFont());
281 * A helper method to build a drop-down choice of values, with tooltips for
287 public static JComboBox<Object> buildComboWithTooltips(
288 List<Object> entries, List<String> tooltips)
290 JComboBox<Object> combo = new JComboBox<>();
291 final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
292 combo.setRenderer(renderer);
293 for (Object attName : entries)
295 combo.addItem(attName);
297 renderer.setTooltips(tooltips);
298 final MouseAdapter mouseListener = new MouseAdapter()
301 public void mouseEntered(MouseEvent e)
303 int j = combo.getSelectedIndex();
306 combo.setToolTipText(tooltips.get(j));
310 public void mouseExited(MouseEvent e)
312 combo.setToolTipText(null);
315 for (Component c : combo.getComponents())
317 c.addMouseListener(mouseListener);
323 * Adds a titled border to the component in the default font and position (top
324 * left), optionally with italic text
330 public static TitledBorder createTitledBorder(JComponent comp,
331 String title, boolean italic)
333 Font font = comp.getFont();
336 font = new Font(font.getName(), Font.ITALIC, font.getSize());
338 Border border = BorderFactory.createTitledBorder("");
339 TitledBorder titledBorder = BorderFactory.createTitledBorder(border,
340 title, TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION,
342 comp.setBorder(titledBorder);