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 a SplitFrame view. The selection is
95 * stored in the ViewStyle of both dna and protein Viewport
97 scaleAsCdna.setEnabled(false);
98 if (ap.av.getCodingComplement() != null)
100 scaleAsCdna.setEnabled(true);
101 scaleAsCdna.setVisible(true);
102 scaleAsCdna.setSelected(ap.av.isScaleProteinAsCdna());
107 Desktop.addInternalFrame(frame,
108 MessageManager.getString("action.change_font_tree_panel"),
113 Desktop.addInternalFrame(frame,
114 MessageManager.getString("action.change_font"), 380, 200,
118 frame.setLayer(JLayeredPane.PALETTE_LAYER);
120 String[] fonts = java.awt.GraphicsEnvironment
121 .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
123 for (int i = 0; i < fonts.length; i++)
125 fontName.addItem(fonts[i]);
128 for (int i = 1; i < 51; i++)
133 fontStyle.addItem("plain");
134 fontStyle.addItem("bold");
135 fontStyle.addItem("italic");
137 fontName.setSelectedItem(oldFont.getName());
138 fontSize.setSelectedItem(oldFont.getSize());
139 fontStyle.setSelectedIndex(oldFont.getStyle());
141 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
142 monospaced.setSelected(fm.getStringBounds("M", getGraphics())
143 .getWidth() == fm.getStringBounds("|", getGraphics())
149 public void smoothFont_actionPerformed(ActionEvent e)
151 ap.av.antiAlias = smoothFont.isSelected();
152 ap.getAnnotationPanel().image = null;
153 ap.paintAlignment(true);
162 protected void ok_actionPerformed(ActionEvent e)
166 frame.setClosed(true);
167 } catch (Exception ex)
173 if (ap.getOverviewPanel() != null)
175 ap.getOverviewPanel().updateOverviewImage();
186 protected void cancel_actionPerformed(ActionEvent e)
190 ap.av.setFont(oldFont, true);
191 ap.av.setScaleProteinAsCdna(oldProteinScale);
192 ap.paintAlignment(true);
193 if (scaleAsCdna.isEnabled())
195 ap.av.setScaleProteinAsCdna(oldProteinScale);
196 ap.av.getCodingComplement().setScaleProteinAsCdna(oldProteinScale);
201 tp.setTreeFont(oldFont);
203 fontName.setSelectedItem(oldFont.getName());
204 fontSize.setSelectedItem(oldFont.getSize());
205 fontStyle.setSelectedIndex(oldFont.getStyle());
209 frame.setClosed(true);
210 } catch (Exception ex)
215 private Font lastSelected = null;
217 private int lastSelStyle = 0;
219 private int lastSelSize = 0;
221 private boolean lastSelMono = false;
228 if (lastSelected == null)
230 // initialise with original font
231 lastSelected = oldFont;
232 lastSelSize = oldFont.getSize();
233 lastSelStyle = oldFont.getStyle();
234 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
235 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
236 .getStringBounds("I", getGraphics()).getWidth();
237 lastSelMono = mw == iw;
240 Font newFont = new Font(fontName.getSelectedItem().toString(),
241 fontStyle.getSelectedIndex(),
242 (Integer) fontSize.getSelectedItem());
243 FontMetrics fm = getGraphics().getFontMetrics(newFont);
244 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
245 .getStringBounds("I", getGraphics()).getWidth();
246 if (mw < 1 || iw < 1)
249 .showInternalMessageDialog(
251 MessageManager.getString("label.font_doesnt_have_letters_defined"),
252 MessageManager.getString("label.invalid_font"), JOptionPane.WARNING_MESSAGE);
254 * Restore previous values - size first to avoid recursive calls to this
257 fontSize.setSelectedItem(lastSelSize);
258 fontName.setSelectedItem(lastSelected.getName());
259 fontStyle.setSelectedIndex(lastSelStyle);
260 monospaced.setSelected(lastSelMono);
265 tp.setTreeFont(newFont);
269 ap.av.setFont(newFont, true);
273 monospaced.setSelected(mw == iw);
274 // remember last selected
275 lastSelected = newFont;
284 protected void fontName_actionPerformed(ActionEvent e)
300 protected void fontSize_actionPerformed(ActionEvent e)
316 protected void fontStyle_actionPerformed(ActionEvent e)
327 * Make selected settings the defaults by storing them (via Cache class) in
328 * the .jalview_properties file (the file is only written when Jalview exits)
332 public void defaultButton_actionPerformed(ActionEvent e)
334 Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
335 Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
336 Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
337 Cache.setProperty("ANTI_ALIAS",
338 Boolean.toString(smoothFont.isSelected()));
339 Cache.setProperty(Preferences.SCALE_PROTEIN_TO_CDNA,
340 Boolean.toString(scaleAsCdna.isSelected()));
344 * Turn on/off scaling of protein characters to 3 times the width of cDNA
348 protected void scaleAsCdna_actionPerformed(ActionEvent e)
350 ap.av.setScaleProteinAsCdna(scaleAsCdna.isSelected());
351 ap.av.getCodingComplement().setScaleProteinAsCdna(
352 scaleAsCdna.isSelected());
353 final SplitFrame splitFrame = (SplitFrame) ap.alignFrame
354 .getSplitViewContainer();
355 splitFrame.adjustLayout();
356 splitFrame.repaint();
357 // ap.paintAlignment(true);
358 // TODO would like to repaint