5e1897d964404000350f12270d0dbcf3705bf0a8
[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 import jalview.util.MessageManager;
28
29 /**
30  * DOCUMENT ME!
31  * 
32  * @author $author$
33  * @version $Revision$
34  */
35 public class FontChooser extends GFontChooser
36 {
37   AlignmentPanel ap;
38
39   TreePanel tp;
40
41   Font oldFont;
42
43   boolean init = true;
44
45   JInternalFrame frame;
46
47   /**
48    * Creates a new FontChooser object.
49    * 
50    * @param ap
51    *          DOCUMENT ME!
52    */
53   public FontChooser(TreePanel tp)
54   {
55     this.tp = tp;
56     ap = tp.treeCanvas.ap;
57     oldFont = tp.getTreeFont();
58     defaultButton.setVisible(false);
59     smoothFont.setEnabled(false);
60     init();
61   }
62
63   /**
64    * Creates a new FontChooser object.
65    * 
66    * @param ap
67    *          DOCUMENT ME!
68    */
69   public FontChooser(AlignmentPanel ap)
70   {
71     oldFont = ap.av.getFont();
72     this.ap = ap;
73     init();
74   }
75
76   void init()
77   {
78     frame = new JInternalFrame();
79     frame.setContentPane(this);
80
81     smoothFont.setSelected(ap.av.antiAlias);
82
83     if (tp != null)
84     {
85       Desktop.addInternalFrame(frame, MessageManager.getString("action.change_font_tree_panel"), 340, 170,
86               false);
87     }
88     else
89     {
90       Desktop.addInternalFrame(frame, MessageManager.getString("action.change_font"), 340, 170, false);
91     }
92
93     frame.setLayer(JLayeredPane.PALETTE_LAYER);
94
95     String[] fonts = java.awt.GraphicsEnvironment
96             .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
97
98     for (int i = 0; i < fonts.length; i++)
99     {
100       fontName.addItem(fonts[i]);
101     }
102
103     for (int i = 1; i < 51; i++)
104     {
105       fontSize.addItem(i + "");
106     }
107
108     fontStyle.addItem("plain");
109     fontStyle.addItem("bold");
110     fontStyle.addItem("italic");
111
112     fontName.setSelectedItem(oldFont.getName());
113     fontSize.setSelectedItem(oldFont.getSize() + "");
114     fontStyle.setSelectedIndex(oldFont.getStyle());
115
116     FontMetrics fm = getGraphics().getFontMetrics(oldFont);
117     monospaced.setSelected(fm.getStringBounds("M", getGraphics())
118             .getWidth() == fm.getStringBounds("|", getGraphics())
119             .getWidth());
120
121     init = false;
122   }
123
124   public void smoothFont_actionPerformed(ActionEvent e)
125   {
126     ap.av.antiAlias = smoothFont.isSelected();
127     ap.annotationPanel.image = null;
128     ap.paintAlignment(true);
129   }
130
131   /**
132    * DOCUMENT ME!
133    * 
134    * @param e
135    *          DOCUMENT ME!
136    */
137   protected void ok_actionPerformed(ActionEvent e)
138   {
139     try
140     {
141       frame.setClosed(true);
142     } catch (Exception ex)
143     {
144     }
145
146     if (ap != null)
147     {
148       if (ap.getOverviewPanel() != null)
149       {
150         ap.getOverviewPanel().updateOverviewImage();
151       }
152     }
153   }
154
155   /**
156    * DOCUMENT ME!
157    * 
158    * @param e
159    *          DOCUMENT ME!
160    */
161   protected void cancel_actionPerformed(ActionEvent e)
162   {
163     if (ap != null)
164     {
165       ap.av.setFont(oldFont);
166       ap.paintAlignment(true);
167     }
168     else if (tp != null)
169     {
170       tp.setTreeFont(oldFont);
171     }
172     fontName.setSelectedItem(oldFont.getName());
173     fontSize.setSelectedItem(oldFont.getSize() + "");
174     fontStyle.setSelectedIndex(oldFont.getStyle());
175
176     try
177     {
178       frame.setClosed(true);
179     } catch (Exception ex)
180     {
181     }
182   }
183
184   private Font lastSelected = null;
185
186   private int lastSelStyle = 0;
187
188   private int lastSelSize = 0;
189
190   private boolean lastSelMono = false;
191
192   /**
193    * DOCUMENT ME!
194    */
195   void changeFont()
196   {
197     if (lastSelected == null)
198     {
199       // initialise with original font
200       lastSelected = oldFont;
201       lastSelSize = oldFont.getSize();
202       lastSelStyle = oldFont.getStyle();
203       FontMetrics fm = getGraphics().getFontMetrics(oldFont);
204       double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
205               .getStringBounds("I", getGraphics()).getWidth();
206       lastSelMono = mw == iw;
207     }
208
209     Font newFont = new Font(fontName.getSelectedItem().toString(),
210             fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
211                     .getSelectedItem().toString()));
212     FontMetrics fm = getGraphics().getFontMetrics(newFont);
213     double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
214             .getStringBounds("I", getGraphics()).getWidth();
215     if (mw < 1 || iw < 1)
216     {
217       fontName.setSelectedItem(lastSelected.getName());
218       fontStyle.setSelectedIndex(lastSelStyle);
219       fontSize.setSelectedItem("" + lastSelSize);
220       monospaced.setSelected(lastSelMono);
221       JOptionPane
222               .showInternalMessageDialog(
223                       this,
224                       "Font doesn't have letters defined\nso cannot be used\nwith alignment data.",
225                       "Invalid Font", JOptionPane.WARNING_MESSAGE);
226       return;
227     }
228     if (tp != null)
229     {
230       tp.setTreeFont(newFont);
231     }
232     else if (ap != null)
233     {
234       ap.av.setFont(newFont);
235       ap.fontChanged();
236     }
237
238     monospaced.setSelected(mw == iw);
239     // remember last selected
240     lastSelected = newFont;
241   }
242
243   /**
244    * DOCUMENT ME!
245    * 
246    * @param e
247    *          DOCUMENT ME!
248    */
249   protected void fontName_actionPerformed(ActionEvent e)
250   {
251     if (init)
252     {
253       return;
254     }
255
256     changeFont();
257   }
258
259   /**
260    * DOCUMENT ME!
261    * 
262    * @param e
263    *          DOCUMENT ME!
264    */
265   protected void fontSize_actionPerformed(ActionEvent e)
266   {
267     if (init)
268     {
269       return;
270     }
271
272     changeFont();
273   }
274
275   /**
276    * DOCUMENT ME!
277    * 
278    * @param e
279    *          DOCUMENT ME!
280    */
281   protected void fontStyle_actionPerformed(ActionEvent e)
282   {
283     if (init)
284     {
285       return;
286     }
287
288     changeFont();
289   }
290
291   /**
292    * DOCUMENT ME!
293    * 
294    * @param e
295    *          DOCUMENT ME!
296    */
297   public void defaultButton_actionPerformed(ActionEvent e)
298   {
299     Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
300     Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
301     Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
302     Cache.setProperty("ANTI_ALIAS",
303             Boolean.toString(smoothFont.isSelected()));
304   }
305 }