JAL-1690 show 'scale protein' in protein _and_ dna menus; default true
[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 a SplitFrame view. The selection is
95      * stored in the ViewStyle of both dna and protein Viewport
96      */
97     if (ap.av.getCodingComplement() != null)
98     {
99       scaleAsCdna.setVisible(true);
100       scaleAsCdna.setSelected(ap.av.isScaleProteinAsCdna());
101     }
102
103     if (tp != null)
104     {
105       Desktop.addInternalFrame(frame,
106               MessageManager.getString("action.change_font_tree_panel"),
107               400, 200, false);
108     }
109     else
110     {
111       Desktop.addInternalFrame(frame,
112               MessageManager.getString("action.change_font"), 380, 200,
113               false);
114     }
115
116     frame.setLayer(JLayeredPane.PALETTE_LAYER);
117
118     String[] fonts = java.awt.GraphicsEnvironment
119             .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
120
121     for (int i = 0; i < fonts.length; i++)
122     {
123       fontName.addItem(fonts[i]);
124     }
125
126     for (int i = 1; i < 51; i++)
127     {
128       fontSize.addItem(i);
129     }
130
131     fontStyle.addItem("plain");
132     fontStyle.addItem("bold");
133     fontStyle.addItem("italic");
134
135     fontName.setSelectedItem(oldFont.getName());
136     fontSize.setSelectedItem(oldFont.getSize());
137     fontStyle.setSelectedIndex(oldFont.getStyle());
138
139     FontMetrics fm = getGraphics().getFontMetrics(oldFont);
140     monospaced.setSelected(fm.getStringBounds("M", getGraphics())
141             .getWidth() == fm.getStringBounds("|", getGraphics())
142             .getWidth());
143
144     init = false;
145   }
146
147   public void smoothFont_actionPerformed(ActionEvent e)
148   {
149     ap.av.antiAlias = smoothFont.isSelected();
150     ap.getAnnotationPanel().image = null;
151     ap.paintAlignment(true);
152   }
153
154   /**
155    * DOCUMENT ME!
156    * 
157    * @param e
158    *          DOCUMENT ME!
159    */
160   protected void ok_actionPerformed(ActionEvent e)
161   {
162     try
163     {
164       frame.setClosed(true);
165     } catch (Exception ex)
166     {
167     }
168
169     if (ap != null)
170     {
171       if (ap.getOverviewPanel() != null)
172       {
173         ap.getOverviewPanel().updateOverviewImage();
174       }
175     }
176   }
177
178   /**
179    * DOCUMENT ME!
180    * 
181    * @param e
182    *          DOCUMENT ME!
183    */
184   protected void cancel_actionPerformed(ActionEvent e)
185   {
186     if (ap != null)
187     {
188       ap.av.setFont(oldFont, true);
189       ap.av.setScaleProteinAsCdna(oldProteinScale);
190       ap.paintAlignment(true);
191       if (scaleAsCdna.isEnabled())
192       {
193         ap.av.setScaleProteinAsCdna(oldProteinScale);
194         ap.av.getCodingComplement().setScaleProteinAsCdna(oldProteinScale);
195       }
196     }
197     else if (tp != null)
198     {
199       tp.setTreeFont(oldFont);
200     }
201     fontName.setSelectedItem(oldFont.getName());
202     fontSize.setSelectedItem(oldFont.getSize());
203     fontStyle.setSelectedIndex(oldFont.getStyle());
204
205     try
206     {
207       frame.setClosed(true);
208     } catch (Exception ex)
209     {
210     }
211   }
212
213   private Font lastSelected = null;
214
215   private int lastSelStyle = 0;
216
217   private int lastSelSize = 0;
218
219   private boolean lastSelMono = false;
220
221   /**
222    * DOCUMENT ME!
223    */
224   void changeFont()
225   {
226     if (lastSelected == null)
227     {
228       // initialise with original font
229       lastSelected = oldFont;
230       lastSelSize = oldFont.getSize();
231       lastSelStyle = oldFont.getStyle();
232       FontMetrics fm = getGraphics().getFontMetrics(oldFont);
233       double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
234               .getStringBounds("I", getGraphics()).getWidth();
235       lastSelMono = mw == iw;
236     }
237
238     Font newFont = new Font(fontName.getSelectedItem().toString(),
239             fontStyle.getSelectedIndex(),
240             (Integer) fontSize.getSelectedItem());
241     FontMetrics fm = getGraphics().getFontMetrics(newFont);
242     double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
243             .getStringBounds("I", getGraphics()).getWidth();
244     if (mw < 1 || iw < 1)
245     {
246       fontName.setSelectedItem(lastSelected.getName());
247       fontStyle.setSelectedIndex(lastSelStyle);
248       fontSize.setSelectedItem(lastSelSize);
249       monospaced.setSelected(lastSelMono);
250       JOptionPane
251               .showInternalMessageDialog(
252                       this,
253                       MessageManager.getString("label.font_doesnt_have_letters_defined"),
254                       MessageManager.getString("label.invalid_font"), JOptionPane.WARNING_MESSAGE);
255       return;
256     }
257     if (tp != null)
258     {
259       tp.setTreeFont(newFont);
260     }
261     else if (ap != null)
262     {
263       ap.av.setFont(newFont, true);
264       ap.fontChanged();
265     }
266
267     monospaced.setSelected(mw == iw);
268     // remember last selected
269     lastSelected = newFont;
270   }
271
272   /**
273    * DOCUMENT ME!
274    * 
275    * @param e
276    *          DOCUMENT ME!
277    */
278   protected void fontName_actionPerformed(ActionEvent e)
279   {
280     if (init)
281     {
282       return;
283     }
284
285     changeFont();
286   }
287
288   /**
289    * DOCUMENT ME!
290    * 
291    * @param e
292    *          DOCUMENT ME!
293    */
294   protected void fontSize_actionPerformed(ActionEvent e)
295   {
296     if (init)
297     {
298       return;
299     }
300
301     changeFont();
302   }
303
304   /**
305    * DOCUMENT ME!
306    * 
307    * @param e
308    *          DOCUMENT ME!
309    */
310   protected void fontStyle_actionPerformed(ActionEvent e)
311   {
312     if (init)
313     {
314       return;
315     }
316
317     changeFont();
318   }
319
320   /**
321    * Make selected settings the defaults by storing them (via Cache class) in
322    * the .jalview_properties file (the file is only written when Jalview exits)
323    * 
324    * @param e
325    */
326   public void defaultButton_actionPerformed(ActionEvent e)
327   {
328     Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
329     Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
330     Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
331     Cache.setProperty("ANTI_ALIAS",
332             Boolean.toString(smoothFont.isSelected()));
333     Cache.setProperty(Preferences.SCALE_PROTEIN_TO_CDNA,
334             Boolean.toString(scaleAsCdna.isSelected()));
335   }
336
337   /**
338    * Turn on/off scaling of protein characters to 3 times the width of cDNA
339    * characters
340    */
341   @Override
342   protected void scaleAsCdna_actionPerformed(ActionEvent e)
343   {
344     ap.av.setScaleProteinAsCdna(scaleAsCdna.isSelected());
345     ap.av.getCodingComplement().setScaleProteinAsCdna(
346             scaleAsCdna.isSelected());
347     final SplitFrame splitFrame = (SplitFrame) ap.alignFrame
348             .getSplitViewContainer();
349     splitFrame.adjustLayout();
350     splitFrame.repaint();
351     // ap.paintAlignment(true);
352     // TODO would like to repaint
353   }
354 }