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.
21 package jalview.appletgui;
23 import java.awt.BorderLayout;
24 import java.awt.Button;
25 import java.awt.Checkbox;
26 import java.awt.Choice;
27 import java.awt.Color;
28 import java.awt.FlowLayout;
30 import java.awt.FontMetrics;
31 import java.awt.Frame;
32 import java.awt.Label;
33 import java.awt.Panel;
34 import java.awt.Toolkit;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.awt.event.ItemEvent;
38 import java.awt.event.ItemListener;
40 import jalview.api.ViewStyleI;
41 import jalview.util.MessageManager;
44 * This dialog allows the user to try different font settings and related
45 * options. Changes are immediately visible on the alignment or tree. The user
46 * can dismiss the dialog by confirming changes with 'OK', or reverting to
47 * previous settings with 'Cancel'.
49 @SuppressWarnings("serial")
50 public class FontChooser extends Panel implements ItemListener
52 private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
54 private Choice fontSize = new Choice();
56 private Choice fontStyle = new Choice();
58 private Choice fontName = new Choice();
60 private Checkbox scaleAsCdna = new Checkbox();
62 private Button ok = new Button();
64 private Button cancel = new Button();
66 private AlignmentPanel ap;
72 private int oldCharWidth = 0;
74 private boolean oldScaleProtein = false;
76 private Font lastSelected = null;
78 private int lastSelStyle = 0;
80 private int lastSelSize = 0;
82 private boolean init = true;
87 * Constructor for a TreePanel font chooser
91 public FontChooser(TreePanel tp)
102 oldFont = tp.getTreeFont();
107 * Constructor for an AlignmentPanel font chooser
111 public FontChooser(AlignmentPanel ap)
114 oldFont = ap.av.getFont();
115 oldCharWidth = ap.av.getViewStyle().getCharWidth();
116 oldScaleProtein = ap.av.getViewStyle().isScaleProteinAsCdna();
121 } catch (Exception e)
129 * Populate choice lists and open this dialog
133 String fonts[] = Toolkit.getDefaultToolkit().getFontList();
134 for (int i = 0; i < fonts.length; i++)
136 fontName.addItem(fonts[i]);
139 for (int i = 1; i < 31; i++)
141 fontSize.addItem(i + "");
144 fontStyle.addItem("plain");
145 fontStyle.addItem("bold");
146 fontStyle.addItem("italic");
148 fontName.select(oldFont.getName());
149 fontSize.select(oldFont.getSize() + "");
150 fontStyle.select(oldFont.getStyle());
152 this.frame = new Frame();
154 jalview.bin.JalviewLite.addFrame(frame,
155 MessageManager.getString("action.change_font"), 440, 115);
161 * Actions on change of font name, size or style.
163 public void itemStateChanged(ItemEvent evt)
165 final Object source = evt.getSource();
166 if (source == fontName)
168 fontName_actionPerformed();
170 else if (source == fontSize)
172 fontSize_actionPerformed();
174 else if (source == fontStyle)
176 fontStyle_actionPerformed();
178 else if (source == scaleAsCdna)
180 scaleAsCdna_actionPerformed();
185 * Close this dialog on OK to confirm any changes. Also updates the overview
186 * window if displayed.
188 protected void ok_actionPerformed()
190 frame.setVisible(false);
193 if (ap.getOverviewPanel() != null)
195 ap.getOverviewPanel().updateOverviewImage();
201 * Close this dialog on Cancel, reverting to previous font settings.
203 protected void cancel_actionPerformed()
207 ap.av.setFont(oldFont);
208 ViewStyleI style = ap.av.getViewStyle();
209 if (style.getCharWidth() != oldCharWidth)
211 style.setCharWidth(oldCharWidth);
212 ap.av.setViewStyle(style);
214 ap.paintAlignment(true);
218 tp.setTreeFont(oldFont);
219 tp.treeCanvas.repaint();
222 fontName.select(oldFont.getName());
223 fontSize.select(oldFont.getSize() + "");
224 fontStyle.select(oldFont.getStyle());
226 frame.setVisible(false);
234 if (lastSelected == null)
236 // initialise with original font
237 lastSelected = oldFont;
238 lastSelSize = oldFont.getSize();
239 lastSelStyle = oldFont.getStyle();
242 Font newFont = new Font(fontName.getSelectedItem().toString(),
243 fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
244 .getSelectedItem().toString()));
245 FontMetrics fm = getGraphics().getFontMetrics(newFont);
246 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
247 .getStringBounds("I", getGraphics()).getWidth();
248 if (mw < 1 || iw < 1)
251 fontName.select(lastSelected.getName());
252 fontStyle.select(lastSelStyle);
253 fontSize.select("" + lastSelSize);
254 JVDialog d = new JVDialog(this.frame,
255 MessageManager.getString("label.invalid_font"), true, 350,
257 Panel mp = new Panel();
258 d.cancel.setVisible(false);
259 mp.setLayout(new FlowLayout());
261 "Font doesn't have letters defined\nso cannot be used\nwith alignment data."));
268 tp.setTreeFont(newFont);
272 ap.av.setFont(newFont);
275 // remember last selected
276 lastSelected = newFont;
279 protected void fontName_actionPerformed()
288 protected void fontSize_actionPerformed()
297 protected void fontStyle_actionPerformed()
307 * Construct this panel's contents
311 private void jbInit() throws Exception
313 this.setLayout(new BorderLayout());
314 this.setBackground(Color.white);
316 Label fontLabel = new Label(MessageManager.getString("label.font"));
317 fontLabel.setFont(VERDANA_11PT);
318 fontLabel.setAlignment(Label.RIGHT);
319 fontSize.setFont(VERDANA_11PT);
320 fontSize.addItemListener(this);
321 fontStyle.setFont(VERDANA_11PT);
322 fontStyle.addItemListener(this);
324 Label sizeLabel = new Label(MessageManager.getString("label.size"));
325 sizeLabel.setAlignment(Label.RIGHT);
326 sizeLabel.setFont(VERDANA_11PT);
328 Label styleLabel = new Label(MessageManager.getString("label.style"));
329 styleLabel.setAlignment(Label.RIGHT);
330 styleLabel.setFont(VERDANA_11PT);
332 fontName.setFont(VERDANA_11PT);
333 fontName.addItemListener(this);
335 scaleAsCdna.setLabel(MessageManager.getString("label.scale_as_cdna"));
336 scaleAsCdna.setFont(VERDANA_11PT);
337 scaleAsCdna.addItemListener(this);
338 scaleAsCdna.setState(ap.av.isScaleProteinAsCdna());
340 ok.setFont(VERDANA_11PT);
341 ok.setLabel(MessageManager.getString("action.ok"));
342 ok.addActionListener(new ActionListener()
345 public void actionPerformed(ActionEvent e)
347 ok_actionPerformed();
350 cancel.setFont(VERDANA_11PT);
351 cancel.setLabel(MessageManager.getString("action.cancel"));
352 cancel.addActionListener(new ActionListener()
355 public void actionPerformed(ActionEvent e)
357 cancel_actionPerformed();
361 Panel fontPanel = new Panel();
362 fontPanel.setLayout(new BorderLayout());
363 Panel stylePanel = new Panel();
364 stylePanel.setLayout(new BorderLayout());
365 Panel sizePanel = new Panel();
366 sizePanel.setLayout(new BorderLayout());
367 Panel scalePanel = new Panel();
368 scalePanel.setLayout(new BorderLayout());
369 Panel okCancelPanel = new Panel();
370 Panel optionsPanel = new Panel();
372 fontPanel.setBackground(Color.white);
373 stylePanel.setBackground(Color.white);
374 sizePanel.setBackground(Color.white);
375 okCancelPanel.setBackground(Color.white);
376 optionsPanel.setBackground(Color.white);
378 fontPanel.add(fontLabel, BorderLayout.WEST);
379 fontPanel.add(fontName, BorderLayout.CENTER);
380 stylePanel.add(styleLabel, BorderLayout.WEST);
381 stylePanel.add(fontStyle, BorderLayout.CENTER);
382 sizePanel.add(sizeLabel, BorderLayout.WEST);
383 sizePanel.add(fontSize, BorderLayout.CENTER);
384 scalePanel.add(scaleAsCdna, BorderLayout.CENTER);
385 okCancelPanel.add(ok, null);
386 okCancelPanel.add(cancel, null);
388 optionsPanel.add(fontPanel, null);
389 optionsPanel.add(sizePanel, null);
390 optionsPanel.add(stylePanel, null);
393 * Only show 'scale protein as cDNA' in protein half of a SplitFrame
395 this.add(optionsPanel, BorderLayout.NORTH);
396 if (ap.alignFrame.getSplitFrame() != null
397 && !ap.av.getAlignment().isNucleotide())
399 this.add(scalePanel, BorderLayout.CENTER);
401 this.add(okCancelPanel, BorderLayout.SOUTH);
405 * Turn on/off scaling of protein characters to 3 times the width of cDNA
408 protected void scaleAsCdna_actionPerformed()
410 ap.av.setScaleProteinAsCdna(scaleAsCdna.getState());
411 ap.alignFrame.getSplitFrame().adjustLayout();
412 ap.paintAlignment(true);