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.util.MessageManager;
25 import java.awt.BorderLayout;
26 import java.awt.Button;
27 import java.awt.Checkbox;
28 import java.awt.Choice;
29 import java.awt.Color;
30 import java.awt.FlowLayout;
32 import java.awt.FontMetrics;
33 import java.awt.Frame;
34 import java.awt.Label;
35 import java.awt.Panel;
36 import java.awt.Toolkit;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.awt.event.ItemEvent;
40 import java.awt.event.ItemListener;
43 * This dialog allows the user to try different font settings and related
44 * options. Changes are immediately visible on the alignment or tree. The user
45 * can dismiss the dialog by confirming changes with 'OK', or reverting to
46 * previous settings with 'Cancel'.
48 @SuppressWarnings("serial")
49 public class FontChooser extends Panel implements ItemListener
51 private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
53 private Choice fontSize = new Choice();
55 private Choice fontStyle = new Choice();
57 private Choice fontName = new Choice();
59 private Checkbox scaleAsCdna = new Checkbox();
61 private Checkbox fontAsCdna = new Checkbox();
63 private Button ok = new Button();
65 private Button cancel = new Button();
67 private AlignmentPanel ap;
73 private Font oldComplementFont;
75 private int oldCharWidth = 0;
78 * the state of 'scale protein to cDNA' on opening the dialog
80 private boolean oldScaleProtein = false;
83 * the state of 'same font for protein and cDNA' on opening the dialog
85 boolean oldMirrorFont;
87 private Font lastSelected = null;
89 private int lastSelStyle = 0;
91 private int lastSelSize = 0;
93 private boolean init = true;
97 boolean inSplitFrame = false;
100 * Constructor for a TreePanel font chooser
104 public FontChooser(TreePanel tp)
109 } catch (Exception e)
115 oldFont = tp.getTreeFont();
120 * Constructor for an AlignmentPanel font chooser
124 public FontChooser(AlignmentPanel ap)
127 oldFont = ap.av.getFont();
128 oldCharWidth = ap.av.getCharWidth();
129 oldScaleProtein = ap.av.isScaleProteinAsCdna();
130 oldMirrorFont = ap.av.isProteinFontAsCdna();
135 } catch (Exception e)
143 * Populate choice lists and open this dialog
147 String fonts[] = Toolkit.getDefaultToolkit().getFontList();
148 for (int i = 0; i < fonts.length; i++)
150 fontName.addItem(fonts[i]);
153 for (int i = 1; i < 31; i++)
155 fontSize.addItem(i + "");
158 fontStyle.addItem("plain");
159 fontStyle.addItem("bold");
160 fontStyle.addItem("italic");
162 fontName.select(oldFont.getName());
163 fontSize.select(oldFont.getSize() + "");
164 fontStyle.select(oldFont.getStyle());
166 this.frame = new Frame();
168 jalview.bin.JalviewLite.addFrame(frame,
169 MessageManager.getString("action.change_font"), 440, 145);
175 * Actions on change of font name, size or style.
178 public void itemStateChanged(ItemEvent evt)
180 final Object source = evt.getSource();
181 if (source == fontName)
183 fontName_actionPerformed();
185 else if (source == fontSize)
187 fontSize_actionPerformed();
189 else if (source == fontStyle)
191 fontStyle_actionPerformed();
193 else if (source == scaleAsCdna)
195 scaleAsCdna_actionPerformed();
197 else if (source == fontAsCdna)
199 mirrorFont_actionPerformed();
204 * Action on checking or unchecking 'use same font across split screen'
205 * option. When checked, the font settings are copied to the other half of the
206 * split screen. When unchecked, the other half is restored to its initial
209 protected void mirrorFont_actionPerformed()
211 boolean selected = fontAsCdna.getState();
212 ap.av.setProteinFontAsCdna(selected);
213 ap.av.getCodingComplement().setProteinFontAsCdna(selected);
217 ap.av.getCodingComplement().setFont(oldComplementFont, true);
223 * Close this dialog on OK to confirm any changes. Also updates the overview
224 * window if displayed.
226 protected void ok_actionPerformed()
228 frame.setVisible(false);
231 if (ap.getOverviewPanel() != null)
233 ap.getOverviewPanel().updateOverviewImage();
239 * Close this dialog on Cancel, reverting to previous font settings.
241 protected void cancel_actionPerformed()
245 ap.av.setScaleProteinAsCdna(oldScaleProtein);
246 ap.av.setProteinFontAsCdna(oldMirrorFont);
248 if (ap.av.getCodingComplement() != null)
250 ap.av.getCodingComplement().setScaleProteinAsCdna(oldScaleProtein);
251 ap.av.getCodingComplement().setProteinFontAsCdna(oldMirrorFont);
252 ap.av.getCodingComplement().setFont(oldComplementFont, true);
253 SplitFrame splitFrame = ap.alignFrame.getSplitFrame();
254 splitFrame.adjustLayout();
255 splitFrame.getComplement(ap.alignFrame).alignPanel.fontChanged();
256 splitFrame.repaint();
259 ap.av.setFont(oldFont, true);
260 if (ap.av.getCharWidth() != oldCharWidth)
262 ap.av.setCharWidth(oldCharWidth);
264 ap.paintAlignment(true, false);
268 tp.setTreeFont(oldFont);
269 tp.treeCanvas.repaint();
272 fontName.select(oldFont.getName());
273 fontSize.select(oldFont.getSize() + "");
274 fontStyle.select(oldFont.getStyle());
276 frame.setVisible(false);
284 if (lastSelected == null)
286 // initialise with original font
287 lastSelected = oldFont;
288 lastSelSize = oldFont.getSize();
289 lastSelStyle = oldFont.getStyle();
292 Font newFont = new Font(fontName.getSelectedItem().toString(),
293 fontStyle.getSelectedIndex(),
294 Integer.parseInt(fontSize.getSelectedItem().toString()));
295 FontMetrics fm = getGraphics().getFontMetrics(newFont);
296 double mw = fm.getStringBounds("M", getGraphics()).getWidth(),
297 iw = fm.getStringBounds("I", getGraphics()).getWidth();
298 if (mw < 1 || iw < 1)
301 fontName.select(lastSelected.getName());
302 fontStyle.select(lastSelStyle);
303 fontSize.select("" + lastSelSize);
304 JVDialog d = new JVDialog(this.frame,
305 MessageManager.getString("label.invalid_font"), true, 350,
307 Panel mp = new Panel();
308 d.cancel.setVisible(false);
309 mp.setLayout(new FlowLayout());
311 "Font doesn't have letters defined\nso cannot be used\nwith alignment data."));
318 tp.setTreeFont(newFont);
322 ap.av.setFont(newFont, true);
326 * and change font in other half of split frame if any
330 if (fontAsCdna.getState())
332 ap.av.getCodingComplement().setFont(newFont, true);
334 SplitFrame splitFrame = ap.alignFrame.getSplitFrame();
335 splitFrame.adjustLayout();
336 splitFrame.getComplement(ap.alignFrame).alignPanel.fontChanged();
337 splitFrame.repaint();
340 // remember last selected
341 lastSelected = newFont;
344 protected void fontName_actionPerformed()
353 protected void fontSize_actionPerformed()
362 protected void fontStyle_actionPerformed()
372 * Construct this panel's contents
376 private void jbInit() throws Exception
378 this.setLayout(new BorderLayout());
379 this.setBackground(Color.white);
381 Label fontLabel = new Label(MessageManager.getString("label.font"));
382 fontLabel.setFont(VERDANA_11PT);
383 fontLabel.setAlignment(Label.RIGHT);
384 fontSize.setFont(VERDANA_11PT);
385 fontSize.addItemListener(this);
386 fontStyle.setFont(VERDANA_11PT);
387 fontStyle.addItemListener(this);
389 Label sizeLabel = new Label(MessageManager.getString("label.size"));
390 sizeLabel.setAlignment(Label.RIGHT);
391 sizeLabel.setFont(VERDANA_11PT);
393 Label styleLabel = new Label(MessageManager.getString("label.style"));
394 styleLabel.setAlignment(Label.RIGHT);
395 styleLabel.setFont(VERDANA_11PT);
397 fontName.setFont(VERDANA_11PT);
398 fontName.addItemListener(this);
400 scaleAsCdna.setLabel(MessageManager.getString("label.scale_as_cdna"));
401 scaleAsCdna.setFont(VERDANA_11PT);
402 scaleAsCdna.addItemListener(this);
403 scaleAsCdna.setState(ap.av.isScaleProteinAsCdna());
405 fontAsCdna.setLabel(MessageManager.getString("label.font_as_cdna"));
406 fontAsCdna.setFont(VERDANA_11PT);
407 fontAsCdna.addItemListener(this);
408 fontAsCdna.setState(ap.av.isProteinFontAsCdna());
410 ok.setFont(VERDANA_11PT);
411 ok.setLabel(MessageManager.getString("action.ok"));
412 ok.addActionListener(new ActionListener()
415 public void actionPerformed(ActionEvent e)
417 ok_actionPerformed();
420 cancel.setFont(VERDANA_11PT);
421 cancel.setLabel(MessageManager.getString("action.cancel"));
422 cancel.addActionListener(new ActionListener()
425 public void actionPerformed(ActionEvent e)
427 cancel_actionPerformed();
431 Panel fontPanel = new Panel();
432 fontPanel.setLayout(new BorderLayout());
433 Panel stylePanel = new Panel();
434 stylePanel.setLayout(new BorderLayout());
435 Panel sizePanel = new Panel();
436 sizePanel.setLayout(new BorderLayout());
437 Panel scalePanel = new Panel();
438 scalePanel.setLayout(new BorderLayout());
439 Panel okCancelPanel = new Panel();
440 Panel optionsPanel = new Panel();
442 fontPanel.setBackground(Color.white);
443 stylePanel.setBackground(Color.white);
444 sizePanel.setBackground(Color.white);
445 okCancelPanel.setBackground(Color.white);
446 optionsPanel.setBackground(Color.white);
448 fontPanel.add(fontLabel, BorderLayout.WEST);
449 fontPanel.add(fontName, BorderLayout.CENTER);
450 stylePanel.add(styleLabel, BorderLayout.WEST);
451 stylePanel.add(fontStyle, BorderLayout.CENTER);
452 sizePanel.add(sizeLabel, BorderLayout.WEST);
453 sizePanel.add(fontSize, BorderLayout.CENTER);
454 scalePanel.add(scaleAsCdna, BorderLayout.NORTH);
455 scalePanel.add(fontAsCdna, BorderLayout.SOUTH);
456 okCancelPanel.add(ok, null);
457 okCancelPanel.add(cancel, null);
459 optionsPanel.add(fontPanel, null);
460 optionsPanel.add(sizePanel, null);
461 optionsPanel.add(stylePanel, null);
464 * Only show 'scale protein as cDNA' in a SplitFrame
466 this.add(optionsPanel, BorderLayout.NORTH);
467 if (ap.alignFrame.getSplitFrame() != null)
470 oldComplementFont = ((AlignViewport) ap.av.getCodingComplement())
472 this.add(scalePanel, BorderLayout.CENTER);
474 this.add(okCancelPanel, BorderLayout.SOUTH);
478 * Turn on/off scaling of protein characters to 3 times the width of cDNA
481 protected void scaleAsCdna_actionPerformed()
483 ap.av.setScaleProteinAsCdna(scaleAsCdna.getState());
484 ap.av.getCodingComplement()
485 .setScaleProteinAsCdna(scaleAsCdna.getState());