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.geom.Rectangle2D;
27 import javax.swing.JInternalFrame;
28 import javax.swing.JLayeredPane;
30 import jalview.bin.Cache;
31 import jalview.jbgui.GFontChooser;
32 import jalview.util.MessageManager;
40 public class FontChooser extends GFontChooser
46 CutAndPasteTransfer cap;
49 * The font on opening the dialog (to be restored on Cancel)
54 * The font on opening the dialog (to be restored on Cancel)
55 * on the other half of a split frame (if applicable)
57 Font oldComplementFont;
60 * the state of 'scale protein as cDNA' on opening the dialog
62 boolean oldProteinScale;
65 * the state of 'same font for protein and cDNA' on opening the dialog
67 boolean oldMirrorFont;
74 * The last font settings selected in the dialog
76 private Font lastSelected = null;
78 private boolean lastSelMono = false;
80 private boolean oldSmoothFont;
82 private boolean oldComplementSmooth;
85 * Creates a new FontChooser for a tree panel
89 public FontChooser(TreePanel treePanel)
92 ap = treePanel.getTreeCanvas().getAssociatedPanel();
93 oldFont = treePanel.getTreeFont();
94 defaultButton.setVisible(false);
95 smoothFont.setEnabled(false);
100 * Creates a new FontChooser for an alignment panel
104 public FontChooser(AlignmentPanel alignPanel)
106 oldFont = alignPanel.av.getFont();
107 oldProteinScale = alignPanel.av.isScaleProteinAsCdna();
108 oldMirrorFont = alignPanel.av.isProteinFontAsCdna();
109 oldSmoothFont = alignPanel.av.antiAlias;
110 this.ap = alignPanel;
115 * Creates a new FontChooser for a CutAndPasteTransfer
118 public FontChooser(CutAndPasteTransfer cap)
120 oldFont = new Font("Monospaced", Font.PLAIN, 12);
127 frame = new JInternalFrame();
128 frame.setFrameIcon(null);
129 frame.setContentPane(this);
133 smoothFont.setSelected(ap.av.antiAlias);
136 * Enable 'scale protein as cDNA' in a SplitFrame view. The selection is
137 * stored in the ViewStyle of both dna and protein Viewport. Also enable
138 * checkbox for copy font changes to other half of split frame.
140 boolean inSplitFrame = ap.av.getCodingComplement() != null;
143 oldComplementFont = ((AlignViewport) ap.av.getCodingComplement())
145 oldComplementSmooth = ((AlignViewport) ap.av
146 .getCodingComplement()).antiAlias;
147 scaleAsCdna.setVisible(true);
148 scaleAsCdna.setSelected(ap.av.isScaleProteinAsCdna());
149 fontAsCdna.setVisible(true);
150 fontAsCdna.setSelected(ap.av.isProteinFontAsCdna());
156 Desktop.addInternalFrame(frame,
157 MessageManager.getString("action.change_font_tree_panel"),
162 Desktop.addInternalFrame(frame,
163 MessageManager.getString("action.change_font"), 380, 220,
167 frame.setLayer(JLayeredPane.PALETTE_LAYER);
169 String[] fonts = java.awt.GraphicsEnvironment
170 .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
172 for (int i = 0; i < fonts.length; i++)
174 fontName.addItem(fonts[i]);
177 for (int i = 1; i < 51; i++)
182 fontStyle.addItem("plain");
183 fontStyle.addItem("bold");
184 fontStyle.addItem("italic");
186 fontName.setSelectedItem(oldFont.getName());
187 fontSize.setSelectedItem(oldFont.getSize());
188 fontStyle.setSelectedIndex(oldFont.getStyle());
190 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
191 monospaced.setSelected(
192 fm.getStringBounds("M", getGraphics()).getWidth() == fm
193 .getStringBounds("|", getGraphics()).getWidth());
199 protected void smoothFont_actionPerformed()
201 ap.av.antiAlias = smoothFont.isSelected();
202 ap.getAnnotationPanel().image = null;
203 ap.paintAlignment(true, false);
204 if (ap.av.getCodingComplement() != null && ap.av.isProteinFontAsCdna())
206 ((AlignViewport) ap.av
207 .getCodingComplement()).antiAlias = ap.av.antiAlias;
208 SplitFrame sv = (SplitFrame) ap.alignFrame.getSplitViewContainer();
222 protected void ok_actionPerformed()
226 frame.setClosed(true);
227 } catch (Exception ex)
233 if (ap.getOverviewPanel() != null)
235 ap.getOverviewPanel().updateOverviewImage();
247 protected void cancel_actionPerformed()
251 tp.setTreeFont(oldFont);
255 ap.av.setFont(oldFont, true);
256 ap.av.setScaleProteinAsCdna(oldProteinScale);
257 ap.av.setProteinFontAsCdna(oldMirrorFont);
258 ap.av.antiAlias = oldSmoothFont;
261 if (scaleAsCdna.isVisible() && scaleAsCdna.isEnabled())
263 ap.av.getCodingComplement().setScaleProteinAsCdna(oldProteinScale);
264 ap.av.getCodingComplement().setProteinFontAsCdna(oldMirrorFont);
265 ((AlignViewport) ap.av
266 .getCodingComplement()).antiAlias = oldComplementSmooth;
267 ap.av.getCodingComplement().setFont(oldComplementFont, true);
268 SplitFrame splitFrame = (SplitFrame) ap.alignFrame
269 .getSplitViewContainer();
270 splitFrame.adjustLayout();
271 splitFrame.repaint();
277 frame.setClosed(true);
278 } catch (Exception ex)
283 private boolean isTreeFont()
288 private boolean isCapFont()
297 if (lastSelected == null)
299 // initialise with original font
300 lastSelected = oldFont;
301 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
302 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
303 double iw = fm.getStringBounds("I", getGraphics()).getWidth();
304 lastSelMono = (mw == iw); // == on double - flaky?
307 Font newFont = new Font(fontName.getSelectedItem().toString(),
308 fontStyle.getSelectedIndex(),
309 (Integer) fontSize.getSelectedItem());
310 FontMetrics fm = getGraphics().getFontMetrics(newFont);
311 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
312 final Rectangle2D iBounds = fm.getStringBounds("I", getGraphics());
313 double iw = iBounds.getWidth();
314 if (mw < 1 || iw < 1)
316 String message = iBounds.getHeight() < 1
318 .getString("label.font_doesnt_have_letters_defined")
319 : MessageManager.getString("label.font_too_small");
320 JvOptionPane.showInternalMessageDialog(this, message,
321 MessageManager.getString("label.invalid_font"),
322 JvOptionPane.WARNING_MESSAGE);
324 * Restore the changed value - note this will reinvoke this method via the
325 * ActionListener, but now validation should pass
327 if (lastSelected.getSize() != (Integer) fontSize.getSelectedItem()) // autoboxing
329 fontSize.setSelectedItem(lastSelected.getSize());
331 if (!lastSelected.getName()
332 .equals(fontName.getSelectedItem().toString()))
334 fontName.setSelectedItem(lastSelected.getName());
336 if (lastSelected.getStyle() != fontStyle.getSelectedIndex())
338 fontStyle.setSelectedIndex(lastSelected.getStyle());
340 if (lastSelMono != monospaced.isSelected())
342 monospaced.setSelected(lastSelMono);
348 tp.setTreeFont(newFont);
352 ap.av.setFont(newFont, true);
356 * adjust other half of split frame if present, whether or not same font or
357 * scale to cDNA is selected, because a font change may affect character
358 * width, and this is kept the same in both panels
360 if (fontAsCdna.isVisible())
362 if (fontAsCdna.isSelected())
364 ap.av.getCodingComplement().setFont(newFont, true);
367 SplitFrame splitFrame = (SplitFrame) ap.alignFrame
368 .getSplitViewContainer();
369 splitFrame.adjustLayout();
370 splitFrame.repaint();
373 else if (isCapFont())
375 cap.setFont(newFont);
378 monospaced.setSelected(mw == iw);
381 * Remember latest valid selection, so it can be restored if followed by an
384 lastSelected = newFont;
388 * Updates on change of selected font name
391 protected void fontName_actionPerformed()
402 * Updates on change of selected font size
405 protected void fontSize_actionPerformed()
416 * Updates on change of selected font style
419 protected void fontStyle_actionPerformed()
430 * Make selected settings the defaults by storing them (via Cache class) in
431 * the .jalview_properties file (the file is only written when Jalview exits)
434 public void defaultButton_actionPerformed()
436 Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
437 Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
438 Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
439 Cache.setProperty("ANTI_ALIAS",
440 Boolean.toString(smoothFont.isSelected()));
441 Cache.setProperty(Preferences.SCALE_PROTEIN_TO_CDNA,
442 Boolean.toString(scaleAsCdna.isSelected()));
446 * Turn on/off scaling of protein characters to 3 times the width of cDNA
450 protected void scaleAsCdna_actionPerformed()
452 ap.av.setScaleProteinAsCdna(scaleAsCdna.isSelected());
453 ap.av.getCodingComplement()
454 .setScaleProteinAsCdna(scaleAsCdna.isSelected());
455 final SplitFrame splitFrame = (SplitFrame) ap.alignFrame
456 .getSplitViewContainer();
457 splitFrame.adjustLayout();
458 splitFrame.repaint();
462 * Turn on/off mirroring of font across split frame. If turning on, also
463 * copies the current font across the split frame. If turning off, restores
464 * the other half of the split frame to its initial font.
467 protected void mirrorFonts_actionPerformed()
469 boolean selected = fontAsCdna.isSelected();
470 ap.av.setProteinFontAsCdna(selected);
471 ap.av.getCodingComplement().setProteinFontAsCdna(selected);
474 * reset other half of split frame if turning option off
478 ap.av.getCodingComplement().setFont(oldComplementFont, true);