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