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.BorderLayout;
26 import java.awt.Color;
28 import java.awt.GridLayout;
29 import java.awt.Rectangle;
30 import java.awt.event.ActionListener;
31 import java.util.Objects;
33 import javax.swing.AbstractButton;
34 import javax.swing.JButton;
35 import javax.swing.JComponent;
36 import javax.swing.JLabel;
37 import javax.swing.JMenu;
38 import javax.swing.JMenuItem;
39 import javax.swing.JPanel;
40 import javax.swing.JScrollBar;
41 import javax.swing.SwingConstants;
44 * useful functions for building Swing GUIs
49 public final class JvSwingUtils
52 * wrap a bare html safe string to around 60 characters per line using a CSS
53 * style class specifying word-wrap and break-word
56 * if true, add <html> wrapper tags
61 public static String wrapTooltip(boolean enclose, String ttext)
63 Objects.requireNonNull(ttext,
64 "Tootip text to format must not be null!");
66 boolean maxLengthExceeded = false;
68 if (ttext.contains("<br>"))
70 String[] htmllines = ttext.split("<br>");
71 for (String line : htmllines)
73 maxLengthExceeded = line.length() > 60;
74 if (maxLengthExceeded)
82 maxLengthExceeded = ttext.length() > 60;
85 if (!maxLengthExceeded)
87 return enclose ? "<html>" + ttext + "</html>" : ttext;
90 return (enclose ? "<html>" : "")
91 + "<style> p.ttip {width: 350; text-align: justify; word-wrap: break-word;}</style><p class=\"ttip\">"
92 + ttext + "</p>" + ((enclose ? "</html>" : ""));
96 public static JButton makeButton(String label, String tooltip,
97 ActionListener action)
99 JButton button = new JButton();
100 button.setText(label);
101 // TODO: get the base font metrics for the Jalview gui from somewhere
102 button.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
103 button.setForeground(Color.black);
104 button.setHorizontalAlignment(SwingConstants.CENTER);
105 button.setToolTipText(tooltip);
106 button.addActionListener(action);
111 * find or add a submenu with the given title in the given menu
115 * @return the new or existing submenu
117 public static JMenu findOrCreateMenu(JMenu menu, String submenu)
119 JMenu submenuinstance = null;
120 for (int i = 0, iSize = menu.getMenuComponentCount(); i < iSize; i++)
122 if (menu.getMenuComponent(i) instanceof JMenu
123 && ((JMenu) menu.getMenuComponent(i)).getText()
126 submenuinstance = (JMenu) menu.getMenuComponent(i);
129 if (submenuinstance == null)
131 submenuinstance = new JMenu(submenu);
132 menu.add(submenuinstance);
134 return submenuinstance;
144 * @return the GUI element created that was added to the layout so it's
145 * attributes can be changed.
147 public static JPanel addtoLayout(JPanel panel, String tooltip,
148 JComponent label, JComponent valBox)
150 JPanel laypanel = new JPanel(new GridLayout(1, 2));
151 JPanel labPanel = new JPanel(new BorderLayout());
152 JPanel valPanel = new JPanel();
153 labPanel.setBounds(new Rectangle(7, 7, 158, 23));
154 valPanel.setBounds(new Rectangle(172, 7, 270, 23));
155 labPanel.add(label, BorderLayout.WEST);
156 valPanel.add(valBox);
157 laypanel.add(labPanel);
158 laypanel.add(valPanel);
159 valPanel.setToolTipText(tooltip);
160 labPanel.setToolTipText(tooltip);
161 valBox.setToolTipText(tooltip);
167 public static void mgAddtoLayout(JPanel cpanel, String tooltip,
168 JLabel jLabel, JComponent name)
170 mgAddtoLayout(cpanel, tooltip, jLabel, name, null);
173 public static void mgAddtoLayout(JPanel cpanel, String tooltip,
174 JLabel jLabel, JComponent name, String params)
183 cpanel.add(name, params);
185 name.setToolTipText(tooltip);
186 jLabel.setToolTipText(tooltip);
190 * standard font for labels and check boxes in dialog boxes
195 public static Font getLabelFont()
197 return getLabelFont(false, false);
200 public static Font getLabelFont(boolean bold, boolean italic)
202 return new java.awt.Font("Verdana",
203 (!bold && !italic) ? Font.PLAIN
204 : (bold ? Font.BOLD : 0) + (italic ? Font.ITALIC : 0),
209 * standard font for editable text areas
213 public static Font getTextAreaFont()
215 return getLabelFont(false, false);
219 * clean up a swing menu. Removes any empty submenus without selection
224 public static void cleanMenu(JMenu webService)
226 for (int i = 0; i < webService.getItemCount();)
228 JMenuItem item = webService.getItem(i);
229 if (item instanceof JMenu && ((JMenu) item).getItemCount() == 0)
231 webService.remove(i);
241 * Returns the proportion of its range that a scrollbar's position represents,
242 * as a value between 0 and 1. For example if the whole range is from 0 to
243 * 200, then a position of 40 gives proportion = 0.2.
245 * @see http://www.javalobby.org/java/forums/t33050.html#91885334
250 public static float getScrollBarProportion(JScrollBar scroll)
253 * The extent (scroll handle width) deduction gives the true operating range
254 * of possible positions.
256 int possibleRange = scroll.getMaximum() - scroll.getMinimum()
257 - scroll.getModel().getExtent();
258 float valueInRange = scroll.getValue()
259 - (scroll.getModel().getExtent() / 2f);
260 float proportion = valueInRange / possibleRange;
265 * Returns the scroll bar position in its range that would match the given
266 * proportion (between 0 and 1) of the whole. For example if the whole range
267 * is from 0 to 200, then a proportion of 0.25 gives position 50.
273 public static int getScrollValueForProportion(JScrollBar scrollbar,
277 * The extent (scroll handle width) deduction gives the true operating range
278 * of possible positions.
280 float fraction = proportion
281 * (scrollbar.getMaximum() - scrollbar.getMinimum()
282 - scrollbar.getModel().getExtent())
283 + (scrollbar.getModel().getExtent() / 2f);
284 return Math.min(Math.round(fraction), scrollbar.getMaximum());
287 public static void jvInitComponent(AbstractButton comp, String i18nString)
289 setColorAndFont(comp);
290 if (i18nString != null && !i18nString.isEmpty())
292 comp.setText(MessageManager.getString(i18nString));
296 public static void jvInitComponent(JComponent comp)
298 setColorAndFont(comp);
301 private static void setColorAndFont(JComponent comp)
303 comp.setBackground(Color.white);
304 comp.setFont(JvSwingUtils.getLabelFont());