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 state of 'scale protein as cDNA' on opening the dialog
54 boolean oldProteinScale;
57 * the state of 'same font for protein and cDNA' on opening the dialog
59 boolean oldMirrorFont;
66 * The last font settings selected in the dialog
68 private Font lastSelected = null;
70 private boolean lastSelMono = false;
73 * Creates a new FontChooser for a tree panel
77 public FontChooser(TreePanel treePanel)
80 ap = treePanel.treeCanvas.ap;
81 oldFont = treePanel.getTreeFont();
82 defaultButton.setVisible(false);
83 smoothFont.setEnabled(false);
88 * Creates a new FontChooser for an alignment panel
92 public FontChooser(AlignmentPanel alignPanel)
94 oldFont = alignPanel.av.getFont();
95 oldProteinScale = alignPanel.av.isScaleProteinAsCdna();
96 oldMirrorFont = alignPanel.av.isProteinFontAsCdna();
104 frame = new JInternalFrame();
105 frame.setContentPane(this);
107 smoothFont.setSelected(ap.av.antiAlias);
110 * Enable 'scale protein as cDNA' in a SplitFrame view. The selection is
111 * stored in the ViewStyle of both dna and protein Viewport. Also enable
112 * checkbox for copy font changes to other half of split frame.
114 boolean inSplitFrame = ap.av.getCodingComplement() != null;
117 scaleAsCdna.setVisible(true);
118 scaleAsCdna.setSelected(ap.av.isScaleProteinAsCdna());
119 fontAsCdna.setVisible(true);
120 fontAsCdna.setSelected(ap.av.isProteinFontAsCdna());
125 Desktop.addInternalFrame(frame,
126 MessageManager.getString("action.change_font_tree_panel"),
131 Desktop.addInternalFrame(frame,
132 MessageManager.getString("action.change_font"), 380, 220,
136 frame.setLayer(JLayeredPane.PALETTE_LAYER);
138 String[] fonts = java.awt.GraphicsEnvironment
139 .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
141 for (int i = 0; i < fonts.length; i++)
143 fontName.addItem(fonts[i]);
146 for (int i = 1; i < 51; i++)
151 fontStyle.addItem("plain");
152 fontStyle.addItem("bold");
153 fontStyle.addItem("italic");
155 fontName.setSelectedItem(oldFont.getName());
156 fontSize.setSelectedItem(oldFont.getSize());
157 fontStyle.setSelectedIndex(oldFont.getStyle());
159 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
160 monospaced.setSelected(fm.getStringBounds("M", getGraphics())
161 .getWidth() == fm.getStringBounds("|", getGraphics())
168 protected void smoothFont_actionPerformed()
170 ap.av.antiAlias = smoothFont.isSelected();
171 ap.getAnnotationPanel().image = null;
172 ap.paintAlignment(true);
182 protected void ok_actionPerformed()
186 frame.setClosed(true);
187 } catch (Exception ex)
193 if (ap.getOverviewPanel() != null)
195 ap.getOverviewPanel().updateOverviewImage();
207 protected void cancel_actionPerformed()
211 ap.av.setFont(oldFont, true);
212 ap.av.setScaleProteinAsCdna(oldProteinScale);
213 ap.av.setProteinFontAsCdna(oldMirrorFont);
214 ap.paintAlignment(true);
216 if (scaleAsCdna.isVisible() && scaleAsCdna.isEnabled())
218 ap.av.getCodingComplement().setScaleProteinAsCdna(oldProteinScale);
219 ap.av.getCodingComplement().setProteinFontAsCdna(oldMirrorFont);
220 ap.av.getCodingComplement().setFont(oldFont, true);
225 tp.setTreeFont(oldFont);
230 frame.setClosed(true);
231 } catch (Exception ex)
241 if (lastSelected == null)
243 // initialise with original font
244 lastSelected = oldFont;
245 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
246 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
247 double iw = fm.getStringBounds("I", getGraphics()).getWidth();
248 lastSelMono = (mw == iw); // == on double - flaky?
251 Font newFont = new Font(fontName.getSelectedItem().toString(),
252 fontStyle.getSelectedIndex(),
253 (Integer) fontSize.getSelectedItem());
254 FontMetrics fm = getGraphics().getFontMetrics(newFont);
255 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
256 final Rectangle2D iBounds = fm.getStringBounds("I", getGraphics());
257 double iw = iBounds.getWidth();
258 if (mw < 1 || iw < 1)
260 String message = iBounds.getHeight() < 1 ? MessageManager
261 .getString("label.font_doesnt_have_letters_defined")
262 : MessageManager.getString("label.font_too_small");
263 JvOptionPane.showInternalMessageDialog(this, message,
264 MessageManager.getString("label.invalid_font"),
265 JvOptionPane.WARNING_MESSAGE);
267 * Restore the changed value - note this will reinvoke this method via the
268 * ActionListener, but now validation should pass
270 if (lastSelected.getSize() != (Integer) fontSize.getSelectedItem()) // autoboxing
272 fontSize.setSelectedItem(lastSelected.getSize());
274 if (!lastSelected.getName().equals(
275 fontName.getSelectedItem().toString()))
277 fontName.setSelectedItem(lastSelected.getName());
279 if (lastSelected.getStyle() != fontStyle.getSelectedIndex())
281 fontStyle.setSelectedIndex(lastSelected.getStyle());
283 if (lastSelMono != monospaced.isSelected())
285 monospaced.setSelected(lastSelMono);
291 tp.setTreeFont(newFont);
295 ap.av.setFont(newFont, true);
299 * adjust other half of split frame if any, if either same
300 * font, or proportionate scaling, is selected
302 if (fontAsCdna.isEnabled())
304 if (fontAsCdna.isSelected())
309 ap.av.getCodingComplement().setFont(newFont, true);
311 if (fontAsCdna.isSelected() || scaleAsCdna.isSelected())
314 * adjust layout for font change and/or sizing
316 SplitFrame splitFrame = (SplitFrame) ap.alignFrame
317 .getSplitViewContainer();
318 splitFrame.repaint();
323 monospaced.setSelected(mw == iw);
326 * Remember latest valid selection, so it can be restored if followed by an
329 lastSelected = newFont;
333 * Updates on change of selected font name
336 protected void fontName_actionPerformed()
347 * Updates on change of selected font size
350 protected void fontSize_actionPerformed()
361 * Updates on change of selected font style
364 protected void fontStyle_actionPerformed()
375 * Make selected settings the defaults by storing them (via Cache class) in
376 * the .jalview_properties file (the file is only written when Jalview exits)
379 public void defaultButton_actionPerformed()
381 Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
382 Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
383 Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
384 Cache.setProperty("ANTI_ALIAS",
385 Boolean.toString(smoothFont.isSelected()));
386 Cache.setProperty(Preferences.SCALE_PROTEIN_TO_CDNA,
387 Boolean.toString(scaleAsCdna.isSelected()));
391 * Turn on/off scaling of protein characters to 3 times the width of cDNA
395 protected void scaleAsCdna_actionPerformed()
397 ap.av.setScaleProteinAsCdna(scaleAsCdna.isSelected());
398 ap.av.getCodingComplement().setScaleProteinAsCdna(
399 scaleAsCdna.isSelected());
400 final SplitFrame splitFrame = (SplitFrame) ap.alignFrame
401 .getSplitViewContainer();
402 splitFrame.adjustLayout();
403 splitFrame.repaint();
407 * Turn on/off mirroring of font across split frame. If turning on, also
408 * copies the current font across the split frame. If turning off, does not
412 protected void mirrorFonts_actionPerformed()
414 boolean selected = fontAsCdna.isSelected();
415 ap.av.setProteinFontAsCdna(selected);
416 ap.av.getCodingComplement().setProteinFontAsCdna(selected);