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.JMenu;
39 import javax.swing.JMenuItem;
40 import javax.swing.JScrollBar;
41 import javax.swing.SwingConstants;
42 import javax.swing.border.Border;
43 import javax.swing.border.TitledBorder;
45 import jalview.util.MessageManager;
46 import jalview.util.Platform;
49 * useful functions for building Swing GUIs
54 public final class JvSwingUtils
56 static final String HTML_PREFIX = (Platform.isJS() ?
57 "<html><div style=\"max-width:350px;overflow-wrap:break-word;display:inline-block\">"
58 : "<html><div style=\"width:350; text-align: justify; word-wrap: break-word;\">"
62 * wrap a bare html safe string to around 60 characters per line using a CSS
63 * style class specifying word-wrap and break-word
66 * if true, add <html> wrapper tags (currently false for only
67 * two references -- both in Jws2Discoverer --
72 public static String wrapTooltip(boolean enclose, String ttext)
74 Objects.requireNonNull(ttext,
75 "Tootip text to format must not be null!");
76 ttext = ttext.trim().replaceAll("<br/>", "<br>");
78 boolean maxLengthExceeded = false;
79 boolean isHTML = ttext.startsWith("<html>");
82 ttext = ttext.substring(6);
84 if (ttext.endsWith("</html>"))
87 ttext = ttext.substring(0, ttext.length() - 7);
89 boolean hasBR = ttext.contains("<br>");
90 enclose |= isHTML || hasBR;
93 int pt = -1, ptlast = -4;
94 while ((pt = ttext.indexOf("<br>", pt + 1)) >= 0) {
95 if (pt - ptlast - 4 > 60) {
96 maxLengthExceeded = true;
103 maxLengthExceeded = ttext.length() > 60;
106 String ret = (!enclose ? ttext : maxLengthExceeded ? HTML_PREFIX + ttext + "</div></html>" :
107 "<html>" + ttext + "</html>");
108 //System.out.println("JvSwUtil " + enclose + " " + maxLengthExceeded + " " + ret);
112 public static JButton makeButton(String label, String tooltip,
113 ActionListener action)
115 JButton button = new JButton();
116 button.setText(label);
117 // TODO: get the base font metrics for the Jalview gui from somewhere
118 button.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
119 button.setForeground(Color.black);
120 button.setHorizontalAlignment(SwingConstants.CENTER);
121 button.setToolTipText(tooltip);
122 button.addActionListener(action);
127 * find or add a submenu with the given title in the given menu
131 * @return the new or existing submenu
133 public static JMenu findOrCreateMenu(JMenu menu, String submenu)
135 JMenu submenuinstance = null;
136 for (int i = 0, iSize = menu.getMenuComponentCount(); i < iSize; i++)
138 if (menu.getMenuComponent(i) instanceof JMenu
139 && ((JMenu) menu.getMenuComponent(i)).getText()
142 submenuinstance = (JMenu) menu.getMenuComponent(i);
145 if (submenuinstance == null)
147 submenuinstance = new JMenu(submenu);
148 menu.add(submenuinstance);
150 return submenuinstance;
155 * A convenience method that that adds a component with label to a container,
156 * sets a tooltip on both component and label, and optionally specifies layout
157 * constraints for the added component (but not the label)
165 public static void addtoLayout(Container container, String tooltip,
166 JComponent label, JComponent comp, String constraints)
168 container.add(label);
169 container.add(comp, constraints);
170 comp.setToolTipText(tooltip); // this doesn't seem to show?
171 label.setToolTipText(tooltip);
175 * standard font for labels and check boxes in dialog boxes
180 public static Font getLabelFont()
182 return getLabelFont(false, false);
185 public static Font getLabelFont(boolean bold, boolean italic)
187 return new java.awt.Font("Verdana",
188 (!bold && !italic) ? Font.PLAIN
189 : (bold ? Font.BOLD : 0) + (italic ? Font.ITALIC : 0),
194 * standard font for editable text areas
198 public static Font getTextAreaFont()
200 return getLabelFont(false, false);
204 * clean up a swing menu. Removes any empty submenus without selection
209 public static void cleanMenu(JMenu webService)
211 for (int i = 0; i < webService.getItemCount();)
213 JMenuItem item = webService.getItem(i);
214 if (item instanceof JMenu && ((JMenu) item).getItemCount() == 0)
216 webService.remove(i);
226 * Returns the proportion of its range that a scrollbar's position represents,
227 * as a value between 0 and 1. For example if the whole range is from 0 to
228 * 200, then a position of 40 gives proportion = 0.2.
230 * @see http://www.javalobby.org/java/forums/t33050.html#91885334
235 public static float getScrollBarProportion(JScrollBar scroll)
238 * The extent (scroll handle width) deduction gives the true operating range
239 * of possible positions.
241 int possibleRange = scroll.getMaximum() - scroll.getMinimum()
242 - scroll.getModel().getExtent();
243 float valueInRange = scroll.getValue()
244 - (scroll.getModel().getExtent() / 2f);
245 float proportion = valueInRange / possibleRange;
250 * Returns the scroll bar position in its range that would match the given
251 * proportion (between 0 and 1) of the whole. For example if the whole range
252 * is from 0 to 200, then a proportion of 0.25 gives position 50.
258 public static int getScrollValueForProportion(JScrollBar scrollbar,
262 * The extent (scroll handle width) deduction gives the true operating range
263 * of possible positions.
265 float fraction = proportion
266 * (scrollbar.getMaximum() - scrollbar.getMinimum()
267 - scrollbar.getModel().getExtent())
268 + (scrollbar.getModel().getExtent() / 2f);
269 return Math.min(Math.round(fraction), scrollbar.getMaximum());
272 public static void jvInitComponent(AbstractButton comp, String i18nString)
274 setColorAndFont(comp);
275 if (i18nString != null && !i18nString.isEmpty())
277 comp.setText(MessageManager.getString(i18nString));
281 public static void jvInitComponent(JComponent comp)
283 setColorAndFont(comp);
286 private static void setColorAndFont(JComponent comp)
288 comp.setBackground(Color.white);
289 comp.setFont(JvSwingUtils.getLabelFont());
293 * A helper method to build a drop-down choice of values, with tooltips for
299 public static JComboBox<Object> buildComboWithTooltips(
300 List<Object> entries, List<String> tooltips)
302 JComboBox<Object> combo = new JComboBox<>();
303 final ComboBoxTooltipRenderer renderer = new ComboBoxTooltipRenderer();
304 combo.setRenderer(renderer);
305 for (Object attName : entries)
307 combo.addItem(attName);
309 renderer.setTooltips(tooltips);
310 final MouseAdapter mouseListener = new MouseAdapter()
313 public void mouseEntered(MouseEvent e)
315 int j = combo.getSelectedIndex();
318 combo.setToolTipText(tooltips.get(j));
322 public void mouseExited(MouseEvent e)
324 combo.setToolTipText(null);
327 for (Component c : combo.getComponents())
329 c.addMouseListener(mouseListener);
335 * Adds a titled border to the component in the default font and position (top
336 * left), optionally with italic text
342 public static TitledBorder createTitledBorder(JComponent comp,
343 String title, boolean italic)
345 Font font = comp.getFont();
348 font = new Font(font.getName(), Font.ITALIC, font.getSize());
350 Border border = BorderFactory.createTitledBorder("");
351 TitledBorder titledBorder = BorderFactory.createTitledBorder(border,
352 title, TitledBorder.LEADING, TitledBorder.DEFAULT_POSITION,
354 comp.setBorder(titledBorder);