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