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.
24 import java.awt.FontMetrics;
25 import java.awt.event.ActionEvent;
27 import javax.swing.JInternalFrame;
28 import javax.swing.JLayeredPane;
29 import javax.swing.JOptionPane;
31 import jalview.bin.Cache;
32 import jalview.jbgui.GFontChooser;
33 import jalview.util.MessageManager;
41 public class FontChooser extends GFontChooser
49 boolean oldProteinScale;
56 * Creates a new FontChooser object.
61 public FontChooser(TreePanel tp)
64 ap = tp.treeCanvas.ap;
65 oldFont = tp.getTreeFont();
66 defaultButton.setVisible(false);
67 smoothFont.setEnabled(false);
72 * Creates a new FontChooser object.
77 public FontChooser(AlignmentPanel ap)
79 oldFont = ap.av.getFont();
80 oldProteinScale = ap.av.isScaleProteinAsCdna();
88 frame = new JInternalFrame();
89 frame.setContentPane(this);
91 smoothFont.setSelected(ap.av.antiAlias);
94 * Enable 'scale protein as cDNA' in protein half of a SplitFrame view. The
95 * selection is stored in the ViewStyle of the Viewport
97 if (ap.av.getCodingComplement() != null
98 && !ap.av.getAlignment().isNucleotide())
100 scaleAsCdna.setVisible(true);
101 scaleAsCdna.setSelected(ap.av.isScaleProteinAsCdna());
106 Desktop.addInternalFrame(frame,
107 MessageManager.getString("action.change_font_tree_panel"),
112 Desktop.addInternalFrame(frame,
113 MessageManager.getString("action.change_font"), 380, 200,
117 frame.setLayer(JLayeredPane.PALETTE_LAYER);
119 String[] fonts = java.awt.GraphicsEnvironment
120 .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
122 for (int i = 0; i < fonts.length; i++)
124 fontName.addItem(fonts[i]);
127 for (int i = 1; i < 51; i++)
132 fontStyle.addItem("plain");
133 fontStyle.addItem("bold");
134 fontStyle.addItem("italic");
136 fontName.setSelectedItem(oldFont.getName());
137 fontSize.setSelectedItem(oldFont.getSize());
138 fontStyle.setSelectedIndex(oldFont.getStyle());
140 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
141 monospaced.setSelected(fm.getStringBounds("M", getGraphics())
142 .getWidth() == fm.getStringBounds("|", getGraphics())
148 public void smoothFont_actionPerformed(ActionEvent e)
150 ap.av.antiAlias = smoothFont.isSelected();
151 ap.getAnnotationPanel().image = null;
152 ap.paintAlignment(true);
161 protected void ok_actionPerformed(ActionEvent e)
165 frame.setClosed(true);
166 } catch (Exception ex)
172 if (ap.getOverviewPanel() != null)
174 ap.getOverviewPanel().updateOverviewImage();
185 protected void cancel_actionPerformed(ActionEvent e)
189 ap.av.setFont(oldFont, true);
190 ap.av.setScaleProteinAsCdna(oldProteinScale);
191 ap.paintAlignment(true);
195 tp.setTreeFont(oldFont);
197 fontName.setSelectedItem(oldFont.getName());
198 fontSize.setSelectedItem(oldFont.getSize());
199 fontStyle.setSelectedIndex(oldFont.getStyle());
203 frame.setClosed(true);
204 } catch (Exception ex)
209 private Font lastSelected = null;
211 private int lastSelStyle = 0;
213 private int lastSelSize = 0;
215 private boolean lastSelMono = false;
222 if (lastSelected == null)
224 // initialise with original font
225 lastSelected = oldFont;
226 lastSelSize = oldFont.getSize();
227 lastSelStyle = oldFont.getStyle();
228 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
229 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
230 .getStringBounds("I", getGraphics()).getWidth();
231 lastSelMono = mw == iw;
234 Font newFont = new Font(fontName.getSelectedItem().toString(),
235 fontStyle.getSelectedIndex(),
236 (Integer) fontSize.getSelectedItem());
237 FontMetrics fm = getGraphics().getFontMetrics(newFont);
238 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
239 .getStringBounds("I", getGraphics()).getWidth();
240 if (mw < 1 || iw < 1)
242 fontName.setSelectedItem(lastSelected.getName());
243 fontStyle.setSelectedIndex(lastSelStyle);
244 fontSize.setSelectedItem(lastSelSize);
245 monospaced.setSelected(lastSelMono);
247 .showInternalMessageDialog(
249 MessageManager.getString("label.font_doesnt_have_letters_defined"),
250 MessageManager.getString("label.invalid_font"), JOptionPane.WARNING_MESSAGE);
255 tp.setTreeFont(newFont);
259 ap.av.setFont(newFont, true);
263 monospaced.setSelected(mw == iw);
264 // remember last selected
265 lastSelected = newFont;
274 protected void fontName_actionPerformed(ActionEvent e)
290 protected void fontSize_actionPerformed(ActionEvent e)
306 protected void fontStyle_actionPerformed(ActionEvent e)
322 public void defaultButton_actionPerformed(ActionEvent e)
324 Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
325 Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
326 Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
327 Cache.setProperty("ANTI_ALIAS",
328 Boolean.toString(smoothFont.isSelected()));
332 * Turn on/off scaling of protein characters to 3 times the width of cDNA
336 protected void scaleAsCdna_actionPerformed(ActionEvent e)
338 ap.av.setScaleProteinAsCdna(scaleAsCdna.isSelected());
339 ((SplitFrame) ap.alignFrame.getSplitViewContainer()).adjustLayout();
340 ap.paintAlignment(true);