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;
34 import javax.swing.JOptionPane;
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())
161 public void smoothFont_actionPerformed(ActionEvent e)
163 ap.av.antiAlias = smoothFont.isSelected();
164 ap.getAnnotationPanel().image = null;
165 ap.paintAlignment(true);
175 protected void ok_actionPerformed(ActionEvent e)
179 frame.setClosed(true);
180 } catch (Exception ex)
186 if (ap.getOverviewPanel() != null)
188 ap.getOverviewPanel().updateOverviewImage();
200 protected void cancel_actionPerformed(ActionEvent e)
204 ap.av.setFont(oldFont, true);
205 ap.av.setScaleProteinAsCdna(oldProteinScale);
206 ap.paintAlignment(true);
207 if (scaleAsCdna.isEnabled())
209 ap.av.setScaleProteinAsCdna(oldProteinScale);
210 ap.av.getCodingComplement().setScaleProteinAsCdna(oldProteinScale);
215 tp.setTreeFont(oldFont);
217 fontName.setSelectedItem(oldFont.getName());
218 fontSize.setSelectedItem(oldFont.getSize());
219 fontStyle.setSelectedIndex(oldFont.getStyle());
223 frame.setClosed(true);
224 } catch (Exception ex)
234 if (lastSelected == null)
236 // initialise with original font
237 lastSelected = oldFont;
238 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
239 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
240 double iw = fm.getStringBounds("I", getGraphics()).getWidth();
241 lastSelMono = (mw == iw); // == on double - flaky?
244 Font newFont = new Font(fontName.getSelectedItem().toString(),
245 fontStyle.getSelectedIndex(),
246 (Integer) fontSize.getSelectedItem());
247 FontMetrics fm = getGraphics().getFontMetrics(newFont);
248 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
249 final Rectangle2D iBounds = fm.getStringBounds("I", getGraphics());
250 double iw = iBounds.getWidth();
251 if (mw < 1 || iw < 1)
253 String message = iBounds.getHeight() < 1 ? MessageManager
254 .getString("label.font_doesnt_have_letters_defined")
255 : MessageManager.getString("label.font_too_small");
256 JvOptionPane.showInternalMessageDialog(this, message,
257 MessageManager.getString("label.invalid_font"),
258 JvOptionPane.WARNING_MESSAGE);
260 * Restore the changed value - note this will reinvoke this method via the
261 * ActionListener, but now validation should pass
263 if (lastSelected.getSize() != (Integer) fontSize.getSelectedItem()) // autoboxing
265 fontSize.setSelectedItem(lastSelected.getSize());
267 if (!lastSelected.getName().equals(
268 fontName.getSelectedItem().toString()))
270 fontName.setSelectedItem(lastSelected.getName());
272 if (lastSelected.getStyle() != fontStyle.getSelectedIndex())
274 fontStyle.setSelectedIndex(lastSelected.getStyle());
276 if (lastSelMono != monospaced.isSelected())
278 monospaced.setSelected(lastSelMono);
284 tp.setTreeFont(newFont);
288 ap.av.setFont(newFont, true);
292 monospaced.setSelected(mw == iw);
295 * Remember latest valid selection, so it can be restored if followed by an
298 lastSelected = newFont;
308 protected void fontName_actionPerformed(ActionEvent e)
325 protected void fontSize_actionPerformed(ActionEvent e)
342 protected void fontStyle_actionPerformed(ActionEvent e)
353 * Make selected settings the defaults by storing them (via Cache class) in
354 * the .jalview_properties file (the file is only written when Jalview exits)
359 public void defaultButton_actionPerformed(ActionEvent e)
361 Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
362 Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
363 Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
364 Cache.setProperty("ANTI_ALIAS",
365 Boolean.toString(smoothFont.isSelected()));
366 Cache.setProperty(Preferences.SCALE_PROTEIN_TO_CDNA,
367 Boolean.toString(scaleAsCdna.isSelected()));
371 * Turn on/off scaling of protein characters to 3 times the width of cDNA
375 protected void scaleAsCdna_actionPerformed(ActionEvent e)
377 ap.av.setScaleProteinAsCdna(scaleAsCdna.isSelected());
378 ap.av.getCodingComplement().setScaleProteinAsCdna(
379 scaleAsCdna.isSelected());
380 final SplitFrame splitFrame = (SplitFrame) ap.alignFrame
381 .getSplitViewContainer();
382 splitFrame.adjustLayout();
383 splitFrame.repaint();
384 // ap.paintAlignment(true);
385 // TODO would like to repaint