5df659bea612c99b1f0dec7ad8486ace17bf3631
[jalview.git] / src / jalview / appletgui / FontChooser.java
1 /*
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
4  * 
5  * This file is part of Jalview.
6  * 
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.
10  *  
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.
15  * 
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  */
18 package jalview.appletgui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22
23 public class FontChooser extends Panel implements ActionListener,
24         ItemListener
25 {
26   AlignmentPanel ap;
27
28   TreePanel tp;
29
30   Font oldFont;
31
32   boolean init = true;
33
34   Frame frame;
35
36   public FontChooser(TreePanel tp)
37   {
38     try
39     {
40       jbInit();
41     } catch (Exception e)
42     {
43       e.printStackTrace();
44     }
45
46     this.tp = tp;
47     oldFont = tp.getTreeFont();
48     init();
49   }
50
51   public FontChooser(AlignmentPanel ap)
52   {
53     try
54     {
55       jbInit();
56     } catch (Exception e)
57     {
58       e.printStackTrace();
59     }
60
61     this.ap = ap;
62     oldFont = ap.av.getFont();
63     init();
64   }
65
66   void init()
67   {
68     String fonts[] = Toolkit.getDefaultToolkit().getFontList();
69     for (int i = 0; i < fonts.length; i++)
70     {
71       fontName.addItem(fonts[i]);
72     }
73
74     for (int i = 1; i < 31; i++)
75     {
76       fontSize.addItem(i + "");
77     }
78
79     fontStyle.addItem("plain");
80     fontStyle.addItem("bold");
81     fontStyle.addItem("italic");
82
83     fontName.select(oldFont.getName());
84     fontSize.select(oldFont.getSize() + "");
85     fontStyle.select(oldFont.getStyle());
86
87     Frame frame = new Frame();
88     this.frame = frame;
89     frame.add(this);
90     jalview.bin.JalviewLite.addFrame(frame, "Change Font", 440, 115);
91
92     init = false;
93   }
94
95   public void actionPerformed(ActionEvent evt)
96   {
97     if (evt.getSource() == ok)
98     {
99       ok_actionPerformed();
100     }
101     else if (evt.getSource() == cancel)
102     {
103       cancel_actionPerformed();
104     }
105   }
106
107   public void itemStateChanged(ItemEvent evt)
108   {
109     if (evt.getSource() == fontName)
110     {
111       fontName_actionPerformed();
112     }
113     else if (evt.getSource() == fontSize)
114     {
115       fontSize_actionPerformed();
116     }
117     else if (evt.getSource() == fontStyle)
118     {
119       fontStyle_actionPerformed();
120     }
121   }
122
123   protected void ok_actionPerformed()
124   {
125     frame.setVisible(false);
126     if (ap != null)
127     {
128       if (ap.getOverviewPanel() != null)
129       {
130         ap.getOverviewPanel().updateOverviewImage();
131       }
132     }
133
134   }
135
136   protected void cancel_actionPerformed()
137   {
138     if (ap != null)
139     {
140       ap.av.setFont(oldFont);
141       ap.paintAlignment(true);
142     }
143     else if (tp != null)
144     {
145       tp.setTreeFont(oldFont);
146       tp.treeCanvas.repaint();
147     }
148
149     fontName.select(oldFont.getName());
150     fontSize.select(oldFont.getSize() + "");
151     fontStyle.select(oldFont.getStyle());
152
153     frame.setVisible(false);
154   }
155
156   private Font lastSelected = null;
157
158   private int lastSelStyle = 0;
159
160   private int lastSelSize = 0;
161
162   /**
163    * DOCUMENT ME!
164    */
165   void changeFont()
166   {
167     if (lastSelected == null)
168     {
169       // initialise with original font
170       lastSelected = oldFont;
171       lastSelSize = oldFont.getSize();
172       lastSelStyle = oldFont.getStyle();
173     }
174
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)
182     {
183       // TODO: JAL-1100
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());
191       mp.add(new Label(
192               "Font doesn't have letters defined\nso cannot be used\nwith alignment data."));
193       d.setMainPanel(mp);
194       d.setVisible(true);
195       return;
196     }
197     if (tp != null)
198     {
199       tp.setTreeFont(newFont);
200     }
201     else if (ap != null)
202     {
203       ap.av.setFont(newFont);
204       ap.fontChanged();
205     }
206     // remember last selected
207     lastSelected = newFont;
208   }
209
210   protected void fontName_actionPerformed()
211   {
212     if (init)
213     {
214       return;
215     }
216     changeFont();
217   }
218
219   protected void fontSize_actionPerformed()
220   {
221     if (init)
222     {
223       return;
224     }
225     changeFont();
226   }
227
228   protected void fontStyle_actionPerformed()
229   {
230     if (init)
231     {
232       return;
233     }
234     changeFont();
235   }
236
237   Label label1 = new Label();
238
239   protected Choice fontSize = new Choice();
240
241   protected Choice fontStyle = new Choice();
242
243   Label label2 = new Label();
244
245   Label label3 = new Label();
246
247   protected Choice fontName = new Choice();
248
249   Button ok = new Button();
250
251   Button cancel = new Button();
252
253   Panel panel1 = new Panel();
254
255   Panel panel2 = new Panel();
256
257   Panel panel3 = new Panel();
258
259   BorderLayout borderLayout1 = new BorderLayout();
260
261   BorderLayout borderLayout2 = new BorderLayout();
262
263   BorderLayout borderLayout3 = new BorderLayout();
264
265   Panel panel4 = new Panel();
266
267   Panel panel5 = new Panel();
268
269   BorderLayout borderLayout4 = new BorderLayout();
270
271   private void jbInit() throws Exception
272   {
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));
290     ok.setLabel("OK");
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);
317   }
318
319 }