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