16d1f5527a4fa44ffabd19882ffb9e4b3f36fd48
[jalview.git] / src / jalview / gui / FontChooser.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2006 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       ap = tp.treeCanvas.ap;\r
54       oldFont = tp.getTreeFont();\r
55       defaultButton.setVisible(false);\r
56       init();\r
57     }\r
58     /**\r
59      * Creates a new FontChooser object.\r
60      *\r
61      * @param ap DOCUMENT ME!\r
62      */\r
63     public FontChooser(AlignmentPanel ap)\r
64     {\r
65       oldFont = ap.av.getFont();\r
66       this.ap = ap;\r
67       init();\r
68     }\r
69 \r
70     void init()\r
71     {\r
72         frame = new JInternalFrame();\r
73         frame.setContentPane(this);\r
74 \r
75         smoothFont.setSelected(ap.av.antiAlias);\r
76 \r
77         if(tp!=null)\r
78            Desktop.addInternalFrame(frame, "Change Font (Tree Panel)", 340, 170, false);\r
79         else\r
80            Desktop.addInternalFrame(frame, "Change Font", 340, 170, false);\r
81 \r
82         frame.setLayer(JLayeredPane.PALETTE_LAYER);\r
83 \r
84         String[] fonts = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment()\r
85                                                      .getAvailableFontFamilyNames();\r
86 \r
87         for (int i = 0; i < fonts.length; i++)\r
88         {\r
89             fontName.addItem(fonts[i]);\r
90         }\r
91 \r
92         for (int i = 1; i < 51; i++)\r
93         {\r
94             fontSize.addItem(i + "");\r
95         }\r
96 \r
97         fontStyle.addItem("plain");\r
98         fontStyle.addItem("bold");\r
99         fontStyle.addItem("italic");\r
100 \r
101         fontName.setSelectedItem(oldFont.getName());\r
102         fontSize.setSelectedItem(oldFont.getSize() + "");\r
103         fontStyle.setSelectedIndex(oldFont.getStyle());\r
104 \r
105         FontMetrics fm = getGraphics().getFontMetrics(oldFont);\r
106         monospaced.setSelected( fm.getStringBounds("M",getGraphics()).getWidth()\r
107                               ==fm.getStringBounds("|",getGraphics()).getWidth());\r
108 \r
109 \r
110         init = false;\r
111     }\r
112 \r
113     public void smoothFont_actionPerformed(ActionEvent e)\r
114     {\r
115       ap.av.antiAlias = smoothFont.isSelected();\r
116       ap.annotationPanel.image = null;\r
117       ap.repaint();\r
118     }\r
119 \r
120 \r
121 \r
122     /**\r
123      * DOCUMENT ME!\r
124      *\r
125      * @param e DOCUMENT ME!\r
126      */\r
127     protected void ok_actionPerformed(ActionEvent e)\r
128     {\r
129         try\r
130         {\r
131             frame.setClosed(true);\r
132         }\r
133         catch (Exception ex)\r
134         {\r
135         }\r
136 \r
137         if(ap!=null)\r
138         {\r
139           if (ap.getOverviewPanel() != null)\r
140           {\r
141             ap.getOverviewPanel().updateOverviewImage();\r
142           }\r
143         }\r
144     }\r
145 \r
146     /**\r
147      * DOCUMENT ME!\r
148      *\r
149      * @param e DOCUMENT ME!\r
150      */\r
151     protected void cancel_actionPerformed(ActionEvent e)\r
152     {\r
153         if(ap!=null)\r
154         {\r
155           ap.av.setFont(oldFont);\r
156           ap.repaint();\r
157         }\r
158         else if(tp!=null)\r
159         {\r
160           tp.setTreeFont(oldFont);\r
161         }\r
162         fontName.setSelectedItem(oldFont.getName());\r
163         fontSize.setSelectedItem(oldFont.getSize() + "");\r
164         fontStyle.setSelectedIndex(oldFont.getStyle());\r
165 \r
166         try\r
167         {\r
168             frame.setClosed(true);\r
169         }\r
170         catch (Exception ex)\r
171         {\r
172         }\r
173     }\r
174 \r
175     /**\r
176      * DOCUMENT ME!\r
177      */\r
178     void changeFont()\r
179     {\r
180       Font newFont = new Font(fontName.getSelectedItem().toString(),\r
181                               fontStyle.getSelectedIndex(),\r
182                               Integer.parseInt(fontSize.getSelectedItem().toString()));\r
183       if (ap != null)\r
184       {\r
185         ap.av.setFont(newFont);\r
186         ap.fontChanged();\r
187       }\r
188       else if(tp != null)\r
189       {\r
190         tp.setTreeFont(newFont);\r
191       }\r
192 \r
193       FontMetrics fm = getGraphics().getFontMetrics(newFont);\r
194 \r
195       monospaced.setSelected( fm.getStringBounds("M",getGraphics()).getWidth()\r
196                                 ==fm.getStringBounds("|",getGraphics()).getWidth());\r
197 \r
198     }\r
199 \r
200     /**\r
201      * DOCUMENT ME!\r
202      *\r
203      * @param e DOCUMENT ME!\r
204      */\r
205     protected void fontName_actionPerformed(ActionEvent e)\r
206     {\r
207         if (init)\r
208         {\r
209             return;\r
210         }\r
211 \r
212         changeFont();\r
213     }\r
214 \r
215     /**\r
216      * DOCUMENT ME!\r
217      *\r
218      * @param e DOCUMENT ME!\r
219      */\r
220     protected void fontSize_actionPerformed(ActionEvent e)\r
221     {\r
222         if (init)\r
223         {\r
224             return;\r
225         }\r
226 \r
227         changeFont();\r
228     }\r
229 \r
230     /**\r
231      * DOCUMENT ME!\r
232      *\r
233      * @param e DOCUMENT ME!\r
234      */\r
235     protected void fontStyle_actionPerformed(ActionEvent e)\r
236     {\r
237         if (init)\r
238         {\r
239             return;\r
240         }\r
241 \r
242         changeFont();\r
243     }\r
244 \r
245     /**\r
246      * DOCUMENT ME!\r
247      *\r
248      * @param e DOCUMENT ME!\r
249      */\r
250     public void defaultButton_actionPerformed(ActionEvent e)\r
251     {\r
252         Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());\r
253         Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");\r
254         Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());\r
255         Cache.setProperty("ANTI_ALIAS", Boolean.toString(smoothFont.isSelected()));\r
256     }\r
257 }\r