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;
27 import javax.swing.JInternalFrame;
28 import javax.swing.JLayeredPane;
29 import javax.swing.JOptionPane;
31 import jalview.bin.Cache;
32 import jalview.jbgui.GFontChooser;
33 import jalview.util.MessageManager;
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
107 scaleAsCdna.setEnabled(false);
108 if (ap.av.getCodingComplement() != null)
110 scaleAsCdna.setEnabled(true);
111 scaleAsCdna.setVisible(true);
112 scaleAsCdna.setSelected(ap.av.isScaleProteinAsCdna());
117 Desktop.addInternalFrame(frame,
118 MessageManager.getString("action.change_font_tree_panel"),
123 Desktop.addInternalFrame(frame,
124 MessageManager.getString("action.change_font"), 380, 200,
128 frame.setLayer(JLayeredPane.PALETTE_LAYER);
130 String[] fonts = java.awt.GraphicsEnvironment
131 .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
133 for (int i = 0; i < fonts.length; i++)
135 fontName.addItem(fonts[i]);
138 for (int i = 1; i < 51; i++)
143 fontStyle.addItem("plain");
144 fontStyle.addItem("bold");
145 fontStyle.addItem("italic");
147 fontName.setSelectedItem(oldFont.getName());
148 fontSize.setSelectedItem(oldFont.getSize());
149 fontStyle.setSelectedIndex(oldFont.getStyle());
151 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
152 monospaced.setSelected(fm.getStringBounds("M", getGraphics())
153 .getWidth() == fm.getStringBounds("|", getGraphics())
159 public void smoothFont_actionPerformed(ActionEvent e)
161 ap.av.antiAlias = smoothFont.isSelected();
162 ap.getAnnotationPanel().image = null;
163 ap.paintAlignment(true);
172 protected void ok_actionPerformed(ActionEvent e)
176 frame.setClosed(true);
177 } catch (Exception ex)
183 if (ap.getOverviewPanel() != null)
185 ap.getOverviewPanel().updateOverviewImage();
196 protected void cancel_actionPerformed(ActionEvent e)
200 ap.av.setFont(oldFont, true);
201 ap.av.setScaleProteinAsCdna(oldProteinScale);
202 ap.paintAlignment(true);
203 if (scaleAsCdna.isEnabled())
205 ap.av.setScaleProteinAsCdna(oldProteinScale);
206 ap.av.getCodingComplement().setScaleProteinAsCdna(oldProteinScale);
211 tp.setTreeFont(oldFont);
213 fontName.setSelectedItem(oldFont.getName());
214 fontSize.setSelectedItem(oldFont.getSize());
215 fontStyle.setSelectedIndex(oldFont.getStyle());
219 frame.setClosed(true);
220 } catch (Exception ex)
230 if (lastSelected == null)
232 // initialise with original font
233 lastSelected = oldFont;
234 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
235 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
236 double iw = fm.getStringBounds("I", getGraphics()).getWidth();
237 lastSelMono = (mw == iw); // == on double - flaky?
240 Font newFont = new Font(fontName.getSelectedItem().toString(),
241 fontStyle.getSelectedIndex(),
242 (Integer) fontSize.getSelectedItem());
243 FontMetrics fm = getGraphics().getFontMetrics(newFont);
244 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
245 .getStringBounds("I", getGraphics()).getWidth();
246 if (mw < 1 || iw < 1)
249 .showInternalMessageDialog(
251 MessageManager.getString("label.font_doesnt_have_letters_defined"),
252 MessageManager.getString("label.invalid_font"), JOptionPane.WARNING_MESSAGE);
254 * Restore the changed value - note this will reinvoke this method via the
255 * ActionListener, but now validation should pass
257 if (lastSelected.getSize() != (Integer) fontSize.getSelectedItem()) // autoboxing
259 fontSize.setSelectedItem(lastSelected.getSize());
261 if (!lastSelected.getName().equals(
262 fontName.getSelectedItem().toString()))
264 fontName.setSelectedItem(lastSelected.getName());
266 if (lastSelected.getStyle() != fontStyle.getSelectedIndex())
268 fontStyle.setSelectedIndex(lastSelected.getStyle());
270 if (lastSelMono != monospaced.isSelected())
272 monospaced.setSelected(lastSelMono);
278 tp.setTreeFont(newFont);
282 ap.av.setFont(newFont, true);
286 monospaced.setSelected(mw == iw);
289 * Remember latest valid selection, so it can be restored if followed by an
292 lastSelected = newFont;
301 protected void fontName_actionPerformed(ActionEvent e)
317 protected void fontSize_actionPerformed(ActionEvent e)
333 protected void fontStyle_actionPerformed(ActionEvent e)
344 * Make selected settings the defaults by storing them (via Cache class) in
345 * the .jalview_properties file (the file is only written when Jalview exits)
349 public void defaultButton_actionPerformed(ActionEvent e)
351 Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
352 Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
353 Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
354 Cache.setProperty("ANTI_ALIAS",
355 Boolean.toString(smoothFont.isSelected()));
356 Cache.setProperty(Preferences.SCALE_PROTEIN_TO_CDNA,
357 Boolean.toString(scaleAsCdna.isSelected()));
361 * Turn on/off scaling of protein characters to 3 times the width of cDNA
365 protected void scaleAsCdna_actionPerformed(ActionEvent e)
367 ap.av.setScaleProteinAsCdna(scaleAsCdna.isSelected());
368 ap.av.getCodingComplement().setScaleProteinAsCdna(
369 scaleAsCdna.isSelected());
370 final SplitFrame splitFrame = (SplitFrame) ap.alignFrame
371 .getSplitViewContainer();
372 splitFrame.adjustLayout();
373 splitFrame.repaint();
374 // ap.paintAlignment(true);
375 // TODO would like to repaint