JAL-1517 fix copyright for 2.8.2
[jalview.git] / src / jalview / gui / FontChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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, MessageManager.getString("action.change_font_tree_panel"), 340, 170,
88               false);
89     }
90     else
91     {
92       Desktop.addInternalFrame(frame, MessageManager.getString("action.change_font"), 340, 170, false);
93     }
94
95     frame.setLayer(JLayeredPane.PALETTE_LAYER);
96
97     String[] fonts = java.awt.GraphicsEnvironment
98             .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
99
100     for (int i = 0; i < fonts.length; i++)
101     {
102       fontName.addItem(fonts[i]);
103     }
104
105     for (int i = 1; i < 51; i++)
106     {
107       fontSize.addItem(i + "");
108     }
109
110     fontStyle.addItem("plain");
111     fontStyle.addItem("bold");
112     fontStyle.addItem("italic");
113
114     fontName.setSelectedItem(oldFont.getName());
115     fontSize.setSelectedItem(oldFont.getSize() + "");
116     fontStyle.setSelectedIndex(oldFont.getStyle());
117
118     FontMetrics fm = getGraphics().getFontMetrics(oldFont);
119     monospaced.setSelected(fm.getStringBounds("M", getGraphics())
120             .getWidth() == fm.getStringBounds("|", getGraphics())
121             .getWidth());
122
123     init = false;
124   }
125
126   public void smoothFont_actionPerformed(ActionEvent e)
127   {
128     ap.av.antiAlias = smoothFont.isSelected();
129     ap.annotationPanel.image = null;
130     ap.paintAlignment(true);
131   }
132
133   /**
134    * DOCUMENT ME!
135    * 
136    * @param e
137    *          DOCUMENT ME!
138    */
139   protected void ok_actionPerformed(ActionEvent e)
140   {
141     try
142     {
143       frame.setClosed(true);
144     } catch (Exception ex)
145     {
146     }
147
148     if (ap != null)
149     {
150       if (ap.getOverviewPanel() != null)
151       {
152         ap.getOverviewPanel().updateOverviewImage();
153       }
154     }
155   }
156
157   /**
158    * DOCUMENT ME!
159    * 
160    * @param e
161    *          DOCUMENT ME!
162    */
163   protected void cancel_actionPerformed(ActionEvent e)
164   {
165     if (ap != null)
166     {
167       ap.av.setFont(oldFont);
168       ap.paintAlignment(true);
169     }
170     else if (tp != null)
171     {
172       tp.setTreeFont(oldFont);
173     }
174     fontName.setSelectedItem(oldFont.getName());
175     fontSize.setSelectedItem(oldFont.getSize() + "");
176     fontStyle.setSelectedIndex(oldFont.getStyle());
177
178     try
179     {
180       frame.setClosed(true);
181     } catch (Exception ex)
182     {
183     }
184   }
185
186   private Font lastSelected = null;
187
188   private int lastSelStyle = 0;
189
190   private int lastSelSize = 0;
191
192   private boolean lastSelMono = false;
193
194   /**
195    * DOCUMENT ME!
196    */
197   void changeFont()
198   {
199     if (lastSelected == null)
200     {
201       // initialise with original font
202       lastSelected = oldFont;
203       lastSelSize = oldFont.getSize();
204       lastSelStyle = oldFont.getStyle();
205       FontMetrics fm = getGraphics().getFontMetrics(oldFont);
206       double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
207               .getStringBounds("I", getGraphics()).getWidth();
208       lastSelMono = mw == iw;
209     }
210
211     Font newFont = new Font(fontName.getSelectedItem().toString(),
212             fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
213                     .getSelectedItem().toString()));
214     FontMetrics fm = getGraphics().getFontMetrics(newFont);
215     double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
216             .getStringBounds("I", getGraphics()).getWidth();
217     if (mw < 1 || iw < 1)
218     {
219       fontName.setSelectedItem(lastSelected.getName());
220       fontStyle.setSelectedIndex(lastSelStyle);
221       fontSize.setSelectedItem("" + lastSelSize);
222       monospaced.setSelected(lastSelMono);
223       JOptionPane
224               .showInternalMessageDialog(
225                       this,
226                       "Font doesn't have letters defined\nso cannot be used\nwith alignment data.",
227                       "Invalid Font", JOptionPane.WARNING_MESSAGE);
228       return;
229     }
230     if (tp != null)
231     {
232       tp.setTreeFont(newFont);
233     }
234     else if (ap != null)
235     {
236       ap.av.setFont(newFont);
237       ap.fontChanged();
238     }
239
240     monospaced.setSelected(mw == iw);
241     // remember last selected
242     lastSelected = newFont;
243   }
244
245   /**
246    * DOCUMENT ME!
247    * 
248    * @param e
249    *          DOCUMENT ME!
250    */
251   protected void fontName_actionPerformed(ActionEvent e)
252   {
253     if (init)
254     {
255       return;
256     }
257
258     changeFont();
259   }
260
261   /**
262    * DOCUMENT ME!
263    * 
264    * @param e
265    *          DOCUMENT ME!
266    */
267   protected void fontSize_actionPerformed(ActionEvent e)
268   {
269     if (init)
270     {
271       return;
272     }
273
274     changeFont();
275   }
276
277   /**
278    * DOCUMENT ME!
279    * 
280    * @param e
281    *          DOCUMENT ME!
282    */
283   protected void fontStyle_actionPerformed(ActionEvent e)
284   {
285     if (init)
286     {
287       return;
288     }
289
290     changeFont();
291   }
292
293   /**
294    * DOCUMENT ME!
295    * 
296    * @param e
297    *          DOCUMENT ME!
298    */
299   public void defaultButton_actionPerformed(ActionEvent e)
300   {
301     Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
302     Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
303     Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
304     Cache.setProperty("ANTI_ALIAS",
305             Boolean.toString(smoothFont.isSelected()));
306   }
307 }