Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.gui

File FontChooser.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
56% of files have more coverage

Code metrics

50
134
14
1
458
293
44
0.33
9.57
14
3.14

Classes

Class Line # Actions
FontChooser 40 134 44
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

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.geom.Rectangle2D;
30   
31    import javax.swing.JInternalFrame;
32    import javax.swing.JLayeredPane;
33   
34    /**
35    * DOCUMENT ME!
36    *
37    * @author $author$
38    * @version $Revision$
39    */
 
40    public class FontChooser extends GFontChooser
41    {
42    AlignmentPanel ap;
43   
44    TreePanel tp;
45   
46    /*
47    * The font on opening the dialog (to be restored on Cancel)
48    */
49    Font oldFont;
50   
51    /*
52    * The font on opening the dialog (to be restored on Cancel)
53    * on the other half of a split frame (if applicable)
54    */
55    Font oldComplementFont;
56   
57    /*
58    * the state of 'scale protein as cDNA' on opening the dialog
59    */
60    boolean oldProteinScale;
61   
62    /*
63    * the state of 'same font for protein and cDNA' on opening the dialog
64    */
65    boolean oldMirrorFont;
66   
67    boolean init = true;
68   
69    JInternalFrame frame;
70   
71    /*
72    * The last font settings selected in the dialog
73    */
74    private Font lastSelected = null;
75   
76    private boolean lastSelMono = false;
77   
78    private boolean oldSmoothFont;
79   
80    private boolean oldComplementSmooth;
81   
82    /**
83    * Creates a new FontChooser for a tree panel
84    *
85    * @param treePanel
86    */
 
87  0 toggle public FontChooser(TreePanel treePanel)
88    {
89  0 this.tp = treePanel;
90  0 ap = treePanel.getTreeCanvas().getAssociatedPanel();
91  0 oldFont = treePanel.getTreeFont();
92  0 defaultButton.setVisible(false);
93  0 smoothFont.setEnabled(false);
94  0 init();
95    }
96   
97    /**
98    * Creates a new FontChooser for an alignment panel
99    *
100    * @param alignPanel
101    */
 
102  0 toggle public FontChooser(AlignmentPanel alignPanel)
103    {
104  0 oldFont = alignPanel.av.getFont();
105  0 oldProteinScale = alignPanel.av.isScaleProteinAsCdna();
106  0 oldMirrorFont = alignPanel.av.isProteinFontAsCdna();
107  0 oldSmoothFont = alignPanel.av.antiAlias;
108  0 this.ap = alignPanel;
109  0 init();
110    }
111   
 
112  0 toggle void init()
113    {
114  0 frame = new JInternalFrame();
115  0 frame.setContentPane(this);
116   
117  0 smoothFont.setSelected(ap.av.antiAlias);
118   
119    /*
120    * Enable 'scale protein as cDNA' in a SplitFrame view. The selection is
121    * stored in the ViewStyle of both dna and protein Viewport. Also enable
122    * checkbox for copy font changes to other half of split frame.
123    */
124  0 boolean inSplitFrame = ap.av.getCodingComplement() != null;
125  0 if (inSplitFrame)
126    {
127  0 oldComplementFont = ((AlignViewport) ap.av.getCodingComplement())
128    .getFont();
129  0 oldComplementSmooth = ((AlignViewport) ap.av
130    .getCodingComplement()).antiAlias;
131  0 scaleAsCdna.setVisible(true);
132  0 scaleAsCdna.setSelected(ap.av.isScaleProteinAsCdna());
133  0 fontAsCdna.setVisible(true);
134  0 fontAsCdna.setSelected(ap.av.isProteinFontAsCdna());
135    }
136   
137  0 if (isTreeFont())
138    {
139  0 Desktop.addInternalFrame(frame,
140    MessageManager.getString("action.change_font_tree_panel"),
141    400, 200, false);
142    }
143    else
144    {
145  0 Desktop.addInternalFrame(frame,
146    MessageManager.getString("action.change_font"), 380, 220,
147    false);
148    }
149   
150  0 frame.setLayer(JLayeredPane.PALETTE_LAYER);
151   
152  0 String[] fonts = java.awt.GraphicsEnvironment
153    .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
154   
155  0 for (int i = 0; i < fonts.length; i++)
156    {
157  0 fontName.addItem(fonts[i]);
158    }
159   
160  0 for (int i = 1; i < 51; i++)
161    {
162  0 fontSize.addItem(i);
163    }
164   
165  0 fontStyle.addItem("plain");
166  0 fontStyle.addItem("bold");
167  0 fontStyle.addItem("italic");
168   
169  0 fontName.setSelectedItem(oldFont.getName());
170  0 fontSize.setSelectedItem(oldFont.getSize());
171  0 fontStyle.setSelectedIndex(oldFont.getStyle());
172   
173  0 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
174  0 monospaced.setSelected(
175    fm.getStringBounds("M", getGraphics()).getWidth() == fm
176    .getStringBounds("|", getGraphics()).getWidth());
177   
178  0 init = false;
179    }
180   
 
181  0 toggle @Override
182    protected void smoothFont_actionPerformed()
183    {
184  0 ap.av.antiAlias = smoothFont.isSelected();
185  0 ap.getAnnotationPanel().image = null;
186  0 ap.paintAlignment(true, false);
187  0 if (ap.av.getCodingComplement() != null && ap.av.isProteinFontAsCdna())
188    {
189  0 ((AlignViewport) ap.av
190    .getCodingComplement()).antiAlias = ap.av.antiAlias;
191  0 SplitFrame sv = (SplitFrame) ap.alignFrame.getSplitViewContainer();
192  0 sv.adjustLayout();
193  0 sv.repaint();
194    }
195   
196    }
197   
198    /**
199    * DOCUMENT ME!
200    *
201    * @param e
202    * DOCUMENT ME!
203    */
 
204  0 toggle @Override
205    protected void ok_actionPerformed()
206    {
207  0 try
208    {
209  0 frame.setClosed(true);
210    } catch (Exception ex)
211    {
212    }
213   
214  0 if (ap != null)
215    {
216  0 if (ap.getOverviewPanel() != null)
217    {
218  0 ap.getOverviewPanel().updateOverviewImage();
219    }
220    }
221    }
222   
223    /**
224    * DOCUMENT ME!
225    *
226    * @param e
227    * DOCUMENT ME!
228    */
 
229  0 toggle @Override
230    protected void cancel_actionPerformed()
231    {
232  0 if (isTreeFont())
233    {
234  0 tp.setTreeFont(oldFont);
235    }
236  0 else if (ap != null)
237    {
238  0 ap.av.setFont(oldFont, true);
239  0 ap.av.setScaleProteinAsCdna(oldProteinScale);
240  0 ap.av.setProteinFontAsCdna(oldMirrorFont);
241  0 ap.av.antiAlias = oldSmoothFont;
242  0 ap.fontChanged();
243   
244  0 if (scaleAsCdna.isVisible() && scaleAsCdna.isEnabled())
245    {
246  0 ap.av.getCodingComplement().setScaleProteinAsCdna(oldProteinScale);
247  0 ap.av.getCodingComplement().setProteinFontAsCdna(oldMirrorFont);
248  0 ((AlignViewport) ap.av
249    .getCodingComplement()).antiAlias = oldComplementSmooth;
250  0 ap.av.getCodingComplement().setFont(oldComplementFont, true);
251  0 SplitFrame splitFrame = (SplitFrame) ap.alignFrame
252    .getSplitViewContainer();
253  0 splitFrame.adjustLayout();
254  0 splitFrame.repaint();
255    }
256    }
257   
258  0 try
259    {
260  0 frame.setClosed(true);
261    } catch (Exception ex)
262    {
263    }
264    }
265   
 
266  0 toggle private boolean isTreeFont()
267    {
268  0 return tp != null;
269    }
270   
271    /**
272    * DOCUMENT ME!
273    */
 
274  0 toggle void changeFont()
275    {
276  0 if (lastSelected == null)
277    {
278    // initialise with original font
279  0 lastSelected = oldFont;
280  0 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
281  0 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
282  0 double iw = fm.getStringBounds("I", getGraphics()).getWidth();
283  0 lastSelMono = (mw == iw); // == on double - flaky?
284    }
285   
286  0 Font newFont = new Font(fontName.getSelectedItem().toString(),
287    fontStyle.getSelectedIndex(),
288    (Integer) fontSize.getSelectedItem());
289  0 FontMetrics fm = getGraphics().getFontMetrics(newFont);
290  0 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
291  0 final Rectangle2D iBounds = fm.getStringBounds("I", getGraphics());
292  0 double iw = iBounds.getWidth();
293  0 if (mw < 1 || iw < 1)
294    {
295  0 String message = iBounds.getHeight() < 1
296    ? MessageManager
297    .getString("label.font_doesnt_have_letters_defined")
298    : MessageManager.getString("label.font_too_small");
299  0 JvOptionPane.showInternalMessageDialog(this, message,
300    MessageManager.getString("label.invalid_font"),
301    JvOptionPane.WARNING_MESSAGE);
302    /*
303    * Restore the changed value - note this will reinvoke this method via the
304    * ActionListener, but now validation should pass
305    */
306  0 if (lastSelected.getSize() != (Integer) fontSize.getSelectedItem()) // autoboxing
307    {
308  0 fontSize.setSelectedItem(lastSelected.getSize());
309    }
310  0 if (!lastSelected.getName()
311    .equals(fontName.getSelectedItem().toString()))
312    {
313  0 fontName.setSelectedItem(lastSelected.getName());
314    }
315  0 if (lastSelected.getStyle() != fontStyle.getSelectedIndex())
316    {
317  0 fontStyle.setSelectedIndex(lastSelected.getStyle());
318    }
319  0 if (lastSelMono != monospaced.isSelected())
320    {
321  0 monospaced.setSelected(lastSelMono);
322    }
323  0 return;
324    }
325  0 if (isTreeFont())
326    {
327  0 tp.setTreeFont(newFont);
328    }
329  0 else if (ap != null)
330    {
331  0 ap.av.setFont(newFont, true);
332  0 ap.fontChanged();
333   
334    /*
335    * adjust other half of split frame if present, whether or not same font or
336    * scale to cDNA is selected, because a font change may affect character
337    * width, and this is kept the same in both panels
338    */
339  0 if (fontAsCdna.isVisible())
340    {
341  0 if (fontAsCdna.isSelected())
342    {
343  0 ap.av.getCodingComplement().setFont(newFont, true);
344    }
345   
346  0 SplitFrame splitFrame = (SplitFrame) ap.alignFrame
347    .getSplitViewContainer();
348  0 splitFrame.adjustLayout();
349  0 splitFrame.repaint();
350    }
351    }
352   
353  0 monospaced.setSelected(mw == iw);
354   
355    /*
356    * Remember latest valid selection, so it can be restored if followed by an
357    * invalid one
358    */
359  0 lastSelected = newFont;
360    }
361   
362    /**
363    * Updates on change of selected font name
364    */
 
365  0 toggle @Override
366    protected void fontName_actionPerformed()
367    {
368  0 if (init)
369    {
370  0 return;
371    }
372   
373  0 changeFont();
374    }
375   
376    /**
377    * Updates on change of selected font size
378    */
 
379  0 toggle @Override
380    protected void fontSize_actionPerformed()
381    {
382  0 if (init)
383    {
384  0 return;
385    }
386   
387  0 changeFont();
388    }
389   
390    /**
391    * Updates on change of selected font style
392    */
 
393  0 toggle @Override
394    protected void fontStyle_actionPerformed()
395    {
396  0 if (init)
397    {
398  0 return;
399    }
400   
401  0 changeFont();
402    }
403   
404    /**
405    * Make selected settings the defaults by storing them (via Cache class) in
406    * the .jalview_properties file (the file is only written when Jalview exits)
407    */
 
408  0 toggle @Override
409    public void defaultButton_actionPerformed()
410    {
411  0 Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
412  0 Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
413  0 Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
414  0 Cache.setProperty("ANTI_ALIAS",
415    Boolean.toString(smoothFont.isSelected()));
416  0 Cache.setProperty(Preferences.SCALE_PROTEIN_TO_CDNA,
417    Boolean.toString(scaleAsCdna.isSelected()));
418    }
419   
420    /**
421    * Turn on/off scaling of protein characters to 3 times the width of cDNA
422    * characters
423    */
 
424  0 toggle @Override
425    protected void scaleAsCdna_actionPerformed()
426    {
427  0 ap.av.setScaleProteinAsCdna(scaleAsCdna.isSelected());
428  0 ap.av.getCodingComplement()
429    .setScaleProteinAsCdna(scaleAsCdna.isSelected());
430  0 final SplitFrame splitFrame = (SplitFrame) ap.alignFrame
431    .getSplitViewContainer();
432  0 splitFrame.adjustLayout();
433  0 splitFrame.repaint();
434    }
435   
436    /**
437    * Turn on/off mirroring of font across split frame. If turning on, also
438    * copies the current font across the split frame. If turning off, restores
439    * the other half of the split frame to its initial font.
440    */
 
441  0 toggle @Override
442    protected void mirrorFonts_actionPerformed()
443    {
444  0 boolean selected = fontAsCdna.isSelected();
445  0 ap.av.setProteinFontAsCdna(selected);
446  0 ap.av.getCodingComplement().setProteinFontAsCdna(selected);
447   
448    /*
449    * reset other half of split frame if turning option off
450    */
451  0 if (!selected)
452    {
453  0 ap.av.getCodingComplement().setFont(oldComplementFont, true);
454    }
455   
456  0 changeFont();
457    }
458    }