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