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.bin.JalviewLite;
25 import jalview.util.MessageManager;
27 import java.awt.BorderLayout;
28 import awt2swing.Button;
29 import awt2swing.Checkbox;
30 import awt2swing.Choice;
31 import java.awt.Color;
32 import java.awt.FlowLayout;
34 import java.awt.FontMetrics;
35 import awt2swing.Frame;
36 import awt2swing.Label;
37 import awt2swing.Panel;
38 import java.awt.Toolkit;
39 import java.awt.event.ActionEvent;
40 import java.awt.event.ActionListener;
41 import java.awt.event.ItemEvent;
42 import java.awt.event.ItemListener;
45 * This dialog allows the user to try different font settings and related
46 * options. Changes are immediately visible on the alignment or tree. The user
47 * can dismiss the dialog by confirming changes with 'OK', or reverting to
48 * previous settings with 'Cancel'.
50 @SuppressWarnings("serial")
51 public class FontChooser extends Panel implements ItemListener
53 private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
55 private Choice fontSize = new Choice();
57 private Choice fontStyle = new Choice();
59 private Choice fontName = new Choice();
61 private Checkbox scaleAsCdna = new Checkbox();
63 private Button ok = new Button();
65 private Button cancel = new Button();
67 private AlignmentPanel ap;
73 private int oldCharWidth = 0;
75 private boolean oldScaleProtein = false;
77 private Font lastSelected = null;
79 private int lastSelStyle = 0;
81 private int lastSelSize = 0;
83 private boolean init = true;
88 * Constructor for a TreePanel font chooser
92 public FontChooser(TreePanel tp)
103 oldFont = tp.getTreeFont();
108 * Constructor for an AlignmentPanel font chooser
112 public FontChooser(AlignmentPanel ap)
115 oldFont = ap.av.getFont();
116 oldCharWidth = ap.av.getViewStyle().getCharWidth();
117 oldScaleProtein = ap.av.getViewStyle().isScaleProteinAsCdna();
122 } catch (Exception e)
130 * Populate choice lists and open this dialog
134 String fonts[] = Toolkit.getDefaultToolkit().getFontList();
135 for (int i = 0; i < fonts.length; i++)
137 fontName.addItem(fonts[i]);
140 for (int i = 1; i < 31; i++)
142 fontSize.addItem(i + "");
145 fontStyle.addItem("plain");
146 fontStyle.addItem("bold");
147 fontStyle.addItem("italic");
149 fontName.select(oldFont.getName());
150 fontSize.select(oldFont.getSize() + "");
151 fontStyle.select(oldFont.getStyle());
153 this.frame = new Frame();
155 JalviewLite.addFrame(frame,
156 MessageManager.getString("action.change_font"), 440, 115);
162 * Actions on change of font name, size or style.
164 public void itemStateChanged(ItemEvent evt)
166 final Object source = evt.getSource();
167 if (source == fontName)
169 fontName_actionPerformed();
171 else if (source == fontSize)
173 fontSize_actionPerformed();
175 else if (source == fontStyle)
177 fontStyle_actionPerformed();
179 else if (source == scaleAsCdna)
181 scaleAsCdna_actionPerformed();
186 * Close this dialog on OK to confirm any changes. Also updates the overview
187 * window if displayed.
189 protected void ok_actionPerformed()
191 frame.setVisible(false);
194 if (ap.getOverviewPanel() != null)
196 ap.getOverviewPanel().updateOverviewImage();
202 * Close this dialog on Cancel, reverting to previous font settings.
204 protected void cancel_actionPerformed()
208 ap.av.setScaleProteinAsCdna(oldScaleProtein);
209 if (ap.av.getCodingComplement() != null)
211 ap.av.getCodingComplement().setScaleProteinAsCdna(oldScaleProtein);
212 ap.alignFrame.getSplitFrame().repaint();
215 ap.av.setFont(oldFont);
216 ViewStyleI style = ap.av.getViewStyle();
217 if (style.getCharWidth() != oldCharWidth)
219 style.setCharWidth(oldCharWidth);
220 ap.av.setViewStyle(style);
222 ap.paintAlignment(true);
226 tp.setTreeFont(oldFont);
227 tp.treeCanvas.repaint();
230 fontName.select(oldFont.getName());
231 fontSize.select(oldFont.getSize() + "");
232 fontStyle.select(oldFont.getStyle());
234 frame.setVisible(false);
242 if (lastSelected == null)
244 // initialise with original font
245 lastSelected = oldFont;
246 lastSelSize = oldFont.getSize();
247 lastSelStyle = oldFont.getStyle();
250 Font newFont = new Font(fontName.getSelectedItem().toString(),
251 fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
252 .getSelectedItem().toString()));
253 FontMetrics fm = getGraphics().getFontMetrics(newFont);
254 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
255 .getStringBounds("I", getGraphics()).getWidth();
256 if (mw < 1 || iw < 1)
259 fontName.select(lastSelected.getName());
260 fontStyle.select(lastSelStyle);
261 fontSize.select("" + lastSelSize);
262 JVDialog d = new JVDialog(this.frame,
263 MessageManager.getString("label.invalid_font"), true, 350,
265 Panel mp = new Panel();
266 d.cancel.setVisible(false);
267 mp.setLayout(new FlowLayout());
269 "Font doesn't have letters defined\nso cannot be used\nwith alignment data."));
276 tp.setTreeFont(newFont);
280 ap.av.setFont(newFont);
283 // remember last selected
284 lastSelected = newFont;
287 protected void fontName_actionPerformed()
296 protected void fontSize_actionPerformed()
305 protected void fontStyle_actionPerformed()
315 * Construct this panel's contents
319 private void jbInit() throws Exception
321 this.setLayout(new BorderLayout());
322 this.setBackground(Color.white);
324 Label fontLabel = new Label(MessageManager.getString("label.font"));
325 fontLabel.setFont(VERDANA_11PT);
326 fontLabel.setAlignment(Label.RIGHT);
327 fontSize.setFont(VERDANA_11PT);
328 fontSize.addItemListener(this);
329 fontStyle.setFont(VERDANA_11PT);
330 fontStyle.addItemListener(this);
332 Label sizeLabel = new Label(MessageManager.getString("label.size"));
333 sizeLabel.setAlignment(Label.RIGHT);
334 sizeLabel.setFont(VERDANA_11PT);
336 Label styleLabel = new Label(MessageManager.getString("label.style"));
337 styleLabel.setAlignment(Label.RIGHT);
338 styleLabel.setFont(VERDANA_11PT);
340 fontName.setFont(VERDANA_11PT);
341 fontName.addItemListener(this);
343 scaleAsCdna.setLabel(MessageManager.getString("label.scale_as_cdna"));
344 scaleAsCdna.setFont(VERDANA_11PT);
345 scaleAsCdna.addItemListener(this);
346 scaleAsCdna.setState(ap.av.isScaleProteinAsCdna());
348 ok.setFont(VERDANA_11PT);
349 ok.setLabel(MessageManager.getString("action.ok"));
350 ok.addActionListener(new ActionListener()
353 public void actionPerformed(ActionEvent e)
355 ok_actionPerformed();
358 cancel.setFont(VERDANA_11PT);
359 cancel.setLabel(MessageManager.getString("action.cancel"));
360 cancel.addActionListener(new ActionListener()
363 public void actionPerformed(ActionEvent e)
365 cancel_actionPerformed();
369 Panel fontPanel = new Panel();
370 fontPanel.setLayout(new BorderLayout());
371 Panel stylePanel = new Panel();
372 stylePanel.setLayout(new BorderLayout());
373 Panel sizePanel = new Panel();
374 sizePanel.setLayout(new BorderLayout());
375 Panel scalePanel = new Panel();
376 scalePanel.setLayout(new BorderLayout());
377 Panel okCancelPanel = new Panel();
378 Panel optionsPanel = new Panel();
380 fontPanel.setBackground(Color.white);
381 stylePanel.setBackground(Color.white);
382 sizePanel.setBackground(Color.white);
383 okCancelPanel.setBackground(Color.white);
384 optionsPanel.setBackground(Color.white);
386 fontPanel.add(fontLabel, BorderLayout.WEST);
387 fontPanel.add(fontName, BorderLayout.CENTER);
388 stylePanel.add(styleLabel, BorderLayout.WEST);
389 stylePanel.add(fontStyle, BorderLayout.CENTER);
390 sizePanel.add(sizeLabel, BorderLayout.WEST);
391 sizePanel.add(fontSize, BorderLayout.CENTER);
392 scalePanel.add(scaleAsCdna, BorderLayout.CENTER);
393 okCancelPanel.add(ok, null);
394 okCancelPanel.add(cancel, null);
396 optionsPanel.add(fontPanel, null);
397 optionsPanel.add(sizePanel, null);
398 optionsPanel.add(stylePanel, null);
401 * Only show 'scale protein as cDNA' in a SplitFrame
403 this.add(optionsPanel, BorderLayout.NORTH);
404 if (ap.alignFrame.getSplitFrame() != null)
406 this.add(scalePanel, BorderLayout.CENTER);
408 this.add(okCancelPanel, BorderLayout.SOUTH);
412 * Turn on/off scaling of protein characters to 3 times the width of cDNA
415 protected void scaleAsCdna_actionPerformed()
417 ap.av.setScaleProteinAsCdna(scaleAsCdna.getState());
418 ap.av.getCodingComplement().setScaleProteinAsCdna(
419 scaleAsCdna.getState());
420 ap.alignFrame.getSplitFrame().adjustLayout();
421 ap.paintAlignment(true);
422 ap.alignFrame.getSplitFrame().repaint();