2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3 * Copyright (C) 2015 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 jalview.api.ViewStyleI;
24 import jalview.util.MessageManager;
26 import java.awt.BorderLayout;
27 import java.awt.Button;
28 import java.awt.Checkbox;
29 import java.awt.Choice;
30 import java.awt.Color;
31 import java.awt.FlowLayout;
33 import java.awt.FontMetrics;
34 import java.awt.Frame;
35 import java.awt.Label;
36 import java.awt.Panel;
37 import java.awt.Toolkit;
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ActionListener;
40 import java.awt.event.ItemEvent;
41 import java.awt.event.ItemListener;
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.setScaleProteinAsCdna(oldScaleProtein);
208 if (ap.av.getCodingComplement() != null)
210 ap.av.getCodingComplement().setScaleProteinAsCdna(oldScaleProtein);
211 ap.alignFrame.getSplitFrame().repaint();
214 ap.av.setFont(oldFont);
215 ViewStyleI style = ap.av.getViewStyle();
216 if (style.getCharWidth() != oldCharWidth)
218 style.setCharWidth(oldCharWidth);
219 ap.av.setViewStyle(style);
221 ap.paintAlignment(true);
225 tp.setTreeFont(oldFont);
226 tp.treeCanvas.repaint();
229 fontName.select(oldFont.getName());
230 fontSize.select(oldFont.getSize() + "");
231 fontStyle.select(oldFont.getStyle());
233 frame.setVisible(false);
241 if (lastSelected == null)
243 // initialise with original font
244 lastSelected = oldFont;
245 lastSelSize = oldFont.getSize();
246 lastSelStyle = oldFont.getStyle();
249 Font newFont = new Font(fontName.getSelectedItem().toString(),
250 fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
251 .getSelectedItem().toString()));
252 FontMetrics fm = getGraphics().getFontMetrics(newFont);
253 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
254 .getStringBounds("I", getGraphics()).getWidth();
255 if (mw < 1 || iw < 1)
258 fontName.select(lastSelected.getName());
259 fontStyle.select(lastSelStyle);
260 fontSize.select("" + lastSelSize);
261 JVDialog d = new JVDialog(this.frame,
262 MessageManager.getString("label.invalid_font"), true, 350,
264 Panel mp = new Panel();
265 d.cancel.setVisible(false);
266 mp.setLayout(new FlowLayout());
268 "Font doesn't have letters defined\nso cannot be used\nwith alignment data."));
275 tp.setTreeFont(newFont);
279 ap.av.setFont(newFont);
282 // remember last selected
283 lastSelected = newFont;
286 protected void fontName_actionPerformed()
295 protected void fontSize_actionPerformed()
304 protected void fontStyle_actionPerformed()
314 * Construct this panel's contents
318 private void jbInit() throws Exception
320 this.setLayout(new BorderLayout());
321 this.setBackground(Color.white);
323 Label fontLabel = new Label(MessageManager.getString("label.font"));
324 fontLabel.setFont(VERDANA_11PT);
325 fontLabel.setAlignment(Label.RIGHT);
326 fontSize.setFont(VERDANA_11PT);
327 fontSize.addItemListener(this);
328 fontStyle.setFont(VERDANA_11PT);
329 fontStyle.addItemListener(this);
331 Label sizeLabel = new Label(MessageManager.getString("label.size"));
332 sizeLabel.setAlignment(Label.RIGHT);
333 sizeLabel.setFont(VERDANA_11PT);
335 Label styleLabel = new Label(MessageManager.getString("label.style"));
336 styleLabel.setAlignment(Label.RIGHT);
337 styleLabel.setFont(VERDANA_11PT);
339 fontName.setFont(VERDANA_11PT);
340 fontName.addItemListener(this);
342 scaleAsCdna.setLabel(MessageManager.getString("label.scale_as_cdna"));
343 scaleAsCdna.setFont(VERDANA_11PT);
344 scaleAsCdna.addItemListener(this);
345 scaleAsCdna.setState(ap.av.isScaleProteinAsCdna());
347 ok.setFont(VERDANA_11PT);
348 ok.setLabel(MessageManager.getString("action.ok"));
349 ok.addActionListener(new ActionListener()
352 public void actionPerformed(ActionEvent e)
354 ok_actionPerformed();
357 cancel.setFont(VERDANA_11PT);
358 cancel.setLabel(MessageManager.getString("action.cancel"));
359 cancel.addActionListener(new ActionListener()
362 public void actionPerformed(ActionEvent e)
364 cancel_actionPerformed();
368 Panel fontPanel = new Panel();
369 fontPanel.setLayout(new BorderLayout());
370 Panel stylePanel = new Panel();
371 stylePanel.setLayout(new BorderLayout());
372 Panel sizePanel = new Panel();
373 sizePanel.setLayout(new BorderLayout());
374 Panel scalePanel = new Panel();
375 scalePanel.setLayout(new BorderLayout());
376 Panel okCancelPanel = new Panel();
377 Panel optionsPanel = new Panel();
379 fontPanel.setBackground(Color.white);
380 stylePanel.setBackground(Color.white);
381 sizePanel.setBackground(Color.white);
382 okCancelPanel.setBackground(Color.white);
383 optionsPanel.setBackground(Color.white);
385 fontPanel.add(fontLabel, BorderLayout.WEST);
386 fontPanel.add(fontName, BorderLayout.CENTER);
387 stylePanel.add(styleLabel, BorderLayout.WEST);
388 stylePanel.add(fontStyle, BorderLayout.CENTER);
389 sizePanel.add(sizeLabel, BorderLayout.WEST);
390 sizePanel.add(fontSize, BorderLayout.CENTER);
391 scalePanel.add(scaleAsCdna, BorderLayout.CENTER);
392 okCancelPanel.add(ok, null);
393 okCancelPanel.add(cancel, null);
395 optionsPanel.add(fontPanel, null);
396 optionsPanel.add(sizePanel, null);
397 optionsPanel.add(stylePanel, null);
400 * Only show 'scale protein as cDNA' in a SplitFrame
402 this.add(optionsPanel, BorderLayout.NORTH);
403 if (ap.alignFrame.getSplitFrame() != null)
405 this.add(scalePanel, BorderLayout.CENTER);
407 this.add(okCancelPanel, BorderLayout.SOUTH);
411 * Turn on/off scaling of protein characters to 3 times the width of cDNA
414 protected void scaleAsCdna_actionPerformed()
416 ap.av.setScaleProteinAsCdna(scaleAsCdna.getState());
417 ap.av.getCodingComplement().setScaleProteinAsCdna(
418 scaleAsCdna.getState());
419 ap.alignFrame.getSplitFrame().adjustLayout();
420 ap.paintAlignment(true);
421 ap.alignFrame.getSplitFrame().repaint();