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