warning dialog - fixes JAL-1099
[jalview.git] / src / jalview / appletgui / FontChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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   private int lastSelStyle=0;
158   private int lastSelSize=0;
159   /**
160    * DOCUMENT ME!
161    */
162   void changeFont()
163   {
164     if (lastSelected==null)
165     {
166       // initialise with original font
167       lastSelected=oldFont;
168       lastSelSize=oldFont.getSize();
169       lastSelStyle=oldFont.getStyle();
170     }
171     
172     Font newFont = new Font(fontName.getSelectedItem().toString(),
173             fontStyle.getSelectedIndex(), Integer.parseInt(fontSize
174                     .getSelectedItem().toString()));
175     FontMetrics fm = getGraphics().getFontMetrics(newFont);
176     double mw=fm.getStringBounds("M", getGraphics())
177             .getWidth(),iw=fm.getStringBounds("I", getGraphics())
178                     .getWidth();
179     if (mw<1 || iw < 1) {
180         // TODO: JAL-1100
181       fontName.select(lastSelected.getName());
182       fontStyle.select(lastSelStyle);
183       fontSize.select(""+lastSelSize);
184       JVDialog d = new JVDialog(this.frame, "Invalid Font", true, 350,200);
185       Panel mp=new Panel();
186       d.cancel.setVisible(false);
187       mp.setLayout(new FlowLayout());
188       mp.add(new Label("Font doesn't have letters defined\nso cannot be used\nwith alignment data."));
189       d.setMainPanel(mp);
190       d.setVisible(true);
191       return;
192     }
193     if (tp != null)
194     {
195       tp.setTreeFont(newFont);
196     }
197     else if (ap != null)
198     {
199       ap.av.setFont(newFont);
200       ap.fontChanged();
201     }
202     // remember last selected
203     lastSelected=newFont;
204   }
205
206   protected void fontName_actionPerformed()
207   {
208     if (init)
209     {
210       return;
211     }
212     changeFont();
213   }
214
215   protected void fontSize_actionPerformed()
216   {
217     if (init)
218     {
219       return;
220     }
221     changeFont();
222   }
223
224   protected void fontStyle_actionPerformed()
225   {
226     if (init)
227     {
228       return;
229     }
230     changeFont();
231   }
232
233   Label label1 = new Label();
234
235   protected Choice fontSize = new Choice();
236
237   protected Choice fontStyle = new Choice();
238
239   Label label2 = new Label();
240
241   Label label3 = new Label();
242
243   protected Choice fontName = new Choice();
244
245   Button ok = new Button();
246
247   Button cancel = new Button();
248
249   Panel panel1 = new Panel();
250
251   Panel panel2 = new Panel();
252
253   Panel panel3 = new Panel();
254
255   BorderLayout borderLayout1 = new BorderLayout();
256
257   BorderLayout borderLayout2 = new BorderLayout();
258
259   BorderLayout borderLayout3 = new BorderLayout();
260
261   Panel panel4 = new Panel();
262
263   Panel panel5 = new Panel();
264
265   BorderLayout borderLayout4 = new BorderLayout();
266
267   private void jbInit() throws Exception
268   {
269     label1.setFont(new java.awt.Font("Verdana", 0, 11));
270     label1.setAlignment(Label.RIGHT);
271     label1.setText("Font: ");
272     this.setLayout(borderLayout4);
273     fontSize.setFont(new java.awt.Font("Verdana", 0, 11));
274     fontSize.addItemListener(this);
275     fontStyle.setFont(new java.awt.Font("Verdana", 0, 11));
276     fontStyle.addItemListener(this);
277     label2.setAlignment(Label.RIGHT);
278     label2.setFont(new java.awt.Font("Verdana", 0, 11));
279     label2.setText("Size: ");
280     label3.setAlignment(Label.RIGHT);
281     label3.setFont(new java.awt.Font("Verdana", 0, 11));
282     label3.setText("Style: ");
283     fontName.setFont(new java.awt.Font("Verdana", 0, 11));
284     fontName.addItemListener(this);
285     ok.setFont(new java.awt.Font("Verdana", 0, 11));
286     ok.setLabel("OK");
287     ok.addActionListener(this);
288     cancel.setFont(new java.awt.Font("Verdana", 0, 11));
289     cancel.setLabel("Cancel");
290     cancel.addActionListener(this);
291     this.setBackground(Color.white);
292     panel1.setLayout(borderLayout1);
293     panel2.setLayout(borderLayout3);
294     panel3.setLayout(borderLayout2);
295     panel5.setBackground(Color.white);
296     panel4.setBackground(Color.white);
297     panel1.setBackground(Color.white);
298     panel2.setBackground(Color.white);
299     panel3.setBackground(Color.white);
300     panel1.add(label1, BorderLayout.WEST);
301     panel1.add(fontName, BorderLayout.CENTER);
302     panel5.add(panel1, null);
303     panel5.add(panel3, null);
304     panel5.add(panel2, null);
305     panel2.add(label3, BorderLayout.WEST);
306     panel2.add(fontStyle, BorderLayout.CENTER);
307     panel3.add(label2, BorderLayout.WEST);
308     panel3.add(fontSize, BorderLayout.CENTER);
309     this.add(panel4, BorderLayout.SOUTH);
310     panel4.add(ok, null);
311     panel4.add(cancel, null);
312     this.add(panel5, BorderLayout.CENTER);
313   }
314
315 }