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 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 Checkbox fontAsCdna = new Checkbox();
64 private Button ok = new Button();
66 private Button cancel = new Button();
68 private AlignmentPanel ap;
74 private int oldCharWidth = 0;
76 private boolean oldScaleProtein = false;
78 private Font lastSelected = null;
80 private int lastSelStyle = 0;
82 private int lastSelSize = 0;
84 private boolean init = true;
88 boolean inSplitFrame = false;
91 * Constructor for a TreePanel font chooser
95 public FontChooser(TreePanel tp)
100 } catch (Exception e)
106 oldFont = tp.getTreeFont();
111 * Constructor for an AlignmentPanel font chooser
115 public FontChooser(AlignmentPanel ap)
118 oldFont = ap.av.getFont();
119 oldCharWidth = ap.av.getViewStyle().getCharWidth();
120 oldScaleProtein = ap.av.getViewStyle().isScaleProteinAsCdna();
125 } catch (Exception e)
133 * Populate choice lists and open this dialog
137 String fonts[] = Toolkit.getDefaultToolkit().getFontList();
138 for (int i = 0; i < fonts.length; i++)
140 fontName.addItem(fonts[i]);
143 for (int i = 1; i < 31; i++)
145 fontSize.addItem(i + "");
148 fontStyle.addItem("plain");
149 fontStyle.addItem("bold");
150 fontStyle.addItem("italic");
152 fontName.select(oldFont.getName());
153 fontSize.select(oldFont.getSize() + "");
154 fontStyle.select(oldFont.getStyle());
156 this.frame = new Frame();
158 jalview.bin.JalviewLite.addFrame(frame,
159 MessageManager.getString("action.change_font"), 440, 145);
165 * Actions on change of font name, size or style.
168 public void itemStateChanged(ItemEvent evt)
170 final Object source = evt.getSource();
171 if (source == fontName)
173 fontName_actionPerformed();
175 else if (source == fontSize)
177 fontSize_actionPerformed();
179 else if (source == fontStyle)
181 fontStyle_actionPerformed();
183 else if (source == scaleAsCdna)
185 scaleAsCdna_actionPerformed();
190 * Close this dialog on OK to confirm any changes. Also updates the overview
191 * window if displayed.
193 protected void ok_actionPerformed()
195 frame.setVisible(false);
198 if (ap.getOverviewPanel() != null)
200 ap.getOverviewPanel().updateOverviewImage();
206 * Close this dialog on Cancel, reverting to previous font settings.
208 protected void cancel_actionPerformed()
212 ap.av.setScaleProteinAsCdna(oldScaleProtein);
213 if (ap.av.getCodingComplement() != null)
215 ap.av.getCodingComplement().setScaleProteinAsCdna(oldScaleProtein);
216 ap.av.getCodingComplement().setFont(oldFont, true);
217 ap.alignFrame.getSplitFrame().repaint();
220 ap.av.setFont(oldFont, true);
221 ViewStyleI style = ap.av.getViewStyle();
222 if (style.getCharWidth() != oldCharWidth)
224 style.setCharWidth(oldCharWidth);
225 ap.av.setViewStyle(style);
227 ap.paintAlignment(true);
231 tp.setTreeFont(oldFont);
232 tp.treeCanvas.repaint();
235 fontName.select(oldFont.getName());
236 fontSize.select(oldFont.getSize() + "");
237 fontStyle.select(oldFont.getStyle());
239 frame.setVisible(false);
247 if (lastSelected == null)
249 // initialise with original font
250 lastSelected = oldFont;
251 lastSelSize = oldFont.getSize();
252 lastSelStyle = oldFont.getStyle();
255 Font newFont = new Font(fontName.getSelectedItem().toString(),
256 fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
257 .getSelectedItem().toString()));
258 FontMetrics fm = getGraphics().getFontMetrics(newFont);
259 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
260 .getStringBounds("I", getGraphics()).getWidth();
261 if (mw < 1 || iw < 1)
264 fontName.select(lastSelected.getName());
265 fontStyle.select(lastSelStyle);
266 fontSize.select("" + lastSelSize);
267 JVDialog d = new JVDialog(this.frame,
268 MessageManager.getString("label.invalid_font"), true, 350,
270 Panel mp = new Panel();
271 d.cancel.setVisible(false);
272 mp.setLayout(new FlowLayout());
274 "Font doesn't have letters defined\nso cannot be used\nwith alignment data."));
281 tp.setTreeFont(newFont);
285 ap.av.setFont(newFont, true);
289 * and change font in other half of split frame if any
291 if (inSplitFrame && fontAsCdna.getState())
293 ap.av.getCodingComplement().setFont(newFont, true);
294 SplitFrame splitFrame = ap.alignFrame.getSplitFrame();
295 splitFrame.adjustLayout();
296 splitFrame.repaint();
299 // remember last selected
300 lastSelected = newFont;
303 protected void fontName_actionPerformed()
312 protected void fontSize_actionPerformed()
321 protected void fontStyle_actionPerformed()
331 * Construct this panel's contents
335 private void jbInit() throws Exception
337 this.setLayout(new BorderLayout());
338 this.setBackground(Color.white);
340 Label fontLabel = new Label(MessageManager.getString("label.font"));
341 fontLabel.setFont(VERDANA_11PT);
342 fontLabel.setAlignment(Label.RIGHT);
343 fontSize.setFont(VERDANA_11PT);
344 fontSize.addItemListener(this);
345 fontStyle.setFont(VERDANA_11PT);
346 fontStyle.addItemListener(this);
348 Label sizeLabel = new Label(MessageManager.getString("label.size"));
349 sizeLabel.setAlignment(Label.RIGHT);
350 sizeLabel.setFont(VERDANA_11PT);
352 Label styleLabel = new Label(MessageManager.getString("label.style"));
353 styleLabel.setAlignment(Label.RIGHT);
354 styleLabel.setFont(VERDANA_11PT);
356 fontName.setFont(VERDANA_11PT);
357 fontName.addItemListener(this);
359 scaleAsCdna.setLabel(MessageManager.getString("label.scale_as_cdna"));
360 scaleAsCdna.setFont(VERDANA_11PT);
361 scaleAsCdna.addItemListener(this);
362 scaleAsCdna.setState(ap.av.isScaleProteinAsCdna());
364 fontAsCdna.setLabel(MessageManager.getString("label.font_as_cdna"));
365 fontAsCdna.setFont(VERDANA_11PT);
366 fontAsCdna.setState(true);
368 ok.setFont(VERDANA_11PT);
369 ok.setLabel(MessageManager.getString("action.ok"));
370 ok.addActionListener(new ActionListener()
373 public void actionPerformed(ActionEvent e)
375 ok_actionPerformed();
378 cancel.setFont(VERDANA_11PT);
379 cancel.setLabel(MessageManager.getString("action.cancel"));
380 cancel.addActionListener(new ActionListener()
383 public void actionPerformed(ActionEvent e)
385 cancel_actionPerformed();
389 Panel fontPanel = new Panel();
390 fontPanel.setLayout(new BorderLayout());
391 Panel stylePanel = new Panel();
392 stylePanel.setLayout(new BorderLayout());
393 Panel sizePanel = new Panel();
394 sizePanel.setLayout(new BorderLayout());
395 Panel scalePanel = new Panel();
396 scalePanel.setLayout(new BorderLayout());
397 Panel okCancelPanel = new Panel();
398 Panel optionsPanel = new Panel();
400 fontPanel.setBackground(Color.white);
401 stylePanel.setBackground(Color.white);
402 sizePanel.setBackground(Color.white);
403 okCancelPanel.setBackground(Color.white);
404 optionsPanel.setBackground(Color.white);
406 fontPanel.add(fontLabel, BorderLayout.WEST);
407 fontPanel.add(fontName, BorderLayout.CENTER);
408 stylePanel.add(styleLabel, BorderLayout.WEST);
409 stylePanel.add(fontStyle, BorderLayout.CENTER);
410 sizePanel.add(sizeLabel, BorderLayout.WEST);
411 sizePanel.add(fontSize, BorderLayout.CENTER);
412 scalePanel.add(scaleAsCdna, BorderLayout.NORTH);
413 scalePanel.add(fontAsCdna, BorderLayout.SOUTH);
414 okCancelPanel.add(ok, null);
415 okCancelPanel.add(cancel, null);
417 optionsPanel.add(fontPanel, null);
418 optionsPanel.add(sizePanel, null);
419 optionsPanel.add(stylePanel, null);
422 * Only show 'scale protein as cDNA' in a SplitFrame
424 this.add(optionsPanel, BorderLayout.NORTH);
425 if (ap.alignFrame.getSplitFrame() != null)
428 this.add(scalePanel, BorderLayout.CENTER);
430 this.add(okCancelPanel, BorderLayout.SOUTH);
434 * Turn on/off scaling of protein characters to 3 times the width of cDNA
437 protected void scaleAsCdna_actionPerformed()
439 ap.av.setScaleProteinAsCdna(scaleAsCdna.getState());
440 ap.av.getCodingComplement().setScaleProteinAsCdna(
441 scaleAsCdna.getState());
442 ap.alignFrame.getSplitFrame().adjustLayout();
443 ap.paintAlignment(true);
444 ap.alignFrame.getSplitFrame().repaint();