JAL-1432 updated copyright notices
[jalview.git] / src / jalview / gui / FontChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 The Jalview Authors
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  * The Jalview Authors are detailed in the 'AUTHORS' file.
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   private Font lastSelected = null;
184
185   private int lastSelStyle = 0;
186
187   private int lastSelSize = 0;
188
189   private boolean lastSelMono = false;
190
191   /**
192    * DOCUMENT ME!
193    */
194   void changeFont()
195   {
196     if (lastSelected == null)
197     {
198       // initialise with original font
199       lastSelected = oldFont;
200       lastSelSize = oldFont.getSize();
201       lastSelStyle = oldFont.getStyle();
202       FontMetrics fm = getGraphics().getFontMetrics(oldFont);
203       double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
204               .getStringBounds("I", getGraphics()).getWidth();
205       lastSelMono = mw == iw;
206     }
207
208     Font newFont = new Font(fontName.getSelectedItem().toString(),
209             fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
210                     .getSelectedItem().toString()));
211     FontMetrics fm = getGraphics().getFontMetrics(newFont);
212     double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
213             .getStringBounds("I", getGraphics()).getWidth();
214     if (mw < 1 || iw < 1)
215     {
216       fontName.setSelectedItem(lastSelected.getName());
217       fontStyle.setSelectedIndex(lastSelStyle);
218       fontSize.setSelectedItem("" + lastSelSize);
219       monospaced.setSelected(lastSelMono);
220       JOptionPane
221               .showInternalMessageDialog(
222                       this,
223                       "Font doesn't have letters defined\nso cannot be used\nwith alignment data.",
224                       "Invalid Font", JOptionPane.WARNING_MESSAGE);
225       return;
226     }
227     if (tp != null)
228     {
229       tp.setTreeFont(newFont);
230     }
231     else if (ap != null)
232     {
233       ap.av.setFont(newFont);
234       ap.fontChanged();
235     }
236
237     monospaced.setSelected(mw == iw);
238     // remember last selected
239     lastSelected = newFont;
240   }
241
242   /**
243    * DOCUMENT ME!
244    * 
245    * @param e
246    *          DOCUMENT ME!
247    */
248   protected void fontName_actionPerformed(ActionEvent e)
249   {
250     if (init)
251     {
252       return;
253     }
254
255     changeFont();
256   }
257
258   /**
259    * DOCUMENT ME!
260    * 
261    * @param e
262    *          DOCUMENT ME!
263    */
264   protected void fontSize_actionPerformed(ActionEvent e)
265   {
266     if (init)
267     {
268       return;
269     }
270
271     changeFont();
272   }
273
274   /**
275    * DOCUMENT ME!
276    * 
277    * @param e
278    *          DOCUMENT ME!
279    */
280   protected void fontStyle_actionPerformed(ActionEvent e)
281   {
282     if (init)
283     {
284       return;
285     }
286
287     changeFont();
288   }
289
290   /**
291    * DOCUMENT ME!
292    * 
293    * @param e
294    *          DOCUMENT ME!
295    */
296   public void defaultButton_actionPerformed(ActionEvent e)
297   {
298     Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
299     Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
300     Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
301     Cache.setProperty("ANTI_ALIAS",
302             Boolean.toString(smoothFont.isSelected()));
303   }
304 }