2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
18 package jalview.appletgui;
20 import jalview.util.MessageManager;
23 import java.awt.event.*;
25 public class FontChooser extends Panel implements ActionListener,
38 public FontChooser(TreePanel tp)
49 oldFont = tp.getTreeFont();
53 public FontChooser(AlignmentPanel ap)
64 oldFont = ap.av.getFont();
70 String fonts[] = Toolkit.getDefaultToolkit().getFontList();
71 for (int i = 0; i < fonts.length; i++)
73 fontName.addItem(fonts[i]);
76 for (int i = 1; i < 31; i++)
78 fontSize.addItem(i + "");
81 fontStyle.addItem("plain");
82 fontStyle.addItem("bold");
83 fontStyle.addItem("italic");
85 fontName.select(oldFont.getName());
86 fontSize.select(oldFont.getSize() + "");
87 fontStyle.select(oldFont.getStyle());
89 Frame frame = new Frame();
92 jalview.bin.JalviewLite.addFrame(frame, MessageManager.getString("action.change_font"), 440, 115);
97 public void actionPerformed(ActionEvent evt)
99 if (evt.getSource() == ok)
101 ok_actionPerformed();
103 else if (evt.getSource() == cancel)
105 cancel_actionPerformed();
109 public void itemStateChanged(ItemEvent evt)
111 if (evt.getSource() == fontName)
113 fontName_actionPerformed();
115 else if (evt.getSource() == fontSize)
117 fontSize_actionPerformed();
119 else if (evt.getSource() == fontStyle)
121 fontStyle_actionPerformed();
125 protected void ok_actionPerformed()
127 frame.setVisible(false);
130 if (ap.getOverviewPanel() != null)
132 ap.getOverviewPanel().updateOverviewImage();
138 protected void cancel_actionPerformed()
142 ap.av.setFont(oldFont);
143 ap.paintAlignment(true);
147 tp.setTreeFont(oldFont);
148 tp.treeCanvas.repaint();
151 fontName.select(oldFont.getName());
152 fontSize.select(oldFont.getSize() + "");
153 fontStyle.select(oldFont.getStyle());
155 frame.setVisible(false);
158 private Font lastSelected = null;
160 private int lastSelStyle = 0;
162 private int lastSelSize = 0;
169 if (lastSelected == null)
171 // initialise with original font
172 lastSelected = oldFont;
173 lastSelSize = oldFont.getSize();
174 lastSelStyle = oldFont.getStyle();
177 Font newFont = new Font(fontName.getSelectedItem().toString(),
178 fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
179 .getSelectedItem().toString()));
180 FontMetrics fm = getGraphics().getFontMetrics(newFont);
181 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
182 .getStringBounds("I", getGraphics()).getWidth();
183 if (mw < 1 || iw < 1)
186 fontName.select(lastSelected.getName());
187 fontStyle.select(lastSelStyle);
188 fontSize.select("" + lastSelSize);
189 JVDialog d = new JVDialog(this.frame, "Invalid Font", true, 350, 200);
190 Panel mp = new Panel();
191 d.cancel.setVisible(false);
192 mp.setLayout(new FlowLayout());
194 "Font doesn't have letters defined\nso cannot be used\nwith alignment data."));
201 tp.setTreeFont(newFont);
205 ap.av.setFont(newFont);
208 // remember last selected
209 lastSelected = newFont;
212 protected void fontName_actionPerformed()
221 protected void fontSize_actionPerformed()
230 protected void fontStyle_actionPerformed()
239 Label label1 = new Label();
241 protected Choice fontSize = new Choice();
243 protected Choice fontStyle = new Choice();
245 Label label2 = new Label();
247 Label label3 = new Label();
249 protected Choice fontName = new Choice();
251 Button ok = new Button();
253 Button cancel = new Button();
255 Panel panel1 = new Panel();
257 Panel panel2 = new Panel();
259 Panel panel3 = new Panel();
261 BorderLayout borderLayout1 = new BorderLayout();
263 BorderLayout borderLayout2 = new BorderLayout();
265 BorderLayout borderLayout3 = new BorderLayout();
267 Panel panel4 = new Panel();
269 Panel panel5 = new Panel();
271 BorderLayout borderLayout4 = new BorderLayout();
273 private void jbInit() throws Exception
275 label1.setFont(new java.awt.Font("Verdana", 0, 11));
276 label1.setAlignment(Label.RIGHT);
277 label1.setText(MessageManager.getString("label.font"));
278 this.setLayout(borderLayout4);
279 fontSize.setFont(new java.awt.Font("Verdana", 0, 11));
280 fontSize.addItemListener(this);
281 fontStyle.setFont(new java.awt.Font("Verdana", 0, 11));
282 fontStyle.addItemListener(this);
283 label2.setAlignment(Label.RIGHT);
284 label2.setFont(new java.awt.Font("Verdana", 0, 11));
285 label2.setText(MessageManager.getString("label.size"));
286 label3.setAlignment(Label.RIGHT);
287 label3.setFont(new java.awt.Font("Verdana", 0, 11));
288 label3.setText(MessageManager.getString("label.style"));
289 fontName.setFont(new java.awt.Font("Verdana", 0, 11));
290 fontName.addItemListener(this);
291 ok.setFont(new java.awt.Font("Verdana", 0, 11));
292 ok.setLabel(MessageManager.getString("action.ok"));
293 ok.addActionListener(this);
294 cancel.setFont(new java.awt.Font("Verdana", 0, 11));
295 cancel.setLabel(MessageManager.getString("action.cancel"));
296 cancel.addActionListener(this);
297 this.setBackground(Color.white);
298 panel1.setLayout(borderLayout1);
299 panel2.setLayout(borderLayout3);
300 panel3.setLayout(borderLayout2);
301 panel5.setBackground(Color.white);
302 panel4.setBackground(Color.white);
303 panel1.setBackground(Color.white);
304 panel2.setBackground(Color.white);
305 panel3.setBackground(Color.white);
306 panel1.add(label1, BorderLayout.WEST);
307 panel1.add(fontName, BorderLayout.CENTER);
308 panel5.add(panel1, null);
309 panel5.add(panel3, null);
310 panel5.add(panel2, null);
311 panel2.add(label3, BorderLayout.WEST);
312 panel2.add(fontStyle, BorderLayout.CENTER);
313 panel3.add(label2, BorderLayout.WEST);
314 panel3.add(fontSize, BorderLayout.CENTER);
315 this.add(panel4, BorderLayout.SOUTH);
316 panel4.add(ok, null);
317 panel4.add(cancel, null);
318 this.add(panel5, BorderLayout.CENTER);