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.
24 import java.awt.FontMetrics;
25 import java.awt.event.ActionEvent;
26 import java.awt.geom.Rectangle2D;
28 import javax.swing.JInternalFrame;
29 import javax.swing.JLayeredPane;
30 import javax.swing.JOptionPane;
32 import jalview.bin.Cache;
33 import jalview.jbgui.GFontChooser;
34 import jalview.util.MessageManager;
42 public class FontChooser extends GFontChooser
49 * The font on opening the dialog (to be restored on Cancel)
53 boolean oldProteinScale;
60 * The last font settings selected in the dialog
62 private Font lastSelected = null;
64 private boolean lastSelMono = false;
67 * Creates a new FontChooser object.
72 public FontChooser(TreePanel tp)
75 ap = tp.treeCanvas.ap;
76 oldFont = tp.getTreeFont();
77 defaultButton.setVisible(false);
78 smoothFont.setEnabled(false);
83 * Creates a new FontChooser object.
88 public FontChooser(AlignmentPanel ap)
90 oldFont = ap.av.getFont();
91 oldProteinScale = ap.av.isScaleProteinAsCdna();
99 frame = new JInternalFrame();
100 frame.setContentPane(this);
102 smoothFont.setSelected(ap.av.antiAlias);
105 * Enable 'scale protein as cDNA' in a SplitFrame view. The selection is
106 * stored in the ViewStyle of both dna and protein Viewport
108 scaleAsCdna.setEnabled(false);
109 if (ap.av.getCodingComplement() != null)
111 scaleAsCdna.setEnabled(true);
112 scaleAsCdna.setVisible(true);
113 scaleAsCdna.setSelected(ap.av.isScaleProteinAsCdna());
118 Desktop.addInternalFrame(frame,
119 MessageManager.getString("action.change_font_tree_panel"),
124 Desktop.addInternalFrame(frame,
125 MessageManager.getString("action.change_font"), 380, 200,
129 frame.setLayer(JLayeredPane.PALETTE_LAYER);
131 String[] fonts = java.awt.GraphicsEnvironment
132 .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
134 for (int i = 0; i < fonts.length; i++)
136 fontName.addItem(fonts[i]);
139 for (int i = 1; i < 51; i++)
144 fontStyle.addItem("plain");
145 fontStyle.addItem("bold");
146 fontStyle.addItem("italic");
148 fontName.setSelectedItem(oldFont.getName());
149 fontSize.setSelectedItem(oldFont.getSize());
150 fontStyle.setSelectedIndex(oldFont.getStyle());
152 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
153 monospaced.setSelected(fm.getStringBounds("M", getGraphics())
154 .getWidth() == fm.getStringBounds("|", getGraphics())
160 public void smoothFont_actionPerformed(ActionEvent e)
162 ap.av.antiAlias = smoothFont.isSelected();
163 ap.getAnnotationPanel().image = null;
164 ap.paintAlignment(true);
173 protected void ok_actionPerformed(ActionEvent e)
177 frame.setClosed(true);
178 } catch (Exception ex)
184 if (ap.getOverviewPanel() != null)
186 ap.getOverviewPanel().updateOverviewImage();
197 protected void cancel_actionPerformed(ActionEvent e)
201 ap.av.setFont(oldFont, true);
202 ap.av.setScaleProteinAsCdna(oldProteinScale);
203 ap.paintAlignment(true);
204 if (scaleAsCdna.isEnabled())
206 ap.av.setScaleProteinAsCdna(oldProteinScale);
207 ap.av.getCodingComplement().setScaleProteinAsCdna(oldProteinScale);
212 tp.setTreeFont(oldFont);
214 fontName.setSelectedItem(oldFont.getName());
215 fontSize.setSelectedItem(oldFont.getSize());
216 fontStyle.setSelectedIndex(oldFont.getStyle());
220 frame.setClosed(true);
221 } catch (Exception ex)
231 if (lastSelected == null)
233 // initialise with original font
234 lastSelected = oldFont;
235 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
236 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
237 double iw = fm.getStringBounds("I", getGraphics()).getWidth();
238 lastSelMono = (mw == iw); // == on double - flaky?
241 Font newFont = new Font(fontName.getSelectedItem().toString(),
242 fontStyle.getSelectedIndex(),
243 (Integer) fontSize.getSelectedItem());
244 FontMetrics fm = getGraphics().getFontMetrics(newFont);
245 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
246 final Rectangle2D iBounds = fm.getStringBounds("I", getGraphics());
247 double iw = iBounds.getWidth();
248 if (mw < 1 || iw < 1)
250 final String messageKey = iBounds.getHeight() < 1 ? "label.font_doesnt_have_letters_defined"
251 : "label.font_too_small";
253 .showInternalMessageDialog(
255 MessageManager.getString(messageKey),
256 MessageManager.getString("label.invalid_font"), JOptionPane.WARNING_MESSAGE);
258 * Restore the changed value - note this will reinvoke this method via the
259 * ActionListener, but now validation should pass
261 if (lastSelected.getSize() != (Integer) fontSize.getSelectedItem()) // autoboxing
263 fontSize.setSelectedItem(lastSelected.getSize());
265 if (!lastSelected.getName().equals(
266 fontName.getSelectedItem().toString()))
268 fontName.setSelectedItem(lastSelected.getName());
270 if (lastSelected.getStyle() != fontStyle.getSelectedIndex())
272 fontStyle.setSelectedIndex(lastSelected.getStyle());
274 if (lastSelMono != monospaced.isSelected())
276 monospaced.setSelected(lastSelMono);
282 tp.setTreeFont(newFont);
286 ap.av.setFont(newFont, true);
290 monospaced.setSelected(mw == iw);
293 * Remember latest valid selection, so it can be restored if followed by an
296 lastSelected = newFont;
305 protected void fontName_actionPerformed(ActionEvent e)
321 protected void fontSize_actionPerformed(ActionEvent e)
337 protected void fontStyle_actionPerformed(ActionEvent e)
348 * Make selected settings the defaults by storing them (via Cache class) in
349 * the .jalview_properties file (the file is only written when Jalview exits)
353 public void defaultButton_actionPerformed(ActionEvent e)
355 Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
356 Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
357 Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
358 Cache.setProperty("ANTI_ALIAS",
359 Boolean.toString(smoothFont.isSelected()));
360 Cache.setProperty(Preferences.SCALE_PROTEIN_TO_CDNA,
361 Boolean.toString(scaleAsCdna.isSelected()));
365 * Turn on/off scaling of protein characters to 3 times the width of cDNA
369 protected void scaleAsCdna_actionPerformed(ActionEvent e)
371 ap.av.setScaleProteinAsCdna(scaleAsCdna.isSelected());
372 ap.av.getCodingComplement().setScaleProteinAsCdna(
373 scaleAsCdna.isSelected());
374 final SplitFrame splitFrame = (SplitFrame) ap.alignFrame
375 .getSplitViewContainer();
376 splitFrame.adjustLayout();
377 splitFrame.repaint();
378 // ap.paintAlignment(true);
379 // TODO would like to repaint