2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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;
27 import java.awt.Rectangle;
28 import java.awt.event.ActionListener;
30 import javax.swing.AbstractButton;
31 import javax.swing.JButton;
32 import javax.swing.JComponent;
33 import javax.swing.JLabel;
34 import javax.swing.JMenu;
35 import javax.swing.JMenuItem;
36 import javax.swing.JPanel;
37 import javax.swing.SwingConstants;
40 * useful functions for building Swing GUIs
45 public final class JvSwingUtils
48 * wrap a bare html safe string to around 60 characters per line using a
57 public static String wrapTooltip(boolean enclose, String ttext)
59 if (ttext.length() < 60)
61 return enclose ? "<html>" + ttext + "</html>" : ttext;
65 return (enclose ? "<html>" : "")
66 + "<table width=350 border=0><tr><td>" + ttext
67 + "</td></tr></table>" + ((enclose ? "</html>" : ""));
71 public static JButton makeButton(String label, String tooltip,
72 ActionListener action)
74 JButton button = new JButton();
75 button.setText(label);
76 // TODO: get the base font metrics for the Jalview gui from somewhere
77 button.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
78 button.setForeground(Color.black);
79 button.setHorizontalAlignment(SwingConstants.CENTER);
80 button.setToolTipText(tooltip);
81 button.addActionListener(action);
86 * find or add a submenu with the given title in the given menu
90 * @return the new or existing submenu
92 public static JMenu findOrCreateMenu(JMenu menu, String submenu)
94 JMenu submenuinstance = null;
95 for (int i = 0, iSize = menu.getMenuComponentCount(); i < iSize; i++)
97 if (menu.getMenuComponent(i) instanceof JMenu
98 && ((JMenu) menu.getMenuComponent(i)).getText().equals(
101 submenuinstance = (JMenu) menu.getMenuComponent(i);
104 if (submenuinstance == null)
106 submenuinstance = new JMenu(submenu);
107 menu.add(submenuinstance);
109 return submenuinstance;
119 * @return the GUI element created that was added to the layout so it's
120 * attributes can be changed.
122 public static JPanel addtoLayout(JPanel panel, String tooltip,
123 JComponent label, JComponent valBox)
125 JPanel laypanel = new JPanel(), labPanel = new JPanel(), valPanel = new JPanel();
126 // laypanel.setSize(panel.getPreferredSize());
127 // laypanel.setLayout(null);
128 labPanel.setBounds(new Rectangle(7, 7, 158, 23));
129 valPanel.setBounds(new Rectangle(172, 7, 270, 23));
130 // labPanel.setLayout(new GridLayout(1,1));
131 // valPanel.setLayout(new GridLayout(1,1));
133 valPanel.add(valBox);
134 laypanel.add(labPanel);
135 laypanel.add(valPanel);
136 valPanel.setToolTipText(tooltip);
137 labPanel.setToolTipText(tooltip);
138 valBox.setToolTipText(tooltip);
144 public static void mgAddtoLayout(JPanel cpanel, String tooltip,
145 JLabel jLabel, JComponent name)
147 mgAddtoLayout(cpanel, tooltip, jLabel, name, null);
150 public static void mgAddtoLayout(JPanel cpanel, String tooltip,
151 JLabel jLabel, JComponent name, String params)
160 cpanel.add(name, params);
162 name.setToolTipText(tooltip);
163 jLabel.setToolTipText(tooltip);
167 * standard font for labels and check boxes in dialog boxes
172 public static Font getLabelFont()
174 return getLabelFont(false, false);
177 public static Font getLabelFont(boolean bold, boolean italic)
179 return new java.awt.Font("Verdana", (!bold && !italic) ? Font.PLAIN
180 : (bold ? Font.BOLD : 0) + (italic ? Font.ITALIC : 0), 11);
184 * standard font for editable text areas
188 public static Font getTextAreaFont()
190 return getLabelFont(false, false);
194 * clean up a swing menu. Removes any empty submenus without selection
199 public static void cleanMenu(JMenu webService)
201 for (int i = 0; i < webService.getItemCount();)
203 JMenuItem item = webService.getItem(i);
204 if (item instanceof JMenu && ((JMenu) item).getItemCount() == 0)
206 webService.remove(i);
215 public static void jvInitComponent(AbstractButton comp, String i18nString)
217 setColorAndFont(comp);
218 if (i18nString != null && !i18nString.isEmpty())
220 comp.setText(MessageManager.getString(i18nString));
224 public static void jvInitComponent(JComponent comp)
226 setColorAndFont(comp);
229 private static void setColorAndFont(JComponent comp)
231 comp.setBackground(Color.white);
232 comp.setFont(JvSwingUtils.getLabelFont());