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.
23 import jalview.bin.Cache;
24 import jalview.jbgui.GFontChooser;
25 import jalview.util.MessageManager;
28 import java.awt.FontMetrics;
29 import java.awt.event.ActionEvent;
30 import java.awt.geom.Rectangle2D;
32 import javax.swing.JInternalFrame;
33 import javax.swing.JLayeredPane;
41 public class FontChooser extends GFontChooser
48 * The font on opening the dialog (to be restored on Cancel)
52 boolean oldProteinScale;
59 * The last font settings selected in the dialog
61 private Font lastSelected = null;
63 private boolean lastSelMono = false;
66 * Creates a new FontChooser object.
71 public FontChooser(TreePanel tp)
74 ap = tp.treeCanvas.ap;
75 oldFont = tp.getTreeFont();
76 defaultButton.setVisible(false);
77 smoothFont.setEnabled(false);
82 * Creates a new FontChooser object.
87 public FontChooser(AlignmentPanel ap)
89 oldFont = ap.av.getFont();
90 oldProteinScale = ap.av.isScaleProteinAsCdna();
98 frame = new JInternalFrame();
99 frame.setContentPane(this);
101 smoothFont.setSelected(ap.av.antiAlias);
104 * Enable 'scale protein as cDNA' in a SplitFrame view. The selection is
105 * stored in the ViewStyle of both dna and protein Viewport. Also enable
106 * checkbox for copy font changes to other half of split frame.
108 boolean inSplitFrame = ap.av.getCodingComplement() != null;
111 scaleAsCdna.setVisible(true);
112 scaleAsCdna.setSelected(ap.av.isScaleProteinAsCdna());
113 fontAsCdna.setVisible(true);
114 fontAsCdna.setSelected(true);
119 Desktop.addInternalFrame(frame,
120 MessageManager.getString("action.change_font_tree_panel"),
125 Desktop.addInternalFrame(frame,
126 MessageManager.getString("action.change_font"), 380, 220,
130 frame.setLayer(JLayeredPane.PALETTE_LAYER);
132 String[] fonts = java.awt.GraphicsEnvironment
133 .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
135 for (int i = 0; i < fonts.length; i++)
137 fontName.addItem(fonts[i]);
140 for (int i = 1; i < 51; i++)
145 fontStyle.addItem("plain");
146 fontStyle.addItem("bold");
147 fontStyle.addItem("italic");
149 fontName.setSelectedItem(oldFont.getName());
150 fontSize.setSelectedItem(oldFont.getSize());
151 fontStyle.setSelectedIndex(oldFont.getStyle());
153 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
154 monospaced.setSelected(fm.getStringBounds("M", getGraphics())
155 .getWidth() == fm.getStringBounds("|", getGraphics())
162 public void smoothFont_actionPerformed(ActionEvent e)
164 ap.av.antiAlias = smoothFont.isSelected();
165 ap.getAnnotationPanel().image = null;
166 ap.paintAlignment(true);
176 protected void ok_actionPerformed(ActionEvent e)
180 frame.setClosed(true);
181 } catch (Exception ex)
187 if (ap.getOverviewPanel() != null)
189 ap.getOverviewPanel().updateOverviewImage();
201 protected void cancel_actionPerformed(ActionEvent e)
205 ap.av.setFont(oldFont, true);
206 ap.av.setScaleProteinAsCdna(oldProteinScale);
207 ap.paintAlignment(true);
208 if (scaleAsCdna.isVisible() && scaleAsCdna.isEnabled())
210 ap.av.getCodingComplement().setScaleProteinAsCdna(oldProteinScale);
211 ap.av.getCodingComplement().setFont(oldFont, true);
216 tp.setTreeFont(oldFont);
218 fontName.setSelectedItem(oldFont.getName());
219 fontSize.setSelectedItem(oldFont.getSize());
220 fontStyle.setSelectedIndex(oldFont.getStyle());
224 frame.setClosed(true);
225 } catch (Exception ex)
235 if (lastSelected == null)
237 // initialise with original font
238 lastSelected = oldFont;
239 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
240 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
241 double iw = fm.getStringBounds("I", getGraphics()).getWidth();
242 lastSelMono = (mw == iw); // == on double - flaky?
245 Font newFont = new Font(fontName.getSelectedItem().toString(),
246 fontStyle.getSelectedIndex(),
247 (Integer) fontSize.getSelectedItem());
248 FontMetrics fm = getGraphics().getFontMetrics(newFont);
249 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
250 final Rectangle2D iBounds = fm.getStringBounds("I", getGraphics());
251 double iw = iBounds.getWidth();
252 if (mw < 1 || iw < 1)
254 String message = iBounds.getHeight() < 1 ? MessageManager
255 .getString("label.font_doesnt_have_letters_defined")
256 : MessageManager.getString("label.font_too_small");
257 JvOptionPane.showInternalMessageDialog(this, message,
258 MessageManager.getString("label.invalid_font"),
259 JvOptionPane.WARNING_MESSAGE);
261 * Restore the changed value - note this will reinvoke this method via the
262 * ActionListener, but now validation should pass
264 if (lastSelected.getSize() != (Integer) fontSize.getSelectedItem()) // autoboxing
266 fontSize.setSelectedItem(lastSelected.getSize());
268 if (!lastSelected.getName().equals(
269 fontName.getSelectedItem().toString()))
271 fontName.setSelectedItem(lastSelected.getName());
273 if (lastSelected.getStyle() != fontStyle.getSelectedIndex())
275 fontStyle.setSelectedIndex(lastSelected.getStyle());
277 if (lastSelMono != monospaced.isSelected())
279 monospaced.setSelected(lastSelMono);
285 tp.setTreeFont(newFont);
289 ap.av.setFont(newFont, true);
293 * and change font in other half of split frame if any
295 if (fontAsCdna.isEnabled() && fontAsCdna.isSelected())
297 ap.av.getCodingComplement().setFont(newFont, true);
298 SplitFrame splitFrame = (SplitFrame) ap.alignFrame
299 .getSplitViewContainer();
300 splitFrame.repaint();
304 monospaced.setSelected(mw == iw);
307 * Remember latest valid selection, so it can be restored if followed by an
310 lastSelected = newFont;
320 protected void fontName_actionPerformed(ActionEvent e)
337 protected void fontSize_actionPerformed(ActionEvent e)
354 protected void fontStyle_actionPerformed(ActionEvent e)
365 * Make selected settings the defaults by storing them (via Cache class) in
366 * the .jalview_properties file (the file is only written when Jalview exits)
371 public void defaultButton_actionPerformed(ActionEvent e)
373 Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
374 Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
375 Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
376 Cache.setProperty("ANTI_ALIAS",
377 Boolean.toString(smoothFont.isSelected()));
378 Cache.setProperty(Preferences.SCALE_PROTEIN_TO_CDNA,
379 Boolean.toString(scaleAsCdna.isSelected()));
383 * Turn on/off scaling of protein characters to 3 times the width of cDNA
387 protected void scaleAsCdna_actionPerformed(ActionEvent e)
389 ap.av.setScaleProteinAsCdna(scaleAsCdna.isSelected());
390 ap.av.getCodingComplement().setScaleProteinAsCdna(
391 scaleAsCdna.isSelected());
392 final SplitFrame splitFrame = (SplitFrame) ap.alignFrame
393 .getSplitViewContainer();
394 splitFrame.adjustLayout();
395 splitFrame.repaint();