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