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;
79 * Creates a new FontChooser for a tree panel
83 public FontChooser(TreePanel treePanel)
86 ap = treePanel.treeCanvas.ap;
87 oldFont = treePanel.getTreeFont();
88 defaultButton.setVisible(false);
89 smoothFont.setEnabled(false);
94 * Creates a new FontChooser for an alignment panel
98 public FontChooser(AlignmentPanel alignPanel)
100 oldFont = alignPanel.av.getFont();
101 oldProteinScale = alignPanel.av.isScaleProteinAsCdna();
102 oldMirrorFont = alignPanel.av.isProteinFontAsCdna();
104 this.ap = alignPanel;
110 frame = new JInternalFrame();
111 frame.setContentPane(this);
113 smoothFont.setSelected(ap.av.antiAlias);
116 * Enable 'scale protein as cDNA' in a SplitFrame view. The selection is
117 * stored in the ViewStyle of both dna and protein Viewport. Also enable
118 * checkbox for copy font changes to other half of split frame.
120 boolean inSplitFrame = ap.av.getCodingComplement() != null;
123 oldComplementFont = ((AlignViewport) ap.av.getCodingComplement())
125 scaleAsCdna.setVisible(true);
126 scaleAsCdna.setSelected(ap.av.isScaleProteinAsCdna());
127 fontAsCdna.setVisible(true);
128 fontAsCdna.setSelected(ap.av.isProteinFontAsCdna());
133 Desktop.addInternalFrame(frame,
134 MessageManager.getString("action.change_font_tree_panel"),
139 Desktop.addInternalFrame(frame,
140 MessageManager.getString("action.change_font"), 380, 220,
144 frame.setLayer(JLayeredPane.PALETTE_LAYER);
146 String[] fonts = java.awt.GraphicsEnvironment
147 .getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
149 for (int i = 0; i < fonts.length; i++)
151 fontName.addItem(fonts[i]);
154 for (int i = 1; i < 51; i++)
159 fontStyle.addItem("plain");
160 fontStyle.addItem("bold");
161 fontStyle.addItem("italic");
163 fontName.setSelectedItem(oldFont.getName());
164 fontSize.setSelectedItem(oldFont.getSize());
165 fontStyle.setSelectedIndex(oldFont.getStyle());
167 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
168 monospaced.setSelected(fm.getStringBounds("M", getGraphics())
169 .getWidth() == fm.getStringBounds("|", getGraphics())
176 protected void smoothFont_actionPerformed()
178 ap.av.antiAlias = smoothFont.isSelected();
179 ap.getAnnotationPanel().image = null;
180 ap.paintAlignment(true);
190 protected void ok_actionPerformed()
194 frame.setClosed(true);
195 } catch (Exception ex)
201 if (ap.getOverviewPanel() != null)
203 ap.getOverviewPanel().updateOverviewImage();
215 protected void cancel_actionPerformed()
219 ap.av.setFont(oldFont, true);
220 ap.av.setScaleProteinAsCdna(oldProteinScale);
221 ap.av.setProteinFontAsCdna(oldMirrorFont);
222 ap.paintAlignment(true);
224 if (scaleAsCdna.isVisible() && scaleAsCdna.isEnabled())
226 ap.av.getCodingComplement().setScaleProteinAsCdna(oldProteinScale);
227 ap.av.getCodingComplement().setProteinFontAsCdna(oldMirrorFont);
228 ap.av.getCodingComplement().setFont(oldComplementFont, true);
229 SplitFrame splitFrame = (SplitFrame) ap.alignFrame
230 .getSplitViewContainer();
231 splitFrame.adjustLayout();
232 splitFrame.repaint();
237 tp.setTreeFont(oldFont);
242 frame.setClosed(true);
243 } catch (Exception ex)
253 if (lastSelected == null)
255 // initialise with original font
256 lastSelected = oldFont;
257 FontMetrics fm = getGraphics().getFontMetrics(oldFont);
258 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
259 double iw = fm.getStringBounds("I", getGraphics()).getWidth();
260 lastSelMono = (mw == iw); // == on double - flaky?
263 Font newFont = new Font(fontName.getSelectedItem().toString(),
264 fontStyle.getSelectedIndex(),
265 (Integer) fontSize.getSelectedItem());
266 FontMetrics fm = getGraphics().getFontMetrics(newFont);
267 double mw = fm.getStringBounds("M", getGraphics()).getWidth();
268 final Rectangle2D iBounds = fm.getStringBounds("I", getGraphics());
269 double iw = iBounds.getWidth();
270 if (mw < 1 || iw < 1)
272 String message = iBounds.getHeight() < 1 ? MessageManager
273 .getString("label.font_doesnt_have_letters_defined")
274 : MessageManager.getString("label.font_too_small");
275 JvOptionPane.showInternalMessageDialog(this, message,
276 MessageManager.getString("label.invalid_font"),
277 JvOptionPane.WARNING_MESSAGE);
279 * Restore the changed value - note this will reinvoke this method via the
280 * ActionListener, but now validation should pass
282 if (lastSelected.getSize() != (Integer) fontSize.getSelectedItem()) // autoboxing
284 fontSize.setSelectedItem(lastSelected.getSize());
286 if (!lastSelected.getName().equals(
287 fontName.getSelectedItem().toString()))
289 fontName.setSelectedItem(lastSelected.getName());
291 if (lastSelected.getStyle() != fontStyle.getSelectedIndex())
293 fontStyle.setSelectedIndex(lastSelected.getStyle());
295 if (lastSelMono != monospaced.isSelected())
297 monospaced.setSelected(lastSelMono);
303 tp.setTreeFont(newFont);
307 ap.av.setFont(newFont, true);
311 * adjust other half of split frame if any, if either same
312 * font, or proportionate scaling, is selected
314 if (fontAsCdna.isEnabled())
316 if (fontAsCdna.isSelected())
321 ap.av.getCodingComplement().setFont(newFont, true);
325 * adjust layout for font change / reset / sizing
327 SplitFrame splitFrame = (SplitFrame) ap.alignFrame
328 .getSplitViewContainer();
329 splitFrame.adjustLayout();
330 splitFrame.repaint();
334 monospaced.setSelected(mw == iw);
337 * Remember latest valid selection, so it can be restored if followed by an
340 lastSelected = newFont;
344 * Updates on change of selected font name
347 protected void fontName_actionPerformed()
358 * Updates on change of selected font size
361 protected void fontSize_actionPerformed()
372 * Updates on change of selected font style
375 protected void fontStyle_actionPerformed()
386 * Make selected settings the defaults by storing them (via Cache class) in
387 * the .jalview_properties file (the file is only written when Jalview exits)
390 public void defaultButton_actionPerformed()
392 Cache.setProperty("FONT_NAME", fontName.getSelectedItem().toString());
393 Cache.setProperty("FONT_STYLE", fontStyle.getSelectedIndex() + "");
394 Cache.setProperty("FONT_SIZE", fontSize.getSelectedItem().toString());
395 Cache.setProperty("ANTI_ALIAS",
396 Boolean.toString(smoothFont.isSelected()));
397 Cache.setProperty(Preferences.SCALE_PROTEIN_TO_CDNA,
398 Boolean.toString(scaleAsCdna.isSelected()));
402 * Turn on/off scaling of protein characters to 3 times the width of cDNA
406 protected void scaleAsCdna_actionPerformed()
408 ap.av.setScaleProteinAsCdna(scaleAsCdna.isSelected());
409 ap.av.getCodingComplement().setScaleProteinAsCdna(
410 scaleAsCdna.isSelected());
411 final SplitFrame splitFrame = (SplitFrame) ap.alignFrame
412 .getSplitViewContainer();
413 splitFrame.adjustLayout();
414 splitFrame.repaint();
418 * Turn on/off mirroring of font across split frame. If turning on, also
419 * copies the current font across the split frame. If turning off, restores
420 * the other half of the split frame to its initial font.
423 protected void mirrorFonts_actionPerformed()
425 boolean selected = fontAsCdna.isSelected();
426 ap.av.setProteinFontAsCdna(selected);
427 ap.av.getCodingComplement().setProteinFontAsCdna(selected);
430 * reset other half of split frame if turning option off
434 ap.av.getCodingComplement().setFont(oldComplementFont, true);