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