JAL-1690 'scale protein as cDNA' options in Preferences, FontChooser
[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 java.awt.Font;
24 import java.awt.FontMetrics;
25 import java.awt.event.ActionEvent;
26
27 import javax.swing.JInternalFrame;
28 import javax.swing.JLayeredPane;
29 import javax.swing.JOptionPane;
30
31 import jalview.bin.Cache;
32 import jalview.jbgui.GFontChooser;
33 import jalview.util.MessageManager;
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 oldProteinScale;
50
51   boolean init = true;
52
53   JInternalFrame frame;
54
55   /**
56    * Creates a new FontChooser object.
57    * 
58    * @param ap
59    *          DOCUMENT ME!
60    */
61   public FontChooser(TreePanel tp)
62   {
63     this.tp = tp;
64     ap = tp.treeCanvas.ap;
65     oldFont = tp.getTreeFont();
66     defaultButton.setVisible(false);
67     smoothFont.setEnabled(false);
68     init();
69   }
70
71   /**
72    * Creates a new FontChooser object.
73    * 
74    * @param ap
75    *          DOCUMENT ME!
76    */
77   public FontChooser(AlignmentPanel ap)
78   {
79     oldFont = ap.av.getFont();
80     oldProteinScale = ap.av.isScaleProteinAsCdna();
81
82     this.ap = ap;
83     init();
84   }
85
86   void init()
87   {
88     frame = new JInternalFrame();
89     frame.setContentPane(this);
90
91     smoothFont.setSelected(ap.av.antiAlias);
92
93     /*
94      * Enable 'scale protein as cDNA' in protein half of a SplitFrame view. The
95      * selection is stored in the ViewStyle of the Viewport
96      */
97     if (ap.av.getCodingComplement() != null
98             && !ap.av.getAlignment().isNucleotide())
99     {
100       scaleAsCdna.setVisible(true);
101       scaleAsCdna.setSelected(ap.av.isScaleProteinAsCdna());
102     }
103
104     if (tp != null)
105     {
106       Desktop.addInternalFrame(frame,
107               MessageManager.getString("action.change_font_tree_panel"),
108               400, 200, false);
109     }
110     else
111     {
112       Desktop.addInternalFrame(frame,
113               MessageManager.getString("action.change_font"), 380, 200,
114               false);
115     }
116
117     frame.setLayer(JLayeredPane.PALETTE_LAYER);
118
119     String[] fonts = java.awt.GraphicsEnvironment
120             .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
121
122     for (int i = 0; i < fonts.length; i++)
123     {
124       fontName.addItem(fonts[i]);
125     }
126
127     for (int i = 1; i < 51; i++)
128     {
129       fontSize.addItem(i);
130     }
131
132     fontStyle.addItem("plain");
133     fontStyle.addItem("bold");
134     fontStyle.addItem("italic");
135
136     fontName.setSelectedItem(oldFont.getName());
137     fontSize.setSelectedItem(oldFont.getSize());
138     fontStyle.setSelectedIndex(oldFont.getStyle());
139
140     FontMetrics fm = getGraphics().getFontMetrics(oldFont);
141     monospaced.setSelected(fm.getStringBounds("M", getGraphics())
142             .getWidth() == fm.getStringBounds("|", getGraphics())
143             .getWidth());
144
145     init = false;
146   }
147
148   public void smoothFont_actionPerformed(ActionEvent e)
149   {
150     ap.av.antiAlias = smoothFont.isSelected();
151     ap.getAnnotationPanel().image = null;
152     ap.paintAlignment(true);
153   }
154
155   /**
156    * DOCUMENT ME!
157    * 
158    * @param e
159    *          DOCUMENT ME!
160    */
161   protected void ok_actionPerformed(ActionEvent e)
162   {
163     try
164     {
165       frame.setClosed(true);
166     } catch (Exception ex)
167     {
168     }
169
170     if (ap != null)
171     {
172       if (ap.getOverviewPanel() != null)
173       {
174         ap.getOverviewPanel().updateOverviewImage();
175       }
176     }
177   }
178
179   /**
180    * DOCUMENT ME!
181    * 
182    * @param e
183    *          DOCUMENT ME!
184    */
185   protected void cancel_actionPerformed(ActionEvent e)
186   {
187     if (ap != null)
188     {
189       ap.av.setFont(oldFont, true);
190       ap.av.setScaleProteinAsCdna(oldProteinScale);
191       ap.paintAlignment(true);
192     }
193     else if (tp != null)
194     {
195       tp.setTreeFont(oldFont);
196     }
197     fontName.setSelectedItem(oldFont.getName());
198     fontSize.setSelectedItem(oldFont.getSize());
199     fontStyle.setSelectedIndex(oldFont.getStyle());
200
201     try
202     {
203       frame.setClosed(true);
204     } catch (Exception ex)
205     {
206     }
207   }
208
209   private Font lastSelected = null;
210
211   private int lastSelStyle = 0;
212
213   private int lastSelSize = 0;
214
215   private boolean lastSelMono = false;
216
217   /**
218    * DOCUMENT ME!
219    */
220   void changeFont()
221   {
222     if (lastSelected == null)
223     {
224       // initialise with original font
225       lastSelected = oldFont;
226       lastSelSize = oldFont.getSize();
227       lastSelStyle = oldFont.getStyle();
228       FontMetrics fm = getGraphics().getFontMetrics(oldFont);
229       double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
230               .getStringBounds("I", getGraphics()).getWidth();
231       lastSelMono = mw == iw;
232     }
233
234     Font newFont = new Font(fontName.getSelectedItem().toString(),
235             fontStyle.getSelectedIndex(),
236             (Integer) fontSize.getSelectedItem());
237     FontMetrics fm = getGraphics().getFontMetrics(newFont);
238     double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
239             .getStringBounds("I", getGraphics()).getWidth();
240     if (mw < 1 || iw < 1)
241     {
242       fontName.setSelectedItem(lastSelected.getName());
243       fontStyle.setSelectedIndex(lastSelStyle);
244       fontSize.setSelectedItem(lastSelSize);
245       monospaced.setSelected(lastSelMono);
246       JOptionPane
247               .showInternalMessageDialog(
248                       this,
249                       MessageManager.getString("label.font_doesnt_have_letters_defined"),
250                       MessageManager.getString("label.invalid_font"), JOptionPane.WARNING_MESSAGE);
251       return;
252     }
253     if (tp != null)
254     {
255       tp.setTreeFont(newFont);
256     }
257     else if (ap != null)
258     {
259       ap.av.setFont(newFont, true);
260       ap.fontChanged();
261     }
262
263     monospaced.setSelected(mw == iw);
264     // remember last selected
265     lastSelected = newFont;
266   }
267
268   /**
269    * DOCUMENT ME!
270    * 
271    * @param e
272    *          DOCUMENT ME!
273    */
274   protected void fontName_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 fontSize_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   protected void fontStyle_actionPerformed(ActionEvent e)
307   {
308     if (init)
309     {
310       return;
311     }
312
313     changeFont();
314   }
315
316   /**
317    * DOCUMENT ME!
318    * 
319    * @param e
320    *          DOCUMENT ME!
321    */
322   public void defaultButton_actionPerformed(ActionEvent e)
323   {
324     Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
325     Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
326     Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
327     Cache.setProperty("ANTI_ALIAS",
328             Boolean.toString(smoothFont.isSelected()));
329   }
330
331   /**
332    * Turn on/off scaling of protein characters to 3 times the width of cDNA
333    * characters
334    */
335   @Override
336   protected void scaleAsCdna_actionPerformed(ActionEvent e)
337   {
338     ap.av.setScaleProteinAsCdna(scaleAsCdna.isSelected());
339     ((SplitFrame) ap.alignFrame.getSplitViewContainer()).adjustLayout();
340     ap.paintAlignment(true);
341   }
342 }