Monospaced indicator
[jalview.git] / src / jalview / gui / FontChooser.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 package jalview.gui;\r
20 \r
21 import jalview.bin.*;\r
22 \r
23 import jalview.jbgui.*;\r
24 \r
25 import java.awt.*;\r
26 import java.awt.event.*;\r
27 \r
28 import javax.swing.*;\r
29 \r
30 \r
31 /**\r
32  * DOCUMENT ME!\r
33  *\r
34  * @author $author$\r
35  * @version $Revision$\r
36  */\r
37 public class FontChooser extends GFontChooser\r
38 {\r
39     AlignmentPanel ap;\r
40     TreePanel tp;\r
41     Font oldFont;\r
42     boolean init = true;\r
43     JInternalFrame frame;\r
44 \r
45     /**\r
46      * Creates a new FontChooser object.\r
47      *\r
48      * @param ap DOCUMENT ME!\r
49      */\r
50     public FontChooser(TreePanel tp)\r
51     {\r
52       this.tp = tp;\r
53       oldFont = tp.getTreeFont();\r
54       defaultButton.setVisible(false);\r
55       init();\r
56     }\r
57     /**\r
58      * Creates a new FontChooser object.\r
59      *\r
60      * @param ap DOCUMENT ME!\r
61      */\r
62     public FontChooser(AlignmentPanel ap)\r
63     {\r
64       oldFont = ap.av.getFont();\r
65       this.ap = ap;\r
66       init();\r
67     }\r
68 \r
69     void init()\r
70     {\r
71         frame = new JInternalFrame();\r
72         frame.setContentPane(this);\r
73         if(tp!=null)\r
74            Desktop.addInternalFrame(frame, "Change Font (Tree Panel)", 540, 100, false);\r
75         else\r
76            Desktop.addInternalFrame(frame, "Change Font", 540, 100, false);\r
77 \r
78         frame.setLayer(JLayeredPane.PALETTE_LAYER);\r
79 \r
80         String[] fonts = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment()\r
81                                                      .getAvailableFontFamilyNames();\r
82 \r
83         for (int i = 0; i < fonts.length; i++)\r
84         {\r
85             fontName.addItem(fonts[i]);\r
86         }\r
87 \r
88         for (int i = 1; i < 51; i++)\r
89         {\r
90             fontSize.addItem(i + "");\r
91         }\r
92 \r
93         fontStyle.addItem("plain");\r
94         fontStyle.addItem("bold");\r
95         fontStyle.addItem("italic");\r
96 \r
97         fontName.setSelectedItem(oldFont.getName());\r
98         fontSize.setSelectedItem(oldFont.getSize() + "");\r
99         fontStyle.setSelectedIndex(oldFont.getStyle());\r
100 \r
101         FontMetrics fm = getGraphics().getFontMetrics(oldFont);\r
102         monospaced.setSelected( fm.getStringBounds("M",getGraphics()).getWidth()\r
103                               ==fm.getStringBounds("|",getGraphics()).getWidth());\r
104 \r
105 \r
106         init = false;\r
107     }\r
108 \r
109     /**\r
110      * DOCUMENT ME!\r
111      *\r
112      * @param e DOCUMENT ME!\r
113      */\r
114     protected void ok_actionPerformed(ActionEvent e)\r
115     {\r
116         try\r
117         {\r
118             frame.setClosed(true);\r
119         }\r
120         catch (Exception ex)\r
121         {\r
122         }\r
123 \r
124         if(ap!=null)\r
125         {\r
126           if (ap.getOverviewPanel() != null)\r
127           {\r
128             ap.getOverviewPanel().updateOverviewImage();\r
129           }\r
130         }\r
131     }\r
132 \r
133     /**\r
134      * DOCUMENT ME!\r
135      *\r
136      * @param e DOCUMENT ME!\r
137      */\r
138     protected void cancel_actionPerformed(ActionEvent e)\r
139     {\r
140         if(ap!=null)\r
141         {\r
142           ap.av.setFont(oldFont);\r
143           ap.repaint();\r
144         }\r
145         else if(tp!=null)\r
146         {\r
147           tp.setTreeFont(oldFont);\r
148         }\r
149         fontName.setSelectedItem(oldFont.getName());\r
150         fontSize.setSelectedItem(oldFont.getSize() + "");\r
151         fontStyle.setSelectedIndex(oldFont.getStyle());\r
152 \r
153         try\r
154         {\r
155             frame.setClosed(true);\r
156         }\r
157         catch (Exception ex)\r
158         {\r
159         }\r
160     }\r
161 \r
162     /**\r
163      * DOCUMENT ME!\r
164      */\r
165     void changeFont()\r
166     {\r
167       Font newFont = new Font(fontName.getSelectedItem().toString(),\r
168                               fontStyle.getSelectedIndex(),\r
169                               Integer.parseInt(fontSize.getSelectedItem().toString()));\r
170       if (ap != null)\r
171       {\r
172         ap.av.setFont(newFont);\r
173         ap.fontChanged();\r
174       }\r
175       else if(tp != null)\r
176       {\r
177         tp.setTreeFont(newFont);\r
178       }\r
179 \r
180       FontMetrics fm = getGraphics().getFontMetrics(newFont);\r
181 \r
182       monospaced.setSelected( fm.getStringBounds("M",getGraphics()).getWidth()\r
183                                 ==fm.getStringBounds("|",getGraphics()).getWidth());\r
184 \r
185     }\r
186 \r
187     /**\r
188      * DOCUMENT ME!\r
189      *\r
190      * @param e DOCUMENT ME!\r
191      */\r
192     protected void fontName_actionPerformed(ActionEvent e)\r
193     {\r
194         if (init)\r
195         {\r
196             return;\r
197         }\r
198 \r
199         changeFont();\r
200     }\r
201 \r
202     /**\r
203      * DOCUMENT ME!\r
204      *\r
205      * @param e DOCUMENT ME!\r
206      */\r
207     protected void fontSize_actionPerformed(ActionEvent e)\r
208     {\r
209         if (init)\r
210         {\r
211             return;\r
212         }\r
213 \r
214         changeFont();\r
215     }\r
216 \r
217     /**\r
218      * DOCUMENT ME!\r
219      *\r
220      * @param e DOCUMENT ME!\r
221      */\r
222     protected void fontStyle_actionPerformed(ActionEvent e)\r
223     {\r
224         if (init)\r
225         {\r
226             return;\r
227         }\r
228 \r
229         changeFont();\r
230     }\r
231 \r
232     /**\r
233      * DOCUMENT ME!\r
234      *\r
235      * @param e DOCUMENT ME!\r
236      */\r
237     public void defaultButton_actionPerformed(ActionEvent e)\r
238     {\r
239         Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());\r
240         Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");\r
241         Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());\r
242     }\r
243 }\r