2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import jalview.bin.Cache;
24 import jalview.jbgui.GFontChooser;
25 import jalview.util.MessageManager;
28 import java.awt.FontMetrics;
29 import java.awt.geom.Rectangle2D;
31 import javax.swing.JInternalFrame;
32 import javax.swing.JLayeredPane;
40 public class FontChooser extends GFontChooser
47 * The font on opening the dialog (to be restored on Cancel)
52 * The font on opening the dialog (to be restored on Cancel)
53 * on the other half of a split frame (if applicable)
55 Font oldComplementFont;
58 * the state of 'scale protein as cDNA' on opening the dialog
60 boolean oldProteinScale;
63 * the state of 'same font for protein and cDNA' on opening the dialog
65 boolean oldMirrorFont;
72 * The last font settings selected in the dialog
74 private Font lastSelected = null;
76 private boolean lastSelMono = false;
78 private boolean oldSmoothFont;
80 private boolean oldComplementSmooth;
83 * Creates a new FontChooser for a tree panel
87 public FontChooser(TreePanel treePanel)
90 ap = treePanel.getTreeCanvas().getAssociatedPanel();
91 oldFont = treePanel.getTreeFont();
92 defaultButton.setVisible(false);
93 smoothFont.setEnabled(false);
98 * Creates a new FontChooser for an alignment panel
102 public FontChooser(AlignmentPanel alignPanel)
104 oldFont = alignPanel.av.getFont();
105 oldProteinScale = alignPanel.av.isScaleProteinAsCdna();
106 oldMirrorFont = alignPanel.av.isProteinFontAsCdna();
107 oldSmoothFont = alignPanel.av.antiAlias;
108 this.ap = alignPanel;
114 frame = new JInternalFrame();
115 frame.setContentPane(this);
117 smoothFont.setSelected(ap.av.antiAlias);
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.
124 boolean inSplitFrame = ap.av.getCodingComplement() != null;
127 oldComplementFont = ((AlignViewport) ap.av.getCodingComplement())
129 oldComplementSmooth = ((AlignViewport) ap.av
130 .getCodingComplement()).antiAlias;
131 scaleAsCdna.setVisible(true);
132 scaleAsCdna.setSelected(ap.av.isScaleProteinAsCdna());
133 fontAsCdna.setVisible(true);
134 fontAsCdna.setSelected(ap.av.isProteinFontAsCdna());
139 Desktop.addInternalFrame(frame,
140 MessageManager.getString("action.change_font_tree_panel"), Desktop.FRAME_MAKE_VISIBLE,
141 400, 200, Desktop.FRAME_NOT_RESIZABLE, Desktop.FRAME_SET_MIN_SIZE_300);
145 Desktop.addInternalFrame(frame,
146 MessageManager.getString("action.change_font"), Desktop.FRAME_MAKE_VISIBLE, 380, 220,
147 Desktop.FRAME_NOT_RESIZABLE, Desktop.FRAME_SET_MIN_SIZE_300);
150 frame.setLayer(JLayeredPane.PALETTE_LAYER);
152 String[] fonts = java.awt.GraphicsEnvironment
153 .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
155 for (int i = 0; i < fonts.length; i++)
157 fontName.addItem(fonts[i]);
160 for (int i = 1; i < 51; i++)
165 fontStyle.addItem("plain");
166 fontStyle.addItem("bold");
167 fontStyle.addItem("italic");
169 fontName.setSelectedItem(oldFont.getName());
170 fontSize.setSelectedItem(oldFont.getSize());
171 fontStyle.setSelectedIndex(oldFont.getStyle());
173 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
174 monospaced.setSelected(
175 fm.getStringBounds("M", getGraphics()).getWidth() == fm
176 .getStringBounds("|", getGraphics()).getWidth());
182 protected void smoothFont_actionPerformed()
184 ap.av.antiAlias = smoothFont.isSelected();
185 ap.getAnnotationPanel().image = null;
186 ap.paintAlignment(true, false);
187 if (ap.av.getCodingComplement() != null && ap.av.isProteinFontAsCdna())
189 ((AlignViewport) ap.av
190 .getCodingComplement()).antiAlias = ap.av.antiAlias;
191 SplitFrame sv = (SplitFrame) ap.alignFrame.getSplitViewContainer();
205 protected void ok_actionPerformed()
209 frame.setClosed(true);
210 } catch (Exception ex)
216 if (ap.getOverviewPanel() != null)
218 ap.getOverviewPanel().updateOverviewImage();
230 protected void cancel_actionPerformed()
234 tp.setTreeFont(oldFont);
238 ap.av.setFont(oldFont, true);
239 ap.av.setScaleProteinAsCdna(oldProteinScale);
240 ap.av.setProteinFontAsCdna(oldMirrorFont);
241 ap.av.antiAlias = oldSmoothFont;
244 if (scaleAsCdna.isVisible() && scaleAsCdna.isEnabled())
246 ap.av.getCodingComplement().setScaleProteinAsCdna(oldProteinScale);
247 ap.av.getCodingComplement().setProteinFontAsCdna(oldMirrorFont);
248 ((AlignViewport) ap.av
249 .getCodingComplement()).antiAlias = oldComplementSmooth;
250 ap.av.getCodingComplement().setFont(oldComplementFont, true);
251 SplitFrame splitFrame = (SplitFrame) ap.alignFrame
252 .getSplitViewContainer();
253 splitFrame.adjustLayout();
254 splitFrame.repaint();
260 frame.setClosed(true);
261 } catch (Exception ex)
266 private boolean isTreeFont()
276 if (lastSelected == null)
278 // initialise with original font
279 lastSelected = oldFont;
280 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
281 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
282 double iw = fm.getStringBounds("I", getGraphics()).getWidth();
283 lastSelMono = (mw == iw); // == on double - flaky?
286 Font newFont = new Font(fontName.getSelectedItem().toString(),
287 fontStyle.getSelectedIndex(),
288 (Integer) fontSize.getSelectedItem());
289 FontMetrics fm = getGraphics().getFontMetrics(newFont);
290 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
291 final Rectangle2D iBounds = fm.getStringBounds("I", getGraphics());
292 double iw = iBounds.getWidth();
293 if (mw < 1 || iw < 1)
295 String message = iBounds.getHeight() < 1
297 .getString("label.font_doesnt_have_letters_defined")
298 : MessageManager.getString("label.font_too_small");
299 JvOptionPane.showInternalMessageDialog(this, message,
300 MessageManager.getString("label.invalid_font"),
301 JvOptionPane.WARNING_MESSAGE);
303 * Restore the changed value - note this will reinvoke this method via the
304 * ActionListener, but now validation should pass
306 if (lastSelected.getSize() != (Integer) fontSize.getSelectedItem()) // autoboxing
308 fontSize.setSelectedItem(lastSelected.getSize());
310 if (!lastSelected.getName()
311 .equals(fontName.getSelectedItem().toString()))
313 fontName.setSelectedItem(lastSelected.getName());
315 if (lastSelected.getStyle() != fontStyle.getSelectedIndex())
317 fontStyle.setSelectedIndex(lastSelected.getStyle());
319 if (lastSelMono != monospaced.isSelected())
321 monospaced.setSelected(lastSelMono);
327 tp.setTreeFont(newFont);
331 ap.av.setFont(newFont, true);
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
339 if (fontAsCdna.isVisible())
341 if (fontAsCdna.isSelected())
343 ap.av.getCodingComplement().setFont(newFont, true);
346 SplitFrame splitFrame = (SplitFrame) ap.alignFrame
347 .getSplitViewContainer();
348 splitFrame.adjustLayout();
349 splitFrame.repaint();
353 monospaced.setSelected(mw == iw);
356 * Remember latest valid selection, so it can be restored if followed by an
359 lastSelected = newFont;
363 * Updates on change of selected font name
366 protected void fontName_actionPerformed()
377 * Updates on change of selected font size
380 protected void fontSize_actionPerformed()
391 * Updates on change of selected font style
394 protected void fontStyle_actionPerformed()
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)
409 public void defaultButton_actionPerformed()
411 Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
412 Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
413 Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
414 Cache.setProperty("ANTI_ALIAS",
415 Boolean.toString(smoothFont.isSelected()));
416 Cache.setProperty(Preferences.SCALE_PROTEIN_TO_CDNA,
417 Boolean.toString(scaleAsCdna.isSelected()));
421 * Turn on/off scaling of protein characters to 3 times the width of cDNA
425 protected void scaleAsCdna_actionPerformed()
427 ap.av.setScaleProteinAsCdna(scaleAsCdna.isSelected());
428 ap.av.getCodingComplement()
429 .setScaleProteinAsCdna(scaleAsCdna.isSelected());
430 final SplitFrame splitFrame = (SplitFrame) ap.alignFrame
431 .getSplitViewContainer();
432 splitFrame.adjustLayout();
433 splitFrame.repaint();
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.
442 protected void mirrorFonts_actionPerformed()
444 boolean selected = fontAsCdna.isSelected();
445 ap.av.setProteinFontAsCdna(selected);
446 ap.av.getCodingComplement().setProteinFontAsCdna(selected);
449 * reset other half of split frame if turning option off
453 ap.av.getCodingComplement().setFont(oldComplementFont, true);