-/*\r
- * Jalview - A Sequence Alignment Editor and Viewer\r
- * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
- *\r
- * This program is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU General Public License\r
- * as published by the Free Software Foundation; either version 2\r
- * of the License, or (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program; if not, write to the Free Software\r
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA\r
- */\r
-\r
-package jalview.appletgui;\r
-\r
-import java.awt.*;\r
-\r
-import java.awt.event.*;\r
-\r
-public class FontChooser\r
- extends Panel implements ActionListener, ItemListener\r
-{\r
- AlignmentPanel ap;\r
- TreePanel tp;\r
- Font oldFont;\r
- boolean init = true;\r
- Frame frame;\r
-\r
- public FontChooser(TreePanel tp)\r
- {\r
- try\r
- {\r
- jbInit();\r
- }\r
- catch (Exception e)\r
- {\r
- e.printStackTrace();\r
- }\r
-\r
- this.tp = tp;\r
- oldFont = tp.getTreeFont();\r
- init();\r
- }\r
-\r
- public FontChooser(AlignmentPanel ap)\r
- {\r
- try\r
- {\r
- jbInit();\r
- }\r
- catch (Exception e)\r
- {\r
- e.printStackTrace();\r
- }\r
-\r
- this.ap = ap;\r
- oldFont = ap.av.getFont();\r
- init();\r
- }\r
-\r
- void init()\r
- {\r
- String fonts[] = Toolkit.getDefaultToolkit().getFontList();\r
- for (int i = 0; i < fonts.length; i++)\r
- {\r
- fontName.addItem(fonts[i]);\r
- }\r
-\r
- for (int i = 1; i < 31; i++)\r
- {\r
- fontSize.addItem(i + "");\r
- }\r
-\r
- fontStyle.addItem("plain");\r
- fontStyle.addItem("bold");\r
- fontStyle.addItem("italic");\r
-\r
- fontName.select(oldFont.getName());\r
- fontSize.select(oldFont.getSize() + "");\r
- fontStyle.select(oldFont.getStyle());\r
-\r
- Frame frame = new Frame();\r
- this.frame = frame;\r
- frame.add(this);\r
- jalview.bin.JalviewLite.addFrame(frame, "Change Font", 440, 115);\r
-\r
- init = false;\r
- }\r
-\r
- public void actionPerformed(ActionEvent evt)\r
- {\r
- if(evt.getSource()==ok)\r
- ok_actionPerformed();\r
- else if(evt.getSource()==cancel)\r
- cancel_actionPerformed();\r
- }\r
-\r
- public void itemStateChanged(ItemEvent evt)\r
- {\r
- if(evt.getSource()==fontName)\r
- fontName_actionPerformed();\r
- else if(evt.getSource()==fontSize)\r
- fontSize_actionPerformed();\r
- else if(evt.getSource()==fontStyle)\r
- fontStyle_actionPerformed();\r
- }\r
-\r
-\r
- protected void ok_actionPerformed()\r
- {\r
- frame.setVisible(false);\r
- if (ap != null)\r
- {\r
- if (ap.getOverviewPanel() != null)\r
- {\r
- ap.getOverviewPanel().updateOverviewImage();\r
- }\r
- }\r
-\r
- }\r
-\r
- protected void cancel_actionPerformed()\r
- {\r
- if(ap!=null)\r
- {\r
- ap.av.setFont(oldFont);\r
- ap.repaint();\r
- }\r
- else if(tp!=null)\r
- {\r
- tp.setTreeFont(oldFont);\r
- tp.treeCanvas.repaint();\r
- }\r
-\r
- fontName.select(oldFont.getName());\r
- fontSize.select(oldFont.getSize() + "");\r
- fontStyle.select(oldFont.getStyle());\r
-\r
- frame.setVisible(false);\r
- }\r
-\r
- void changeFont()\r
- {\r
- Font newFont = new Font(fontName.getSelectedItem().toString(),\r
- fontStyle.getSelectedIndex(),\r
- Integer.parseInt(fontSize.getSelectedItem().\r
- toString())\r
- );\r
- if (ap != null)\r
- {\r
- ap.av.setFont(newFont);\r
- ap.fontChanged();\r
- }\r
- else if(tp != null)\r
- {\r
- tp.setTreeFont(newFont);\r
- }\r
- }\r
-\r
- protected void fontName_actionPerformed()\r
- {\r
- if (init)\r
- {\r
- return;\r
- }\r
- changeFont();\r
- }\r
-\r
- protected void fontSize_actionPerformed()\r
- {\r
- if (init)\r
- {\r
- return;\r
- }\r
- changeFont();\r
- }\r
-\r
- protected void fontStyle_actionPerformed()\r
- {\r
- if (init)\r
- {\r
- return;\r
- }\r
- changeFont();\r
- }\r
- Label label1 = new Label();\r
- protected Choice fontSize = new Choice();\r
- protected Choice fontStyle = new Choice();\r
- Label label2 = new Label();\r
- Label label3 = new Label();\r
- protected Choice fontName = new Choice();\r
- Button ok = new Button();\r
- Button cancel = new Button();\r
- Panel panel1 = new Panel();\r
- Panel panel2 = new Panel();\r
- Panel panel3 = new Panel();\r
- BorderLayout borderLayout1 = new BorderLayout();\r
- BorderLayout borderLayout2 = new BorderLayout();\r
- BorderLayout borderLayout3 = new BorderLayout();\r
- Panel panel4 = new Panel();\r
- Panel panel5 = new Panel();\r
- BorderLayout borderLayout4 = new BorderLayout();\r
-\r
-\r
- private void jbInit() throws Exception {\r
- label1.setFont(new java.awt.Font("Verdana", 0, 11));\r
- label1.setAlignment(Label.RIGHT);\r
- label1.setText("Font: ");\r
- this.setLayout(borderLayout4);\r
- fontSize.setFont(new java.awt.Font("Verdana", 0, 11));\r
- fontSize.addItemListener(this);\r
- fontStyle.setFont(new java.awt.Font("Verdana", 0, 11));\r
- fontStyle.addItemListener(this);\r
- label2.setAlignment(Label.RIGHT);\r
- label2.setFont(new java.awt.Font("Verdana", 0, 11));\r
- label2.setText("Size: ");\r
- label3.setAlignment(Label.RIGHT);\r
- label3.setFont(new java.awt.Font("Verdana", 0, 11));\r
- label3.setText("Style: ");\r
- fontName.setFont(new java.awt.Font("Verdana", 0, 11));\r
- fontName.addItemListener(this);\r
- ok.setFont(new java.awt.Font("Verdana", 0, 11));\r
- ok.setLabel("OK");\r
- ok.addActionListener(this);\r
- cancel.setFont(new java.awt.Font("Verdana", 0, 11));\r
- cancel.setLabel("Cancel");\r
- cancel.addActionListener(this);\r
- this.setBackground(Color.white);\r
- panel1.setLayout(borderLayout1);\r
- panel2.setLayout(borderLayout3);\r
- panel3.setLayout(borderLayout2);\r
- panel5.setBackground(Color.white);\r
- panel4.setBackground(Color.white);\r
- panel1.setBackground(Color.white);\r
- panel2.setBackground(Color.white);\r
- panel3.setBackground(Color.white);\r
- panel1.add(label1, BorderLayout.WEST);\r
- panel1.add(fontName, BorderLayout.CENTER);\r
- panel5.add(panel1, null);\r
- panel5.add(panel3, null);\r
- panel5.add(panel2, null);\r
- panel2.add(label3, BorderLayout.WEST);\r
- panel2.add(fontStyle, BorderLayout.CENTER);\r
- panel3.add(label2, BorderLayout.WEST);\r
- panel3.add(fontSize, BorderLayout.CENTER);\r
- this.add(panel4, BorderLayout.SOUTH);\r
- panel4.add(ok, null);\r
- panel4.add(cancel, null);\r
- this.add(panel5, BorderLayout.CENTER);\r
- }\r
-\r
-}\r
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ *
+ * This file is part of Jalview.
+ *
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * Jalview is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+package jalview.appletgui;
+
+import jalview.api.ViewStyleI;
+import jalview.util.MessageManager;
+
+import java.awt.BorderLayout;
+import java.awt.Button;
+import java.awt.Checkbox;
+import java.awt.Choice;
+import java.awt.Color;
+import java.awt.FlowLayout;
+import java.awt.Font;
+import java.awt.FontMetrics;
+import java.awt.Frame;
+import java.awt.Label;
+import java.awt.Panel;
+import java.awt.Toolkit;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
+
+/**
+ * This dialog allows the user to try different font settings and related
+ * options. Changes are immediately visible on the alignment or tree. The user
+ * can dismiss the dialog by confirming changes with 'OK', or reverting to
+ * previous settings with 'Cancel'.
+ */
+@SuppressWarnings("serial")
+public class FontChooser extends Panel implements ItemListener
+{
+ private static final Font VERDANA_11PT = new Font("Verdana", 0, 11);
+
+ private Choice fontSize = new Choice();
+
+ private Choice fontStyle = new Choice();
+
+ private Choice fontName = new Choice();
+
+ private Checkbox scaleAsCdna = new Checkbox();
+
+ private Button ok = new Button();
+
+ private Button cancel = new Button();
+
+ private AlignmentPanel ap;
+
+ private TreePanel tp;
+
+ private Font oldFont;
+
+ private int oldCharWidth = 0;
+
+ private boolean oldScaleProtein = false;
+
+ private Font lastSelected = null;
+
+ private int lastSelStyle = 0;
+
+ private int lastSelSize = 0;
+
+ private boolean init = true;
+
+ private Frame frame;
+
+ /**
+ * Constructor for a TreePanel font chooser
+ *
+ * @param tp
+ */
+ public FontChooser(TreePanel tp)
+ {
+ try
+ {
+ jbInit();
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+
+ this.tp = tp;
+ oldFont = tp.getTreeFont();
+ init();
+ }
+
+ /**
+ * Constructor for an AlignmentPanel font chooser
+ *
+ * @param ap
+ */
+ public FontChooser(AlignmentPanel ap)
+ {
+ this.ap = ap;
+ oldFont = ap.av.getFont();
+ oldCharWidth = ap.av.getViewStyle().getCharWidth();
+ oldScaleProtein = ap.av.getViewStyle().isScaleProteinAsCdna();
+
+ try
+ {
+ jbInit();
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ init();
+ }
+
+ /**
+ * Populate choice lists and open this dialog
+ */
+ void init()
+ {
+ String fonts[] = Toolkit.getDefaultToolkit().getFontList();
+ for (int i = 0; i < fonts.length; i++)
+ {
+ fontName.addItem(fonts[i]);
+ }
+
+ for (int i = 1; i < 31; i++)
+ {
+ fontSize.addItem(i + "");
+ }
+
+ fontStyle.addItem("plain");
+ fontStyle.addItem("bold");
+ fontStyle.addItem("italic");
+
+ fontName.select(oldFont.getName());
+ fontSize.select(oldFont.getSize() + "");
+ fontStyle.select(oldFont.getStyle());
+
+ this.frame = new Frame();
+ frame.add(this);
+ jalview.bin.JalviewLite.addFrame(frame,
+ MessageManager.getString("action.change_font"), 440, 115);
+
+ init = false;
+ }
+
+ /**
+ * Actions on change of font name, size or style.
+ */
+ public void itemStateChanged(ItemEvent evt)
+ {
+ final Object source = evt.getSource();
+ if (source == fontName)
+ {
+ fontName_actionPerformed();
+ }
+ else if (source == fontSize)
+ {
+ fontSize_actionPerformed();
+ }
+ else if (source == fontStyle)
+ {
+ fontStyle_actionPerformed();
+ }
+ else if (source == scaleAsCdna)
+ {
+ scaleAsCdna_actionPerformed();
+ }
+ }
+
+ /**
+ * Close this dialog on OK to confirm any changes. Also updates the overview
+ * window if displayed.
+ */
+ protected void ok_actionPerformed()
+ {
+ frame.setVisible(false);
+ if (ap != null)
+ {
+ if (ap.getOverviewPanel() != null)
+ {
+ ap.getOverviewPanel().updateOverviewImage();
+ }
+ }
+ }
+
+ /**
+ * Close this dialog on Cancel, reverting to previous font settings.
+ */
+ protected void cancel_actionPerformed()
+ {
+ if (ap != null)
+ {
+ ap.av.setScaleProteinAsCdna(oldScaleProtein);
+ if (ap.av.getCodingComplement() != null)
+ {
+ ap.av.getCodingComplement().setScaleProteinAsCdna(oldScaleProtein);
+ ap.alignFrame.getSplitFrame().repaint();
+ }
+
+ ap.av.setFont(oldFont);
+ ViewStyleI style = ap.av.getViewStyle();
+ if (style.getCharWidth() != oldCharWidth)
+ {
+ style.setCharWidth(oldCharWidth);
+ ap.av.setViewStyle(style);
+ }
+ ap.paintAlignment(true);
+ }
+ else if (tp != null)
+ {
+ tp.setTreeFont(oldFont);
+ tp.treeCanvas.repaint();
+ }
+
+ fontName.select(oldFont.getName());
+ fontSize.select(oldFont.getSize() + "");
+ fontStyle.select(oldFont.getStyle());
+
+ frame.setVisible(false);
+ }
+
+ /**
+ * DOCUMENT ME!
+ */
+ void changeFont()
+ {
+ if (lastSelected == null)
+ {
+ // initialise with original font
+ lastSelected = oldFont;
+ lastSelSize = oldFont.getSize();
+ lastSelStyle = oldFont.getStyle();
+ }
+
+ Font newFont = new Font(fontName.getSelectedItem().toString(),
+ fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
+ .getSelectedItem().toString()));
+ FontMetrics fm = getGraphics().getFontMetrics(newFont);
+ double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
+ .getStringBounds("I", getGraphics()).getWidth();
+ if (mw < 1 || iw < 1)
+ {
+ // TODO: JAL-1100
+ fontName.select(lastSelected.getName());
+ fontStyle.select(lastSelStyle);
+ fontSize.select("" + lastSelSize);
+ JVDialog d = new JVDialog(this.frame,
+ MessageManager.getString("label.invalid_font"), true, 350,
+ 200);
+ Panel mp = new Panel();
+ d.cancel.setVisible(false);
+ mp.setLayout(new FlowLayout());
+ mp.add(new Label(
+ "Font doesn't have letters defined\nso cannot be used\nwith alignment data."));
+ d.setMainPanel(mp);
+ d.setVisible(true);
+ return;
+ }
+ if (tp != null)
+ {
+ tp.setTreeFont(newFont);
+ }
+ else if (ap != null)
+ {
+ ap.av.setFont(newFont);
+ ap.fontChanged();
+ }
+ // remember last selected
+ lastSelected = newFont;
+ }
+
+ protected void fontName_actionPerformed()
+ {
+ if (init)
+ {
+ return;
+ }
+ changeFont();
+ }
+
+ protected void fontSize_actionPerformed()
+ {
+ if (init)
+ {
+ return;
+ }
+ changeFont();
+ }
+
+ protected void fontStyle_actionPerformed()
+ {
+ if (init)
+ {
+ return;
+ }
+ changeFont();
+ }
+
+ /**
+ * Construct this panel's contents
+ *
+ * @throws Exception
+ */
+ private void jbInit() throws Exception
+ {
+ this.setLayout(new BorderLayout());
+ this.setBackground(Color.white);
+
+ Label fontLabel = new Label(MessageManager.getString("label.font"));
+ fontLabel.setFont(VERDANA_11PT);
+ fontLabel.setAlignment(Label.RIGHT);
+ fontSize.setFont(VERDANA_11PT);
+ fontSize.addItemListener(this);
+ fontStyle.setFont(VERDANA_11PT);
+ fontStyle.addItemListener(this);
+
+ Label sizeLabel = new Label(MessageManager.getString("label.size"));
+ sizeLabel.setAlignment(Label.RIGHT);
+ sizeLabel.setFont(VERDANA_11PT);
+
+ Label styleLabel = new Label(MessageManager.getString("label.style"));
+ styleLabel.setAlignment(Label.RIGHT);
+ styleLabel.setFont(VERDANA_11PT);
+
+ fontName.setFont(VERDANA_11PT);
+ fontName.addItemListener(this);
+
+ scaleAsCdna.setLabel(MessageManager.getString("label.scale_as_cdna"));
+ scaleAsCdna.setFont(VERDANA_11PT);
+ scaleAsCdna.addItemListener(this);
+ scaleAsCdna.setState(ap.av.isScaleProteinAsCdna());
+
+ ok.setFont(VERDANA_11PT);
+ ok.setLabel(MessageManager.getString("action.ok"));
+ ok.addActionListener(new ActionListener()
+ {
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ ok_actionPerformed();
+ }
+ });
+ cancel.setFont(VERDANA_11PT);
+ cancel.setLabel(MessageManager.getString("action.cancel"));
+ cancel.addActionListener(new ActionListener()
+ {
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ cancel_actionPerformed();
+ }
+ });
+
+ Panel fontPanel = new Panel();
+ fontPanel.setLayout(new BorderLayout());
+ Panel stylePanel = new Panel();
+ stylePanel.setLayout(new BorderLayout());
+ Panel sizePanel = new Panel();
+ sizePanel.setLayout(new BorderLayout());
+ Panel scalePanel = new Panel();
+ scalePanel.setLayout(new BorderLayout());
+ Panel okCancelPanel = new Panel();
+ Panel optionsPanel = new Panel();
+
+ fontPanel.setBackground(Color.white);
+ stylePanel.setBackground(Color.white);
+ sizePanel.setBackground(Color.white);
+ okCancelPanel.setBackground(Color.white);
+ optionsPanel.setBackground(Color.white);
+
+ fontPanel.add(fontLabel, BorderLayout.WEST);
+ fontPanel.add(fontName, BorderLayout.CENTER);
+ stylePanel.add(styleLabel, BorderLayout.WEST);
+ stylePanel.add(fontStyle, BorderLayout.CENTER);
+ sizePanel.add(sizeLabel, BorderLayout.WEST);
+ sizePanel.add(fontSize, BorderLayout.CENTER);
+ scalePanel.add(scaleAsCdna, BorderLayout.CENTER);
+ okCancelPanel.add(ok, null);
+ okCancelPanel.add(cancel, null);
+
+ optionsPanel.add(fontPanel, null);
+ optionsPanel.add(sizePanel, null);
+ optionsPanel.add(stylePanel, null);
+
+ /*
+ * Only show 'scale protein as cDNA' in a SplitFrame
+ */
+ this.add(optionsPanel, BorderLayout.NORTH);
+ if (ap.alignFrame.getSplitFrame() != null)
+ {
+ this.add(scalePanel, BorderLayout.CENTER);
+ }
+ this.add(okCancelPanel, BorderLayout.SOUTH);
+ }
+
+ /**
+ * Turn on/off scaling of protein characters to 3 times the width of cDNA
+ * characters
+ */
+ protected void scaleAsCdna_actionPerformed()
+ {
+ ap.av.setScaleProteinAsCdna(scaleAsCdna.getState());
+ ap.av.getCodingComplement().setScaleProteinAsCdna(
+ scaleAsCdna.getState());
+ ap.alignFrame.getSplitFrame().adjustLayout();
+ ap.paintAlignment(true);
+ ap.alignFrame.getSplitFrame().repaint();
+ }
+
+}