1. Project Clover database Fri Dec 6 2024 13:47:14 GMT
  2. Package jalview.gui

File FontChooser.java

 

Coverage histogram

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

Code metrics

54
142
16
1
485
312
48
0.34
8.88
16
3

Classes

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