2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
3 * Copyright (C) 2014 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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.appletgui;
21 import jalview.util.MessageManager;
24 import java.awt.event.*;
26 public class FontChooser extends Panel implements ActionListener,
39 public FontChooser(TreePanel tp)
50 oldFont = tp.getTreeFont();
54 public FontChooser(AlignmentPanel ap)
65 oldFont = ap.av.getFont();
71 String fonts[] = Toolkit.getDefaultToolkit().getFontList();
72 for (int i = 0; i < fonts.length; i++)
74 fontName.addItem(fonts[i]);
77 for (int i = 1; i < 31; i++)
79 fontSize.addItem(i + "");
82 fontStyle.addItem("plain");
83 fontStyle.addItem("bold");
84 fontStyle.addItem("italic");
86 fontName.select(oldFont.getName());
87 fontSize.select(oldFont.getSize() + "");
88 fontStyle.select(oldFont.getStyle());
90 Frame frame = new Frame();
93 jalview.bin.JalviewLite.addFrame(frame, MessageManager.getString("action.change_font"), 440, 115);
98 public void actionPerformed(ActionEvent evt)
100 if (evt.getSource() == ok)
102 ok_actionPerformed();
104 else if (evt.getSource() == cancel)
106 cancel_actionPerformed();
110 public void itemStateChanged(ItemEvent evt)
112 if (evt.getSource() == fontName)
114 fontName_actionPerformed();
116 else if (evt.getSource() == fontSize)
118 fontSize_actionPerformed();
120 else if (evt.getSource() == fontStyle)
122 fontStyle_actionPerformed();
126 protected void ok_actionPerformed()
128 frame.setVisible(false);
131 if (ap.getOverviewPanel() != null)
133 ap.getOverviewPanel().updateOverviewImage();
139 protected void cancel_actionPerformed()
143 ap.av.setFont(oldFont);
144 ap.paintAlignment(true);
148 tp.setTreeFont(oldFont);
149 tp.treeCanvas.repaint();
152 fontName.select(oldFont.getName());
153 fontSize.select(oldFont.getSize() + "");
154 fontStyle.select(oldFont.getStyle());
156 frame.setVisible(false);
159 private Font lastSelected = null;
161 private int lastSelStyle = 0;
163 private int lastSelSize = 0;
170 if (lastSelected == null)
172 // initialise with original font
173 lastSelected = oldFont;
174 lastSelSize = oldFont.getSize();
175 lastSelStyle = oldFont.getStyle();
178 Font newFont = new Font(fontName.getSelectedItem().toString(),
179 fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
180 .getSelectedItem().toString()));
181 FontMetrics fm = getGraphics().getFontMetrics(newFont);
182 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
183 .getStringBounds("I", getGraphics()).getWidth();
184 if (mw < 1 || iw < 1)
187 fontName.select(lastSelected.getName());
188 fontStyle.select(lastSelStyle);
189 fontSize.select("" + lastSelSize);
190 JVDialog d = new JVDialog(this.frame, MessageManager.getString("label.invalid_font"), true, 350, 200);
191 Panel mp = new Panel();
192 d.cancel.setVisible(false);
193 mp.setLayout(new FlowLayout());
195 "Font doesn't have letters defined\nso cannot be used\nwith alignment data."));
202 tp.setTreeFont(newFont);
206 ap.av.setFont(newFont);
209 // remember last selected
210 lastSelected = newFont;
213 protected void fontName_actionPerformed()
222 protected void fontSize_actionPerformed()
231 protected void fontStyle_actionPerformed()
240 Label label1 = new Label();
242 protected Choice fontSize = new Choice();
244 protected Choice fontStyle = new Choice();
246 Label label2 = new Label();
248 Label label3 = new Label();
250 protected Choice fontName = new Choice();
252 Button ok = new Button();
254 Button cancel = new Button();
256 Panel panel1 = new Panel();
258 Panel panel2 = new Panel();
260 Panel panel3 = new Panel();
262 BorderLayout borderLayout1 = new BorderLayout();
264 BorderLayout borderLayout2 = new BorderLayout();
266 BorderLayout borderLayout3 = new BorderLayout();
268 Panel panel4 = new Panel();
270 Panel panel5 = new Panel();
272 BorderLayout borderLayout4 = new BorderLayout();
274 private void jbInit() throws Exception
276 label1.setFont(new java.awt.Font("Verdana", 0, 11));
277 label1.setAlignment(Label.RIGHT);
278 label1.setText(MessageManager.getString("label.font"));
279 this.setLayout(borderLayout4);
280 fontSize.setFont(new java.awt.Font("Verdana", 0, 11));
281 fontSize.addItemListener(this);
282 fontStyle.setFont(new java.awt.Font("Verdana", 0, 11));
283 fontStyle.addItemListener(this);
284 label2.setAlignment(Label.RIGHT);
285 label2.setFont(new java.awt.Font("Verdana", 0, 11));
286 label2.setText(MessageManager.getString("label.size"));
287 label3.setAlignment(Label.RIGHT);
288 label3.setFont(new java.awt.Font("Verdana", 0, 11));
289 label3.setText(MessageManager.getString("label.style"));
290 fontName.setFont(new java.awt.Font("Verdana", 0, 11));
291 fontName.addItemListener(this);
292 ok.setFont(new java.awt.Font("Verdana", 0, 11));
293 ok.setLabel(MessageManager.getString("action.ok"));
294 ok.addActionListener(this);
295 cancel.setFont(new java.awt.Font("Verdana", 0, 11));
296 cancel.setLabel(MessageManager.getString("action.cancel"));
297 cancel.addActionListener(this);
298 this.setBackground(Color.white);
299 panel1.setLayout(borderLayout1);
300 panel2.setLayout(borderLayout3);
301 panel3.setLayout(borderLayout2);
302 panel5.setBackground(Color.white);
303 panel4.setBackground(Color.white);
304 panel1.setBackground(Color.white);
305 panel2.setBackground(Color.white);
306 panel3.setBackground(Color.white);
307 panel1.add(label1, BorderLayout.WEST);
308 panel1.add(fontName, BorderLayout.CENTER);
309 panel5.add(panel1, null);
310 panel5.add(panel3, null);
311 panel5.add(panel2, null);
312 panel2.add(label3, BorderLayout.WEST);
313 panel2.add(fontStyle, BorderLayout.CENTER);
314 panel3.add(label2, BorderLayout.WEST);
315 panel3.add(fontSize, BorderLayout.CENTER);
316 this.add(panel4, BorderLayout.SOUTH);
317 panel4.add(ok, null);
318 panel4.add(cancel, null);
319 this.add(panel5, BorderLayout.CENTER);