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.util.MessageManager;
26 import java.awt.event.*;
28 public class FontChooser extends Panel implements ActionListener,
41 public FontChooser(TreePanel tp)
52 oldFont = tp.getTreeFont();
56 public FontChooser(AlignmentPanel ap)
67 oldFont = ap.av.getFont();
73 String fonts[] = Toolkit.getDefaultToolkit().getFontList();
74 for (int i = 0; i < fonts.length; i++)
76 fontName.addItem(fonts[i]);
79 for (int i = 1; i < 31; i++)
81 fontSize.addItem(i + "");
84 fontStyle.addItem("plain");
85 fontStyle.addItem("bold");
86 fontStyle.addItem("italic");
88 fontName.select(oldFont.getName());
89 fontSize.select(oldFont.getSize() + "");
90 fontStyle.select(oldFont.getStyle());
92 Frame frame = new Frame();
95 jalview.bin.JalviewLite.addFrame(frame, MessageManager.getString("action.change_font"), 440, 115);
100 public void actionPerformed(ActionEvent evt)
102 if (evt.getSource() == ok)
104 ok_actionPerformed();
106 else if (evt.getSource() == cancel)
108 cancel_actionPerformed();
112 public void itemStateChanged(ItemEvent evt)
114 if (evt.getSource() == fontName)
116 fontName_actionPerformed();
118 else if (evt.getSource() == fontSize)
120 fontSize_actionPerformed();
122 else if (evt.getSource() == fontStyle)
124 fontStyle_actionPerformed();
128 protected void ok_actionPerformed()
130 frame.setVisible(false);
133 if (ap.getOverviewPanel() != null)
135 ap.getOverviewPanel().updateOverviewImage();
141 protected void cancel_actionPerformed()
145 ap.av.setFont(oldFont);
146 ap.paintAlignment(true);
150 tp.setTreeFont(oldFont);
151 tp.treeCanvas.repaint();
154 fontName.select(oldFont.getName());
155 fontSize.select(oldFont.getSize() + "");
156 fontStyle.select(oldFont.getStyle());
158 frame.setVisible(false);
161 private Font lastSelected = null;
163 private int lastSelStyle = 0;
165 private int lastSelSize = 0;
172 if (lastSelected == null)
174 // initialise with original font
175 lastSelected = oldFont;
176 lastSelSize = oldFont.getSize();
177 lastSelStyle = oldFont.getStyle();
180 Font newFont = new Font(fontName.getSelectedItem().toString(),
181 fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
182 .getSelectedItem().toString()));
183 FontMetrics fm = getGraphics().getFontMetrics(newFont);
184 double mw = fm.getStringBounds("M", getGraphics()).getWidth(), iw = fm
185 .getStringBounds("I", getGraphics()).getWidth();
186 if (mw < 1 || iw < 1)
189 fontName.select(lastSelected.getName());
190 fontStyle.select(lastSelStyle);
191 fontSize.select("" + lastSelSize);
192 JVDialog d = new JVDialog(this.frame, MessageManager.getString("label.invalid_font"), true, 350, 200);
193 Panel mp = new Panel();
194 d.cancel.setVisible(false);
195 mp.setLayout(new FlowLayout());
197 "Font doesn't have letters defined\nso cannot be used\nwith alignment data."));
204 tp.setTreeFont(newFont);
208 ap.av.setFont(newFont);
211 // remember last selected
212 lastSelected = newFont;
215 protected void fontName_actionPerformed()
224 protected void fontSize_actionPerformed()
233 protected void fontStyle_actionPerformed()
242 Label label1 = new Label();
244 protected Choice fontSize = new Choice();
246 protected Choice fontStyle = new Choice();
248 Label label2 = new Label();
250 Label label3 = new Label();
252 protected Choice fontName = new Choice();
254 Button ok = new Button();
256 Button cancel = new Button();
258 Panel panel1 = new Panel();
260 Panel panel2 = new Panel();
262 Panel panel3 = new Panel();
264 BorderLayout borderLayout1 = new BorderLayout();
266 BorderLayout borderLayout2 = new BorderLayout();
268 BorderLayout borderLayout3 = new BorderLayout();
270 Panel panel4 = new Panel();
272 Panel panel5 = new Panel();
274 BorderLayout borderLayout4 = new BorderLayout();
276 private void jbInit() throws Exception
278 label1.setFont(new java.awt.Font("Verdana", 0, 11));
279 label1.setAlignment(Label.RIGHT);
280 label1.setText(MessageManager.getString("label.font"));
281 this.setLayout(borderLayout4);
282 fontSize.setFont(new java.awt.Font("Verdana", 0, 11));
283 fontSize.addItemListener(this);
284 fontStyle.setFont(new java.awt.Font("Verdana", 0, 11));
285 fontStyle.addItemListener(this);
286 label2.setAlignment(Label.RIGHT);
287 label2.setFont(new java.awt.Font("Verdana", 0, 11));
288 label2.setText(MessageManager.getString("label.size"));
289 label3.setAlignment(Label.RIGHT);
290 label3.setFont(new java.awt.Font("Verdana", 0, 11));
291 label3.setText(MessageManager.getString("label.style"));
292 fontName.setFont(new java.awt.Font("Verdana", 0, 11));
293 fontName.addItemListener(this);
294 ok.setFont(new java.awt.Font("Verdana", 0, 11));
295 ok.setLabel(MessageManager.getString("action.ok"));
296 ok.addActionListener(this);
297 cancel.setFont(new java.awt.Font("Verdana", 0, 11));
298 cancel.setLabel(MessageManager.getString("action.cancel"));
299 cancel.addActionListener(this);
300 this.setBackground(Color.white);
301 panel1.setLayout(borderLayout1);
302 panel2.setLayout(borderLayout3);
303 panel3.setLayout(borderLayout2);
304 panel5.setBackground(Color.white);
305 panel4.setBackground(Color.white);
306 panel1.setBackground(Color.white);
307 panel2.setBackground(Color.white);
308 panel3.setBackground(Color.white);
309 panel1.add(label1, BorderLayout.WEST);
310 panel1.add(fontName, BorderLayout.CENTER);
311 panel5.add(panel1, null);
312 panel5.add(panel3, null);
313 panel5.add(panel2, null);
314 panel2.add(label3, BorderLayout.WEST);
315 panel2.add(fontStyle, BorderLayout.CENTER);
316 panel3.add(label2, BorderLayout.WEST);
317 panel3.add(fontSize, BorderLayout.CENTER);
318 this.add(panel4, BorderLayout.SOUTH);
319 panel4.add(ok, null);
320 panel4.add(cancel, null);
321 this.add(panel5, BorderLayout.CENTER);