JAL-1620 version bump and release notes
[jalview.git] / src / jalview / gui / FontChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
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
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.gui;
22
23 import java.awt.*;
24 import java.awt.event.*;
25 import javax.swing.*;
26
27 import jalview.bin.*;
28 import jalview.jbgui.*;
29 import jalview.util.MessageManager;
30
31 /**
32  * DOCUMENT ME!
33  * 
34  * @author $author$
35  * @version $Revision$
36  */
37 public class FontChooser extends GFontChooser
38 {
39   AlignmentPanel ap;
40
41   TreePanel tp;
42
43   Font oldFont;
44
45   boolean init = true;
46
47   JInternalFrame frame;
48
49   /**
50    * Creates a new FontChooser object.
51    * 
52    * @param ap
53    *          DOCUMENT ME!
54    */
55   public FontChooser(TreePanel tp)
56   {
57     this.tp = tp;
58     ap = tp.treeCanvas.ap;
59     oldFont = tp.getTreeFont();
60     defaultButton.setVisible(false);
61     smoothFont.setEnabled(false);
62     init();
63   }
64
65   /**
66    * Creates a new FontChooser object.
67    * 
68    * @param ap
69    *          DOCUMENT ME!
70    */
71   public FontChooser(AlignmentPanel ap)
72   {
73     oldFont = ap.av.getFont();
74     this.ap = ap;
75     init();
76   }
77
78   void init()
79   {
80     frame = new JInternalFrame();
81     frame.setContentPane(this);
82
83     smoothFont.setSelected(ap.av.antiAlias);
84
85     if (tp != null)
86     {
87       Desktop.addInternalFrame(frame,
88               MessageManager.getString("action.change_font_tree_panel"),
89               340, 170, false);
90     }
91     else
92     {
93       Desktop.addInternalFrame(frame,
94               MessageManager.getString("action.change_font"), 340, 170,
95               false);
96     }
97
98     frame.setLayer(JLayeredPane.PALETTE_LAYER);
99
100     String[] fonts = java.awt.GraphicsEnvironment
101             .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
102
103     for (int i = 0; i < fonts.length; i++)
104     {
105       fontName.addItem(fonts[i]);
106     }
107
108     for (int i = 1; i < 51; i++)
109     {
110       fontSize.addItem(i + "");
111     }
112
113     fontStyle.addItem("plain");
114     fontStyle.addItem("bold");
115     fontStyle.addItem("italic");
116
117     fontName.setSelectedItem(oldFont.getName());
118     fontSize.setSelectedItem(oldFont.getSize() + "");
119     fontStyle.setSelectedIndex(oldFont.getStyle());
120
121     FontMetrics fm = getGraphics().getFontMetrics(oldFont);
122     monospaced.setSelected(fm.getStringBounds("M", getGraphics())
123             .getWidth() == fm.getStringBounds("|", getGraphics())
124             .getWidth());
125
126     init = false;
127   }
128
129   public void smoothFont_actionPerformed(ActionEvent e)
130   {
131     ap.av.antiAlias = smoothFont.isSelected();
132     ap.annotationPanel.image = null;
133     ap.paintAlignment(true);
134   }
135
136   /**
137    * DOCUMENT ME!
138    * 
139    * @param e
140    *          DOCUMENT ME!
141    */
142   protected void ok_actionPerformed(ActionEvent e)
143   {
144     try
145     {
146       frame.setClosed(true);
147     } catch (Exception ex)
148     {
149     }
150
151     if (ap != null)
152     {
153       if (ap.getOverviewPanel() != null)
154       {
155         ap.getOverviewPanel().updateOverviewImage();
156       }
157     }
158   }
159
160   /**
161    * DOCUMENT ME!
162    * 
163    * @param e
164    *          DOCUMENT ME!
165    */
166   protected void cancel_actionPerformed(ActionEvent e)
167   {
168     if (ap != null)
169     {
170       ap.av.setFont(oldFont);
171       ap.paintAlignment(true);
172     }
173     else if (tp != null)
174     {
175       tp.setTreeFont(oldFont);
176     }
177     fontName.setSelectedItem(oldFont.getName());
178     fontSize.setSelectedItem(oldFont.getSize() + "");
179     fontStyle.setSelectedIndex(oldFont.getStyle());
180
181     try
182     {
183       frame.setClosed(true);
184     } catch (Exception ex)
185     {
186     }
187   }
188
189   private Font lastSelected = null;
190
191   private int lastSelStyle = 0;
192
193   private int lastSelSize = 0;
194
195   private boolean lastSelMono = false;
196
197   /**
198    * DOCUMENT ME!
199    */
200   void changeFont()
201   {
202     if (lastSelected == null)
203     {
204       // initialise with original font
205       lastSelected = oldFont;
206       lastSelSize = oldFont.getSize();
207       lastSelStyle = oldFont.getStyle();
208       FontMetrics fm = getGraphics().getFontMetrics(oldFont);
209       double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
210               .getStringBounds("I", getGraphics()).getWidth();
211       lastSelMono = mw == iw;
212     }
213
214     Font newFont = new Font(fontName.getSelectedItem().toString(),
215             fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
216                     .getSelectedItem().toString()));
217     FontMetrics fm = getGraphics().getFontMetrics(newFont);
218     double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
219             .getStringBounds("I", getGraphics()).getWidth();
220     if (mw < 1 || iw < 1)
221     {
222       fontName.setSelectedItem(lastSelected.getName());
223       fontStyle.setSelectedIndex(lastSelStyle);
224       fontSize.setSelectedItem("" + lastSelSize);
225       monospaced.setSelected(lastSelMono);
226       JOptionPane
227               .showInternalMessageDialog(
228                       this,
229                       MessageManager.getString("label.font_doesnt_have_letters_defined"),
230                       MessageManager.getString("label.invalid_font"), JOptionPane.WARNING_MESSAGE);
231       return;
232     }
233     if (tp != null)
234     {
235       tp.setTreeFont(newFont);
236     }
237     else if (ap != null)
238     {
239       ap.av.setFont(newFont);
240       ap.fontChanged();
241     }
242
243     monospaced.setSelected(mw == iw);
244     // remember last selected
245     lastSelected = newFont;
246   }
247
248   /**
249    * DOCUMENT ME!
250    * 
251    * @param e
252    *          DOCUMENT ME!
253    */
254   protected void fontName_actionPerformed(ActionEvent e)
255   {
256     if (init)
257     {
258       return;
259     }
260
261     changeFont();
262   }
263
264   /**
265    * DOCUMENT ME!
266    * 
267    * @param e
268    *          DOCUMENT ME!
269    */
270   protected void fontSize_actionPerformed(ActionEvent e)
271   {
272     if (init)
273     {
274       return;
275     }
276
277     changeFont();
278   }
279
280   /**
281    * DOCUMENT ME!
282    * 
283    * @param e
284    *          DOCUMENT ME!
285    */
286   protected void fontStyle_actionPerformed(ActionEvent e)
287   {
288     if (init)
289     {
290       return;
291     }
292
293     changeFont();
294   }
295
296   /**
297    * DOCUMENT ME!
298    * 
299    * @param e
300    *          DOCUMENT ME!
301    */
302   public void defaultButton_actionPerformed(ActionEvent e)
303   {
304     Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
305     Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
306     Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
307     Cache.setProperty("ANTI_ALIAS",
308             Boolean.toString(smoothFont.isSelected()));
309   }
310 }