2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3 * Copyright (C) 2014 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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
22 import java.awt.event.*;
26 import jalview.jbgui.*;
27 import jalview.util.MessageManager;
35 public class FontChooser extends GFontChooser
48 * Creates a new FontChooser object.
53 public FontChooser(TreePanel tp)
56 ap = tp.treeCanvas.ap;
57 oldFont = tp.getTreeFont();
58 defaultButton.setVisible(false);
59 smoothFont.setEnabled(false);
64 * Creates a new FontChooser object.
69 public FontChooser(AlignmentPanel ap)
71 oldFont = ap.av.getFont();
78 frame = new JInternalFrame();
79 frame.setContentPane(this);
81 smoothFont.setSelected(ap.av.antiAlias);
85 Desktop.addInternalFrame(frame, MessageManager.getString("action.change_font_tree_panel"), 340, 170,
90 Desktop.addInternalFrame(frame, MessageManager.getString("action.change_font"), 340, 170, false);
93 frame.setLayer(JLayeredPane.PALETTE_LAYER);
95 String[] fonts = java.awt.GraphicsEnvironment
96 .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
98 for (int i = 0; i < fonts.length; i++)
100 fontName.addItem(fonts[i]);
103 for (int i = 1; i < 51; i++)
105 fontSize.addItem(i + "");
108 fontStyle.addItem("plain");
109 fontStyle.addItem("bold");
110 fontStyle.addItem("italic");
112 fontName.setSelectedItem(oldFont.getName());
113 fontSize.setSelectedItem(oldFont.getSize() + "");
114 fontStyle.setSelectedIndex(oldFont.getStyle());
116 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
117 monospaced.setSelected(fm.getStringBounds("M", getGraphics())
118 .getWidth() == fm.getStringBounds("|", getGraphics())
124 public void smoothFont_actionPerformed(ActionEvent e)
126 ap.av.antiAlias = smoothFont.isSelected();
127 ap.annotationPanel.image = null;
128 ap.paintAlignment(true);
137 protected void ok_actionPerformed(ActionEvent e)
141 frame.setClosed(true);
142 } catch (Exception ex)
148 if (ap.getOverviewPanel() != null)
150 ap.getOverviewPanel().updateOverviewImage();
161 protected void cancel_actionPerformed(ActionEvent e)
165 ap.av.setFont(oldFont);
166 ap.paintAlignment(true);
170 tp.setTreeFont(oldFont);
172 fontName.setSelectedItem(oldFont.getName());
173 fontSize.setSelectedItem(oldFont.getSize() + "");
174 fontStyle.setSelectedIndex(oldFont.getStyle());
178 frame.setClosed(true);
179 } catch (Exception ex)
184 private Font lastSelected = null;
186 private int lastSelStyle = 0;
188 private int lastSelSize = 0;
190 private boolean lastSelMono = false;
197 if (lastSelected == null)
199 // initialise with original font
200 lastSelected = oldFont;
201 lastSelSize = oldFont.getSize();
202 lastSelStyle = oldFont.getStyle();
203 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
204 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
205 .getStringBounds("I", getGraphics()).getWidth();
206 lastSelMono = mw == iw;
209 Font newFont = new Font(fontName.getSelectedItem().toString(),
210 fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
211 .getSelectedItem().toString()));
212 FontMetrics fm = getGraphics().getFontMetrics(newFont);
213 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
214 .getStringBounds("I", getGraphics()).getWidth();
215 if (mw < 1 || iw < 1)
217 fontName.setSelectedItem(lastSelected.getName());
218 fontStyle.setSelectedIndex(lastSelStyle);
219 fontSize.setSelectedItem("" + lastSelSize);
220 monospaced.setSelected(lastSelMono);
222 .showInternalMessageDialog(
224 "Font doesn't have letters defined\nso cannot be used\nwith alignment data.",
225 "Invalid Font", JOptionPane.WARNING_MESSAGE);
230 tp.setTreeFont(newFont);
234 ap.av.setFont(newFont);
238 monospaced.setSelected(mw == iw);
239 // remember last selected
240 lastSelected = newFont;
249 protected void fontName_actionPerformed(ActionEvent e)
265 protected void fontSize_actionPerformed(ActionEvent e)
281 protected void fontStyle_actionPerformed(ActionEvent e)
297 public void defaultButton_actionPerformed(ActionEvent e)
299 Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
300 Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
301 Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
302 Cache.setProperty("ANTI_ALIAS",
303 Boolean.toString(smoothFont.isSelected()));