2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
21 import java.awt.event.*;
25 import jalview.jbgui.*;
26 import jalview.util.MessageManager;
34 public class FontChooser extends GFontChooser
47 * Creates a new FontChooser object.
52 public FontChooser(TreePanel tp)
55 ap = tp.treeCanvas.ap;
56 oldFont = tp.getTreeFont();
57 defaultButton.setVisible(false);
58 smoothFont.setEnabled(false);
63 * Creates a new FontChooser object.
68 public FontChooser(AlignmentPanel ap)
70 oldFont = ap.av.getFont();
77 frame = new JInternalFrame();
78 frame.setContentPane(this);
80 smoothFont.setSelected(ap.av.antiAlias);
84 Desktop.addInternalFrame(frame, MessageManager.getString("action.change_font_tree_panel"), 340, 170,
89 Desktop.addInternalFrame(frame, MessageManager.getString("action.change_font"), 340, 170, false);
92 frame.setLayer(JLayeredPane.PALETTE_LAYER);
94 String[] fonts = java.awt.GraphicsEnvironment
95 .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
97 for (int i = 0; i < fonts.length; i++)
99 fontName.addItem(fonts[i]);
102 for (int i = 1; i < 51; i++)
104 fontSize.addItem(i + "");
107 fontStyle.addItem("plain");
108 fontStyle.addItem("bold");
109 fontStyle.addItem("italic");
111 fontName.setSelectedItem(oldFont.getName());
112 fontSize.setSelectedItem(oldFont.getSize() + "");
113 fontStyle.setSelectedIndex(oldFont.getStyle());
115 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
116 monospaced.setSelected(fm.getStringBounds("M", getGraphics())
117 .getWidth() == fm.getStringBounds("|", getGraphics())
123 public void smoothFont_actionPerformed(ActionEvent e)
125 ap.av.antiAlias = smoothFont.isSelected();
126 ap.annotationPanel.image = null;
127 ap.paintAlignment(true);
136 protected void ok_actionPerformed(ActionEvent e)
140 frame.setClosed(true);
141 } catch (Exception ex)
147 if (ap.getOverviewPanel() != null)
149 ap.getOverviewPanel().updateOverviewImage();
160 protected void cancel_actionPerformed(ActionEvent e)
164 ap.av.setFont(oldFont);
165 ap.paintAlignment(true);
169 tp.setTreeFont(oldFont);
171 fontName.setSelectedItem(oldFont.getName());
172 fontSize.setSelectedItem(oldFont.getSize() + "");
173 fontStyle.setSelectedIndex(oldFont.getStyle());
177 frame.setClosed(true);
178 } catch (Exception ex)
183 private Font lastSelected = null;
185 private int lastSelStyle = 0;
187 private int lastSelSize = 0;
189 private boolean lastSelMono = false;
196 if (lastSelected == null)
198 // initialise with original font
199 lastSelected = oldFont;
200 lastSelSize = oldFont.getSize();
201 lastSelStyle = oldFont.getStyle();
202 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
203 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
204 .getStringBounds("I", getGraphics()).getWidth();
205 lastSelMono = mw == iw;
208 Font newFont = new Font(fontName.getSelectedItem().toString(),
209 fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
210 .getSelectedItem().toString()));
211 FontMetrics fm = getGraphics().getFontMetrics(newFont);
212 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
213 .getStringBounds("I", getGraphics()).getWidth();
214 if (mw < 1 || iw < 1)
216 fontName.setSelectedItem(lastSelected.getName());
217 fontStyle.setSelectedIndex(lastSelStyle);
218 fontSize.setSelectedItem("" + lastSelSize);
219 monospaced.setSelected(lastSelMono);
221 .showInternalMessageDialog(
223 "Font doesn't have letters defined\nso cannot be used\nwith alignment data.",
224 "Invalid Font", JOptionPane.WARNING_MESSAGE);
229 tp.setTreeFont(newFont);
233 ap.av.setFont(newFont);
237 monospaced.setSelected(mw == iw);
238 // remember last selected
239 lastSelected = newFont;
248 protected void fontName_actionPerformed(ActionEvent e)
264 protected void fontSize_actionPerformed(ActionEvent e)
280 protected void fontStyle_actionPerformed(ActionEvent e)
296 public void defaultButton_actionPerformed(ActionEvent e)
298 Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
299 Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
300 Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
301 Cache.setProperty("ANTI_ALIAS",
302 Boolean.toString(smoothFont.isSelected()));