Merge develop to Release_2_8_3_Branch
[jalview.git] / src / jalview / appletgui / FontChooser.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10  * of the License, or (at your option) any later version.
11  *  
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.
16  * 
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.
20  */
21 package jalview.appletgui;
22
23 import jalview.api.ViewStyleI;
24 import jalview.util.MessageManager;
25
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;
31 import java.awt.Font;
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;
41
42 public class FontChooser extends Panel implements ActionListener,
43         ItemListener
44 {
45   AlignmentPanel ap;
46
47   TreePanel tp;
48
49   Font oldFont;
50
51   int oldCharWidth = 0;
52
53   boolean init = true;
54
55   Frame frame;
56
57   public FontChooser(TreePanel tp)
58   {
59     try
60     {
61       jbInit();
62     } catch (Exception e)
63     {
64       e.printStackTrace();
65     }
66
67     this.tp = tp;
68     oldFont = tp.getTreeFont();
69     init();
70   }
71
72   public FontChooser(AlignmentPanel ap)
73   {
74     try
75     {
76       jbInit();
77     } catch (Exception e)
78     {
79       e.printStackTrace();
80     }
81
82     this.ap = ap;
83     oldFont = ap.av.getFont();
84     oldCharWidth = ap.av.getViewStyle().getCharWidth();
85     init();
86   }
87
88   void init()
89   {
90     String fonts[] = Toolkit.getDefaultToolkit().getFontList();
91     for (int i = 0; i < fonts.length; i++)
92     {
93       fontName.addItem(fonts[i]);
94     }
95
96     for (int i = 1; i < 31; i++)
97     {
98       fontSize.addItem(i + "");
99     }
100
101     fontStyle.addItem("plain");
102     fontStyle.addItem("bold");
103     fontStyle.addItem("italic");
104
105     fontName.select(oldFont.getName());
106     fontSize.select(oldFont.getSize() + "");
107     fontStyle.select(oldFont.getStyle());
108
109     Frame frame = new Frame();
110     this.frame = frame;
111     frame.add(this);
112     jalview.bin.JalviewLite.addFrame(frame,
113             MessageManager.getString("action.change_font"), 440, 115);
114
115     init = false;
116   }
117
118   public void actionPerformed(ActionEvent evt)
119   {
120     if (evt.getSource() == ok)
121     {
122       ok_actionPerformed();
123     }
124     else if (evt.getSource() == cancel)
125     {
126       cancel_actionPerformed();
127     }
128   }
129
130   public void itemStateChanged(ItemEvent evt)
131   {
132     if (evt.getSource() == fontName)
133     {
134       fontName_actionPerformed();
135     }
136     else if (evt.getSource() == fontSize)
137     {
138       fontSize_actionPerformed();
139     }
140     else if (evt.getSource() == fontStyle)
141     {
142       fontStyle_actionPerformed();
143     }
144   }
145
146   protected void ok_actionPerformed()
147   {
148     frame.setVisible(false);
149     if (ap != null)
150     {
151       if (ap.getOverviewPanel() != null)
152       {
153         ap.getOverviewPanel().updateOverviewImage();
154       }
155     }
156
157   }
158
159   protected void cancel_actionPerformed()
160   {
161     if (ap != null)
162     {
163       ap.av.setFont(oldFont);
164       ViewStyleI style = ap.av.getViewStyle();
165       if (style.getCharWidth() != oldCharWidth)
166       {
167         style.setCharWidth(oldCharWidth);
168         ap.av.setViewStyle(style);
169       }
170       ap.paintAlignment(true);
171     }
172     else if (tp != null)
173     {
174       tp.setTreeFont(oldFont);
175       tp.treeCanvas.repaint();
176     }
177
178     fontName.select(oldFont.getName());
179     fontSize.select(oldFont.getSize() + "");
180     fontStyle.select(oldFont.getStyle());
181
182     frame.setVisible(false);
183   }
184
185   private Font lastSelected = null;
186
187   private int lastSelStyle = 0;
188
189   private int lastSelSize = 0;
190
191   /**
192    * DOCUMENT ME!
193    */
194   void changeFont()
195   {
196     if (lastSelected == null)
197     {
198       // initialise with original font
199       lastSelected = oldFont;
200       lastSelSize = oldFont.getSize();
201       lastSelStyle = oldFont.getStyle();
202     }
203
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)
211     {
212       // TODO: JAL-1100
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,
218               200);
219       Panel mp = new Panel();
220       d.cancel.setVisible(false);
221       mp.setLayout(new FlowLayout());
222       mp.add(new Label(
223               "Font doesn't have letters defined\nso cannot be used\nwith alignment data."));
224       d.setMainPanel(mp);
225       d.setVisible(true);
226       return;
227     }
228     if (tp != null)
229     {
230       tp.setTreeFont(newFont);
231     }
232     else if (ap != null)
233     {
234       ap.av.setFont(newFont);
235       ap.fontChanged();
236     }
237     // remember last selected
238     lastSelected = newFont;
239   }
240
241   protected void fontName_actionPerformed()
242   {
243     if (init)
244     {
245       return;
246     }
247     changeFont();
248   }
249
250   protected void fontSize_actionPerformed()
251   {
252     if (init)
253     {
254       return;
255     }
256     changeFont();
257   }
258
259   protected void fontStyle_actionPerformed()
260   {
261     if (init)
262     {
263       return;
264     }
265     changeFont();
266   }
267
268   Label label1 = new Label();
269
270   protected Choice fontSize = new Choice();
271
272   protected Choice fontStyle = new Choice();
273
274   Label label2 = new Label();
275
276   Label label3 = new Label();
277
278   protected Choice fontName = new Choice();
279
280   Button ok = new Button();
281
282   Button cancel = new Button();
283
284   Panel panel1 = new Panel();
285
286   Panel panel2 = new Panel();
287
288   Panel panel3 = new Panel();
289
290   BorderLayout borderLayout1 = new BorderLayout();
291
292   BorderLayout borderLayout2 = new BorderLayout();
293
294   BorderLayout borderLayout3 = new BorderLayout();
295
296   Panel panel4 = new Panel();
297
298   Panel panel5 = new Panel();
299
300   BorderLayout borderLayout4 = new BorderLayout();
301
302   private void jbInit() throws Exception
303   {
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);
348   }
349
350 }