Merge develop to Release_2_8_3_Branch
[jalview.git] / src / jalview / gui / JvSwingUtils.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.gui;
22
23 import jalview.util.MessageManager;
24
25 import java.awt.BorderLayout;
26 import java.awt.Color;
27 import java.awt.Font;
28 import java.awt.GridLayout;
29 import java.awt.Rectangle;
30 import java.awt.event.ActionListener;
31
32 import javax.swing.AbstractButton;
33 import javax.swing.JButton;
34 import javax.swing.JComponent;
35 import javax.swing.JLabel;
36 import javax.swing.JMenu;
37 import javax.swing.JMenuItem;
38 import javax.swing.JPanel;
39 import javax.swing.JScrollBar;
40 import javax.swing.SwingConstants;
41
42 /**
43  * useful functions for building Swing GUIs
44  * 
45  * @author JimP
46  * 
47  */
48 public final class JvSwingUtils
49 {
50   /**
51    * wrap a bare html safe string to around 60 characters per line using a
52    * <table width=350>
53    * <tr>
54    * <td></td> field
55  * @param enclose TODO
56  * @param ttext
57    * 
58    * @return
59    */
60   public static String wrapTooltip(boolean enclose, String ttext)
61   {
62     if (ttext.length() < 60)
63     {
64       return enclose ? "<html>" + ttext + "</html>" : ttext;
65     }
66     else
67     {
68       return (enclose ? "<html>" : "")
69               + "<table width=350 border=0><tr><td>" + ttext
70               + "</td></tr></table>" + ((enclose ? "</html>" : ""));
71     }
72   }
73
74   public static JButton makeButton(String label, String tooltip,
75           ActionListener action)
76   {
77     JButton button = new JButton();
78     button.setText(label);
79     // TODO: get the base font metrics for the Jalview gui from somewhere
80     button.setFont(new java.awt.Font("Verdana", Font.PLAIN, 10));
81     button.setForeground(Color.black);
82     button.setHorizontalAlignment(SwingConstants.CENTER);
83     button.setToolTipText(tooltip);
84     button.addActionListener(action);
85     return button;
86   }
87
88   /**
89    * find or add a submenu with the given title in the given menu
90    * 
91    * @param menu
92    * @param submenu
93    * @return the new or existing submenu
94    */
95   public static JMenu findOrCreateMenu(JMenu menu, String submenu)
96   {
97     JMenu submenuinstance = null;
98     for (int i = 0, iSize = menu.getMenuComponentCount(); i < iSize; i++)
99     {
100       if (menu.getMenuComponent(i) instanceof JMenu
101               && ((JMenu) menu.getMenuComponent(i)).getText().equals(
102                       submenu))
103       {
104         submenuinstance = (JMenu) menu.getMenuComponent(i);
105       }
106     }
107     if (submenuinstance == null)
108     {
109       submenuinstance = new JMenu(submenu);
110       menu.add(submenuinstance);
111     }
112     return submenuinstance;
113
114   }
115
116   /**
117    * 
118    * @param panel
119    * @param tooltip
120    * @param label
121    * @param valBox
122    * @return the GUI element created that was added to the layout so it's
123    *         attributes can be changed.
124    */
125   public static JPanel addtoLayout(JPanel panel, String tooltip,
126           JComponent label, JComponent valBox)
127   {
128     JPanel laypanel = new JPanel(new GridLayout(1, 2));
129     JPanel labPanel = new JPanel(new BorderLayout());
130     JPanel valPanel = new JPanel();
131     // laypanel.setSize(panel.getPreferredSize());
132     // laypanel.setLayout(null);
133     labPanel.setBounds(new Rectangle(7, 7, 158, 23));
134     valPanel.setBounds(new Rectangle(172, 7, 270, 23));
135     // labPanel.setLayout(new GridLayout(1,1));
136     // valPanel.setLayout(new GridLayout(1,1));
137     labPanel.add(label, BorderLayout.WEST);
138     valPanel.add(valBox);
139     laypanel.add(labPanel);
140     laypanel.add(valPanel);
141     valPanel.setToolTipText(tooltip);
142     labPanel.setToolTipText(tooltip);
143     valBox.setToolTipText(tooltip);
144     panel.add(laypanel);
145     panel.validate();
146     return laypanel;
147   }
148
149   public static void mgAddtoLayout(JPanel cpanel, String tooltip,
150           JLabel jLabel, JComponent name)
151   {
152     mgAddtoLayout(cpanel, tooltip, jLabel, name, null);
153   }
154
155   public static void mgAddtoLayout(JPanel cpanel, String tooltip,
156           JLabel jLabel, JComponent name, String params)
157   {
158     cpanel.add(jLabel);
159     if (params == null)
160     {
161       cpanel.add(name);
162     }
163     else
164     {
165       cpanel.add(name, params);
166     }
167     name.setToolTipText(tooltip);
168     jLabel.setToolTipText(tooltip);
169   }
170
171   /**
172    * standard font for labels and check boxes in dialog boxes
173    * 
174    * @return
175    */
176
177   public static Font getLabelFont()
178   {
179     return getLabelFont(false, false);
180   }
181
182   public static Font getLabelFont(boolean bold, boolean italic)
183   {
184     return new java.awt.Font("Verdana", (!bold && !italic) ? Font.PLAIN
185             : (bold ? Font.BOLD : 0) + (italic ? Font.ITALIC : 0), 11);
186   }
187
188   /**
189    * standard font for editable text areas
190    * 
191    * @return
192    */
193   public static Font getTextAreaFont()
194   {
195     return getLabelFont(false, false);
196   }
197
198   /**
199    * clean up a swing menu. Removes any empty submenus without selection
200    * listeners.
201    * 
202    * @param webService
203    */
204   public static void cleanMenu(JMenu webService)
205   {
206     for (int i = 0; i < webService.getItemCount();)
207     {
208       JMenuItem item = webService.getItem(i);
209       if (item instanceof JMenu && ((JMenu) item).getItemCount() == 0)
210       {
211         webService.remove(i);
212       }
213       else
214       {
215         i++;
216       }
217     }
218   }
219
220   /**
221    * Returns the proportion of its range that a scrollbar's position represents,
222    * as a value between 0 and 1. For example if the whole range is from 0 to
223    * 200, then a position of 40 gives proportion = 0.2.
224    * 
225    * @see http://www.javalobby.org/java/forums/t33050.html#91885334
226    * 
227    * @param scroll
228    * @return
229    */
230   public static float getScrollBarProportion(JScrollBar scroll)
231   {
232     /*
233      * The extent (scroll handle width) deduction gives the true operating range
234      * of possible positions.
235      */
236     int possibleRange = scroll.getMaximum() - scroll.getMinimum()
237             - scroll.getModel().getExtent();
238     float valueInRange = scroll.getValue()
239             - (scroll.getModel().getExtent() / 2f);
240     float proportion = valueInRange / possibleRange;
241     return proportion;
242   }
243
244   /**
245    * Returns the scroll bar position in its range that would match the given
246    * proportion (between 0 and 1) of the whole. For example if the whole range
247    * is from 0 to 200, then a proportion of 0.25 gives position 50.
248    * 
249    * @param scrollbar
250    * @param proportion
251    * @return
252    */
253   public static int getScrollValueForProportion(JScrollBar scrollbar,
254           float proportion)
255   {
256     /*
257      * The extent (scroll handle width) deduction gives the true operating range
258      * of possible positions.
259      */
260     float fraction = proportion
261             * (scrollbar.getMaximum() - scrollbar.getMinimum() - scrollbar
262                     .getModel().getExtent())
263             + (scrollbar.getModel().getExtent() / 2f);
264     return Math.min(Math.round(fraction), scrollbar.getMaximum());
265   }
266
267   public static void jvInitComponent(AbstractButton comp, String i18nString)
268   {
269     setColorAndFont(comp);
270     if (i18nString != null && !i18nString.isEmpty())
271     {
272       comp.setText(MessageManager.getString(i18nString));
273     }
274   }
275
276   public static void jvInitComponent(JComponent comp)
277   {
278     setColorAndFont(comp);
279   }
280
281   private static void setColorAndFont(JComponent comp)
282   {
283     comp.setBackground(Color.white);
284     comp.setFont(JvSwingUtils.getLabelFont());
285   }
286
287 }