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.geom.Rectangle2D;
31 import javax.swing.JInternalFrame;
32 import javax.swing.JLayeredPane;
40 public class FontChooser extends GFontChooser
47 * The font on opening the dialog (to be restored on Cancel)
52 * The font on opening the dialog (to be restored on Cancel)
53 * on the other half of a split frame (if applicable)
55 Font oldComplementFont;
58 * the state of 'scale protein as cDNA' on opening the dialog
60 boolean oldProteinScale;
63 * the state of 'same font for protein and cDNA' on opening the dialog
65 boolean oldMirrorFont;
72 * The last font settings selected in the dialog
74 private Font lastSelected = null;
76 private boolean lastSelMono = false;
78 private boolean oldSmoothFont;
80 private boolean oldComplementSmooth;
83 * Creates a new FontChooser for a tree panel
87 public FontChooser(TreePanel treePanel)
90 ap = treePanel.treeCanvas.ap;
91 oldFont = treePanel.getTreeFont();
92 defaultButton.setVisible(false);
93 smoothFont.setEnabled(false);
98 * Creates a new FontChooser for an alignment panel
102 public FontChooser(AlignmentPanel alignPanel)
104 oldFont = alignPanel.av.getFont();
105 oldProteinScale = alignPanel.av.isScaleProteinAsCdna();
106 oldMirrorFont = alignPanel.av.isProteinFontAsCdna();
107 oldSmoothFont = alignPanel.av.antiAlias;
108 this.ap = alignPanel;
114 frame = new JInternalFrame();
115 frame.setContentPane(this);
117 smoothFont.setSelected(ap.av.antiAlias);
120 * Enable 'scale protein as cDNA' in a SplitFrame view. The selection is
121 * stored in the ViewStyle of both dna and protein Viewport. Also enable
122 * checkbox for copy font changes to other half of split frame.
124 boolean inSplitFrame = ap.av.getCodingComplement() != null;
127 oldComplementFont = ((AlignViewport) ap.av.getCodingComplement())
129 oldComplementSmooth = ((AlignViewport) ap.av.getCodingComplement()).antiAlias;
130 scaleAsCdna.setVisible(true);
131 scaleAsCdna.setSelected(ap.av.isScaleProteinAsCdna());
132 fontAsCdna.setVisible(true);
133 fontAsCdna.setSelected(ap.av.isProteinFontAsCdna());
138 Desktop.addInternalFrame(frame,
139 MessageManager.getString("action.change_font_tree_panel"),
144 Desktop.addInternalFrame(frame,
145 MessageManager.getString("action.change_font"), 380, 220,
149 frame.setLayer(JLayeredPane.PALETTE_LAYER);
151 String[] fonts = java.awt.GraphicsEnvironment
152 .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
154 for (int i = 0; i < fonts.length; i++)
156 fontName.addItem(fonts[i]);
159 for (int i = 1; i < 51; i++)
164 fontStyle.addItem("plain");
165 fontStyle.addItem("bold");
166 fontStyle.addItem("italic");
168 fontName.setSelectedItem(oldFont.getName());
169 fontSize.setSelectedItem(oldFont.getSize());
170 fontStyle.setSelectedIndex(oldFont.getStyle());
172 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
173 monospaced.setSelected(fm.getStringBounds("M", getGraphics())
174 .getWidth() == fm.getStringBounds("|", getGraphics())
181 protected void smoothFont_actionPerformed()
183 ap.av.antiAlias = smoothFont.isSelected();
184 ap.getAnnotationPanel().image = null;
185 ap.paintAlignment(true);
186 if (ap.av.getCodingComplement() != null && ap.av.isProteinFontAsCdna())
188 ((AlignViewport) ap.av.getCodingComplement()).antiAlias = ap.av.antiAlias;
189 SplitFrame sv = (SplitFrame) ap.alignFrame.getSplitViewContainer();
203 protected void ok_actionPerformed()
207 frame.setClosed(true);
208 } catch (Exception ex)
214 if (ap.getOverviewPanel() != null)
216 ap.getOverviewPanel().updateOverviewImage();
228 protected void cancel_actionPerformed()
232 ap.av.setFont(oldFont, true);
233 ap.av.setScaleProteinAsCdna(oldProteinScale);
234 ap.av.setProteinFontAsCdna(oldMirrorFont);
235 ap.av.antiAlias = oldSmoothFont;
236 ap.paintAlignment(true);
238 if (scaleAsCdna.isVisible() && scaleAsCdna.isEnabled())
240 ap.av.getCodingComplement().setScaleProteinAsCdna(oldProteinScale);
241 ap.av.getCodingComplement().setProteinFontAsCdna(oldMirrorFont);
242 ((AlignViewport) ap.av.getCodingComplement()).antiAlias = oldComplementSmooth;
243 ap.av.getCodingComplement().setFont(oldComplementFont, true);
244 SplitFrame splitFrame = (SplitFrame) ap.alignFrame
245 .getSplitViewContainer();
246 splitFrame.adjustLayout();
247 splitFrame.repaint();
252 tp.setTreeFont(oldFont);
257 frame.setClosed(true);
258 } catch (Exception ex)
268 if (lastSelected == null)
270 // initialise with original font
271 lastSelected = oldFont;
272 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
273 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
274 double iw = fm.getStringBounds("I", getGraphics()).getWidth();
275 lastSelMono = (mw == iw); // == on double - flaky?
278 Font newFont = new Font(fontName.getSelectedItem().toString(),
279 fontStyle.getSelectedIndex(),
280 (Integer) fontSize.getSelectedItem());
281 FontMetrics fm = getGraphics().getFontMetrics(newFont);
282 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
283 final Rectangle2D iBounds = fm.getStringBounds("I", getGraphics());
284 double iw = iBounds.getWidth();
285 if (mw < 1 || iw < 1)
287 String message = iBounds.getHeight() < 1 ? MessageManager
288 .getString("label.font_doesnt_have_letters_defined")
289 : MessageManager.getString("label.font_too_small");
290 JvOptionPane.showInternalMessageDialog(this, message,
291 MessageManager.getString("label.invalid_font"),
292 JvOptionPane.WARNING_MESSAGE);
294 * Restore the changed value - note this will reinvoke this method via the
295 * ActionListener, but now validation should pass
297 if (lastSelected.getSize() != (Integer) fontSize.getSelectedItem()) // autoboxing
299 fontSize.setSelectedItem(lastSelected.getSize());
301 if (!lastSelected.getName().equals(
302 fontName.getSelectedItem().toString()))
304 fontName.setSelectedItem(lastSelected.getName());
306 if (lastSelected.getStyle() != fontStyle.getSelectedIndex())
308 fontStyle.setSelectedIndex(lastSelected.getStyle());
310 if (lastSelMono != monospaced.isSelected())
312 monospaced.setSelected(lastSelMono);
318 tp.setTreeFont(newFont);
322 ap.av.setFont(newFont, true);
326 * adjust other half of split frame if any, if either same
327 * font, or proportionate scaling, is selected
329 if (fontAsCdna.isEnabled())
331 if (fontAsCdna.isSelected())
336 ap.av.getCodingComplement().setFont(newFont, true);
340 * adjust layout for font change / reset / sizing
342 SplitFrame splitFrame = (SplitFrame) ap.alignFrame
343 .getSplitViewContainer();
344 splitFrame.adjustLayout();
345 splitFrame.repaint();
349 monospaced.setSelected(mw == iw);
352 * Remember latest valid selection, so it can be restored if followed by an
355 lastSelected = newFont;
359 * Updates on change of selected font name
362 protected void fontName_actionPerformed()
373 * Updates on change of selected font size
376 protected void fontSize_actionPerformed()
387 * Updates on change of selected font style
390 protected void fontStyle_actionPerformed()
401 * Make selected settings the defaults by storing them (via Cache class) in
402 * the .jalview_properties file (the file is only written when Jalview exits)
405 public void defaultButton_actionPerformed()
407 Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
408 Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
409 Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
410 Cache.setProperty("ANTI_ALIAS",
411 Boolean.toString(smoothFont.isSelected()));
412 Cache.setProperty(Preferences.SCALE_PROTEIN_TO_CDNA,
413 Boolean.toString(scaleAsCdna.isSelected()));
417 * Turn on/off scaling of protein characters to 3 times the width of cDNA
421 protected void scaleAsCdna_actionPerformed()
423 ap.av.setScaleProteinAsCdna(scaleAsCdna.isSelected());
424 ap.av.getCodingComplement().setScaleProteinAsCdna(
425 scaleAsCdna.isSelected());
426 final SplitFrame splitFrame = (SplitFrame) ap.alignFrame
427 .getSplitViewContainer();
428 splitFrame.adjustLayout();
429 splitFrame.repaint();
433 * Turn on/off mirroring of font across split frame. If turning on, also
434 * copies the current font across the split frame. If turning off, restores
435 * the other half of the split frame to its initial font.
438 protected void mirrorFonts_actionPerformed()
440 boolean selected = fontAsCdna.isSelected();
441 ap.av.setProteinFontAsCdna(selected);
442 ap.av.getCodingComplement().setProteinFontAsCdna(selected);
445 * reset other half of split frame if turning option off
449 ap.av.getCodingComplement().setFont(oldComplementFont, true);