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;
21 import java.awt.event.*;
23 public class FontChooser extends Panel implements ActionListener,
36 public FontChooser(TreePanel tp)
47 oldFont = tp.getTreeFont();
51 public FontChooser(AlignmentPanel ap)
62 oldFont = ap.av.getFont();
68 String fonts[] = Toolkit.getDefaultToolkit().getFontList();
69 for (int i = 0; i < fonts.length; i++)
71 fontName.addItem(fonts[i]);
74 for (int i = 1; i < 31; i++)
76 fontSize.addItem(i + "");
79 fontStyle.addItem("plain");
80 fontStyle.addItem("bold");
81 fontStyle.addItem("italic");
83 fontName.select(oldFont.getName());
84 fontSize.select(oldFont.getSize() + "");
85 fontStyle.select(oldFont.getStyle());
87 Frame frame = new Frame();
90 jalview.bin.JalviewLite.addFrame(frame, "Change Font", 440, 115);
95 public void actionPerformed(ActionEvent evt)
97 if (evt.getSource() == ok)
101 else if (evt.getSource() == cancel)
103 cancel_actionPerformed();
107 public void itemStateChanged(ItemEvent evt)
109 if (evt.getSource() == fontName)
111 fontName_actionPerformed();
113 else if (evt.getSource() == fontSize)
115 fontSize_actionPerformed();
117 else if (evt.getSource() == fontStyle)
119 fontStyle_actionPerformed();
123 protected void ok_actionPerformed()
125 frame.setVisible(false);
128 if (ap.getOverviewPanel() != null)
130 ap.getOverviewPanel().updateOverviewImage();
136 protected void cancel_actionPerformed()
140 ap.av.setFont(oldFont);
141 ap.paintAlignment(true);
145 tp.setTreeFont(oldFont);
146 tp.treeCanvas.repaint();
149 fontName.select(oldFont.getName());
150 fontSize.select(oldFont.getSize() + "");
151 fontStyle.select(oldFont.getStyle());
153 frame.setVisible(false);
156 private Font lastSelected = null;
158 private int lastSelStyle = 0;
160 private int lastSelSize = 0;
167 if (lastSelected == null)
169 // initialise with original font
170 lastSelected = oldFont;
171 lastSelSize = oldFont.getSize();
172 lastSelStyle = oldFont.getStyle();
175 Font newFont = new Font(fontName.getSelectedItem().toString(),
176 fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
177 .getSelectedItem().toString()));
178 FontMetrics fm = getGraphics().getFontMetrics(newFont);
179 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
180 .getStringBounds("I", getGraphics()).getWidth();
181 if (mw < 1 || iw < 1)
184 fontName.select(lastSelected.getName());
185 fontStyle.select(lastSelStyle);
186 fontSize.select("" + lastSelSize);
187 JVDialog d = new JVDialog(this.frame, "Invalid Font", true, 350, 200);
188 Panel mp = new Panel();
189 d.cancel.setVisible(false);
190 mp.setLayout(new FlowLayout());
192 "Font doesn't have letters defined\nso cannot be used\nwith alignment data."));
199 tp.setTreeFont(newFont);
203 ap.av.setFont(newFont);
206 // remember last selected
207 lastSelected = newFont;
210 protected void fontName_actionPerformed()
219 protected void fontSize_actionPerformed()
228 protected void fontStyle_actionPerformed()
237 Label label1 = new Label();
239 protected Choice fontSize = new Choice();
241 protected Choice fontStyle = new Choice();
243 Label label2 = new Label();
245 Label label3 = new Label();
247 protected Choice fontName = new Choice();
249 Button ok = new Button();
251 Button cancel = new Button();
253 Panel panel1 = new Panel();
255 Panel panel2 = new Panel();
257 Panel panel3 = new Panel();
259 BorderLayout borderLayout1 = new BorderLayout();
261 BorderLayout borderLayout2 = new BorderLayout();
263 BorderLayout borderLayout3 = new BorderLayout();
265 Panel panel4 = new Panel();
267 Panel panel5 = new Panel();
269 BorderLayout borderLayout4 = new BorderLayout();
271 private void jbInit() throws Exception
273 label1.setFont(new java.awt.Font("Verdana", 0, 11));
274 label1.setAlignment(Label.RIGHT);
275 label1.setText("Font: ");
276 this.setLayout(borderLayout4);
277 fontSize.setFont(new java.awt.Font("Verdana", 0, 11));
278 fontSize.addItemListener(this);
279 fontStyle.setFont(new java.awt.Font("Verdana", 0, 11));
280 fontStyle.addItemListener(this);
281 label2.setAlignment(Label.RIGHT);
282 label2.setFont(new java.awt.Font("Verdana", 0, 11));
283 label2.setText("Size: ");
284 label3.setAlignment(Label.RIGHT);
285 label3.setFont(new java.awt.Font("Verdana", 0, 11));
286 label3.setText("Style: ");
287 fontName.setFont(new java.awt.Font("Verdana", 0, 11));
288 fontName.addItemListener(this);
289 ok.setFont(new java.awt.Font("Verdana", 0, 11));
291 ok.addActionListener(this);
292 cancel.setFont(new java.awt.Font("Verdana", 0, 11));
293 cancel.setLabel("Cancel");
294 cancel.addActionListener(this);
295 this.setBackground(Color.white);
296 panel1.setLayout(borderLayout1);
297 panel2.setLayout(borderLayout3);
298 panel3.setLayout(borderLayout2);
299 panel5.setBackground(Color.white);
300 panel4.setBackground(Color.white);
301 panel1.setBackground(Color.white);
302 panel2.setBackground(Color.white);
303 panel3.setBackground(Color.white);
304 panel1.add(label1, BorderLayout.WEST);
305 panel1.add(fontName, BorderLayout.CENTER);
306 panel5.add(panel1, null);
307 panel5.add(panel3, null);
308 panel5.add(panel2, null);
309 panel2.add(label3, BorderLayout.WEST);
310 panel2.add(fontStyle, BorderLayout.CENTER);
311 panel3.add(label2, BorderLayout.WEST);
312 panel3.add(fontSize, BorderLayout.CENTER);
313 this.add(panel4, BorderLayout.SOUTH);
314 panel4.add(ok, null);
315 panel4.add(cancel, null);
316 this.add(panel5, BorderLayout.CENTER);