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 if (ap.av.getCodingComplement() != null)
99 scaleAsCdna.setVisible(true);
100 scaleAsCdna.setSelected(ap.av.isScaleProteinAsCdna());
105 Desktop.addInternalFrame(frame,
106 MessageManager.getString("action.change_font_tree_panel"),
111 Desktop.addInternalFrame(frame,
112 MessageManager.getString("action.change_font"), 380, 200,
116 frame.setLayer(JLayeredPane.PALETTE_LAYER);
118 String[] fonts = java.awt.GraphicsEnvironment
119 .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
121 for (int i = 0; i < fonts.length; i++)
123 fontName.addItem(fonts[i]);
126 for (int i = 1; i < 51; i++)
131 fontStyle.addItem("plain");
132 fontStyle.addItem("bold");
133 fontStyle.addItem("italic");
135 fontName.setSelectedItem(oldFont.getName());
136 fontSize.setSelectedItem(oldFont.getSize());
137 fontStyle.setSelectedIndex(oldFont.getStyle());
139 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
140 monospaced.setSelected(fm.getStringBounds("M", getGraphics())
141 .getWidth() == fm.getStringBounds("|", getGraphics())
147 public void smoothFont_actionPerformed(ActionEvent e)
149 ap.av.antiAlias = smoothFont.isSelected();
150 ap.getAnnotationPanel().image = null;
151 ap.paintAlignment(true);
160 protected void ok_actionPerformed(ActionEvent e)
164 frame.setClosed(true);
165 } catch (Exception ex)
171 if (ap.getOverviewPanel() != null)
173 ap.getOverviewPanel().updateOverviewImage();
184 protected void cancel_actionPerformed(ActionEvent e)
188 ap.av.setFont(oldFont, true);
189 ap.av.setScaleProteinAsCdna(oldProteinScale);
190 ap.paintAlignment(true);
191 if (scaleAsCdna.isEnabled())
193 ap.av.setScaleProteinAsCdna(oldProteinScale);
194 ap.av.getCodingComplement().setScaleProteinAsCdna(oldProteinScale);
199 tp.setTreeFont(oldFont);
201 fontName.setSelectedItem(oldFont.getName());
202 fontSize.setSelectedItem(oldFont.getSize());
203 fontStyle.setSelectedIndex(oldFont.getStyle());
207 frame.setClosed(true);
208 } catch (Exception ex)
213 private Font lastSelected = null;
215 private int lastSelStyle = 0;
217 private int lastSelSize = 0;
219 private boolean lastSelMono = false;
226 if (lastSelected == null)
228 // initialise with original font
229 lastSelected = oldFont;
230 lastSelSize = oldFont.getSize();
231 lastSelStyle = oldFont.getStyle();
232 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
233 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
234 .getStringBounds("I", getGraphics()).getWidth();
235 lastSelMono = mw == iw;
238 Font newFont = new Font(fontName.getSelectedItem().toString(),
239 fontStyle.getSelectedIndex(),
240 (Integer) fontSize.getSelectedItem());
241 FontMetrics fm = getGraphics().getFontMetrics(newFont);
242 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
243 .getStringBounds("I", getGraphics()).getWidth();
244 if (mw < 1 || iw < 1)
246 fontName.setSelectedItem(lastSelected.getName());
247 fontStyle.setSelectedIndex(lastSelStyle);
248 fontSize.setSelectedItem(lastSelSize);
249 monospaced.setSelected(lastSelMono);
251 .showInternalMessageDialog(
253 MessageManager.getString("label.font_doesnt_have_letters_defined"),
254 MessageManager.getString("label.invalid_font"), JOptionPane.WARNING_MESSAGE);
259 tp.setTreeFont(newFont);
263 ap.av.setFont(newFont, true);
267 monospaced.setSelected(mw == iw);
268 // remember last selected
269 lastSelected = newFont;
278 protected void fontName_actionPerformed(ActionEvent e)
294 protected void fontSize_actionPerformed(ActionEvent e)
310 protected void fontStyle_actionPerformed(ActionEvent e)
321 * Make selected settings the defaults by storing them (via Cache class) in
322 * the .jalview_properties file (the file is only written when Jalview exits)
326 public void defaultButton_actionPerformed(ActionEvent e)
328 Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
329 Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
330 Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
331 Cache.setProperty("ANTI_ALIAS",
332 Boolean.toString(smoothFont.isSelected()));
333 Cache.setProperty(Preferences.SCALE_PROTEIN_TO_CDNA,
334 Boolean.toString(scaleAsCdna.isSelected()));
338 * Turn on/off scaling of protein characters to 3 times the width of cDNA
342 protected void scaleAsCdna_actionPerformed(ActionEvent e)
344 ap.av.setScaleProteinAsCdna(scaleAsCdna.isSelected());
345 ap.av.getCodingComplement().setScaleProteinAsCdna(
346 scaleAsCdna.isSelected());
347 final SplitFrame splitFrame = (SplitFrame) ap.alignFrame
348 .getSplitViewContainer();
349 splitFrame.adjustLayout();
350 splitFrame.repaint();
351 // ap.paintAlignment(true);
352 // TODO would like to repaint