2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
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.
21 package jalview.appletgui;
23 import jalview.api.ViewStyleI;
24 import jalview.util.MessageManager;
26 import java.awt.BorderLayout;
27 import java.awt.Button;
28 import java.awt.Choice;
29 import java.awt.Color;
30 import java.awt.FlowLayout;
32 import java.awt.FontMetrics;
33 import java.awt.Frame;
34 import java.awt.Label;
35 import java.awt.Panel;
36 import java.awt.Toolkit;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.awt.event.ItemEvent;
40 import java.awt.event.ItemListener;
42 public class FontChooser extends Panel implements ActionListener,
57 public FontChooser(TreePanel tp)
68 oldFont = tp.getTreeFont();
72 public FontChooser(AlignmentPanel ap)
83 oldFont = ap.av.getFont();
84 oldCharWidth = ap.av.getViewStyle().getCharWidth();
90 String fonts[] = Toolkit.getDefaultToolkit().getFontList();
91 for (int i = 0; i < fonts.length; i++)
93 fontName.addItem(fonts[i]);
96 for (int i = 1; i < 31; i++)
98 fontSize.addItem(i + "");
101 fontStyle.addItem("plain");
102 fontStyle.addItem("bold");
103 fontStyle.addItem("italic");
105 fontName.select(oldFont.getName());
106 fontSize.select(oldFont.getSize() + "");
107 fontStyle.select(oldFont.getStyle());
109 Frame frame = new Frame();
112 jalview.bin.JalviewLite.addFrame(frame,
113 MessageManager.getString("action.change_font"), 440, 115);
118 public void actionPerformed(ActionEvent evt)
120 if (evt.getSource() == ok)
122 ok_actionPerformed();
124 else if (evt.getSource() == cancel)
126 cancel_actionPerformed();
130 public void itemStateChanged(ItemEvent evt)
132 if (evt.getSource() == fontName)
134 fontName_actionPerformed();
136 else if (evt.getSource() == fontSize)
138 fontSize_actionPerformed();
140 else if (evt.getSource() == fontStyle)
142 fontStyle_actionPerformed();
146 protected void ok_actionPerformed()
148 frame.setVisible(false);
151 if (ap.getOverviewPanel() != null)
153 ap.getOverviewPanel().updateOverviewImage();
159 protected void cancel_actionPerformed()
163 ap.av.setFont(oldFont);
164 ViewStyleI style = ap.av.getViewStyle();
165 if (style.getCharWidth() != oldCharWidth)
167 style.setCharWidth(oldCharWidth);
168 ap.av.setViewStyle(style);
170 ap.paintAlignment(true);
174 tp.setTreeFont(oldFont);
175 tp.treeCanvas.repaint();
178 fontName.select(oldFont.getName());
179 fontSize.select(oldFont.getSize() + "");
180 fontStyle.select(oldFont.getStyle());
182 frame.setVisible(false);
185 private Font lastSelected = null;
187 private int lastSelStyle = 0;
189 private int lastSelSize = 0;
196 if (lastSelected == null)
198 // initialise with original font
199 lastSelected = oldFont;
200 lastSelSize = oldFont.getSize();
201 lastSelStyle = oldFont.getStyle();
204 Font newFont = new Font(fontName.getSelectedItem().toString(),
205 fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
206 .getSelectedItem().toString()));
207 FontMetrics fm = getGraphics().getFontMetrics(newFont);
208 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
209 .getStringBounds("I", getGraphics()).getWidth();
210 if (mw < 1 || iw < 1)
213 fontName.select(lastSelected.getName());
214 fontStyle.select(lastSelStyle);
215 fontSize.select("" + lastSelSize);
216 JVDialog d = new JVDialog(this.frame,
217 MessageManager.getString("label.invalid_font"), true, 350,
219 Panel mp = new Panel();
220 d.cancel.setVisible(false);
221 mp.setLayout(new FlowLayout());
223 "Font doesn't have letters defined\nso cannot be used\nwith alignment data."));
230 tp.setTreeFont(newFont);
234 ap.av.setFont(newFont);
237 // remember last selected
238 lastSelected = newFont;
241 protected void fontName_actionPerformed()
250 protected void fontSize_actionPerformed()
259 protected void fontStyle_actionPerformed()
268 Label label1 = new Label();
270 protected Choice fontSize = new Choice();
272 protected Choice fontStyle = new Choice();
274 Label label2 = new Label();
276 Label label3 = new Label();
278 protected Choice fontName = new Choice();
280 Button ok = new Button();
282 Button cancel = new Button();
284 Panel panel1 = new Panel();
286 Panel panel2 = new Panel();
288 Panel panel3 = new Panel();
290 BorderLayout borderLayout1 = new BorderLayout();
292 BorderLayout borderLayout2 = new BorderLayout();
294 BorderLayout borderLayout3 = new BorderLayout();
296 Panel panel4 = new Panel();
298 Panel panel5 = new Panel();
300 BorderLayout borderLayout4 = new BorderLayout();
302 private void jbInit() throws Exception
304 label1.setFont(new java.awt.Font("Verdana", 0, 11));
305 label1.setAlignment(Label.RIGHT);
306 label1.setText(MessageManager.getString("label.font"));
307 this.setLayout(borderLayout4);
308 fontSize.setFont(new java.awt.Font("Verdana", 0, 11));
309 fontSize.addItemListener(this);
310 fontStyle.setFont(new java.awt.Font("Verdana", 0, 11));
311 fontStyle.addItemListener(this);
312 label2.setAlignment(Label.RIGHT);
313 label2.setFont(new java.awt.Font("Verdana", 0, 11));
314 label2.setText(MessageManager.getString("label.size"));
315 label3.setAlignment(Label.RIGHT);
316 label3.setFont(new java.awt.Font("Verdana", 0, 11));
317 label3.setText(MessageManager.getString("label.style"));
318 fontName.setFont(new java.awt.Font("Verdana", 0, 11));
319 fontName.addItemListener(this);
320 ok.setFont(new java.awt.Font("Verdana", 0, 11));
321 ok.setLabel(MessageManager.getString("action.ok"));
322 ok.addActionListener(this);
323 cancel.setFont(new java.awt.Font("Verdana", 0, 11));
324 cancel.setLabel(MessageManager.getString("action.cancel"));
325 cancel.addActionListener(this);
326 this.setBackground(Color.white);
327 panel1.setLayout(borderLayout1);
328 panel2.setLayout(borderLayout3);
329 panel3.setLayout(borderLayout2);
330 panel5.setBackground(Color.white);
331 panel4.setBackground(Color.white);
332 panel1.setBackground(Color.white);
333 panel2.setBackground(Color.white);
334 panel3.setBackground(Color.white);
335 panel1.add(label1, BorderLayout.WEST);
336 panel1.add(fontName, BorderLayout.CENTER);
337 panel5.add(panel1, null);
338 panel5.add(panel3, null);
339 panel5.add(panel2, null);
340 panel2.add(label3, BorderLayout.WEST);
341 panel2.add(fontStyle, BorderLayout.CENTER);
342 panel3.add(label2, BorderLayout.WEST);
343 panel3.add(fontSize, BorderLayout.CENTER);
344 this.add(panel4, BorderLayout.SOUTH);
345 panel4.add(ok, null);
346 panel4.add(cancel, null);
347 this.add(panel5, BorderLayout.CENTER);